window.onload=function()
{
	initSubmenus();
	initImages();
}

/*
Find menu-items and where they are, transfer cooordinates to corresponding submenus
*/
function initSubmenus()
{
	var menuPositions = [];
	var navigation_area = $("navigation_area");
	var subnavigation_area = $("subnavigation_area");

	//Position submenus
	iterateChildren(navigation_area, "span", function(topnav, counter)
	{
		menuPositions[counter]=findPosX(topnav)-100;
	}, true);
	
	iterateChildren(subnavigation_area, "div", function(subnav, counter)
	{
		subnav.style.left=""+menuPositions[counter]+"px";
	}, true);
	
	//Open active submenu
	var index = openSubnavigationIndex();
	var openSubNav = subnavigationByIndex(index);
	openSubNav.style.visibility="visible";
	
	//Add event handlers
	iterateChildren(navigation_area, "a", function(topnav, counter)
	{
		topnav.onmouseover=new Function("closeAuxilliarySubnavigations(false);subnavigationByIndex("+counter+").style.visibility='visible';");
	}, true);
	
	var images = $("images");
	images.onmouseover=closeAuxilliarySubnavigations;
}

/*
Find Images inside #images and insert code for their onmouseover-event to show the plus-sign which in turn displays the large image inside the popup on click
*/
function initImages()
{
	//Init size, magnification & Tooltip
	var totalImageSize = 0;
	var images = $("images");
	
	iterateChildren(images, "a", function(link, counter)
	{
		if(!link.firstChild || !link.firstChild.tagName || !link.firstChild.tagName.toLowerCase() == "a")
			return true;
		var image = link.firstChild;
		image.largeVersion = link.getAttribute("href");
		images.appendChild(image);
		images.removeChild(link);
		if(image.largeVersion)
		{
			image.onmouseover=new Function("handleImageOver(this);");
		}
		
		totalImageSize+=image.clientWidth;
	}, true);
	images.style.minWidth=""+Math.max(totalImageSize, 622)+"px";
	
}

function handleImageMaginfy(image_src)
{
	var popup = $("popup");
	popup.src=image_src;
	popup.style.display="block";
	popup.onmouseover=showPopUpClose;
}

function showPopUpClose()
{
	var popup = $("popup");
	var close_sign = displaySignOnElement(popup, "x", true);
	
	close_sign.onclick=popUpClose;
}


function popUpClose()
{
	var popup = $("popup");
	popup.style.display="none";
	popup.src="";
	hideSign();
}

function hideSign()
{
	var image_mag = $("image_mag");
	image_mag.style.display="none";
}


function displaySignOnElement(element, signtext, top)
{
	var image_mag = $("image_mag");
	emptyElement(image_mag);
	image_mag.appendChild(document.createTextNode(signtext));
	
	var appendX = element.clientWidth-18;
	var appendY = element.clientHeight-17;
	if(top===true)
		appendY = 3;
	
	var element_pos = findPosition(element);
	image_mag.style.display="block";
	element_pos[0]=element_pos[0]+(appendX);
	element_pos[1]=element_pos[1]+(appendY);
	
	setElementPos(image_mag, element_pos[0], element_pos[1]);
	
	return image_mag;
}


function handleImageOver(image)
{
	//Create tooltip
	var tooltip = $("tooltip");
	emptyElement(tooltip);
	var tooltipText = document.createTextNode(image.getAttribute("title"));
	tooltip.appendChild(tooltipText);
	
	//Show plus sign
	var image_mag = displaySignOnElement(image, "+", false);
	
	image_mag.onclick=new Function("handleImageMaginfy('"+image.largeVersion+"');");
	$("main_content").onmousemove=hideSign;
}

function closeAuxilliarySubnavigations()
{
	var leaveActiveOpen = true;
	if(arguments.length==1)
		leaveActiveOpen=arguments[0];
	var open = openSubnavigationIndex();
	var subnavigation_area = $("subnavigation_area");
	
	iterateChildren(subnavigation_area, "div", function(subnav, counter)
	{
		if(counter!=open || leaveActiveOpen==false)
			subnav.style.visibility="hidden";
		else
			subnav.style.visibility="visible";
	}, true)
}


function subnavigationByIndex(index)
{
	var subnavigation_area = $("subnavigation_area");
	
	iterateChildren(subnavigation_area, "div", function(subnav, counter)
	{
		if(counter==index)
		{
			index=subnav;
			return false;
		}
		return null;
	}, true);
	return index;
}


function openSubnavigationIndex()
{
	var navigation_area = $("navigation_area");
	var index=null;
	
	iterateChildren(navigation_area, "a", function(topnav, counter)
	{
		if(topnav.className=="active")
		{
			index=counter;
			return false;
		}
		return null;
	}, true);
	return index;
}

//Helpers
function emptyElement(element)
{
	element=$(element);
	var nodes=$A(element.childNodes);
	nodes.each(function(node)
	{
		element.removeChild(node);
	});
}

function setElementPos(element, left, top)
{
	if(typeof(left)!="string")
		left=""+left+"px";
	if(typeof(top)!="string")
		top=""+top+"px";
		
	element.style.left=left;
	element.style.top=top;
}


function iterateChildren(parent, tagName, code, directOnly)
{
	var allChildren = parent.getElementsByTagName(tagName);
	var i=0;
	var counter=0;
	
	for(i=0;i<allChildren.length;i++)
	{
		var child=allChildren[i];
		if(!directOnly || child.parentNode==parent)
		{
			counter++;
			var codeReturn = code(child, counter);
			if(codeReturn===false)
				return;
		}
	}
}

//"Stolen" functions
function root()
{
	return document.getElementsByTagName("body")[0];
}


function findPosX(element)
{
	return findPosition(element)[0];
}

function findPosY(element)
{
	return findPosition(element)[1];
}

function findPosition(element)
{
	if(element.offsetParent)
	{
		for (var posX = 0, posY = 0; element.offsetParent; element = element.offsetParent)
		{
			posX += element.offsetLeft;
			posY += element.offsetTop;
		}
		return [ posX, posY ];
	}
	else
	{
		return [ element.x, element.y ];
	}
}
