﻿$(
    function(){                
        var leftTabs = "div.leftTab";
        var rightTabs = "div.rightTab";
        var allTabs = "div.leftTab, div.rightTab";  
        
        var index = 8;                
        $(allTabs).click(function(){
                var url = $(this).attr("url");
                if(url != null)
                    window.location = url;
            }
        );
        
        $(leftTabs).each(function(inteLem, objLink){                                        
                var tab = $(this);
                tab.data("url", "");
                
                tab.hover(function(){
                        if(index != inteLem){                                    
                            changePic(inteLem); 
                        }
                        removeStyles();
                        tab.addClass("hoverLeftTab");  
                        index = inteLem;
                    }
                );

            }
        );   
        
        
        $(rightTabs).each(function(inteLem, objLink){
                var tab = $(this);
                tab.hover(function(){    
                        var checkIndex = inteLem + 4; 
                        if(index != checkIndex){
                            changePic(checkIndex); 
                        }
                        index = checkIndex;
                        removeStyles();
                        tab.addClass("hoverRightTab");                                                               
                    }
                );
            }
        ); 
        $("div.pic").bind("click", GotoSwitch);
        
        $("div.dhl a").hover(function(){
            if(index != 8){
                removeStyles();
                changePic(8);
                index = 8;
            }
            $("div.pic").bind("click", GotoSwitch);
        });
        
        var tabCount = $(leftTabs).size();
        tabCount += $(rightTabs).size();
        $.preloadImages(tabCount);                                     
    }
                
)

function GotoSwitch() {
    var elem = $("div.dhl a");
    window.location = elem.attr("href");
}

function changePic(index){
    var picsUrl = 'App_Themes/Normal/Images/HomePics/pic' + index + '.jpg';
    var innerDiv = '#picContent' + index;
    $("div.pic")
        .stop(true, true)
        .fadeOut(250, function(){
                $(this).empty();
                $(innerDiv).clone().appendTo(this);
                $(this)                    
                    .css({"background-image": "url(" + picsUrl + ")"})
                    .fadeIn(500)                                      
            }
        );
    $("div.pic").unbind('click');
} 

function removeStyles(){
    $("div.leftTab").removeClass("hoverLeftTab");
    $("div.rightTab").removeClass("hoverRightTab");
    
}

$.preloadImages = function(number){ 
    for(var i = 0; i<number; i++){
        var picsUrl = 'App_Themes/Normal/Images/HomePics/pic' + i + '.jpg';
        $("<img />").attr("src", picsUrl);        
    }
   
}
