
var newsAnimationInProgress = 0;
var nav4height = 0;

$(document).ready(function(){
		
	var heightLevel1, heightLevel3=0;
	var open = 0;
	var openNews = 0, isNewsStacked = 0;
	var isIE = 0, isSafari = 0;
	var t = 0;
	
	var l4 = $("#nav4level"); 
	var showl4 = false;
	
	jQuery.each(jQuery.browser, function(i, val) {
		if($.browser.msie){ isIE = 1; }
		if($.browser.safari){ isSafari = 1; }
	});

	
	/*
	 * Treat the first list element as the header of the appearing menu
	 */
	$("#ul-navi li ul li:first-child").each(function() {
		
		if ( $(this).parent().find("span span").attr("class") == "nav2-title" ) {
			$(this).children().removeAttr("href");
			$(this).attr("class","nav2-headline");
			$(this).children().children().find("img").remove();
			$(this).children().children().append('<span style="float: right;" class="x_close"><a href="javascript:closeAll();"><img src="fileadmin/templates/img/navi/nav_close.gif" width="15" height="15" class="nav_close" /></a></span>');
			$(this).prepend('<img src="fileadmin/templates/img/arrows/nav_button_down.png" class="nav2-headline_button" />');
			$(this).children().children().removeAttr("class");
		}
	});	
	
	
	$("#ul-navi .nav_close").live("mouseover", function() {
		$(this).attr("src", "fileadmin/templates/img/navi/nav_close_rollover.gif");
	});
	
	$("#ul-navi .nav_close").live("mouseout", function() {
		$(this).attr("src", "fileadmin/templates/img/navi/nav_close.gif");
	});
	
	
	/* Highlight 1 Level on mouse over with child elements*/
	
	$("#ul-navi").find(".nav1level_span").mouseover(function() {
		$(this).css("color", "#FFFFFF");
	});
	
	$("#ul-navi").find(".nav1level_span a").mouseover(function() {
		$(this).css("color", "#FFFFFF");
	});
	
	
	
	$("#ul-navi").find(".nav1level_span").mouseout(function() {
		$(this).css("color", "#a7a7a7")
	});
	
	$("#ul-navi").find(".nav1level_span a").mouseout(function() {
		$(this).css("color", "#a7a7a7")
	});
	
	
	$("#ul-navi li ul").unbind();
	
	$("#ul-navi .nav1level_span").click(function(){    
		//$(this).stop();
		
		var this_li = $(this).parent();
		var numChild = this_li.find("ul").children().size();
		
		if (numChild > 0) {
		
		$("#nav_layer").css({"display": "block"});
		//$("#nav_layer").animate({"opacity": "0.7"}, 300);
		
		
		$('#ul-navi .nav1level').children(".nav_arrow").animate({"opacity": "1.0"}, 700);
		
		this_li.children(".nav_arrow").stop();
		this_li.children(".nav_arrow").animate({"opacity": "0.0"}, 1000);
		
		this_li.find(".nav2-headline_button").css("opacity","0.0");
		this_li.find(".nav2-headline_button").animate({"opacity": "1.0"}, 2000);
		
		/* Close all other open menus */
		
		setTimeout('$("#nav4level").stop(); $("#nav4level").css("display", "none"); $("#nav4helper").css({"display": "none"});', 200);
		$("#ul-navi li ul").animate({height:'1px'},{queue:false, duration:450, easing: 'easeOutQuint'});
		
		/* restore "selected" color and rebind mouseout event */ 
		$('#ul-navi .nav2titlewrapper').attr("class", "nav2level nav2titlewrapper");
		$('#ul-navi .nav2titlewrapper').children().children().attr("src", "/fileadmin/templates/img/arrows/navi2_black.png");
		$('#ul-navi .nav2titlewrapper').bind("mouseout", function(){  
			$(this).attr("class", "nav2level nav2titlewrapper");
			$(this).children().children().attr("src", "/fileadmin/templates/img/arrows/navi2_black.png");
		});
		
		$('#ul-navi .nav3level').attr("class", "nav2level nav2titlewrapper");
		$('#ul-navi .nav3level').bind("mouseout", function(){  
			$(this).attr("class", ".nav3level");
			//$(this).children("img").attr("src", "/fileadmin/templates/img/arrows/circle_white_bg.jpg");		
		});
		
		
		if (open) {
			$("#ul-navi li ul li").animate({height:'45px'},{queue:false, duration:200, easing: 'easeOutQuint'});
			$("#ul-navi li ul li:first").animate({height:'30px'},{queue:false, duration:200, easing: 'easeOutQuint'});
		}
		
		
		var numChildChild = this_li.find("ul").find("ul").children().size();
		
		/* Calulate height of the box of second level elements */
		//if ( heightLevel3 > 0 ) {
		//	heightLevel1 = ((numChild-numChildChild-2)*10)+75+heightLevel3;
		//} else if ( heightLevel3 == -1 ) {
		//	heightLevel1 = ((numChild-numChildChild-2)*10)+75;
		//} else {
			//heightLevel1 = ((numChild-numChildChild-1)*45)+35;
			heightLevel1 = ((numChild-numChildChild-1)*45)+35;
			
			if (heightLevel1 < 140) {
				heightLevel1 =  heightLevel1-3;
			} 
		//}
		
		
		/* workaround for ie7 bug - reposition nav2 menu before opening */
		if (isIE) {
			var title = $(this).parent().find(".nav1level_span").html();
			var mleft = title.length*6+40;
			if (title.length == 7) { mleft = mleft+8 }
			$(this).parent().find("ul:first").css("marginLeft", "-"+mleft+"px");
		}
		
		if (isSafari) {
			$(this).parent().find("ul:first").css("marginLeft", "-30px");
		}
		
		/* Open clicked menu */
		this_li.find("ul").animate({"height" : heightLevel1+"px"},{queue:false, duration:450, easing: 'easeOutQuint'}); 
		//$(".content").animate({opacity:'0.4'});

		$('#tooltip_icon').css("display", "none");
		
		heightLevel3 = 0;
		open = 1;
		showl4 = false;
		}

	});
	

	/* NAV SECOND LEVEL */
	
	$("#ul-navi .nav2titlewrapper").mouseover(function(){  
		$(this).attr("class", "nav2level_hover nav2titlewrapper");
		$(this).children().children().attr("src", "/fileadmin/templates/img/arrows/navi2_white.png");

	});
	
	$("#ul-navi .nav2titlewrapper").mouseout(function(){  
		$(this).attr("class", "nav2level nav2titlewrapper");
		$(this).children().children().attr("src", "/fileadmin/templates/img/arrows/navi2_black.png");
	});
	
	

	$("#ul-navi .nav2level").click(function() {
		var clicked_height = $(this).css("height");
		clicked_height = clicked_height.substr(0,clicked_height.indexOf("px"));

		if ($(this).find('.nav4ul').html() == null) {
			setTimeout('$("#nav4level").stop(); $("#nav4level").css("display", "none");$("#nav4helper").css({"display": "none"});', 200);
		}
		
		$('#ul-navi .nav2titlewrapper').attr("class", "nav2level nav2titlewrapper");
		$('#ul-navi .nav2titlewrapper').bind("mouseout", function(){  
			$(this).attr("class", "nav2level nav2titlewrapper");
			$(this).children().children().attr("src", "/fileadmin/templates/img/arrows/navi2_black.png");
		});
			
			$(this).children('.nav2titlewrapper').unbind("mouseout");
			$(this).children('.nav2titlewrapper').attr("class", "nav2level_hover nav2titlewrapper");
			
			var numChild = $(this).find("ul").children().size();
			var numChildChild = $(this).find("ul .nav4ul").children().size();
			
			numChild = numChild - numChildChild;
			
			if (numChild > 0) {
			
				$("#ul-navi li ul li").animate({height:'10px'},{queue:false, duration:600, easing: 'easeOutQuint'});
				$("#ul-navi li ul li:first").animate({height:'30px'},{queue:false, duration:600, easing: 'easeOutQuint'});
		
				var height3 = (numChild*30)+45;
				
				if (numChild > 0) {
					$(this).animate({height: height3+'px'},{queue:false, duration:900, easing: 'easeOutQuint'});
				} else {
					$(this).animate({height:'45px'},{queue:false, duration:600, easing: 'easeOutQuint'});
				}
	
				numParent = numChild;
	
				if (numParent > 0) {
					heightLevel3 = numParent*30;
				} else {
					heightLevel3 = -1;
				
				}
			
				/* Adjust height */ 

				var numChild = $(this).parent().parent().find("ul").children().size();
				var numChildChild = $(this).parent().parent().find("ul").find("ul").children().size();
				
				/* Calulate height of the box of second level elements */
				if ( heightLevel3 > 0 ) {
					heightLevel1 = ((numChild-numChildChild-2)*10)+80+heightLevel3;
				} else if ( heightLevel3 == -1 ) {
					heightLevel1 = ((numChild-numChildChild-2)*10)+85;
				} else {
					heightLevel1 = ((numChild-numChildChild-1)*45)+35;
				}
				
				if (heightLevel1 > 90) {
					$(this).parent().parent().find("ul").animate({"height" : heightLevel1+"px"},{queue:false, duration:450, easing: 'easeOutQuint'});
				}
				 
				
				/* close 4th level */
				if (showl4 == false) {
					//var nav4pos = $("#nav4level").offset();
					//nav4pos = nav4pos.left-(($(window).width()-990)/2);
					//$("#nav4level").animate({"left": nav4pos-200, "opacity":"0.0"},{queue:false, duration:500, easing: 'easeOutQuint'});
					$("#nav4level").css({"opacity":"1.0"});
					//setTimeout('$("#nav4level").css({"display": "none", "opacity": "1.0"})', 600);
					
					//showl4 = false;
				} else {
					showl4 = false;
				}
	
				$("#ul-navi li ul li ul li").stop();
				$("#ul-navi li ul li ul li").animate({'height':'23px'},{queue:false, duration:1000, easing: 'easeOutQuint'});
			}
		//}
	});
	
	
	/* NAV THIRD LEVEL */
	$("#ul-navi .nav3level").mouseover(function(){  
		$(this).attr("class", "nav2level_hover nav3level");
	});
	
	$("#ul-navi .nav3level").mouseout(function(){  
		$(this).attr("class", "nav2level nav3level");
	});
	
	$("#ul-navi .nav3level").click(function(){  
		
		$('#ul-navi .nav3level').attr("class", "nav2level nav2titlewrapper");
		$('#ul-navi .nav3level').bind("mouseout", function(){  
			$(this).attr("class", "nav2level nav3level");
		});
		
			
		$(this).unbind("mouseout");
		$(this).attr("class", "nav2level_hover nav3level");
		
		l4.empty();
		l4.append($(this).children("ul").clone());
		
		var childElem = $(this).children("ul").find("li").size();
		nav4height = childElem*31;
		
		var curr = $(this).offset();

		var curr_left = curr.left-(($(window).width()-990)/2);
		
		var timeHeight = childElem*500;
		var timeTop = childElem*400;
		
		l4.stop();
		l4.css({
			"top": curr.top,
			"left": curr_left+10, 
			"display":"block", 
			"height" : "30px"
		});
		
		l4.animate({"left":curr_left+187},{queue:false, duration:1200, easing: 'easeOutQuint'});
		
		var myTimer = {};
		myTimer = $.timer(900,function(){
			
			$('#nav4level').animate({'top':curr.top-nav4height+30},{queue:false, duration: timeTop, easing: 'easeOutQuint'});
			setTimeout("$('#nav4level').animate({'height':'220px'},{queue:false, duration:"+timeHeight+", easing: 'easeOutQuint'});" ,100);
			showl4 = true;
		});
		
		//l4.css({"top": +curr.top-nav4height+30, "left": +curr_left+10, "display":"block", "height" : +nav4height+"px"});
		//l4.animate({"left":curr_left+187},{queue:false, duration:1800, easing: 'easeOutQuint'});
		//showl4 = true;
	});
	
	
	
	/* NAV FOURTH LEVEL */
	$(".nav4li").live("mouseover", function(){  
		$(this).attr("class", "nav4li nav4li_hover");
	});
	
	$(".nav4li").live("mouseout", function(){  
		$(this).attr("class", "nav4li");
	});



	/* Close nav on click in content area */
	
	$("#nav_layer").click(function(){    
		$(this).stop();
		
		setTimeout("closeAll();", 50);	
		open = 0;
		
	});
	
	
	/* functions for news area behaviour */
	
	//$('#static-ul a').remove();
	
	var elem = $("#navi ul li ul li:contains('Latest News')");

	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('Aktuelle News')");
	}
	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('Dernières News')");
	}
	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('Laatste nieuws')");
	}
	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('As últimas novidades')");
	}
	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('Viimeisimmät uutiset')");
	}
	if (elem.html() == null) {
		elem = $("#navi ul li ul li:contains('Últimas Noticias')");
	}
	
	
	elem.attr('id','news');
	elem.find("a:first").attr('href','#').attr("onclick", 'return false;');
	
	elem.unbind("click");
	elem.click( function() {  
		var newsitems = $(this).find('#static-ul');

		newsitems.css("height", "auto");
		
		// Clone level4 content to absolute div
		l4.children().remove();
		l4.append(newsitems.clone());
		
		/*l4.find("#static-ul").mouseleave(function() {
			clearTimeout(t);
			t = setTimeout("alert('away')", 1000);
		});
		*/
		
		// Reposition absolute div;
		var childElem = newsitems.find("li").size();

		//var nav4height = childElem*45;
		nav4height = childElem*46;
		
		$(".news-item").css("height", "45px");
		isNewsStacked = 0;
		
		var curr = $(this).offset();
		
		var curr_left = curr.left-(($(window).width()-990)/2);
		
		l4.stop();
		l4.css({
			//"top": +curr.top-nav4height+45, 
			"top": +curr.top,
			"left": +curr_left+10, 
			"display":"block", 
			"height" : "45px", 
			"width": "220px"
		});
		
		newsAnimationInProgress = 1;
		l4.animate({"left":curr_left+187},{queue:false, duration:900, easing: 'easeOutQuint'});
		$('#nav4helper').css({
			"top": +curr.top+24,
			"left": +curr_left+187
		});
		var myTimer = {};
		myTimer = $.timer(900,function(){
			
			$('#nav4level').animate({'top':curr.top-nav4height+45},{queue:false, duration: 600, easing: 'linear'});
			$('#nav4level').animate({'height': nav4height+'px'}, 600, 'linear', function() {resetNewsAni();});
			showl4 = true;
		});
		
		return false;
		
	});
	
	$(".news-item").live("mouseover",function() {
	
		if (newsAnimationInProgress == 0 && $(this).height() <= 45) {
			$(".news-item").find(".news_title").css({
				"backgroundColor": "#FFFFFF",
				"backgroundImage": "none",
				"color": "#000000"
			});
			
			$('#nav4helper').css({"display": "block" });
			
			$(".news-item").animate({"height": "10px"},{queue:false, duration:800, easing: 'linear'});
			
			//$(".news-item").css({"border": "none", "margin": "0px", "padding": "0px" });


			l4.css('height', 'auto');
			$(this).parent().css('height', 'auto');
			$(this).parent().parent().css({"bottom": "0px"});
			

			if (!isNewsStacked) {
				var top = l4.css("top");
				
				top = top.substr(0, top.indexOf("px"))-125;
				l4.animate({'top': top+'px'}, {queue:false, duration: 800, easing: 'linear'});
				//l4.css({'top': top+'px'});
				
			}
			
			$(this).find(".news_title").css({
				"backgroundColor": "transparent",
				"backgroundImage": "url(/fileadmin/templates/img/navi/navi_bg_black_2x2.png)",
				"color": "#FFFFFF"
			});
			
			//$(this).find(".news_title").attr("class", ".news_title .news_title_hover");
			$(this).stop();
			$(this).find('.news_content').unbind();
			
			//$(this).animate({'height': '310px'},{queue:false, duration:800, easing: 'linear'});
			$(this).animate({'height': '310px'}, 800, 'linear', function() {
				setTimeout("$('#nav4helper').css({'display': 'none'});", 200)
			});
			
			isNewsStacked = 1;
		}
	});
	
});

	function resetNewsAni() {
		newsAnimationInProgress = 0; 
	}
	
	function closeAll() {
		//l4.css({"top": +curr.top, "left": +curr.left-100, "display":"block", "height" : +nav4height+"px"});
		
		//$("#nav_layer").animate({"opacity": "0.0"}, 300);
		$("#nav_layer").css({"display": "none"});
		
		
		var nav4pos = $("#nav4level").offset();
		nav4pos = nav4pos.left-(($(window).width()-990)/2);
		
		$("#nav4level").animate({"left": nav4pos-200},{queue:false, duration:400, easing: 'easeOutQuint'});
		setTimeout('$("#nav4level").css("display", "none")', 200);
		$("#nav4level").html("");
		$('#nav4helper').css({'display': 'none'});
		showl4 = false;
		
		
		$("#ul-navi li ul").animate({height:"0px"},{queue:false, duration:800, easing: "easeOutQuint"});  
		
		$("#ul-navi li img").animate({opacity: "1.0"},{queue:false, duration:1000, easing: "easeOutQuint"});
		$("#ul-navi li ul li").animate({height: "45px"},{queue:false, duration:200, easing: "easeOutQuint"});
		$("#ul-navi li ul li:first").animate({height: "30px"},{queue:false, duration:200, easing: "easeOutQuint"});
		$('#tooltip_icon').css("display", "block");

	}