// var root_path = '';   declare globally !!!!!

$(function(){
    
    
    // Preload images
    var i = 0;
    var oImage  = new Array();
    
    
    // Submemu
    var menu     = $('#menu');
    var submenus = $('#submenu .submenu');
    var posMenu  = menu.offset();
    var timer    = null;
    var timer2   = setTimeout(function(){submenus.fadeOut('slow'); },1700); 
    var timer3   = null;

    $('#menu ul li a').hover(
        function(){
            removeCurActive($('#menu ul li a'));
            removeActive($('#menu ul li a'));
            var curmenu = $(this);
            curmenu.addClass('menu-'+curmenu.attr('class')); 
            if (timer2) clearTimeout(timer2);
            if (timer) clearTimeout(timer);
            submenus.hide();
            var subMenu = getSubmenus( this );    
            subMenu.slideDown('fast', function(){
                if(submenus.filter(':visible').length > 1)
                {
                     submenus.hide();
                }
            });
        },
        function(){
            var subMenu = getSubmenus( this );   
            timer = setTimeout(function(){subMenu.hide( 1,function(){
                removeActive($('#menu ul li a'));
                restoreCurActive($('#menu ul li a')); 
            });
            },100);
        }
    );
     
    submenus.hover(
        function(){
            if (timer) clearTimeout(timer);
            if (timer2) clearTimeout(timer2);
        },
        function(){
            timer2 = setTimeout(function(){
                removeActive($('#menu ul li a'));
                restoreCurActive($('#menu ul li a'));
                submenus.fadeOut('slow'); 
            },1700);   
        }
    );
    
    
    
    // Scroll navigation buttons
    $('.nav .btnnext').click(function(){
        $('.bigimg ul li:first-child').hide().next().css('display','block').end().appendTo('.bigimg ul');
    });
    
    $('.nav .btnprev').click(function(){
        $('.bigimg ul li:first-child').hide().siblings(':last-child').css('display','block').prependTo('.bigimg ul');
    });
    
    
    //  Slide show
    var w = $('.preview').width();  // container width
    var h = $('.preview').height(); // container height
   
    w = w ? w : 197;
    h = h ? h : 125;
    
    $('.simpleSlideShow').slideShow({
                    interval : 4,
                    slideSize:{ width:w, height:h }
    }).auto(); 
    
});

function getSubmenus( topMenu )
{
        var arr = $(topMenu).attr('class').split(' ')[0].split('-');
        return $('.sub-' + arr[1]);
}
function removeActive( topMenus )
{
     $(topMenus).each(function(i,obj){
                var item = $(obj);
                var arr = item.attr('class').split(' ');
                if(arr.length > 1)
                {
                    item.removeClass(arr[1]);
                }
     });
}
function removeCurActive(topMenus)
{
    topMenus.parent().each(function(i,obj){
        var obj = $(obj);
        if(obj.attr('class').length > 0)
        {
            $('body').data('class',obj.attr('class')).data('index',i);
            obj.attr('class','');
        }
    });
}
function restoreCurActive(topMenus)
{
    topMenus.parent().eq($('body').data('index')).attr('class',$('body').data('class'));
}
