var categoryMenuTimer;
var mousePosX, mousePosY;

$(document).ready(function() {
	$(document).pngFix();
	
	$('.mediumBeschriftung').each(function() {
		$(this).parent().prev('a').attr('title', $(this).text());
	});
	
	// FancyBox starten
	$("div.text a[rel='lytebox[group]']").fancybox();
	$("a[rel='fancybox']").fancybox();
	
	$('ul.categoryMenu > li').each(function() {
		var subMenu = $(this).children('ul');
		
		$(this).mouseenter(function() {
			$(subMenu).show();
		}).mouseleave(function() {
			if(categoryMenuTimer) { clearTimeout(categoryMenuTimer); }
			categoryMenuTimer = window.setTimeout("OnCategoryMenuTimer()", 500);
		});
		
		$(this).mousemove(function(e) {
			if(e.pageY > $(this).offset().top && e.pageY < $(this).offset().top + 40)
			{
				
			}
			else
			{
				if(categoryMenuTimer) { clearTimeout(categoryMenuTimer); }
				categoryMenuTimer = window.setTimeout("OnCategoryMenuTimer()", 500);
			}
		});
	});
	
	$('ul.navigation > li').each(function() {
		var subMenu = $(this).children('ul');
		
		if(subMenu.length > 0)
		{
			$(this).mouseenter(function() {
				$(subMenu).show();
			}).mouseleave(function() {
				$(subMenu).hide();
			});
		}
	});
	
	// Mausposition speichern
	$().mousemove(function(e) {
		mousePosX = e.pageX;
		mousePosY = e.pageY;
	});
	
	$('ul.categoryMenu ul:has(a.active)').show();
});

function OnCategoryMenuTimer()
{
	// Verstecke alle SubMenus, die kein aktiven a Tag haben
	$('ul.categoryMenu ul:visible:not(:has(a.active))').each(function() {
		if(mousePosX < $(this).offset().left || mousePosX > ($(this).offset().left + $(this).width()))
		{
			$(this).hide();
		}
		
		if(mousePosY < $(this).offset().top || mousePosY > ($(this).offset().top + $(this).height() + $(this).parent('li').height()))
		{
			$(this).hide();
		}
	});
}

/* Menu code */

var menu_hide_timeout = 500;
document.write(
	'<style type="text/css">' + 
	'.popup_hover {display:none;}  ' + 
	'</style>'
);

$(document).ready(function(){
	var hideExecute;
	$('#menu ul.popup_hover').css('display','none').removeClass("popup_hover").addClass("popup_hover_js").hover(
		function() {
			$(this).addClass("thisisvisible_h");
		}, function() {
			$(this).removeClass("thisisvisible_h");
			if (hideExecute) clearTimeout(hideExecute);
			hideExecute = window.setTimeout("menu_hide()", 500);
		}
	);
	$('#menu a.popup_menu').hover(
		function() { //mouseover
			$(this).parent().parent().children("li").children("ul").hide();
			$(this).next("ul").show().addClass("thisisvisible_a");
			$(this).next("ul").children("li").children("ul").hide();
		}, function() {
			$(this).next("ul").removeClass("thisisvisible_a")
			if (hideExecute) clearTimeout(hideExecute);
			hideExecute = window.setTimeout("menu_hide()", 500);
		}
	);
});

function menu_hide(oh) {
	var elms = $('#menu ul.popup_hover_js:visible').not('.thisisvisible_h, .thisisvisible_a'), to_hide = elms.length;
	if (oh != to_hide && to_hide > 1 && $.browser.msie) {
		var one_hidden = false;
		for (var i = 0; i < to_hide; i++) {
			if ($(elms.get(i)).children("li").children("ul").not('.thisisvisible_h, .thisisvisible_a').length == 0) {
				$(elms.get(i)).hide();
				one_hidden = true;
				break;
			}
		}
		if (one_hidden) menu_hide(to_hide);
		else $('#menu ul.popup_hover_js:visible').not('.thisisvisible_h, .thisisvisible_a').hide();
	} else {
		$('#menu ul.popup_hover_js:visible').not('.thisisvisible_h, .thisisvisible_a').hide();
	}
}
