  var sky_pos = 0;
  $("#header-scroll").css("margin-left", "-1500px");
  setInterval(function(){
    $("#header").css("background-position", sky_pos--+'px 0px');
  }, 100);

function isValidEmail (email, strict)
{
 if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

$(document).ready(function(){
  var check = true;

  $(".main_menu > li:has(ul) > a").bind("click", function(){
    $(this).next("ul").slideDown('slow');
    $(this).parents("li").siblings().find("ul").slideUp("slow");
    return false;
  });

  $(".main_menu > li:has(ul) > ul > li > a").bind("click", function(){
    return true;
  });

  $("#contacts a").bind("mousemove, click", function(){
    $(this).next("div").slideDown('slow');
    $(this).parents("li").siblings().find("div").slideUp("slow");
    return false;
  });

  $jScroller.add("#scroller_container","#scroller","left",10, true);
  $jScroller.start();

  $('form input[type=text], form input[type=password], textarea')
         .bind('focus', Function("if(this.value==this.defaultValue) this.value=''"))
         .bind('blur' , Function("if(this.value=='') this.value=this.defaultValue"));

  $(".main_menu").bind("mouseleave", function(){
      $(this).find("li ul").slideUp();
      $(this).find(".active").parents("ul").slideDown('fast');
  });

  $("input, textarea").not("#src-smb").uniform();

  $(".active").parents("ul").slideDown('fast');
  $("#captcha").hide();

  $("select[name='probne[category]']").chosen().change(function (){
    if ($(this).val() == 1) {
      $("#school").slideDown();
    } else {
      $("#school").slideUp().find(".details").empty();
    }
  });
  $("#school_regions").chosen().change(function (){
    var _id = $(this).val();
      $.ajax({
        type: "POST",
        url: '/ajax.php',
        data: "action=region&id=" + _id,
        success: function(html){
          $("#school .details").html("");
          $("#school .details:eq(0)").html(html).find("select").slideDown("slow").chosen().change(function(){
              var _id = $(this).val();
              $.ajax({
                type: "POST",
                url: '/ajax.php',
                data: "action=state&id=" + _id,
                success: function(html){
                  $("#school .details:eq(1)").html(html).find("select").slideDown("slow").chosen().change(function(){
                     var _id = $(this).val();
                     $.ajax({
                       type: "POST",
                       url: '/ajax.php',
                       data: "action=area&id=" + _id,
                       success: function(html){
                         $("#school .details:eq(2)").html(html).find("select").slideDown("slow").chosen();
                       }
                     });
                  });
                }
              });
          });
        }
      });
  });

  $("#check").bind("click", function (){
      $("#check_form").dialog('open');
  });
  $("#check_form").dialog({
      autoOpen         : false,
      width            : 480,
      height           : 320,
      show             : "blind",
      hide             : "explode",
      title            : 'Перевірка стану реєстрації',
      modal            : true,
      resizable        : false,
      buttons          : {
              "Пошук":function(){
                    var _id = $(this).find("input[name='code']").val();
                    var _ob = $(this);
                    if (_id == "" || _id.length != 10) $( this ).dialog( "close" );
                    $.ajax({
                      type: "POST",
                      url: '/api.php',
                      data: "api=reg_osn&code=" + _id,
                      success: function(html){
                        if (html == "[]") {
                          _ob.find("table").fadeOut();
                          _ob.find(".nf").fadeIn();
                        } else {
                          json = $.parseJSON(html);
                          switch (json[0].reg_status) {
                            case "1": _status = '<span class="orange">очікує другий ввод після першого</span>'; break;
                            case "2": _status = '<span class="red">очікує розгляду регламентної комісії</span>'; break;
                            case "3": _status = '<span class="brown">В реєстрації відмовлено</span>'; break;
                            case "4": _status = '<span class="green">Сертифікат надруковано та відправлено</span>'; break;
                            case "5": _status = '<span class="orange">Документи в стадії обробки</span>'; break;
                            case "6": _status = '<span class="green">Сертифікат надруковано</span>'; break;
                          }
                          switch (json[0].reg_reason) {
                            case "0": _reason = ""; break;
                            case "1": _reason = "Надання не всіх документів, необхідних для здійснення реєстрації"; break;
                            case "2": _reason = "Неналежне оформлення комплекту реєстраційних документів"; break;
                            case "3": _reason = "Зазначення в заяві-реєстраційній картці недостовірних даних"; break;
                            case "4": _reason = "Неможливість створення особливих (специфічних) умов проходження зовнішнього оцінювання відповідно до висновку закладу охорони здоров’я"; break;
                          }
                          _ob.find(".reg_date").html(json[0].reg_date);
                          _ob.find(".reg_status").html(_status);
                          _ob.find(".reg_reason").html(_reason);
                          if (json[0].reg_date == "") _ob.find("tr:eq(1)").fadeOut(); else _ob.find("tr:eq(1)").fadeIn();
                          if (_reason == "") _ob.find("tr:last").fadeOut(); else _ob.find("tr:last").fadeIn();
                          _ob.find(".nf").fadeOut();
                          _ob.find("table").fadeIn();
                        }
                       }
                    });
              },
              "Повернутися": function(){
                    $( this ).dialog( "close" );
              }
      }
  });

  $("#home_regions").chosen().change(function (){
    var _id = $(this).val();
      $.ajax({
        type: "POST",
        url: '/ajax.php',
        data: "action=home&id=" + _id,
        success: function(html){
          $("#home .details").html("");
          $("#home .details:eq(0)").html(html).find("select").slideDown("slow").chosen();
        }
      });
  });

  $("select[name='probne[place]']").chosen();
  $("#probne_register .small select").uniform();

  $("#probne_register input[type=submit]").bind("click", function(){
    var check = true;
    var error = "<ul>";
    if ($(".agree:checked").size() != 1) {
       $(".agree:not(:checked)").parents("label").css("color", "red");
       error += "<li>Для успішної реєстрації Ви повинні підтвердити свою згоду з правилами проведення пробного тестування</li>";
       check = false;
    } else $(".agree").parents("label").css("color", "#666");
    if ($("#subjects input[type=checkbox]:checked").size() == 0) {
       $("#subjects input[type=checkbox]").parents("label").css("color", "red");
       error += "<li>Ви повинні вибрати не менше одного тестування</li>";
       check = false;
    }  else $("#subjects input[type=checkbox]").parents("label").css("color", "#666");
    if ($("input[name='probne[password1]']").val() == "" || $("input[name='probne[password1]']").val() != $("input[name='probne[password1]']").val()) {
       error += "<li>Паролі мають співпадати</li>";
       $("#probne_register input[type='password']").css("background", "pink");
       check = false;
    } else $("#probne_register input[type='password']").css("background", "white");
    if ($("input[name='probne[password1]']").val().length < 6) {
       error += "<li>Пароль повинен містити не менше 6 символів</li>";
       $("#probne_register input[type='password']").css("background", "pink");
       check = false;
    } else $("#probne_register input[type='password']").css("background", "white");
    if ($("select[name='probne[category]']").val() == "0") {
       error += "<li>Ви не обрали категорiю учасника</li>";
       $("select[name='probne[category]']").parents("label").css("color", "red");
       check = false;
    } else $("select[name='probne[category]']").parents("label").css("color", "#666");
    if ($("select[name='probne[category]']").val() == "1" && $("select[name='probne[school]']").val() == undefined) {
       error += "<li>Ви не обрали навчальний заклад</li>";
       $("select[name='probne[category]']").parents("label").css("color", "red");
       check = false;
    } else $("select[name='probne[category]']").parents("label").css("color", "#666");
    if ($("select[name='probne[place]']").val() == "0" || $("select[name='probne[place]']").val() == "") {
       error += "<li>Ви не обрали мicце тестування</li>";
       $("select[name='probne[place]']").parents("label").css("color", "red");
       check = false;
    } else $("select[name='probne[place]']").parents("label").css("color", "#666");
    if ($("select[name='probne[home]']").val() == undefined) {
       error += "<li>Ви не вибрали місце проживання</li>";
       $("select[name='probne[home]']").parent().css("color", "red");
       check = false;
    } else $("select[name='probne[home]']").parent().css("color", "#666");
    if ($("input[name='probne[lastname]']").val() == "") {
       error += "<li>Ви не вказали прізвище</li>";
       $("input[name='probne[lastname]']").css("background", "pink");
       check = false;
    } else $("input[name='probne[lastname]']").css("background", "white");
    if ($("input[name='probne[firstname]']").val() == "") {
       error += "<li>Ви не вказали ім&rsquo;я</li>";
       $("input[name='probne[firstname]']").css("background", "pink");
       check = false;
    } else $("input[name='probne[firstname]']").css("background", "white");
    if ($("input[name='probne[mobile]']").val() == "") {
       error += "<li>Ви не вказали мобільний телефон</li>";
       $("input[name='probne[mobile]']").css("background", "pink");
       check = false;
    } else $("input[name='probne[mobile]']").css("background", "white");
    error += "</ul>";
    $("#error").html(error);
    if ($("#email").val() == "" && check) $( "#dialog" ).dialog( "open" );
    if (check) $("#probne_register").submit();
    else {
         $("#error").dialog("open");
    }
    return false;
  });

  $("#dialog").dialog({
      autoOpen         : false,
      width            : 420,
      height           : 240,
      show             : "blind",
      hide             : "explode",
      title            : 'Увага!',
      modal            : true,
      resizable        : false,
      buttons          : {
              "Продовжити":function(){
                    $( this ).dialog( "close" );
                    $("#email").focus();
              },
              "Повернутися": function(){
                    $( this ).dialog( "close" );
                    if (check) { $("#probne_register").submit(); }
              }
      }
  });

  $("#error").dialog({
      autoOpen        : false,
      width           : 500,
      height          : 450,
      show            : "blind",
      hide            : "explode",
      title           : 'Виправте помилки!',
      modal           : true,
      resizable       : false
  });

  $("#probne_personal_cabinet select").uniform();

  $(".order").bind("click", function(){
    $(this).addClass("reg");
    $("#confirm").dialog("open");
    return false;
  });

  $("#confirm").dialog({
      autoOpen          : false,
      width             : 320,
      height            : 200,
      show              : "blind",
      hide              : "explode",
      title             : 'Пiдтвердження!',
      modal             : true,
      resizable         : false,
      buttons           : {
              "Так":function() {
                   var lang = $(".reg").parents("tr").find("select").val();
                   var subj = $(".reg").attr("href");
                   $.ajax({
                      type: "POST",
                      url: '/ajax.php',
                      data: "action=reg&lang="+lang+"&subj=" + subj,
                      success: function(html){
                        $(".reg").removeClass("reg");
                        $( this ).dialog( "close" );
                        location.reload(true);
                      }
                   });
              },
              "Нi": function(){
                    $(".reg").removeClass("reg");
                    $( this ).dialog( "close" );
              }
      }
  });

  $(".forgot label:last").hide();

  $(".forgot input[type=radio]").bind("click", function(){
             if ($(this).val() != 1)
                $(".forgot label:last").slideDown("slow");
             else
                $(".forgot label:last").slideUp("slow");
  });


});// document.ready
