$(document).ready(function(){
    $("a[rel=lightbox]").fancybox({
        autoScale: false
    });
    var throughMenuBg = 'url("/images/through-menu-bg.png")';
    
    $("#through-menu ul li").hover(function(){
        if($(this).children('.submenu').is('div')){
            $(this).children('.submenu').show();
            throughMenuBg = ($(this).children('a').css('background-image') != 'none')?$(this).children('a').css('background-image'):'url("/images/through-menu-bg.png")';
            $(this).children('a').addClass('hover').css('background', '#006eb5');
        }
    }, function(){
        if($(this).children('.submenu').is('div')){
            $(this).children('.submenu').hide();
            $(this).children('a').removeClass('hover').css('background', throughMenuBg+' repeat-x');
        }
    });
    
    
    
    var totalSlItems = $('#slider .slider-item').length;
    var thisId = parseInt($('#slider .slider-item.curr').attr('id').substr(9));
    $("#slider .slider-item").not('.curr').hide();
    var timer;
    
    $("#slider").each(function(){
        autoPlay();
    });
    
    $("#slider .left-but").click(function(){
        var nextId = (thisId - 1) <= 0? totalSlItems : (thisId -1);
        slideImg(nextId);
        clearInterval(timer);
        autoPlay();
        return false;
    });
    $("#slider .right-but").click(function(){
        var nextId = (thisId + 1)> totalSlItems? 1 : (thisId + 1);
        slideImg(nextId);
        clearInterval(timer);
        autoPlay();
        return false;
    });
    function autoPlay(){
        timer = setInterval(function(){ 
            nextId = thisId+1;
            if(nextId > totalSlItems) nextId = 1;            
            slideImg(nextId);
        }, 5000);
    }
    
    function slideImg(nextId){
        $('#sliderId-'+thisId).removeClass('curr').fadeOut('slow');
        $('#sliderId-'+nextId).addClass('curr').fadeIn('slow');
        thisId = nextId;        
    }
});
