﻿/**
 * @author Martin Haseneyer
 */

var	foldCounter = 0;
var CONST__HIGHLIGHTED = "erhellt";
var CONST__TOOLTIP = "Dieses Menü auf- bzw. zuklappen";

function goadmin()
	{
		window.open("admin/index.php","AdminCenter","width=500,height=350,left=200,top=200");
	}

function foldNavigation(id)
{
	var node = document.getElementById(id);
	if (node.style.display == "none")
		node.style.display= 'block';
	else
		node.style.display= 'none';
	// remove the selection from the clicked link
	node.blur();
}

/**
	@param	idName	name of the ID which is to be processed
	@param	clName	name of the class which is to be used for navigation
	@param	acName	name of the element which is used for "active"
	@param	inName	name of the "inner" element in an active link, e.g. "h2" in <li class="active"><h2>Text</h2></li>
*/
function makeNavigationFoldable(idName, clName, acName, inName)
{
	function makeNavigationFoldableIntern(element, idName, clName, acName, inName)
	{
		var isSuccessful = true;
		// the element is an <ul> element; so we want to pick the children...
		if (element.hasChildNodes())
		{
			var children = element.childNodes;
			var childrenCount = children.length;
			for (var i = 0; i < childrenCount; i++)
			{
				if (
					children[i].nodeName.toLowerCase() == "li"
					&& children[i].className == acName
				)
				{
					// we have an <li> element. It may contain either an
					var child = children[i];
					if (child.hasChildNodes)
					{
						// children list
						var liNodes = child.childNodes;
						var liCount = liNodes.length;
						// we will need these for node replacement
						var innerNode = null;
						var newNode = null;
						// child walk
						for (var j = 0; j < liCount; j++)
						{
							if (liNodes[j].nodeName.toLowerCase() == inName)
								innerNode = liNodes[j];
							else if (
								liNodes[j].nodeName.toLowerCase() == "ul"
								&& innerNode != null
							)
							{
								// set the id so the fold function knows which node to manipulate
								liNodes[j].setAttribute("id", "fold" + foldCounter);
								// generate a new node to replace the current node with
								var newNode = document.createElement("a");
								newNode.setAttribute("href", "javascript:foldNavigation(\"fold" + foldCounter + "\");");
								newNode.setAttribute("class", CONST__HIGHLIGHTED);
								newNode.setAttribute("title", CONST__TOOLTIP);
								newNode.appendChild(document.createTextNode(innerNode.innerHTML));
								foldCounter++;
								child.replaceChild(newNode, innerNode);
							}
							else if (
								liNodes[j].nodeName.toLowerCase() == "ul"
								&& innerNode == null
							)
							{
								// the innerNode is not given; that means that the "inner Node" is not
								// a header, but a link; so we have to go deeper to find the "inner Node"
								// work with the children
								makeNavigationFoldableIntern(liNodes[j], idName, clName, acName, inName);
							}
						}
					}
				}
			}
		}
		return isSuccessful;
	}

	// begin of the public function

	var element = document.getElementById(idName);
	if (element.hasChildNodes())
	{
		var children = element.childNodes;
		var childrenCount = children.length;
		var text = childrenCount + " Kinder: ";
		// children walk
		for (var i = 0; i < childrenCount; i++)
		{
			if (
				children[i].nodeName.toLowerCase() == "ul"
				&& children[i].className == clName
			)
			{
				var navigationList = children[i];
				return makeNavigationFoldableIntern(navigationList, idName, clName, acName, inName);
			}
		}
	}
}

// this function replaces contact form links with mailto-like links
replaceMailLinks = function()
{
	// regexes and so on to break a contact form link into mail parts
	var regExCN = /.*kontaktformular?.*[Cc][Nn]=([A-Za-z0-9\._-]+).*$/;
	var regExOU = /.*kontaktformular?.*[Oo][Uu]=([A-Za-z0-9\.\-+]+).*$/;
	var regExRN = /.*kontaktformular?.*[Rr][Nn]=([A-Za-z0-9\. \-+]+).*$/;
	var debug = document.getElementById("debug");

	// walk through a list of links (<a> elements)
	var elements = document.getElementsByTagName("a");
	for (var i = 0; i < elements.length; i++)
	{
		// define some items where the information shall go to...
		var cn = null;
		var ou = null;
		var rn = null;
		// if there is a regex match, fill the variables with the resulting parts
		var href = elements[i].getAttribute("href");
		if (regExCN.exec(href))
		{
			cn = RegExp.$1;
		}
		if (regExOU.exec(href))
		{
			ou = RegExp.$1;
		}
		if (regExRN.exec(href))
		{
			rn = RegExp.$1;
			while (rn.indexOf('+') >= 0)
			{
				rn = rn.replace(/\+/, '%20');
			}
		}
		// write the result string
		if (cn != null)
		{
			var result = 'javascript:mailto("' + cn + '", ';
			if (ou != null)
				result += '"' + ou + '"';
			else
				result += 'null';
			result += ', ';
			if (rn != null)
				result += '"' + rn + '"';
			else
				result += 'null';
			result += ');';
			// overwrite the href attribute that pointed to the contact form
			elements[i].setAttribute('href', result);
		}
	}
}

// the function that is called by the mailto-like links
mailto = function(cn, ou, rn)
{
	// remember the spaces, brackets and so on have to be escaped...
	// %20%3C is " <"; %3E is ">"
	var completeParameters = "mailto:";
	if (rn != null)
	{
	
		completeParameters += rn + " <";
	}
	completeParameters += cn + "@";
	if (ou != null)
	{
		completeParameters += ou;
	} 
	else
	{
		// organisational unit depends on the first letter of container name
		switch (cn.substr(0, 1))
		{
			case "w":
				completeParameters += "wiwi";
			case "j":
				completeParameters += "rewi";
			break;
			default:
				alert("Hier ist ein Fehler aufgetreten. Bitte laden Sie die Seite neu.");
			break;
		}
	}
	completeParameters += ".uni-jena.de";
	if (rn != null)
	{
		completeParameters += ">";
	}
//	alert(completeParameters);
	var mailWindow = window.open("mailto:" + completeParameters);
	if(mailWindow != null)
	{
		mailWindow.close();
	}
}
