$(function(){
    taste1=false;
    taste2=false;
    taste3=false;
    taste4=false;
    taste5=false;
    lang_id = $.cookie("lang_id");
    if (!(lang_id in {"ua":"", "pl":"","us":"","cz":"","ru":""})) {
        lang_id = "ua";
        savelang();
    }

    if(window.location.hash.length>1) {
        $("#gunshot").css("display","block");
        thepage = (window.location.hash.substring(1,window.location.hash.length-5));
        $(".menu a[href*=" + thepage + "]").after($("#clone_bullet"));
        $("#module_container").load("/Rozdily/" + thepage + ".htmla",function(){
            try {
                eval(thepage+"()");
            } catch(err) {}
        });
    } else if (window.location.toString().indexOf("Rozdily")>0) {
    } else {
        $("#gunshot").css("display","block");
        $(".menu div.currentpage").append($("#clone_bullet"));
        $("#module_container").load("/Rozdily/golovna.htmla",function(){
            try {
                golovna();
            } catch(err) {}
        });
        window.location.hash = "golovna.html";
    }
    $(".newsblock>a").click(function(){
        $("#module_container").load($(this).attr("href")+"a");
        console.log($(this).attr("href")+"a");
        window.location.hash = $(this).attr("href").substring(8);
        return false;
    });
    $(".menu>div").hover(function(){
        $(this).append($("#bullet"));
    }).mousedown(function(event){
        event.preventDefault();
        $("#gunshot").css("display","block");
        $(this).append($("#clone_bullet"));
        $("#module_container").load($(this).find("a").attr("href") + "a",function(){
            var curpage;
            curpage = (window.location.hash.substring(1,window.location.hash.length-5));
            try {
                eval(curpage+"()");
            } catch(err) {}
        });
        var curpath = ($(this).find("a").attr("href") + "a").substr(1);
        curpath = curpath.substr(curpath.indexOf("/")+1);
        window.location.hash = curpath.substr(0,curpath.length-1);
    });

    $("#module_container a:not([class=noajax])").live("click", function(event){
        event.preventDefault();
        $("#module_container").load($(this).attr("href") + "a",function(){
            var curpage;
            curpage = (window.location.hash.substring(1,window.location.hash.length-5));
            try {
                eval(curpage+"()");
            } catch(err) {}
          return false;
        });
        var curpath = ($(this).attr("href") + "a").substr(1);
        curpath = curpath.substr(curpath.indexOf("/")+1);
        window.location.hash = curpath.substr(0,curpath.length-1);
        thepage = (window.location.hash.substring(1,window.location.hash.length-5));
        $(".menu a[href*=" + thepage + "]").after($('#clone_bullet'));
    });

    $("#cockgun").jmp3();
    $("#gunshot").jmp3();
    $(document).mousemove(function(){
        //jQuery("#cockgun").css("display","none");
	jQuery("#gunshot").css("display","none");
    });

    function savelang() {
        var date = new Date();
        date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
        $.cookie("lang_id", lang_id, {path: '/', expires: date});
    }
    function zamovyty (){
        $("#nickname").blur(function(){
            if ($(this).attr("value").length>=3) {
              $("#imgnick").attr("src","/images/tick.gif");
              taste1 = true;
              if (taste1 && taste2 && taste3 && taste4 && taste5) $("#btnchk").removeAttr("disabled");
              return true;
            } else {
              $("#imgnick").attr("src","/images/delete.gif");
              taste1 = false;
              $("#btnchk").attr("disabled","disabled");
              return false;
            }
        });

        $("#phone").blur(function(){
          var filter=/^([0-9]{5,20})$/i;
          if (filter.test($(this).attr("value"))) {
            $("#imgphone").attr("src","/images/tick.gif");
            taste2 = true;
            if (taste1 && taste2 && taste3 && taste4 && taste5) $("#btnchk").removeAttr("disabled");
            return true;
          } else {
            $("#imgphone").attr("src","/images/delete.gif");
            taste2 = false;
            $("#btnchk").attr("disabled","disabled");
            return false;
          }
        });

        $("#soap").blur(function(){
          var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
          if(filter.test($(this).attr("value"))) {
            $("#imgsoap").attr("src","/images/tick.gif");
            taste3 = true;
            if (taste1 && taste2 && taste3 && taste4 && taste5) $("#btnchk").removeAttr("disabled");
            return true;
          } else {
            $("#imgsoap").attr("src","/images/delete.gif");
            taste3 = false;
            $("#btnchk").attr("disabled","disabled");
            return false;
          }
        });

        $("#msg").blur(function(){
          if ($(this).attr("value").length>=10) {
            $("#imgmsg").attr("src","/images/tick.gif");
            taste4 = true;
            if (taste1 && taste2 && taste3 && taste4 && taste5) $("#btnchk").removeAttr("disabled");
            return true;
          } else {
            $("#imgmsg").attr("src","/images/delete.gif");
            taste4 = false;
            $("#btnchk").attr("disabled","disabled");
            return false;
          }
        });

        $("#captcha").keypress(function(){
          if ($(this).attr("value").length==5 && !isNaN($(this).attr("value"))) {
            taste5 = true;
            if (taste1 && taste2 && taste3 && taste4 && taste5) $("#btnchk").removeAttr("disabled");
            return true;
          } else {
            taste5 = false;
            $("#btnchk").attr("disabled","disabled");
            return true;
          }
        });

        $("#btnchk").one("click", function(){
          $.post("/Rozdily/Zamovyty/send.html", {
            nickname: $("#nickname").attr("value"),
            phone: $("#phone").attr("value"),
            soap: $("#soap").attr("value"),
            date: $("#startDateDay :selected").attr("value") + " " + $("#startDateMonth :selected").attr("value") + " " + $("#startDateYear :selected").attr("value"),
            time: $("#timeStartTime :selected").attr("value") + "-" + $("#timeStopTime :selected").attr("value"),
            msg: $("#msg").val(),
            captcha: $("#captcha").val(),
            random: $("#random").attr("value")
          }, function(data){
              $("#module_container").html(data);
            });
        });
      }
    if (window.location.toString().indexOf("set_lang=")>0) {
        var strlpos = window.location.toString().indexOf("set_lang=");
        lang_id = window.location.toString().substr(strlpos+9, 2);
        if (!(lang_id in {"ua":"", "pl":"","us":"","cz":"","ru":""})) {
            lang_id = "ua";
        }
        savelang();
        window.location = "/";
    }
    $("#langpane div.disabled").fadeTo(700, 0.2);
    $("#langpane div:not(.disabled)").click(function(){
        lang_id = this.id;
        if (!(lang_id in {"ua":"", "pl":"","us":"","cz":"","ru":""})) {
            lang_id = "ua";
        }
        savelang();
        location.reload();
    });
    $(".game>div:not(#details_pane)").live('click', function(){
      $('#details_pane').remove();
      $(this).parent().append("<div id='details_pane'><img src='/templates/human/images/ajax-loader.gif' alt='loading...' /></div>");
      $("#details_pane").load('/Rozdily/statystyka'+ $(this).parent().attr('id').substr(4) +'.htmla');
    });
});
