var elementArray = new Array();
var menuElementArray = new Array();
var timeout = 0;
var menuTimeout = 0;
var try_again = true;

function fadeInNextElement(originalSpeed)
{
	if(!originalSpeed && timeout > 100)
	{
		timeout = 100;
	}
	
	if(elementArray.length > 0)
	{
		var element = elementArray.pop();
		$(element).fadeIn("fast");
		window.setTimeout(fadeInNextElement, timeout);
	}
}

function fadeInNextMenuItem()
{	
	if(menuElementArray.length > 0)
	{
		var element = menuElementArray.pop();
		$(element).fadeIn("fast");
		window.setTimeout(fadeInNextMenuItem, menuTimeout);
	}
}

function showTooltipp()
{
	// "this" is the a tag
	/* slide without text content
	$(this)
		.find(".tooltipp")
		.css("height", $(this).find(".tooltipp").css("height"));
	
	if($(this).find(".temp").length > 0)
	{
		$(this)
			.find(".tooltipp")
			.html($(this).find(".temp").html())
			.end()
			.find(".temp")
			.remove();
	}
	
	$(this)			
		.append("<span class=\"temp\"></span>")
		.find(".temp")
		.html($(this).find(".tooltipp").html())
		.hide()
		.end()
		.find(".tooltipp")
		.html("")
		.hide()
		.slideDown("fast", function()
									{
										$(this)
											.html($(this).next(".temp").html())
											.show()
									}
		);
		*/
		
	$(this)
		.find(".tooltipp")
		.hide()
		.slideDown("fast");
}

function hideTooltipp()
{
	/* slide without text content
	var size = $(this).find(".tooltipp").css("height");
	var text = $(this).find(".tooltipp").html();
	$(this).find(".tooltipp").css("height", size);
	$(this).find(".tooltipp").html("");
	$(this).find(".tooltipp").show().slideUp("fast", function(){$(this).html(text)});
	*/
	
	$(this)
		.find(".tooltipp")
		.show()
		.slideUp("fast");
}

function clickfunction(){
	try_again  = true;
	$("#contentcontainer a, #contentcontainer .thumbempty").animate({opacity:1.0});
		
	// extract variables from href of clicked link
	launchString = $(this).attr("href").substring($(this).attr("href").indexOf("?") + 1, $(this).attr("href").length);
	var launchStringArray = launchString.split("&"); 
	for (var i = 0; i <= launchStringArray.length - 1; i++)
	{
		var left = launchStringArray[i].substring(0, launchStringArray[i].indexOf("=")); 
		var right = launchStringArray[i].substring(launchStringArray[i].indexOf("=") + 1, launchString.length); 
		if (isNaN(right))
		{ 
			right = '"' + right + '"'; 
		} 
		eval("var " + left + " = " + right);
	}
	
	// clicked link was secondary navigation link
	if(jQuery.inArray($(this)[0], $("#secnavlist a")) != -1)
	{
		// first, check whether the requested page is the same as the currently shown, if so, do nothing and leave
		if(($("#thinavlistaktiv").length == 0) && ($(this).attr("id") == "secnavlistaktiv"))
		{
			return false;
		}
		
		// load corresponding section title into the head title tag
		$.post("_projekte_include.php", {list: list, type: "title"}, function(data){document.title = data;});
		
		// remove current active secondary navigation link id
		$("#secnavlistaktiv").removeAttr("id");
		
		// and assign it to the clicked link
		$(this).attr("id", "secnavlistaktiv");
		
		// load corresponding third navigation
		$("#thinavcontainer").fadeOut("fast", function()
													   {
														   $(this).load("_projekte_include.php", {list: list, type: "thinav"}, function()
																																		{
																																		    $("#thinavcontainer a").hide();
																																		    $("#thinavcontainer").show();
																																			$("#thinavcontainer a").click(clickfunction);
																																			menuElementArray = $.makeArray($("#thinavcontainer a")).reverse();
																																			menuTimeout = 500 / menuElementArray.length;
																																			fadeInNextMenuItem();
																																		}
														   );
														}
		);
	}
	
	// clicked link was third navigation link
	else if(jQuery.inArray($(this)[0], $("#thinavlist a")) != -1)
	{
		// first, check whether the requested page is the same as the currently shown, if so, do nothing and leave
		if($(this).attr("id") == "thinavlistaktiv")
		{
			return false;
		}
		
		// remove current active third navigation link id
		$("#thinavlistaktiv").removeAttr("id");
		
		// and assign it to the clicked link
		$(this).attr("id", "thinavlistaktiv");
	}
	
	// fade out the entire content holder container, reload the appropriate content into it and then fade it back in
	var toFadeOut = $("#contentcontainer a:not(." + list + "), #contentcontainer .thumbempty:not(." + list + ")");
	var notToFadeOut = $("#contentcontainer a, #contentcontainer .thumbempty");
	if(toFadeOut.length == notToFadeOut.length)
	{
		fadeSpeed = "fast";
	}
	else
	{
		fadeSpeed = "slow";
	}
	toFadeOut.fadeOut(fadeSpeed, function()
											 {
												 $("#contentcontainer a:not(." + list + "), #contentcontainer .thumbempty:not(." + list + ")").hide();
												 if(!try_again)
												  {
													  return;
												  }
												  try_again = false;
												 if($("#contentcontainer a:visible, #contentcontainer .thumbempty:visible").length == 0)
												 {
													 $("#contentcontainer").hide();
													 $("#contentcontainer").load("_projekte_include.php", {list: list}, function()
																														{
																															$("#contentcontainer a").mouseenter(showTooltipp).mouseleave(hideTooltipp);
																															elementArray = $.makeArray($("#contentcontainer a, #contentcontainer .thumbempty")).reverse();
																															timeout = 1000 / elementArray.length;
																															$("#contentcontainer a, #contentcontainer .thumbempty").hide();
																															$("#contentcontainer").show();
																															fadeInNextElement();
																														}
													 );
												  }
												  else
												  {
													  $("#contentcontainer").load("_projekte_include.php", {list: list}, function()
																														{
																															/*
																															elementArray = $.makeArray($("#contentcontainer .thumbempty")).reverse();
																															timeout = 1000 / elementArray.length;
																															$("#contentcontainer .thumbempty").hide();
																															fadeInNextElement();
																															*/
																														}
													 );
												  }
											  }
	);


	// prevent default browser behaviour execution
	return false;
}

$(document).ready(function ()
							{
								/* Fade in when first loading
								$("#contentcontainer a, #contentcontainer .thumbempty").hide();
								$("#contentcontainer").show();
								elementArray = $.makeArray($("#contentcontainer a, #contentcontainer .thumbempty")).reverse();
								timeout = 1000 / elementArray.length;
								fadeInNextElement();
								*/
								$("#contentcontainer a").mouseenter(showTooltipp).mouseleave(hideTooltipp);
								$("#navcontainer a").click(clickfunction).preventDefault();
							}
);
