//button_link
$( window ).load(function(){	
    var width = $(document).width();
    var height = $(document).height();
    if(width<=900 || height<=500){
        $("html").css("overflow","auto");
        //	$("body").css("overflow","auto");	
    }
    //
});
$(document).ready(function(){

    var hash= window.location.hash;

    if(hash.length>2){
        triger++;
        href=hash.split("#").join("");
        href="/"+lang+"/"+href;

        $(document).stop(false,true);





        $("#cContainer").fadeOut(500,function(){            
            $("#cContainer").empty().css("background","url(/images/loader.gif) no-repeat center 200px").show();            
            jQuery.get(href,{ajax:"1"}, function(data) {
                $("#cContainer").fadeOut(500,function(){
                    $("#cContainer").css("background","none").html(data).fadeIn(500);
                });
            })
        });
    } 

});

$(window).resize(function(){
    resizeEvent();
});
$(document).ready(function(){
    Cufon.replace('.language_bar a',{ fontFamily: 'OptimaLT' });

    Cufon.replace('.footer .text',{ fontFamily: 'HelveticaLT Thin' });

    //	$(document).pngFix();


    $(".topLine").mousemove(function(e){
        var pageWidth = $(document).width();
        var buttonWidth = $(".button").width();
        var kelintas = Math.floor(e.pageX/buttonWidth);
        makeMouseOver(kelintas);
    });

    $(".content").mousemove(function(e){
        makeMouseOver($("a[selected=1]").attr("btn"));

    });

    $(".header_lt").mousemove(function(e){
        makeMouseOver($("a[selected=1]").attr("btn"));
    });
    $(".header_en").mousemove(function(e){
        makeMouseOver($("a[selected=1]").attr("btn"));
    });
    $(".header_ru").mousemove(function(e){
        makeMouseOver($("a[selected=1]").attr("btn"));
    });	
    $(".topline").mousemove(function(e){
        makeMouseOver($("a[selected=1]").attr("btn"));
    });

});



function makeMouseOver(link){
    $("a[btn]").attr("class","button_link");	
    $("a[btn="+link+"]").attr("class","linkOnOver");
    //	$("a[btn="+link+"]").html(data["buttons"][0][link]);

    if(lang=="ru")	Cufon.replace($("a[btn]"),{ fontFamily: 'RuButtonFont'}); else
        Cufon.replace($("a[btn]"),{ fontFamily: 'OptimaLT' });

}
var triger = 0;

function startChangePhoto(id,lang){
    $("body").css("overflow","hidden");
    //

    triger++;
    changePhoto(id,lang,triger);
    jQuery.get("/"+lang+"/",{ajax:"photoList"}, function(data) {
        $("#apaciaHomepage").append(data);
        $(document).oneTime(100,function(){
            $("#divContainer").fadeOut("fast").css("background","none").fadeIn("fast");
            $("#pageContainer").css("top",( $("#photoContainer").height() + 90)+"px").fadeIn(1000);
        });


    });

}

function changePhoto(id,lang,t){

    if(t!==triger) return;
    $(document).stop(false,true);
    var documentWidth= $(document).width();
    var documentHeight= screen.height;
    jQuery.getJSON('/'+lang+'/',{photo:id,ajax:"1",width:documentWidth,height:documentHeight}, function(json) {
        if(t!==triger) return;
        $("#photoContainer").css("background","url("+json["background"]+") no-repeat center bottom");
        $(".page").attr("class","page");
        $("div[btnid="+json["oldId"]+"]").attr("class","page active");
        $("#realBackground").css("background","url("+json["background"]+") no-repeat center bottom").fadeIn(1000,function(){
            if(t!==triger) return;
            $("photoContainer").show();
            if(t!==triger) return;
            $("#realBackground").append("<h1 class='title' style='display:none' ></h1>");

            $(".title").html(json["text"]);

            Cufon.replace($("h1.title"),{textShadow: '#000 1px 1px'},{ fontFamily: 'HelveticaLT Thin' })
            if(t!==triger) return;
            $(document).oneTime(10,function(){


                var width = $("h1.title").width();
                var height = $("h1.title").height();

                var documentWidth= $(document).width();
                var documentHeight= $("#realBackground").height();





                var fromLeft = Math.round(documentWidth/100)*json["fromLeft"];
                var fromTop = Math.round(documentHeight/100)*json["fromTop"]+120;

                if((fromLeft+width)>documentWidth) fromLeft = documentWidth-10-width;
                if((fromTop+height)>documentHeight) fromTop = documentHeight-10;
                $(".title").css("top",fromTop+"px");
                $(".title").css("left",fromLeft+"px");
                $("h1.title").show();
                if(t!==triger) return;
                $("#photoContainer").show().fadeOut(1000,function(){

                    if(t!==triger) return;
                    $(document).oneTime(json["time"],"slider",function(){
                        if(t!==triger) return;
                        $("#photoContainer").fadeIn(1000,function(){
                            $("h1.title").remove();
                            if(t!==triger) return;
                            $("#photoContainer").fadeOut(1000);
                            $("#realBackground").fadeOut(1000,function(){
                                if(t!==triger) return;
                                changePhoto(json["id"],lang,t);
                            });
                        });
                    });	
                });	
            });
        });	

    });	


    return;	



}

function chngPht(id,lang){
    triger++;
    changePhoto(id,lang,triger);
}
function parduodame(lang,t){

    $("#center").stopTime("timer");

    jQuery.facebox(function() {
        jQuery.get($(t).attr("href"), function(data) {
            jQuery.facebox(data);
        })
    })
}

function video(t){
    $("#center").stopTime("timer");
    jQuery.facebox(function() {
        jQuery.get($(t).attr("href"), function(data) {
            jQuery.facebox(data);
        })
    });
    return false;
}

function makeAjax(t){
    triger++;

    $(document).stop(false,true);
    var link = $(t);
    var href = $(link).attr("href");
    
    href=href.split("http://www.altas-auto.lt").join("");

     href=href.split("http://www.altas-auto.lt").join("");
     href=href.split("ФОТОАЛЬБОМ").join("fotoalbom");
    
    window.location.hash = href.split("/"+lang+"/").join("");    


    $("#cContainer").fadeOut(500,function(){			
        $("#cContainer").empty().css("background","url(/images/loader.gif) no-repeat center 200px").show();			
        jQuery.get(href,{ajax:"1"}, function(data) {
            $("#cContainer").fadeOut(500,function(){
                $("#cContainer").css("background","none").html(data).fadeIn(500);
            });
        })
    });


}

function checkSize(){
    var turinys = $("#kairePuseVidus").html();
    var turinys2 = $("#textBlock").html();
    $("#textBlock").empty();
    $("#kairePuseVidus").empty();
    $(document).oneTime(25,function(){
        var aukstis = $("#cContainer").height()-82;
        $("#contentContainer").css("height",aukstis+"px");


        $("#textBlockParent").css("height",(aukstis-55)+"px").css("width",($(".button").width()*4-40)+"px");
        jQuery("#textBlockParent.apie-mus").css("height",(aukstis-35)+"px");


        $("#contactApaciaBlock").css("height",(aukstis-55)+"px").css("margin-left",'10px').css("width",($(".button").width()*4-50)+"px");


        $("#textBlock").css("height",(aukstis-65)+"px").html(turinys2).jScrollPane({dragMinHeight: 46,dragMaxHeight: 46});
        if( $("#textBlock")[0])
            $("#textBlock")[0].scrollTo(0);

        var plotis = $(".topLine").width() - $(".button").width()*4-7;
        var iskaires = $(".button").width()*4;
        $("#kairePuse").css("height",(aukstis+74)+"px").css("width",plotis+"px").css("left",iskaires+"px");
        $("#kairePuseVidus").css("height",(aukstis+61)+"px").css("width",(plotis-9)+"px").html(turinys).jScrollPane({dragMinHeight: 46,dragMaxHeight: 46});

    });

}
function changeGalleryPhoto(t,img){
    $(".itemImage").css("border","2px solid #212121");
    $(t).css("border","2px solid #0f80bd");

    $("#photoContainer").fadeOut('fast',function(){
        $("#photoContainer").css("background","url("+img+") no-repeat center");		
        $("#photoContainer").fadeIn();
    });
}

function showFirst(t){
    triger++;
    $(document).stop(false,true);
    var link = $(t);
    var href = $(link).attr("href");
    jQuery.get(href,{ajax:"2"}, function(data) {
        $(".content").fadeOut(500,function(){
            $(".content").html(data)
            $(".content").fadeIn(500);

        });

    })
}


function changeText(link,container){

    window.location.hash = $(link).attr("href").split("/"+lang+"/").join("");            
    jQuery.get($(link).attr("href"),{ajax:"2"}, function(data) {
        //$(container).removeAttr("filter");
        $(container).css("background-color","#E3E3E3");
        $(container).fadeOut(500,function(){

            $(container).html(data).jScrollPane({dragMinHeight: 46,dragMaxHeight: 46,scrollTo:0});
            $(container).fadeIn(500, function() {
                jQuery(this).css("background-color","transparent");
            });

        });


    });
}

function rasom(kam,kur,lang){
    var href="/"+lang+"/forma-"+kur+".html";
    jQuery.get(href,{ajax:"3",kam:kam}, function(data) {

        $("#kontaktuKonteineris").fadeOut(500,function(){

            $("#kontaktuKonteineris").html(data)
            $("#kontaktuKonteineris").fadeIn(500);
            $("#kairePuseVidus").jScrollPane({dragMinHeight: 46,dragMaxHeight: 46,scrollTo:0});

        });


    });
}

function siusti(kam,kur,lang){
    var href="/"+lang+"/forma-"+kur+".html?ajax=3&kam="+kam;
    jQuery.post(href,$("#kontaktuForma").serialize(), function(data) {
        $("#kontaktuKonteineris").fadeOut(200,function(){

            $("#kontaktuKonteineris").html(data).fadeIn(200);

        });


    });
}

function goBack(link){
    $("#kontaktuKonteineris").fadeOut(500,function(){
        jQuery.get(link,{grizti:"1"}, function(data) {

            $("#kontaktuKonteineris").html(data)
            $("#kairePuseVidus").jScrollPane({dragMinHeight: 46,dragMaxHeight: 46,scrollTo:0});
            $("#kontaktuKonteineris").fadeIn(500);

        });

    });
}

function slideMenu(x){
    $("div.button a").each(function(i){$("a[btn="+i+"]").attr("selected","none");});
    $(".button").animate({ 
        top: "0px"
    }, 200 , function(){
        $("a[btn="+x+"]").attr("selected","1");
        var div = $(".button")[x];
        $(div).animate({ 
            top: "-2px"
        }, 200 );
        makeMouseOver($("a[selected=1]").attr("btn"));
    });
}

function getBack(link){
    jQuery.get(link,{ajax:"1"}, function(data) {
        $("#cContainer").fadeOut(500,function(){
            $("#cContainer").empty().css("background","url(/images/loader.gif) no-repeat center 200px").show();

            $("#cContainer").fadeOut(500,function(){
                $("#cContainer").css("background","none").html(data).fadeIn(500);
            });

        });
    })
}

function moveUp(){

    $("#text").animate(
    {top: (180-$("#text").height())},
    1000
    );
}
function moveDown(){
    $("#text").animate(
    {top: 0},
    1000
    );

}
function stopmove(){
    $("#text").stop(false,false);
}
function kontaktuFormavimas(){
    $(document).oneTime(10,function(){
        //	jQuery("#cContainer").pngFix();
        var aukstis = $("#cContainer").height()-82;


        $("#contentContainer").css("height",aukstis+"px");

        $("#kontaktuKairePuse").css("width",($(".button").width()*4-30)+"px");

        $("#kontaktuKairePuse").css("height",(aukstis-10)+"px");
        var plotis = $(".topLine").width() - $(".button").width()*4-3;
        var iskaires = $(".button").width()*4;
        $("#kairePuse").css("height",(aukstis+78)+"px");
        $("#kairePuse").css("width",plotis+"px");
        $("#kairePuse").css("left",iskaires+"px");


        $("#kairePuseVidus").css("height",(aukstis+68)+"px");
        $("#kairePuseVidus").css("width",(plotis-2)+"px").jScrollPane({dragMinHeight: 46,dragMaxHeight: 46});

        var aukstis = $("#cContainer").height()-220;
        $("#textBlock").css("height",aukstis+"px").css("width",($(".button").width()*4-55)+'px').jScrollPane({dragMinHeight: 46,dragMaxHeight: 46});   
        //$("#textBlock").css({"height":(aukstis-0)+"px"});

        var plotis = $(".button").width()*4;



    });

}

function contentDivSize(){
    var height = $(window).height();
    $(".content").css("height",(height-150)+"px");
    $("#cContainer").css("height",(height-150)+"px");
    $("#photoContainer").css("height",(height-232)+"px");
    $("#divContainer").css("height",(height-232)+"px");
    $("#realBackground").css("height",(height-232)+"px");

}
function preloadImages(lang,eventas){

    var documentWidth= $(document).width();
    var documentHeight=screen.height;


    jQuery.getJSON('/'+lang+'/',{ajax:"preloader",width:documentWidth,height:documentHeight}, function(json) {
        var c = json.length;
        preloadImageRecursion(json,0,c,eventas);
    });

}
function preloadImageRecursion(json,id,total,callback){
    if(id==total){eval(callback); return;}

    jQuery("<img>").attr("src", json[id]);


    preloadImageRecursion(json,(id+1),total,callback);

}
function resizeEvent(){
    var width = $(document).width();
    var height = $(document).height();
    if(width<=900 || height<=500){
        $("html").css("overflow","auto");
        //	$("body").css("overflow","auto");	
    }else{
        $("html").css("overflow","hidden");
    }
    contentDivSize();
    $("#pageContainer").css("top",( $("#photoContainer").height() + 90)+"px");
}



