var waitTimer;
$(function() {
    $('div#photofader').hide().height(0);

    $('#menu ul li.firstlevel a.passive')
		    .mouseenter(function() {
		        delClasses();
		        $(this).addClass('active');
		    })

	        .mouseout(function() {
	            //Tab active on page - Tab always visible
	            if (!$(this).hasClass('active') || !$("#menu ul li.firstlevel a.active").parent('li').children('ul').length > 0) {
	                //Scroll tab down
	                $(this).removeClass('active');
	            }

	        });

    // main menu hover handling
    $('div#menu ul li.firstlevel').mouseenter(function() {
        $('div#menu ul li ul').hide();
        if ($(this).children('ul').length > 0) {
            clearTimeout(waitTimer);
            $('div#photofader').animate({ 'height': '275px' }, 300);
            $(this).children('ul').show();
            $(this).children('ul').children('li').children('ul').show();
        }
        else {
            waitTimer = setTimeout(handleMouseLeave2, 500);
        }
    });

    $('div#menu ul li.firstlevel').mouseleave(function() {
        if ($(this).children('ul').length > 0) {
            waitTimer = setTimeout(handleMouseLeave, 500);
        }

    });

    // overlay hover handling
    $('div#photofader').mouseenter(function() {
        if ($(this).height() > 0) {
            clearTimeout(waitTimer);
            $('div#photofader').height(275);
        }
    });

    $('div#photofader').mouseleave(function() {
        waitTimer = setTimeout(handleMouseLeave, 500);
    });

});
		
	delClasses = function() {
	
		$("#menu ul li.firstlevel a").each(function (i) {
	        if ($(this).hasClass("active")) {
			$(this).stop().animate({backgroundPosition:"(left 0%)"}, {duration:200, complete:function(){
						$(this).css({backgroundPosition:"left 0%"})
					}})
	        } 
	        });

		$('#menu ul li.firstlevel').children('a').removeClass('active');
	}

	handleMouseLeave = function() {		
		$('div#menu ul li ul').hide();
		if($("#menu ul li.firstlevel a.active").parent('li').children('ul').length > 0)
		{
			delClasses();			
		}
		
		$('div#photofader').stop().animate({'height':'0px'}, {complete:function() {
			$('div#photofader').hide();
	
		}});
	};

	handleMouseLeave2 = function() {		
		$('div#photofader').stop().animate({'height':'0px'}, {complete:function() {
			$('div#photofader').hide();
	
		}});
	};
	

