/**
===========================
 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 : {"aktivitaeten":{"unterricht":{"faecher":{"franzoesisch":{"index.html":"Franz\u00f6sisch"},"gemeinschaftskunde":{"index.html":"Gemeinschaftskunde"},"kunst":{"beispiele":{"index.html":"Beispiele aus dem Unterricht in BK"},"index.html":"Bildende Kunst"},"mathematik":{"index.html":"Mathematik"},"sport":{"index.html":"Sport"},"spanisch":{"index.html":"Spanisch"},"biologie":{"index.html":"Biologie"},"chemie":{"index.html":"Chemie"},"englisch":{"index.html":"Englisch"},"musik":{"index.html":"Musik"},"nat":{"index.html":"Naturph\u00e4nomene"},"index.html":"Aus den F\u00e4chern","latein":{"index.html":"Latein"},"physik":{"index.html":"Physik"},"religion":{"index.html":"Religion"},"deutsch":{"index.html":"Deutsch"},"geschichte":{"index.html":"Geschichte"},"nwt":{"index.html":"NwT"},"geographie":{"index.html":"Geografie"},"psychologie":{"index.html":"Psychologie"}},"index.html":"Unterricht"},"ags":{"index.html":"Arbeitsgemeinschaften","latein-ag":{"index.html":"Latein-AG"},"mosttheater":{"index.html":"Mittel- und Oberstufentheater-AG"},"theater":{"index.html":"Theater-AG"}},"aktuelly":{"index.html":"aktuelly"},"cafeteria":{"index.html":"Cafeteria"},"elternseminar":{"index.html":"ElternSeminar"},"eskimo":{"index.html":"Eskimo"},"index.html":"Aktivit\u00e4ten","kulturforscher":{"index.html":"Kultur.Forscher!"},"laus - der hort":{"index.html":"LAUS - Unser Hort","konzept":{"index.html":"Konzept der LAUS"}},"maerzwoche":{"index.html":"M\u00e4rzwoche"},"patenprogramm":{"index.html":"Patenprogramm"},"schuelerseite":{"index.html":"Aktivit\u00e4ten der Sch\u00fclerinnen und Sch\u00fcler","junior":{"index.html":"Sch\u00fclerfirma JUNIOR"},"smv":{"SMV-Tag Juli 2009":{"index.html":"SMV-Tag"},"index.html":"SMV"}},"schulsanitaetsdienst":{"index.html":"Schulsanit\u00e4tsdienst"},"studienbibliothek":{"index.html":"Studienbibliothek"},"suchtpraevention":{"index.html":"Suchtpr\u00e4vention"},"unterstufenbibliothek":{"index.html":"Unter- und Mittelstufenbibliothek"},"veranstaltungen":{"hocketse":{"index.html":"Hocketse"},"index.html":"Veranstaltungen"},"exkursionen":{"index.html":"Exkursionen"},"projekte":{"index.html":"Projekte"},"austausch":{"drittortbegegnungen":{"index.html":"Drittortbegegnung"},"index.html":"Austausch","sydney":{"index.html":"Sydney"},"villier":{"index.html":"Villiers Saint Georges"}},"bogy":{"index.html":"Berufsorientierung","praktikum":{"index.html":"BOGY-Praktikum"}}},"personen":{"schulleitung":{"index.html":"Schulleitung"},"foerdervereine":{"index.html":"F\u00f6rderer und Freunde","verein_freunde":{"index.html":"F\u00f6rderverein des Elly-Heuss-Knapp-Gymnasiums"}},"index.html":"Personen","schueler":{"Kursstufe":{"index.html":"Kursstufe"},"index.html":"Unsere Sch\u00fclerinnen und Sch\u00fcler"},"lehrer":{"index.html":"Unsere Lehrerinnen und Lehrer"},"eltern":{"elternbeirat":{"index.html":"Ihr Elternbeirat"},"index.html":"Unsere Eltern"},"ehemalige":{"index.html":"Unsere Ehemaligen"},"partner":{"index.html":"Unsere Partner"}},"profil":{"ellyheussknapp":{"index.html":"Elly Heuss-Knapp"},"fremdsprachlich":{"index.html":"Das Elly in der Sprache seiner Partner"},"ganztagsschule":{"index.html":"Ganztagsschule"},"hausordnung":{"index.html":"Hausordnung"},"index.html":"Schulprofil","kurzinfo":{"index.html":"Alles in K\u00fcrze"},"leitbild":{"index.html":"Leitbild"},"methodencurriculum":{"index.html":"Methodencurriculum"},"poolstunden":{"index.html":"Poolstunden"},"qualitaetsentwicklung":{"index.html":"Qualit\u00e4tsentwicklung","evaluation":{"index.html":"Evaluation"}},"schulprofil":{"index.html":"Schulprofil"},"schulprogramm":{"index.html":"Schulprogramm"},"sozialcurriculum":{"index.html":"Sozialcurriculum"},"geschaeftsverteilung":{"index.html":"Gesch\u00e4ftsverteilung"}},"service":{"termine":{"index.html":"Termine"},"index.html":"Service","news":{"index.html":"Neues von der Schulleitung"},"stundenplan":{"index.html":"Stundenplan"},"download":{"index.html":"Download-Center"}},"kontakt":{"impressum":{"index.html":"Impressum"},"anfahrt":{"index.html":"Anfahrt"},"index.html":"Kontakt","mailformular":{"index.html":"Mailformular"},"gaestebuch":{"index.html":"G\u00e4stebuch"}}}
};

HoverNavigation.init();
