// global variables
var debugHistory = "";
var debugSteps = 0;
var debugMode = 0;
var item_count;
var search_clicked = false;

var posCurrent;
var posCurrentLink;
var posOver1;
var posOver1Link;
var posOver2;
var posOver2Link;

// start function
window.onload = function start() {
	item_count = parseInt(document.getElementById("item_count").firstChild.data);
	if (!isNaN(item_count)) {
		addToHistory("Procedure is warming up ... ;) "+item_count+" menu items at all.");
		hideDepth();
		checkAndOpen();
	} else {
		addToHistory("Unfortunately unable to count menu items :(");
	}
	// show where user is
	determinePagePos();
}

// DropDown Menu Functions

/* Every menu item has its fixed depth (between 1 and 3) and position. The script starts with the position of
 * the clicked menu item. Then it displays all following items with the depth of the clicked one - 1 (which means
 * all subitems). Notice: To ensure only the needed menu items are shown, all other ones except of depth1 / ml1(main points) and
 * the ignored ones (for depth3 / ml3) are hidden.
 *
 * Proudly copyrighted by Tobias Krebs :D
 */

function hideDepth(ignoreDepth) {
	addToHistory("Hide all submenu items. Ignore "+(ignoreDepth == undefined ? "nothing" : ignoreDepth)+".");
	for (var i = 1; i <= item_count; i++) {
		var tmp = document.getElementById("pos"+i);
		if (tmp != null) {
			if (tmp.className != "ml1" && tmp.className != ignoreDepth) {
				tmp.style.display = "none";
			}
		} else {
			addToHistory("tmp was null for pos"+i+" when trying to hide it.");
		}
	}
}

function addToHistory(text) {
	debugSteps++;
	debugHistory += debugSteps+". "+text+"<br />";
	if (debugMode == 1) {
		startDebug();
	}
}

function open(pos) {
	hideDepth(document.getElementById("pos"+pos).className);
	addToHistory("Submenu is opened on position "+pos+".");
	openRecursive(pos, pos);
}

function openRecursive(dynamicPos, startPos) {
	var startElement = document.getElementById("pos"+startPos);
	var thisElement = document.getElementById("pos"+dynamicPos);
	var nextElement;

	if (dynamicPos == item_count) {
		nextElement = false;
	} else {
		nextElement = document.getElementById("pos"+(dynamicPos+1));
	}

	var startElementDepth = parseInt(startElement.className.charAt(startElement.className.length - 1));
	var thisElementDepth = parseInt(thisElement.className.charAt(thisElement.className.length - 1));
	var nextElementDepth;
	if (nextElement != false) {
		nextElementDepth = parseInt(nextElement.className.charAt(nextElement.className.length - 1));
	}

	if ((startElementDepth + 1) == thisElementDepth) {
		thisElement.style.display = "block";
	}

	if (nextElement != false && nextElementDepth > startElementDepth) {
		dynamicPos++;
		openRecursive(dynamicPos, startPos);
	}
}

// Automatically determine position in menu and open it

function checkAndOpen() {
	var url = document.URL;
	for (var i = 1; i <= item_count; i++) {
		var tmp_attributes = document.getElementById("pos"+i).firstChild.attributes;
		for (var j = 0; j < tmp_attributes.length; j++) {
			var tmp_attr_name = tmp_attributes[j].nodeName;
			var tmp_attr_value = tmp_attributes[j].nodeValue;
			if (tmp_attr_name == "href") {
				var tmp_filename = tmp_attr_value.substr(1, tmp_attr_value.length);
				if (url.indexOf(tmp_filename) != -1) {
					var tmp_element = document.getElementById("pos"+i);
					// change style of current element
					posCurrent = tmp_element.firstChild.innerHTML;
					posCurrentLink = tmp_element.firstChild.href;
					tmp_element.firstChild.style.fontWeight = "bold";
					tmp_element.firstChild.style.textDecoration = "underline";
					var tmp_depth = parseInt(tmp_element.className.charAt(tmp_element.className.length - 1));
					if (tmp_depth == 3) {
						var tmp_nh = nextHighest(i, tmp_depth - 2);
						open(tmp_nh);
						posOver1 = document.getElementById("pos"+tmp_nh).firstChild.innerHTML;
						posOver1Link = document.getElementById("pos"+tmp_nh).firstChild.href;
						tmp_nh = nextHighest(i, tmp_depth - 1);
						open(tmp_nh);
						posOver2 = document.getElementById("pos"+tmp_nh).firstChild.innerHTML;
						posOver2Link = document.getElementById("pos"+tmp_nh).firstChild.href;
					} else if (tmp_depth == 2) {
						var tmp_nh = nextHighest(i, tmp_depth - 1);
						open(tmp_nh);
						posOver1 = document.getElementById("pos"+tmp_nh).firstChild.innerHTML;
						posOver1Link = document.getElementById("pos"+tmp_nh).firstChild.href;
					}
				}
			}
		}
	}
}

function nextHighest(pos, requiredDepth) {
	var resPos = pos;
	for (var i = pos; i > 0; i--) {
		var tmp = document.getElementById("pos"+i).className;
		if (tmp == "ml"+requiredDepth) {
			resPos = i;
			break;
		}
	}
	return resPos;
}

// Start and show history for debug mode
function startDebug() {
	debugMode = 1;
	document.getElementById("menu_debug").innerHTML = "<b>Menu Debuglog:</b><br />"+debugHistory;
	document.getElementById("menu_debug").style.cursor = "auto";
	document.getElementById("page_debug").style.display = "block";
}

// determine and output position on page menu
function determinePagePos() {
	var posString = '';
	var startPos = 'Startseite';
	var showBC = true;

	if (posOver1 != undefined) {
		posString += ' -> <a href="'+posOver1Link+'">'+posOver1+'</a>';
	}

	if (posOver2 != undefined) {
		posString += ' -> <a href="'+posOver2Link+'">'+posOver2+'</a>';
	}

	if (posCurrent != startPos && posCurrent != undefined) {
		posString += ' -> <a href="'+posCurrentLink+'">'+posCurrent+'</a>';
	}

	if (posCurrent == undefined && (document.URL).search('php') != -1) {
		showBC = false;
	}
	
	if (showBC) {
		posString = posString.replace(/\- /g, '');
		document.getElementById("positionline").innerHTML = 'Sie sind hier: <a href="./index.php">'+startPos+'</a>'+posString;
	}
}

// search field
function changeSearch() {
	if (!search_clicked) {
		document.getElementById("searchinput").value = "";
		search_clicked = true;
	}
}

function navclick(id) {
	open(id);
	return false;
}