function prealert(s){
	var q="";
	for (key in s) q+= key+" = "+s[key]+"\n";
	alert(q);
}

$(document).ready(function(){

	var old_id = 0;
	var oneIsActive = false;
	var minheight = 33;
	var maxheight = 50;
	var fadeSpeed = "slow";
	var containerheight = $('#primNav').height();
	var anz_topmenus = 0;
	var linkTargetURL = "#";
	var openedSubsubmenu = "";
	
	// Jedem LI auf Ebene 1 eine ID geben
	$('.navitem_level1:first').addClass('navitem_level1_first');
	$('.navitem_level1:last').addClass('navitem_level1_last');
	
	$('.navitem_level1').each(function(){
		$(this).attr("id", "mainItem"+anz_topmenus);
		anz_topmenus++;
	});
	
	var x=0;
	// Prüfen, ob ein Submenüeintrag aktiv ist.
	$('.submenu').each(function(){
		// if ($(this).children("li").hasClass("active")) {
		if ($(this).parent().hasClass("active")) {
			oneIsActive = true;
		}
		else {
			// $(this).slideUp();
			// $(this).hide();
		}
		
		
	});
	// Wenn ein Submenü aktiv ist, wird dieses angezeigt, und die Items auf Ebene 1 verkleinert.
	if (oneIsActive){
		// $('.navitem_level1').css("height", minheight+"px");
		$('.navitem_level1 a.level1').css("height", minheight+"px");
		// $('#primNav .active').css("height", maxheight+"px");
		$('#primNav .active a.level1').css("height", maxheight+"px");
	}
	
	
	$('#primNav a.level1').bind('click', function(){ // JEDEN LINK AUF EBENE 1 DURCHGEHEN UND AKTIONEN ZUWEISEN
		linkTargetURL = $(this).attr("href");
		
		if ($(this).parent().hasClass("active")) {
			var thisHasClassActive = true; // wird für ganz unten benötigt, um mitzuteilen, dass der aktuelle Link schon geöffnet ist.
		}
		
			// var height = $(this).children("span").height();
			var height = $(this).height();
			var id = $(this).parent().attr('id');
			var thisSubmenu = $(this).parent().children('.submenu');
			var submenuVisible = $(thisSubmenu).css('display');
			
			
			// if (height==minheight) $(this).children("span").animate({"height": maxheight+"px"}, "slow");
			if (height==minheight) $(this).animate({"height": maxheight+"px"}, "slow");
			
			
			// if ( ($('.activel1 .submenu .activel2').html()==undefined) ) { // Wenn kein Link aus Level 2 angeklickt wurde...  FALSE ist es, wenn ein Submenüeintrag aktiv ist
		
			if ( $(this).next('.submenu:visible').html()!=undefined ) { // Wenn vom angeklickten parent das Submenu sichtbar ist, nicht einfahren, alle anderen schon 
				var thisSubmenuISOpen = true;
			}
			else var thisSubmenuISOpen = false;					

			
				if (!thisSubmenuISOpen) $('.submenu:visible').slideUp("slow");
				
				$('.navitem_level1:not(#'+id+')').removeClass('active');
				$('.navitem_level1:not(#'+id+')').removeClass('activel1');
				
				if (submenuVisible=="none") { // Untermenüs aufklappen, weil noch keins angezeigt wird
				  $('.navitem_level1:not(#'+id+') a.level1').animate({"height": minheight+"px"}, "slow");
				  $(thisSubmenu).slideDown("slow");
				  $(this).parent().addClass('active');
				  $(this).parent().addClass('activel1');
				}
				else { // alles wieder zuklappen
				  if (!thisSubmenuISOpen) $('.navitem_level1:not(#'+id+') > a.level1').animate({"height": maxheight+"px"}, "slow");
				  $(this).css("border-bottom", "none");
				}
				
				$(this).blur();
				
				if (thisHasClassActive){
					if ( ($('.activel1 .submenu .activel2').html()==undefined) ) { // Wenn kein Link aus Level 2 angeklickt wurde...
						return false; // Keine Seite öffnen! (bezieht sich auf Level 1
					}
				}
			
			if ( $(this).children("span").html()=="Finanzlexikon") { // Finanzlexikon kann direkt angeklickt werden
				$(this).parent().addClass('active');
				$(this).parent().addClass('activel1');
			}
			
			window.setTimeout("location.href = '/"+linkTargetURL+"'", 900);
			return false;
	});
	
	// $('#primNav .active').unbind('click');
	
	$(".subsubmenu").css("display", "block");
	$(".subsubmenu").hide();
	var x = 0;
	$(".subsubmenu").each(function(){
		$(this).attr("id", "subsub"+x);
		x++;
		var koords = $('#primNav').offset();
		$(this).css("position", "absolute");
		$(this).css("top", (koords.top)+20+"px");
		$(this).css("left", (koords.left)+223+"px");
		$(this).css("border", "1px outset gray");
	});
	
	
	
	$('.open .navitem_level2 > a.level2').bind('mouseover', function(){
			var koords = $(this).parent("li").offset();
			var subsubmenu = $(this).next(".subsubmenu");
			var thisSubsubid = $(subsubmenu).attr("id");
			// alert($(subsubmenu).text());
			
			if ($.browser.msie && $.browser.version=="7.0") {
				$(subsubmenu).css("border", "1px outset gray");
				// $(this).append($(subsubmenu));
				// $(subsubmenu).show();
				
			}
			// else {
				if (openedSubsubmenu != thisSubsubid) {
					// $('#'+openedSubsubmenu).hide();
					
					$(subsubmenu).css("position", "absolute");
					$(subsubmenu).css("top", (koords.top)+20+"px");
					$(subsubmenu).css("left", (koords.left)+223+"px");
					$(subsubmenu).css("border", "1px outset gray");
						
					openedSubsubmenu = thisSubsubid;
				}
			// }
	});
	
		
		$('li.navitem_level2').bind('mouseover', function(){
			$('.subsubmenu').hide();
			$(this).children(".subsubmenu").show();
		});
		
		if ($.browser.msie && $.browser.version=="7.0") {
			
			$(".subsubmenu").bind('mouseover', function(){
				$(this).bind('mouseout', function(){
					$(this).hide();
				});
			});
		}
		else {
			$('li.navitem_level2').bind('mouseout', function(){
				$(this).children(".subsubmenu").hide();
			});
		}
	
	
	
	
	$('.navitem_level2 > a.level2').bind('click', function(){
		$(this).blur();
		if ($.browser.mozilla) {
			$('.navitem_level2').removeClass("active");
			$('.navitem_level2').removeClass("activel2");
			// $(this).parent().addClass("active");
			$(this).parent().addClass("activel2");
		}
	});
	
	$('.navitem_level3 > a.level3').bind('click', function(){
		$(this).blur();
	});

});
