/**
===========================
 globale Seiten-Navigation
===========================
*
* Dieses Script enthält die Struktur der
* globalen Seiten-Navigation in einem Objekt
* (in JSON-Schreibweise), damit die globale
* Navigation durch Aufklappmenüs erweitert
* werden kann.
*
* WICHTIG: Die Inhalte des Objektes "navi" werden dynamisch
*          vom einem serverseitigen Script aktualisiert,
*          sodass manuelle Änderungen verloren gehen werden!
*
* erstellt von Felix Riesterer (Felix.Riesterer@gmx.net)
**/

var HoverNavigation = {

	n : null, // contains reference to <div id="navigation">
	m : null, // contains reference to our new <div id="hover-navigation">

	baseURL : false,
	oldEl : null, // contains a reference to last hovered element
	hide : false, // flag for time out before hiding menue
	timer : 0,
	interval : false, // will contain a reference to an interval object

	oldWinOnLoad : false,
	oldDocOnMouseMove : false,

	init : function () {
		var t = this;
		t.oldWinOnLoad = window.onload;
		window.onload = function () {
			if (typeof t.oldWinOnLoad == "function") {
				t.oldWinOnLoad();
			}
			t.setup();
		};
	},

	setup : function () {
		var t = this, scripts, i;

		// create "contains" method for all HTML element objects if not supported by browser
		if (window.Node && Node.prototype && !Node.prototype.contains) {
			Node.prototype.contains = function (arg) {
				return !!(this.compareDocumentPosition(arg) & 16);
			};
		}

		// find "navigation" div
		t.n = document.getElementById("navigation");

		if (t.n) {
			// set some URLs and load additional CSS
			scripts = document.getElementsByTagName("script");			
			for (i = 0; i < scripts.length; i++) {
				if (scripts[i].src && scripts[i].src.match(/hover-navigation\.js$/)) {
					// set baseURL
					t.baseURL = scripts[i].src.replace(/^((http:\/\/[^\/]+)?\/[^\/]+\/).*/i, "$1");

					// load CSS
					document.getElementsByTagName("head")[0].appendChild(
						t.createElement({
							link : {
								type : "text/css",
								rel : "stylesheet",
								href : scripts[i].src.replace(/js\/hover-navigation\.js/, "css/hover-navigation.css")
							}
						})
					);
				}
			}

			// create <div> element
			t.m = t.createElement({div:{id:"hover-navigation"}});
			t.n.insertBefore(t.m, t.n.firstChild);

			// set event handler
			t.oldDocOnMouseMove = document.onmousemove;
			document.onmousemove = function (e) {
				if (typeof t.oldDocOnMouseMove == "function") {
					t.oldDocOnMouseMove(e);
				}
				t.mouseMove(e);
			};
		}

		t.interval = window.setInterval(function () {
			if (t.hide) {
				t.timer--;
				if (t.timer < 1) {
					t.m.style.display = "";
				}
			}
		}, 10);
	},

	createElement : function (o) {
		/* o must be an object of this structure:
			{
				<tagName> : {
					attributeName : value,
					attributeName2: value2
				}
			}
		*/
		var element, e, a;

		for (e in o) {
			if (e) {
				element = document.createElement(e);
				for (a in o[e]) {
					element[a] = o[e][a];
				}
			}
		}

		return element;
	},

	mouseMove : function (e) {
		var t = HoverNavigation,
			el, test, inMenue, sub, u, menues;

		if (!e) {
			e = window.event; // IE
		}

		el = e.target || e.srcElement;

		// find out if we are inside our navigation
		test = t.n.contains(el);
		inMenue = t.m.contains(el);

		if (test) {
			// show menue
			t.hide = false; // disable hide countdown
			t.timer = 50; // re-set timer

			if (!inMenue
				&& el.tagName
				&& el.tagName.toLowerCase() == "a"
				&& el.href
				&& el.href != ""
			) {
				if (el != t.lastEl) {
					// build new menue
					t.menue(el);
				}

				t.lastEl = el; // remember this element as "hovered last"

				// reposition hover menue
				if (!inMenue) {
					t.m.style.left = (el.offsetLeft + el.offsetWidth - 10) + "px";
					t.m.style.top = (
						el.offsetTop - (
							(t.m.firstChild && t.m.firstChild.childNodes.length > 1) ?
								Math.floor(t.m.offsetHeight / 3)
								: 0
						)
					) + "px";
				}

				// show menue (if not empty)
				t.m.style.display = t.m.firstChild ? "block" : "";

			} else {
				// dynamically display sub-menues
				menues = [];
				sub = el;
				while (sub != t.n) {
					if (sub.tagName && sub.tagName.toLowerCase() == "ul") {
						menues[menues.length] = sub;
					}

					if (sub.tagName && sub.tagName.toLowerCase() == "li") {
						// do we have a hidden sub-menue?
						if (sub.childNodes.length > 1) {
							u = sub.childNodes[1];
							menues[menues.length] = u;
							u.style.display = "block";
							u.style.left = (u.parentNode.parentNode.offsetWidth - 10) + "px";
							u.style.top = (
								u.parentNode.offsetTop - (
									u.childNodes.length > 1 ?
										Math.floor(u.offsetHeight / 3)
										: 0
								)
							) + "px";
						}
					}
					sub = sub.parentNode;
				}

				// hide all sub-menues
				sub = t.m.getElementsByTagName("ul");
				for (u = 0; u < sub.length; u++) {
					sub[u].style.display = "";
				}

				// re-display only selected menues
				for (u = 0; u < menues.length; u++) {
					menues[u].style.display = "block";
				}
			}

		} else {
			// let menue hide
			t.hide = true;
		}
	},

	menue : function (el) {
		var t = this,
			path = el.href.replace(/^(http:\/\/[^\/]+)?\/[^\/]+\//i, "").replace(/\/[^\/]+$/, ""),
			chunk, list;

		// clear old menue contents
		while (t.m.firstChild) {
			t.m.removeChild(t.m.firstChild);
		}

		chunk = '["' + path.split("/").join('"]["') + '"]';
		eval ("list = t.navi" + chunk);

		if (list = t.createList(list, path)) {
			t.m.appendChild(list);
		}
	},

	createList : function (list, path) {
		var t = this,
			o = t.createElement({ul:{}}),
			test, length, li, x, a, i, ul;

		// create <li> elements
		for (i in list) {
			if (!i.match(/^index\.htm/i)) {
				test = 0;
				li = t.createElement({li:{}});
				for (x in list[i]) {
					test++;
					if (x.match(/^index\.htm/i)) {
						li.appendChild(
							t.createElement({
								a : {
									href : t.baseURL+path+"/"+i+"/"+x
								}
							})
						);
						li.firstChild.appendChild(
							document.createTextNode(list[i][x])
						);
					}
				}

				if (test > 1) {
					// sub-menue!
					li.appendChild(t.createList(list[i], path + "/" + i));
				}

				// add <li> to <ul>
				o.appendChild(li);
			}
		}

		return o.firstChild ? o : false;
	},

	// Folgendes Objekt wird dynamisch aktualisiert!
	navi : {"wer":{"schulleitung":{"index.html":"Schulleitung"},"hausmeister":{"index.html":"Hausmeister"},"index.html":"Schulteam","lehrerkollegium":{"index.html":"Kollegium"},"schulkonferenz":{"index.html":"Schulkonferenz"},"schulpflegschaft":{"index.html":"Schulpflegschaft"},"sekretariat":{"index.html":"Sekretariat"}},"foerderverein":{"index.html":"F\u00f6rderverein"},"ogs":{"angebote":{"index.html":"Angebote"},"index.html":"Offene Ganztagsschule","leitung":{"index.html":"Leitung"},"mitarbeiter":{"index.html":"OGS Team"},"oeffnungszeiten":{"index.html":"\u00d6ffnungszeiten"}},"was":{"unterricht":{"faecher":{"deutsch":{"index.html":"Deutsch"},"index.html":"Aus den F\u00e4chern","mathematik":{"index.html":"Mathematik"}},"index.html":"Unterricht"},"ags":{"index.html":"Arbeitsgemeinschaften"},"index.html":"Aktivit\u00e4ten","projekte":{"index.html":"Projekte"}},"wie":{"termine":{"index.html":"Termine"},"angebote":{"index.html":"Angebote"},"arbeitsschwerpunkte":{"index.html":"Arbeitsschwerpunkte"},"faq":{"index.html":"Fragen und Antworten"},"index.html":"Unsere Schule","kooperationen":{"index.html":"Kooperationen"},"leitziele":{"index.html":"Leitziele"},"news":{"index.html":"Aktuell"},"qualitaetsprofil":{"index.html":"Qualit\u00e4tsprofil"},"schulleben":{"index.html":"Schulleben"},"unterrichtszeiten":{"index.html":"Unterrichtszeiten"},"unterstuetzen":{"index.html":"Wir unterst\u00fctzen"},"download":{"index.html":"Download-Ordner"}},"kontakt":{"index.html":"Kontakt \/ Impressum"}}
};

HoverNavigation.init();
