var timeout    = 1100;
var closetimer = 0;
var menuitem = 0;
var submenu = 0;
var submenuitem = 0;
var subsubmenu = 0;
var subsubmenuitem = 0;

function menu_open(){
    menu_canceltimer();
    menu_close();

    menuitem = $(this)
    menuitem.addClass('active');

    tab = new Array();
    tab = $(this).attr('id').split('-');
    var top;
    var left;

    top = $(this).offset().top+$(this).height() + 1;
    left = $(this).offset().left;

    submenu = $('#submenu-' + tab[2]);

    $('#submenu-' + tab[2]).css('left',left);
    $('#submenu-' + tab[2]).css('top',top);
    $('#submenu-' + tab[2]).show();
}

function subsubmenu_close(){
    if(subsubmenu)
        subsubmenu.hide();
    if(submenuitem)
        submenuitem.removeClass('active');
}

function submenu_open(){
    menu_canceltimer();
    subsubmenu_close();
    submenuitem = $(this);
    submenuitem.addClass('active');
    tab = new Array();
    tab = $(this).attr('id').split('-');
    var top;
    var left;

    subsubmenu = $('#subsubmenu-' + tab[2]);

    top = $(this).offset().top;
    left = $(this).offset().left + $(this).width();
    $('#subsubmenu-' + tab[2]).css('left',left);
    $('#subsubmenu-' + tab[2]).css('top',top);
    $('#subsubmenu-' + tab[2]).show();

}

function subsubmenu_open(){
    menu_canceltimer();
}
function menu_close(){
    if(submenu)
        submenu.hide();
    if(menuitem)
        menuitem.removeClass('active');
    subsubmenu_close();
}

function menu_timer(){
    closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer(){
    if(closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }

}

$(document).ready(function(){
    $('.menu-item').bind('mouseover', menu_open);
    $('.menu-item').bind('mouseout',  menu_timer);

    $('.submenu-item').bind('mouseover', submenu_open);
    $('.submenu-item').bind('mouseout', menu_timer);

    $('.subsubmenu-item').bind('mouseover', subsubmenu_open);
    $('.subsubmenu-item').bind('mouseout', menu_timer);
    $("a.fancy").fancybox({
        'hideOnContentClick': true,
         showNavArrows : true
    });


    $('#training-menu-content ul.level_1 span').click(function(){
        $('#training-menu-content ul.level_1 span').siblings().slideUp()
        $(this).siblings().slideDown()
    })
    $('ul.level_2 span.harded').parents('.level_2').show();
    
    if($('ul.level_2 span.harded').length == 0) {
	$('.level_2:first').show();
    }



   /* $('div.submenu-item:first-child,div.subsubmenu-item:first-child').each(function(){
        $(this).hover(function() {
            $(this).parent().parent().addClass('active');
        },function(){
            $(this).parent().parent().removeClass('active');
        });
        $(this).addClass('first');
    })

    $('div.submenu-item:last-child,div.subsubmenu-item:last-child').each(function(){
        $(this).hover(function() {
            $(this).parent().addClass('active');
        },function(){
            $(this).parent().removeClass('active');
        });
        $(this).addClass('last');
    })
	*/
    //$('.offer-box').bind('mouseover',function(){$(this).addClass('active')})
   // $('.offer-box').bind('mouseout',function(){$(this).removeClass('active')})

});

document.onclick = menu_close;


