/**
===========================
 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 + "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 : {"faecher":{"arbeitslehre":{"Betriebspraktikum":{"index.html":"Betriebspraktikum"},"berufsorientierung":{"index.html":"Berufsorientierung"},"index.html":"Arbeitslehre\/WAT","wahlpflicht":{"index.html":"Al WPU"}},"biologie":{"biomembranen":{"index.html":"Biomembranen"},"index.html":"Biologie","synapsen":{"index.html":"Synapsen"}},"bkunst":{"index.html":"Bildende Kunst"},"chemie":{"index.html":"Chemie","weinherstellung":{"index.html":"Haeckel-Wein"}},"deutsch":{"index.html":"Deutsch"},"englisch":{"index.html":"Englisch"},"geschichte":{"index.html":"Geschichte"},"index.html":"Aus den F\u00e4chern","informatik":{"homepagegestaltung":{"index.html":"Homepagegestaltung"},"index.html":"Informatik"},"mathematik":{"index.html":"Mathematik"},"musik":{"index.html":"Musik"},"physik":{"index.html":"Physik"},"sport":{"bjs":{"index.html":"Bundesjugendspiele"},"fussball":{"index.html":"Fu\u00dfball"},"handball":{"index.html":"Handball"},"index.html":"Sport","laufen":{"index.html":"Laufen"},"skilager":{"index.html":"Skilager"}}},"wer":{"schulleitung":{"index.html":"Schulleitung"},"index.html":"Personen","lehrer":{"index.html":"Die Seite der Lehrer"},"eltern":{"index.html":"Die Seite der Eltern"},"hauspersonal":{"index.html":"Haeckel"},"ehemalige":{"index.html":"unsere Ehemaligen"}},"internbereich":{"Lehrer":{"index.html":"Lehrer intern"},"index.html":"Internbereich","sekzwei":{"index.html":"Abiturstufe"},"vertretungsplan":{"index.html":"Vertretungsplan"}},"was":{"ags":{"index.html":"AG und F\u00f6rderunterricht","modelleisenbahn":{"index.html":"Modelleisenbahn"},"volleyball":{"index.html":"Volleyball"}},"index.html":"Schulalltag","sozbereich":{"index.html":"Sozialp\u00e4dagogischer Bereich"},"team9":{"index.html":"Team 9"},"exkursionen":{"index.html":"Exkursionen"}},"wie":{"termine":{"index.html":"Termine"},"Unterrichtszeiten":{"index.html":"Unterrichtszeiten"},"index.html":"Informationen","news":{"index.html":"Neues von der Schulleitung"},"stundenplan":{"index.html":"Stundenplan"},"download":{"index.html":"Download-Ordner"}},"kontakt":{"impressum":{"index.html":"Impressum"},"anfahrt":{"index.html":"Anfahrt"},"index.html":"Kontakt","mailformular":{"index.html":"Mailformular"},"gaestebuch":{"index.html":"G\u00e4stebuch"},"forum":{"index.html":"Forum"}},"projekte":{"grundschulprojekte":{"index.html":"Grundschulprojekt"},"index.html":"Projekte","kooperationen":{"index.html":"Kooperationen"},"mittelalter":{"index.html":"Mittelalterprojekt"},"nawiwoche":{"index.html":"Nawiwoche"},"projektwochen":{"cafeteriagestaltung":{"index.html":"Neugestaltung der Cafeteria"},"index.html":"Projektwochen","mauertour_2009":{"index.html":"Mauertour 2009"},"mauertour_2010":{"index.html":"Mauertour 2010"},"perlenmacher":{"index.html":"Die Perlenmacher"},"schulrenovierung":{"index.html":"Schulhausrenovierungen"},"spielplatzerkundung":{"index.html":"Spielplatzerkundung"}},"sarahwiener":{"index.html":"Kochen nach Sarah Wiener"},"schulpartnerschaft":{"index.html":"Schulpartnerschaft"},"sommernachtstraum":{"index.html":"Sommernachtstraum"},"stadtumbau":{"index.html":"Schulhofgestaltung"},"stoffe_4000_jahre":{"index.html":"Stoffprojekt"},"tag_der_mathematik":{"index.html":"Tag der Mathematik"},"tdot":{"index.html":"Tag der offenen T\u00fcr"},"team_knorrbremse":{"index.html":"Teamtraining der Knorr-Bremse"},"weihnachtsrevue2009":{"index.html":"Weihnachtsrevue 2009"}}}
};

HoverNavigation.init();