
// Slidedown Subnavigation 
function select_nav(navpos)
{
	$("#sub" + navpos).slideDown("fast");
	$(".navi_sub").not("#sub" + navpos).hide();
}

function close_nav()
{
	$(".navi_sub").hide();
	$(".navi_button").removeClass("navi_button_selected");
}

var nav_timer = 0; // Handle for subnav delay timer 

// Initiate page if javascript is running 
$(function() 
{
	$("#content").pngFix(); 
	
	// activates acordion in left col for mouseover: displays segment, if hidden
	$("#left-container > .a_head").bind("mouseover", function() {
		$(this).oneTime('100ms', 'accTimer',
                function() {
			$(this).next("div:hidden").slideDown("fast");
			$(this).parent("div").children(".a_head").not(this).next("div").slideUp("fast");
			});
	}).bind("mouseout", function() {
		$(this).stopTime('accTimer');
	});
	
	// Toggle visibility of chosen segment
	$(".a_head").click(function() {
		$(this).next("div").slideToggle("fast");
		});
	
	// Activate References Box
	var num_references = $(".references").size();
	var curr_ref = 1;

	// if num_references > 1, show buttons to navigate references and init timer
	if(num_references > 1)
	{
		$(".references:first").parent("div").append('<div id="ref_control" align="right" style="margin-top:4px;"><img id="ref_left" style="cursor:pointer; margin-right:3px; margin-top: -5px;" src="/img/e/basic/r_button_left.png" alt="" align="middle" /><span id="ref_display" class="small"> 1 / '+num_references+' </span><img id="ref_right" style="cursor:pointer; margin-right:3px; margin-top: -5px;" src="/img/e/basic/r_button_right.png" alt=""  align="middle" /></div>');
		
		$("#ref_left").click(function() {
			$(".references:first").stopTime('referenceTimer');
			$("#ref_control").fadeOut("normal");
			$("#reference"+	curr_ref).fadeOut("normal", function() {
					curr_ref = curr_ref == 1 ? num_references : curr_ref - 1;
					$("#ref_display").html(curr_ref + ' / ' + num_references + ' ');
					$("#reference"+	curr_ref).add("#ref_control").fadeIn("normal");
			});
		});
		
		$("#ref_right").click(function() {
			$(".references:first").stopTime('referenceTimer');
			$("#ref_control").fadeOut("normal");
			$("#reference"+	curr_ref).fadeOut("normal", function() {
					curr_ref = curr_ref == num_references ? 1 : curr_ref + 1;
					$("#ref_display").html(curr_ref + ' / ' + num_references + ' ');
					$("#reference"+	curr_ref).add("#ref_control").fadeIn("normal");
			});
		});
			
		$(".references").not("#reference1").hide().parent("div").bind("mouseover", function() {
				$(".references:first").stopTime('referenceTimer');
		});
		
/*		$(".references").not("#reference1").hide().parent("div").bind("mouseover", function() {
				$(".references:first").parent("div").css("background-color", "#333333").end().stopTime('referenceTimer');
			}).bind("mouseout", function() {
				$(".references:first").parent("div").css("background-color", "").end().everyTime('6s', 'referenceTimer', function() {
				$("#ref_control").fadeOut("normal");
				$("#reference"+	curr_ref).fadeOut("normal", function() {
					curr_ref = curr_ref == num_references ? 1 : curr_ref + 1;
					$("#ref_display").html(curr_ref + ' / ' + num_references + ' ');
					$("#reference"+	curr_ref).add("#ref_control").fadeIn("normal");
				});
			});
		}); */
		
		$(".references:first").everyTime('6s', 'referenceTimer', function() {
			$("#ref_control").fadeOut("normal");
			$("#reference"+	curr_ref).fadeOut("normal", function() {
				curr_ref = curr_ref == num_references ? 1 : curr_ref + 1;
				$("#ref_display").html(curr_ref + ' / ' + num_references + ' ');
				$("#reference"+	curr_ref).add("#ref_control").fadeIn("normal");
			});
		});
	}
	
	// Activate HOT NEWS 
	var num_hot_news = $("#hot_news").children("div").size();
	var curr_news = 1;

	// if num_hot_news > 1, init timer and textcontainers
	if(num_hot_news > 1)
	{
		var x = 0;
		$("#hot_news").children("div").each(function() {
			$(this).attr("id", "hot_news_" + (++x));
		});
		
		$("#hot_news").everyTime('8s', 'hotNewsTimer', function() {
			$("#top_news_text").fadeOut("slow", function() {
				curr_news = curr_news == num_hot_news ? 1 : curr_news + 1;
				$("#top_news_text").html($("#hot_news_"+curr_news).html()).fadeIn("slow");
			});
		});
	}
	
	// Clear the decorative >> from the field on first focus
	$("#searchfield").one('focus', function() {
		$(this).val('');
		});
		
	// Build Main Navigation
	$(".navi_button").each( function() {
		
		var navi_sub_html = $(this).next(".navi_sub").html(); // Get html-content of subnav-div

		var pos = $(this).attr("name");

		if(!navi_sub_html.match(/^[\s\n\r\t]*$/)) // if subnavigation exists
		{
			// bind mouseover event to corresponding navi button
			$(this).bind("mouseover", function() {
				window.clearTimeout(nav_timer); // stop previous open timer
				$("html").stopTime("closeNavTimer"); // stop close sub nav timer
				$(this).addClass("navi_button_selected");
				var navpos = $(this).attr("name");
				$(".navi_sub").not("#sub"+navpos).hide();
				$(".navi_button").not(this).removeClass("navi_button_selected");
				nav_timer = window.setTimeout("select_nav("+navpos+");", 400);
			});
			
			$(this).bind("mouseout", function() {
				$("html").oneTime("2s", "closeNavTimer", function() {
					close_nav(); // close sub navs after 2 Seconds
				});
			});
			
			var _href = $(this).attr("href");
			if(_href.match(/^javascript:;|#$/)) // if not linked, prevent closing of subnavi
			{
				$(this).click( function() {
					return false;
				});
			}
			$(this).next(".navi_sub").replaceWith('');
			$("body").append('<div id="sub' + pos + '" class="navi_sub">' + navi_sub_html + '</div>');
			
			$("#sub" + pos).bind("mouseout", function() {
				$("html").oneTime("2s", "closeNavTimer", function() {
					close_nav(); // close sub navs after 2 Seconds
				});
			}).children("a").each(function() {
				$(this).bind("mouseover", function() {
					$("html").stopTime("closeNavTimer"); // stop close sub nav timer
				});
			});
		}
		else
		{
			$(this).bind("mouseover", function() {
				window.clearTimeout(nav_timer); // stop previous open timer
				$("html").stopTime("closeNavTimer"); // stop close sub nav timer
				$(".navi_sub").hide();
				$(".navi_button").not(this).removeClass("navi_button_selected");
			});
		}
	
	});


	$("html").click(function() {
		window.clearTimeout(nav_timer);
		$(".navi_sub").hide();
		$(".navi_button").removeClass("navi_button_selected");
		//nav_timer = window.setTimeout("select_nav("+0+");", 500);
		});
		
	$("#nav_carousel").nav3Dcarousel( {
					height: 182, // 182
					width: 108, // 108
					radiusX: 210,
					radiusY: 26,
					centerX: 690,
					centerY: 110,
					perspective: -10,
					padding: 48,
					showTooltip: 0,
					textBox: 0,
					damping: 30000,
					elasticity: 0.0010,
					timer: "40ms"
				}).css('visibility', 'visible'); 
	
	
	
});