var ha = {
    title: "Human Ark",
    loadFile: "",
    oninit: function (event) {
        $('#menu a:not(.fbk):not(.ytb):not(#menu08 a):not(.vmo), #reklamy a, #filmy a, a#logout').address(function () {
            return $(this).attr('href').replace(location.pathname, '');
        });
        $("#share a").attr("target","_blank");
        $("nav").append("<div id=\"loading\"><img src=\"/images/loader.gif\" /></div>");   
        subs.init();
        ftp.init();
        unsubscribe.init();
    },
    
    highlight : function (no) {
        $("ul#menu li a").removeClass("open");
        $("ul#menu_en li a").removeClass("open");
        if((no>9)&&(no<20)){
            $("#menu"+no+" a").addClass("open");
        }
        else  if(no>0){
            $("#menu0"+no+" a").addClass("open");
            $("#menu_en0"+no+" a").addClass("open");
        }
    },
    
    loadSection : function(_file, _high, _title, _lang, _id){
        ha.highlight(_high);
        var id = "";
        if(_id){
            id = "&id=" + _id;
        }
        ha.loadFile = "/php/pagehandle.php?l=" + _lang + "&p=" + _file + id;
        ha.setTitle(_title);
    },
   
    onchange: function (event) {
        var noChange = false;
    
        ha.translate(event.pathNames[0]);
        if (event.pathNames[1] == "reklamy") {
            ha.loadSection(event.pathNames[1], 4, "Reklamy", event.pathNames[0]);
        }
        else if (event.pathNames[1] == "reklama") {
            ha.loadSection(event.pathNames[1], 4, "Reklama", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "filmy") {
            ha.loadSection(event.pathNames[1], 5, "Filmy", event.pathNames[0]);
        }
         else if ((event.pathNames[1] == "inne") && (event.pathNames[2] > 0 )) {
            ha.loadSection(event.pathNames[1], 6, "Inne", event.pathNames[0], event.pathNames[2]);
        }
          else if (event.pathNames[1] == "inne") {
            ha.loadSection(event.pathNames[1], 6, "Inne", event.pathNames[0]);
        }
       
        else if (event.pathNames[1] == "film") {
            ha.loadSection(event.pathNames[1], 5, "Film", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "reel") {
            ha.loadSection(event.pathNames[1], 3, "Reel", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "kontakt") {
            ha.loadSection(event.pathNames[1], 7, "Kontakt");
        }
        else if (((event.pathNames[1] == undefined) && ((event.pathNames[0] == undefined) || (event.pathNames[0] == "pl"))) || (event.pathNames[1] == "aktualnosci")) {
            ha.loadSection("aktualnosci", 1, "Aktualności", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "firma") {
            ha.loadSection(event.pathNames[1], 2, "O nas - Firma", event.pathNames[0]);
        }
        else if (event.pathNames[1] == "ludzie") {
            ha.loadSection(event.pathNames[1], 2, "O nas - Ludzie", event.pathNames[0]);
        }
        
        else if (event.pathNames[1] == "technologia") {
            ha.loadSection(event.pathNames[1], 2, "O nas - Technologia", event.pathNames[0]);
        }
        
        else if (event.pathNames[1] == "praca") {
            ha.loadSection(event.pathNames[1], 10, "Praca", event.pathNames[0]);
        }
        
         else if (event.pathNames[1] == "strefa_klienta") {
            ha.loadSection(event.pathNames[1], 9, "Strafa Klienta", event.pathNames[0]);
        }
        // ENGLISH
        
        else if ((((event.pathNames[1] == undefined) || (event.pathNames[1] == "")) && (event.pathNames[0] == "en")) || (event.pathNames[1] == "news")) {
            ha.loadSection("news", 1, "News", event.pathNames[0], event.pathNames[2]);
        }
        
        else if (event.pathNames[1] == "ads") {
            ha.loadSection(event.pathNames[1], 4, "Ads", event.pathNames[0]);
        }
         else if (event.pathNames[1] == "ad") {
            ha.loadSection(event.pathNames[1], 4, "Ad", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "films") {
            ha.loadSection(event.pathNames[1], 5, "Films", event.pathNames[0]);
        }
    
        else if (event.pathNames[1] == "film") {
            ha.loadSection(event.pathNames[1], 5, "Film", event.pathNames[0], event.pathNames[2]);
        }
        else if (event.pathNames[1] == "contact_us") {
            ha.loadSection(event.pathNames[1], 7, "Contact us", event.pathNames[0]);
        }
        else if (event.pathNames[1] == "company") {
            ha.loadSection(event.pathNames[1], 2, "About us - Company", event.pathNames[0]);
        }
        else if (event.pathNames[1] == "people") {
            ha.loadSection(event.pathNames[1], 2, "About us - People", event.pathNames[0]);
        }
        
        else if (event.pathNames[1] == "technology") {
            ha.loadSection(event.pathNames[1], 2, "About us - Technology", event.pathNames[0]);
        }
        
        else if (event.pathNames[1] == "jobs") {
            ha.loadSection(event.pathNames[1], 10, "Jobs", event.pathNames[0]);
        }
        
          else if ((event.pathNames[1] == "other") && (event.pathNames[2] > 0 )) {
            ha.loadSection(event.pathNames[1], 6, "Other", event.pathNames[0], event.pathNames[2]);
        }
          else if (event.pathNames[1] == "other") {
            ha.loadSection(event.pathNames[1], 6, "Other", event.pathNames[0]);
        }
        
          else if ((event.pathNames[1] == "makingof") && (event.pathNames[2] > 0 )) {
            ha.loadSection(event.pathNames[1], 65, "Making of", event.pathNames[0], event.pathNames[2]);
        }
        
           else if (event.pathNames[1] == "makingof") {
            ha.loadSection(event.pathNames[1], 65, "Making of", event.pathNames[0]);
        }
        
         else if (event.pathNames[1] == "client_area") {
            ha.loadSection(event.pathNames[1], 9, "Client Area", event.pathNames[0]);
        }
        
            else if (event.pathNames[1] == "unsubscribe") {
            ha.loadSection(event.pathNames[1], 9, "Wypisz z subskrypcji / Unsubscribe", event.pathNames[0]);
        }
        
        // ENGLISH END

        else {
            ha.highlight(0);
            ha.loadSection("/php/pagehandle.php?p=error", 1, "Błąd / Error", event.pathNames[2])
        }
        
       
        
        if (noChange != true) {
            ha.doLoad();
        }

    },
    doLoad: function () {
        $("section").fadeOut(300);
        $("#loading").fadeIn(200);

        setTimeout(function () {
            $("section").load(ha.loadFile, ha.onload);
        }, 400);
    },
    onload: function () {
       
        $("section").fadeIn(300);
        $("#loading").fadeOut(200);
        

        $("section img").css({"opacity": "0"});
        $("section img").load(function(){            
            $(this).animate({
            opacity: 1
        }, 200)           
        });
        unsubscribe.init();
        box.init();
        ftp.init();
        
        if($("#nazwa").length){
            
            document.title += " - " + $("#nazwa").html();
            
        }

    },
 
    setTitle : function(tit){
        document.title = ha.title + " - " + tit;
    },
    createLoader : function(){
        
    },
    
    translate : function(_lang){
        if((_lang == "pl")&&(($("#menu11 a").attr("href") != "/en"))){
            $("#wrapper nav").load("/tpl/menu.html", subs.init);
        }
        else if((_lang == "en")&&(($("#menu11 a").attr("href") != "/pl"))){
            $("#wrapper nav").load("/tpl/menu_en.html", subs.init);
        }
        subs.translate(_lang);
    },
    getLang : function(){
        if($("#menu11 a").attr("href") == "/en"){
            return "pl";
        }
        else {
            return "en";
        }
    }
}

var subs = {
    baseval : "",
    newsletterHTML : "<input type=\"text\" name=\"email\" id=\"email\" value=\"twój e-mail\" /><input type=\"image\" id=\"zapisz\" src=\"/images/submit.gif\" />",
    init : function() {
        subs.addForm();
    },
    translate : function(l){
      if(l=="pl"){
        subs.newsletterHTML = "<input type=\"text\" name=\"email\" id=\"email\" value=\"twój e-mail\" /><input type=\"image\" id=\"zapisz\" src=\"/images/submit.gif\" />";
        subs.baseval = "twój e-mail";
      }
      else {
        subs.newsletterHTML = "<input type=\"text\" name=\"email\" id=\"email\" value=\"your e-mail\" /><input type=\"image\" id=\"zapisz\" src=\"/images/submit.gif\" />";
        subs.baseval = "your e-mail";
      }
    },
    addForm : function() {
        $("#menu08 a").addClass("newsletter");
        $("#menu08 a").click(subs.showForm);
        $("#menu08").append("<div id=\"newsletter\">"+subs.newsletterHTML+"</div>");
        subs.baseval = $("#email").val();
        $("#email").focus(subs.handleInput);
        $("#email").blur(subs.handleInput);
        $("#zapisz").click(subs.subscribe);
    },
    handleInput : function(){
        var val = $("#email").val();
        if(val == subs.baseval){
            $("#email").val("");
        }
        else if(val == "") {
             $("#email").val(subs.baseval);
        }
    },
    showForm : function(e) {
        e.preventDefault();
        $("#newsletter").fadeIn(300);
        return false;
    },
    subscribe : function(){
    var email = $("#email").val();
    var str="email="+email;    
    $.post("php/subscribe.php", str, function (data, textStatus)
    {
        if(data == 1){
            if(ha.getLang() == "pl"){
                alert("Zły format adresu e-mail. Spróbuj jeszcze raz.");
            }
            else {
                alert("Wrong format of the e-mail address. Please try again.");
            }
        }
        else if(data == 2){
            if(ha.getLang() == "pl"){
                alert("Podany adres e-mail już znajduje się z naszej bazie.");
            }
            else {
                alert("The e-mail is already subscribed to our newsletter.");
            }
        }
        else if(data == 0){
            $("#newsletter input").hide();
            $("#newsletter").append("<p>Zapisano. Dziękujemy.</p>");
            setTimeout(function(){$("#newsletter").fadeOut(400);},1500);
            setTimeout(function(){$("#newsletter input").show();$("#newsletter p").remove();},1900);
        }
    });
    
}
}

unsubscribe = {
     init : function(){
        if($("#unsubscribe").length){
            $("#unsubscribe").submit(unsubscribe.onsubmit);
            $("#unemail").focus(unsubscribe.onfocus);
            $("#unemail").blur(unsubscribe.onblur);
          
 
           }
     },
     onfocus : function(e) {
        var baseVal = "e-mail";
  
      if($(e.currentTarget).val() == baseVal){
        $(e.currentTarget).val("");
      }
    },

    onblur : function(e){
        var baseVal = "e-mail";
      if($(e.currentTarget).val() == ""){
        $(e.currentTarget).val(baseVal);
      }
    },


    onsubmit : function(){
        var forms = $("#unsubscribe input");
        var str = "email=" + $(forms[0]).val();
       // $("#loginbut").html('<img src="/img/ajax-loader.gif" alt="" />&nbsp;');
        $.post("/php/unsubscribe.php", str, unsubscribe.recPost);
        
        return false;
    },
    
      recPost : function (data, textStatus) {	
      
            alert(data);
	    
	}
    
}

ftp = {
    pass : "",
    init : function(){
        if($("#ftplogin").length){
            $("#ftplogin").submit(ftp.onsubmit);
            $("#login").focus(ftp.onfocus);
            $("#fakepass").focus(ftp.pwdfocus);
            $("#login").blur(ftp.onblur);
            $("#pass").blur(ftp.pwdblur);
            $("#pass").hide();
           }     
    },
    onfocus : function(e) {
        var baseVal;
        if($(e.currentTarget).attr("id") == "login"){
            baseVal = "login";
        }
            
      if($(e.currentTarget).val() == baseVal){
        $(e.currentTarget).val("human-ark_");
      }
    },

    onblur : function(e){
         var baseVal;
        if($(e.currentTarget).attr("id") == "login"){
            baseVal = "login";
        }
      if(($(e.currentTarget).val() == "")||($(e.currentTarget).val() == "human-ark_")){
        $(e.currentTarget).val(baseVal);
      }
    },
    
    pwdfocus : function() {
            $('#fakepass').hide();
            $('#pass').show();
            $('#pass').focus();
        },

    pwdblur : function() {
            if ($('#pass').val() == '') {
                $('#pass').hide();
                $('#fakepass').show();
            }
        },


    onsubmit : function(){
        var forms = $("#ftplogin input");
        var str = "user=" + $(forms[0]).val() + "&pass=" + $(forms[1]).val();
       // $("#loginbut").html('<img src="/img/ajax-loader.gif" alt="" />&nbsp;');
        $.post("ftp/login.php", str, ftp.postftp);
        
        return false;
    },
    postftp : function (data, textStatus) {	
        if(data == "OK"){
	    $("#files").load("ftp/list.php?lang="+ha.getLang(), function(){		  
	    $("#files ul ul").hide();		  
	  //  $("#login").slideUp("fast");		  
	    $("#files").fadeIn("fast");		  
	});
	}
	else {
         //   $("#loginbut").html('<a href="" onclick="ftplogin(); return false;">log in</a>');
            alert("Wrong username or password. Please try again.");
	    }
	},
    toggle : function(i, f){
	    
	    if(f!=undefined){
	       
	       $("#ul"+i+"_"+f).slideToggle();
	       
	    }
	    
	    else {
	       
	        $("#ul"+i).slideToggle();
	       
	    }
	  }    
}


var box = {
    boxHTML : "<div id=\"box\"></div>",
    init : function(){
        $(".alink").bind("click",box.popIt);
        $(document).bind("keyup",box.keyup);
    },
    draw : function(){
      $("body").append(box.boxHTML);  
    },
    popIt : function(e){
        
        e.preventDefault();
        var src = $(e.currentTarget).attr("href")
        var img = "<img src="+src+" alt=\"\" />";
        box.draw();
        
        $("#box").bind("click",box.kill);
        $("#box").hide().append(img);
       // var offset = $("#box").offset();
        setTimeout(function(){$("#box").fadeIn(400)},100);
        setTimeout(function(){$("#wrapper").hide()},500);
        $('html,body').animate({scrollTop: 0}, 600);

    },
    kill : function(){
        $("#box").fadeOut(300,box.remove);
        $("#wrapper").show();
        $('html,body').animate({scrollTop: 0}, 400);
    },
    remove : function(e){
         $("#box").remove();
    },
    keyup : function(event) {
    if($("#box").length){
    if (event.keyCode == 27) {
     box.kill();
    }
    }
    }
}


$.address.init(ha.oninit).change(ha.onchange);  
