var visibleElem=0;
var k=0;
var Links=[];
var visiblePhoto=1;
var sm=0;
var timeR = null;
var to=null;
function aLinksFilling(){
    var alt="";
    for(var z=1;z<=$("div#works img").size();z++){
        alt=$("div#works img."+z.toString()).attr("alt");
        Links.push(alt);
    }
}
function arrayIndexOf(array, value){
    for(var j = 0; j < array.length; j++){
        if(array[j] == value){
            return j;
        }
    }
    return null;
}
function address(){
    var a = window.document.location.href;
    var q = a.toString();
    var s = q.substr(q.indexOf("#")+1,100);
    if(q.indexOf("#")!==-1&&s!=="contact"&&s!=="services"&&s!=="about"&&s!=="mm"){
        visibleElem=arrayIndexOf(Links, s);
        k=-940*visibleElem;
        $("div#works").css({marginLeft:k+"px"});
    }
}
function visibleImage(){
    var e=$("div#works"+" img."+(visibleElem+1).toString());
    var a=(visibleElem+1).toString()+"-work.png";
    if(visibleElem+1<=9){a="0"+a}
    a="images/"+a;
    e.attr("src",a);
    e.attr("style","");
    $("div.images-container div div").removeClass("hide");
}
function initArrows(){
    $("div.previous a").hide(0);
    $("div.next a").hide(0);
    $("div.product").mousemove(function(e){
        if(e.pageX<=parseInt($("body").width())/2){
            if(!($.browser.msie && parseFloat($.browser.version)<7.0)){$("div.previous a").fadeIn(350);$("div.next a").fadeOut(350)}
            else{$("div.previous a").show(0);$("div.next a").hide(350)}
        }
        else{
            if(!($.browser.msie && parseFloat($.browser.version)<7.0)){$("div.next a").fadeIn(350);$("div.previous a").fadeOut(350)}
            else{$("div.next a").show(0);$("div.previous a").hide(0)}
        }
    });
    $("div.product").hover(function(){},function(){
        if(!($.browser.msie && parseFloat($.browser.version)<7.0)){$("div.previous a").fadeOut(300);$("div.next a").fadeOut(300);}
        else{$("div.previous a").hide(300);$("div.next a").hide(300);}
    });

}
function updateSelects(){
    if($("ul.selects li a").hasClass("active")){$("ul.selects li a").removeClass("active")}
    $("li#s_"+visibleElem.toString()+" a").addClass("active");
    var b=window.document.location.href.split("#");
    window.document.location.href=b[0]+"#"+Links[visibleElem];
}
function initSelects(){
    $("ul.selects li").hide(0);
    for(var i=0;i<=($("div#works img").size()-1);i++){$("li#s_"+i.toString()).show(0);}
    updateSelects();
    timeR=setInterval("timeRefresh()",10000)
}
function initSelect(){
    var id="";
    for(var i=0;i<=($("div#works img").size()-1);i++){
        $("ul.selects li#s_"+i.toString()).click(function(){
            id=$(this).attr("id");
            id=parseInt(id.substr(id.indexOf("_")+1,10));
            k=-940*(id);
            $("div#works").animate({marginLeft:k},500);
            visibleElem = id;
            updateSelects();
            rTimer()
        })
    }
}
function arrowRight(){
    if(visibleElem==($("div#works img").size()-1)){
        k=0;
        $("div#works").animate({marginLeft:k},500);
        visibleElem=0;
    }
    else{
        visibleElem=visibleElem+1;
        k=-940*visibleElem;
        $("div#works").animate({marginLeft:k},500);

    }
    updateSelects();
}
function arrowLeft(){
    if(visibleElem==0){
        k=-940*($("div#works img").size()-1);
        $("div#works").animate({marginLeft:k},500);
        visibleElem=$("div#works img").size()-1;
    }
    else{
        visibleElem=visibleElem-1;
        k=-940*visibleElem;
        $("div#works").animate({marginLeft:k},500);
    }
    updateSelects();
}
function initImage(){
   $("div.next a").click(function(){arrowRight();});
   $("div.previous a").click(function(){arrowLeft();});
}
function keyPress(){
    $(document).keydown(function(event){        
        if(event.keyCode==37){
            event.preventDefault();
            arrowLeft();
            rTimer();
        }
        if(event.keyCode==39){
            event.preventDefault();
            arrowRight();
            rTimer();
        }
    })
}
function updatePhotoPanel(){
    var c=$("div.photos-block img#p_"+visiblePhoto.toString()).attr("alt");
    $("div.p-description").text(c);
    var d="("+visiblePhoto.toString()+" of "+$("div.photos-block img").size().toString()+")";
    $("li.photo-numbers").text(d);
}
function renderPhoto(y){
    var b =$("div.photos-container");
    visiblePhoto=y;
    sm=-540*(y-1);
    b.animate({marginLeft:sm},1000);
    updatePhotoPanel();
}
function initPhoto(){
    var s=$("div.photos-block img").size();
    $("a.p-previouse").click(function(){
        if(visiblePhoto==1){
            renderPhoto(s)
        }
        else{
            renderPhoto(visiblePhoto-1)
        }
    });
    $("a.p-next").click(function(){
       if(visiblePhoto==s){
           renderPhoto(1)
       }
       else{
           renderPhoto(visiblePhoto+1)
       }
    });
}
function navigation() {
    $("a.ancLinks").click(function(){
        var elementClick = $(this).attr("href");
        var destination = $(elementClick).offset().top;
        if($.browser.opera){$("html").animate({ scrollTop: destination}, 800);}
        else{$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 800, function(){window.location.hash = elementClick});}
        return false;
    });
}
function timeRefresh(){
    if(visibleElem==($("div#works img").size()-1)){
        visibleElem=0;
        $("div#works").animate({marginLeft:0},800);
    }
    else{
        visibleElem=visibleElem+1;
        $("div#works").animate({marginLeft:-940*visibleElem},800);
    }
    updateSelects();
}
function rTimer(){
    clearInterval(timeR);
    clearTimeout(to);
    to=setTimeout(function(){timeR=setInterval("timeRefresh()",10000)},2000);
}
function stopRefresh(){
    $("div.images-container").hover(function(){
        if(to!=null){clearTimeout(to);}
        clearInterval(timeR);
    },function(){
        if(to!=null){clearTimeout(to);}
        to=setTimeout(function(){timeR=setInterval("timeRefresh()",10000)},2000);
    });
    $("div.product a").hover(function(){
            if(to!=null){clearTimeout(to);}
            clearInterval(timeR);
    },function(){
            if(to!=null){clearTimeout(to);}
            to=setTimeout(function(){timeR=setInterval("timeRefresh()",10000)},2000);
    });
}
$(document).ready(stopRefresh);
$(document).ready(aLinksFilling);
$(document).ready(address);
$(document).ready(navigation);
$(document).ready(initPhoto);
$(document).ready(updatePhotoPanel);
$(document).ready(keyPress);
$(document).ready(initArrows);
$(document).ready(initImage);
$(document).ready(initSelects);
$(document).ready(initSelect);