var activeLine=0;
var visibleElem=0;
var activeTab=1;
var k=0;
var c=0;
var vEVal=[0,0,0,0];
var tabs=["icons","iphone","interfaces"];
var Links=[[],[],[]];
var visiblePhoto=1;
var sm=0;
function aLinksFilling(){
    var alt="";
    for(var i=0;i<Links.length;i++){
        for(var z=1;z<=$("div#"+tabs[i]+" img").size();z++){
            alt=$("div#"+tabs[i]+" img."+z.toString()).attr("alt");
            Links[i].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"){
        //var b=a.split("_");
        var b=q.split("_");
        b[0]=b[0].substr(b[0].indexOf("#")+1,100);
        activeLine=arrayIndexOf(tabs, b[0]);
        visibleElem=arrayIndexOf(Links[activeLine], b[1]);
        c=-480*activeLine;
        k=-940*visibleElem;
        $("div.images-container").css({marginTop:c+"px"});
        $("div#"+tabs[activeLine]).css({marginLeft:k+"px"});
    }

}
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]+"#"+tabs[activeLine]+"_"+Links[activeLine][visibleElem];
}

function initSelect(){
    var id="";
    for(var i=0;i<=($("div#"+tabs[activeLine]+" img").size()-1);i++){
        $("ul.selects li#s_"+i.toString()).click(function(){
            id=$(this).attr("id");
            id=parseInt(id.substr(id.indexOf("_")+1,10));
            if(id>=visibleElem){
                k=k-940*(id-visibleElem);
                $("div#"+tabs[activeLine]).animate({marginLeft:k},'1000');
            }
            else{
                k=k+940*(visibleElem-id);
                $("div#"+tabs[activeLine]).animate({marginLeft:k},'1000');
            }
            visibleElem = id;
            updateSelects();
        })
    }
}
function arrowRight(){
    if(visibleElem==($("div#"+tabs[activeLine]+" img").size()-1)){
        k=0;
        $("div#"+tabs[activeLine]).animate({marginLeft:k},'2000');
        visibleElem=0;
    }
    else{
        visibleElem=visibleElem+1;
        k=-940*visibleElem;
        $("div#"+tabs[activeLine]).animate({marginLeft:k},'1000');

    }
    updateSelects();
}
function arrowLeft(){
    if(visibleElem==0){
        k=-940*($("div#"+tabs[activeLine]+" img").size()-1);
        $("div#"+tabs[activeLine]).animate({marginLeft:k},'2000');
        visibleElem=$("div#"+tabs[activeLine]+" img").size()-1;
    }
    else{
        visibleElem=visibleElem-1;
        k=-940*visibleElem;
        $("div#"+tabs[activeLine]).animate({marginLeft:k},'1000');
    }
    updateSelects();
}
function initImage(){
   $("div.next a").click(function(){arrowRight();});
   $("div.previous a").click(function(){arrowLeft();});
}
function updateTab(){
    if($("ul.tabs li a").hasClass("active")){$("ul.tabs li a").removeClass("active")}
    $("a#tab_"+activeLine.toString()).addClass("active");
}
function Tabs(x){
    vEVal[activeLine]=visibleElem;
    activeLine=x;
    visibleElem=vEVal[activeLine];
    k=-940*visibleElem;
    c=-480*activeLine;
    $("div.images-container").animate({marginTop:c},"4000");
    updateTab();    
}
function initTabs(){
    var id ="";
    for(var n=0;n<=3;n++){
        $("a#tab_"+n.toString()).click(function(){
            id=$(this).attr("id");
            id=parseInt(id.substr(id.indexOf("_")+1,10));
            Tabs(id);
        })
    }
}
function keyPress(){
    $(document).keydown(function(event){        
        if(event.keyCode==37){
            event.preventDefault();
            arrowLeft();
        }
        if(event.keyCode==39){
            event.preventDefault();
            arrowRight();
        }
        if($.browser.opera!==true){
            if(event.keyCode==38&&event.ctrlKey){
                event.preventDefault();
                if(activeLine==0){
                    Tabs(2);
                }
                else{
                    Tabs(activeLine-1);
                }
            }
            if(event.keyCode==40&&event.ctrlKey){
                event.preventDefault();
                if(activeLine==2){
                    Tabs(0);
                }
                else{
                    Tabs(activeLine+1);
                }
            }
        }
})
}
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 sImages(){$("div.images-container").removeClass("hide");}
$(document).ready(aLinksFilling);
$(document).ready(address);
$(document).ready(navigation);
$(document).ready(initPhoto);
$(document).ready(updatePhotoPanel);
$(document).ready(initTabs);
$(document).ready(updateTab);
$(document).ready(keyPress);
$(document).ready(initArrows);
$(document).ready(initImage);
$(document).ready(initSelect);