$(init);

function init() {

  $("form.userform").each(
    function() {
      FormRegistry.init($(this));
    }
  );

  loginForm();
  deco();
  faq();

  reg_form();

  memberList();

  businessList();

  $("#l_submit").hover(
    function() {
      $(this).addClass("hover");
    },
    function() {
      $(this).removeClass("hover");
    }
  );

  $("#vote_submit").hover(
    function() {
      $(this).addClass("hover");
    },
    function() {
      $(this).removeClass("hover");
    }
  );

  $("#search input.submit").hover(
    function() {
      $(this).id("hovering");
    },
    function() {
      $(this).id("");
    }
  );



}

function memberList() {

  $("#member2,#member3,#member4,#member5").hide();

  $("#f_member_count").find("label").click(
    function() {
      $("#member2,#member3,#member4,#member5").hide();
      var count = parseInt($(this).find("input:eq(0)").val()) + 1;
      for (var i = 0; i < count; i++) {
        $("#member" + i).show();
      }
    }
  );

  /* $("#member_count").find("label").click(
    function() {
      var count = $(this).find("input:eq(0)").val();
      $("div.member").not("#captain").remove();
      if (count > 1) {
        while (count > 1) {
          var html = $("div.member:last").html();
          $("div.member:last").after('<div class="member">' + html + '</div>');
          $("div.member:last h4").html("Komandos narys");

          window.last = parseInt($("div.member:last *[@name]:last")[0].name.match(/\d+/));
          count--;
        }
      }
      fixNames();
    }
  ); */

  $("#pass_2").blur(
    function() {
      if ($(this).val() != $("#pass_1").val()) {
        alert("Slaptažodžiai nesutampa");
      }
    }
  );

}

function businessList() {

  $("#business_list").parent().submit(

    function() {

      $("#business_list input[@type='checkbox']").each(
        function() {
          if ($(this)[0].checked) {
            var temp = $(this).parent().next().val();
            $(this)[0].name = $(this).parent().next()[0].name.substr(0, $(this).parent().next()[0].name.length - 1) + " (" + temp + ")]";
            $(this).parent().next()[0].name = "";
          }
        }
      );

    }

  );

  $("#business_list input[@type='checkbox']").click(
    function() { 
      if ($(this)[0].checked) {
        $(this).parent().after('<input type="text" class="inp" maxlength="1" onchange="this.value=parseInt(this.value); if(this.value==\'N\') this.value=0; if(this.value==\'NaN\') this.value=0;" name="' + $(this)[0].name + '" />');
        $(this)[0].name = "";
      }
      else {
        $(this)[0].name = $(this).parent().next()[0].name;
        $(this).parent().next().remove();
      }
    }
  );

/*
  $("#business_list input[@type='checkbox']").not(":checked").click(
    function() {
      if ($("#business_list input:checked").size() == 6) {
        $(this)[0].checked = false;
        alert(ErrorMessages.tooManyCats);
        return false;
      }
      else {
        $(this).parent().after('<p class="rate">rate</p>');
      }
    }
  ); */

  /*$("#business_list input:checked").click(
    function() {
      var next = $(this).parent().next();
      if (next[0].tagName.toUpperCase() == "P") {
        $(next).remove();
      }
    }
  );*/

}


function faq() {

  $("#faq_list p.a:visible").hide();

  $("#faq_list p a").click(
    function() {
      $("#faq_list p.a:visible").hide();
      $(this).parent().parent().next().show();
      return false;
    }

  );

}


function reg_form() {

  $("#tr_city, #co_year, #other_services").hide();

  $("#tr_switch_on").click(
    function() {
      $("#tr_city").show();
    }
  );

  $("#tr_switch_off").click(
    function() {
      $("#tr_city").hide();
    }
  );

  $("#co_switch_on").click(
    function() {
      $("#co_year").show();
    }
  );

  $("#co_switch_off").click(
    function() {
      $("#co_year").hide();
    }
  );

  $("#other_switch").click(
    function() {
      if ($(this)[0].checked == true) {
        $("#other_services").show();
      }
      else {
        $("#other_services").hide();
      }
    }
  );

}




function loginForm() {

  var form = $("form.login_form");

  $("label", form).hide();

  $("#l_user")
    .after('<input type="text" id="usr_h" value="' + $("#l_user_label").text() + '" />')
    .find("#usr_h").addClass("inp").addClass("inactive")
    .end()
    .hide()
    .blur(
      function() {
        if ($(this).val() == '') {
          $(this).hide();
          $("#usr_h").show();
        }
      }
    );

  $("#usr_h")
    .focus(
      function() {
        $("#l_user").show();
        $(this).hide();
        $("#l_user")[0].focus();
      }
    );

  $("#l_pass")
    .after('<input type="text" id="pwd_h" value="' + $("#l_pass_label").text() + '" />')
    .find("#pwd_h").addClass("inp").addClass("inactive")
    .end()
    .hide()
    .blur(
      function() {
        if ($(this).val() == '') {
          $(this).hide();
          $("#pwd_h").show();
        }
      }
    );

  $("#pwd_h")
    .focus(
      function() {
        $("#l_pass").show();
        $(this).hide();
        $("#l_pass")[0].focus();
      }
    );


}

function findPos(obj) {
  var curleft = obj.offsetLeft || 0;
  var curtop = obj.offsetTop || 0;
  while (obj = obj.offsetParent) {
    curleft += obj.offsetLeft
    curtop += obj.offsetTop
  }
  return {x: curleft, y: curtop};
}

function deco() {

  $("#wrapper").before('<div id="reg_deco" class="deco"></div>');
  $("#secondary .block").each(
    function(id) {
      var top = findPos($(this)[0]).y;
      $("#wrapper").before('<div id="deco_' + id + '" class="deco"><div></div></div>');
      $("#deco_" + id).css("top", top + 17 + "px");
    }
  );

  $("#reg_deco").css("top", $("#page")[0].offsetTop + 223 + "px");

}