//--- CSS navigation menu -------------------------------------
// Requires: inArray(), multipleClasses()

var cssMenus = {
	delay: 1000, //set delay in milliseconds before menu disappears onmouseout
	LIs: [],
	ULs: [],
	revertLIs: [],
	revertULs: [],
	streamLIs: [],
	streamULs: [],
	activeLI: null,
	activeUL: null,

	clearStreamLI:function() {
		// remove "on" class from list items
		for (var x = 0; x < this.streamLIs.length; x++) {
			multipleClasses("remove", this.streamLIs[x], "on");
		}

		this.streamLIs = [];
	}, 

	hideStreamUL:function() {
		// hide lists
		for (var x = 0; x < this.streamULs.length; x++) {
			this.streamULs[x].style.display = "none";
		}

		this.streamULs = [];
	},

	showActive:function() {
		// show lists
		for (var x = 0; x < this.revertULs.length; x++) {
			this.revertULs[x].style.display = "block";
			this.streamULs[x] = this.revertULs[x];
		}

		// add "on" class to list items
		for (var x = 0; x < this.revertLIs.length; x++) {
			multipleClasses("add", this.revertLIs[x], "on");
			this.streamLIs[x] = this.revertLIs[x];
		}

		// reset activeUL
		this.activeUL = this.revertULs[this.revertULs.length-1];

		// reset activeLI
		this.activeLI = this.revertLIs[this.revertLIs.length-1];
	},

	dropit:function(obj, e, dropmenuID) {
		// actions for list items
		if (e.type == "mouseover") { cssMenus.over(obj); }
		obj.onmouseout = function() { cssMenus.delayer(); }

		// add "on" class to the active list item, set activeLI
		multipleClasses("add", obj, "on");
		this.activeLI = obj;

		// if list item not already in streamLIs, add to streamLIs
		if (!this.streamLIs.inArray(this.activeLI)) {
			this.streamLIs[this.streamLIs.length] = this.activeLI;
		}

		// if a submenu list exists and is not already active, then show submenu list
		if ((dropmenuID != null) && (dropmenuID > "") && (dropmenuID != this.activeUL)) {
			// show new list, set activeUL
			document.getElementById(dropmenuID).style.display = "block";
			this.activeUL = document.getElementById(dropmenuID);

			// if list not already in streamULs and not firstLevel, add to streamULs
			if (!this.streamULs.inArray(this.activeUL) && !multipleClasses("check", this.activeUL, "firstLevel")) {
				this.streamULs[this.streamULs.length] = this.activeUL;
			}

			// actions for lists
			this.activeUL.onmouseover = function() { cssMenus.clearDelay(); }
			this.activeUL.onmouseout = function() { cssMenus.delayer(); }
		}
	},

	over:function(obj) {
		this.clearDelay();		
		this.hide(obj);
	},

	clearDelay:function() {
		if (this.delayhide != "undefined") {
			clearTimeout(this.delayhide);
		}
	},

	hide:function(obj) {
		// if list item is on firstLevel
		if (multipleClasses("check", obj.parentNode, "firstLevel")) {
			// if list not already active
			if (obj.parentNode != this.activeUL) {
				this.hideStreamUL();
				this.clearStreamLI();
			}
		}

		// if list item is on secondLevel
		else if (multipleClasses("check", obj.parentNode, "secondLevel")) {
			// if thirdLevel exists
			if (this.streamULs[1]) {
				// hide thirdLevel
				this.streamULs[1].style.display = "none";

				// remove thirdLevel from streamULs
				this.streamULs.splice(1, 1);

				if (this.streamLIs[2]) {
					// remove thirdLevel "on" class
					multipleClasses("remove", this.streamLIs[2], "on");

					// remove list item from streamLIs
					this.streamLIs.splice(2, 1);
				}
			}

			// if secondLevel already exists
			if (this.streamLIs[1]) {
				// remove "on" class
				multipleClasses("remove", this.streamLIs[1], "on");

				// remove list item from streamLIs
				this.streamLIs.splice(1, 1);
			}
		}

		// if list item is on thirdLevel
		else if (multipleClasses("check", obj.parentNode, "thirdLevel")) {
			// if thirdLevel list item already exists
			if (this.streamLIs[2]) {
				// remove thirdLevel "on" class
				multipleClasses("remove", this.streamLIs[2], "on");

				// remove list item from streamLIs
				this.streamLIs.splice(2, 1);
			}
		}
	},

	delayer:function() {
		cssMenus.clearDelay();
		this.delayhide = setTimeout(function() { cssMenus.revert(); }, this.delay);
	},

	revert:function() {
		this.hideStreamUL();
		this.clearStreamLI();
		this.showActive();
	},

	init:function() {
		// executed at page load

		// build obects of all unordered lists and list items in the navigation div
		this.ULs = document.getElementById(arguments[0]).getElementsByTagName("ul");
		this.LIs = document.getElementById(arguments[0]).getElementsByTagName("li");

		// build array of active lists to revert to
		for (var x = 0; x < this.ULs.length; x++) {
			// if list class contains "on" and not "firstLevel"
			if (multipleClasses("check", this.ULs[x], "on")) {
				if (!multipleClasses("check", this.ULs[x], "firstLevel")) {
					this.revertULs[this.revertULs.length] = this.ULs[x];
					this.streamULs[this.streamULs.length] = this.ULs[x];
				}
			}
		}

		// set active list
		this.activeUL = this.revertULs[this.revertULs.length-1];

		// build array of active list items to revert to
		for (var x = 0; x < this.LIs.length; x++) {
			// if list item class contains "on"
			if (multipleClasses("check", this.LIs[x], "on")) {
				this.revertLIs[this.revertLIs.length] = this.LIs[x];
				this.streamLIs[this.streamLIs.length] = this.LIs[x];
			}
		}

		// set active list item
		this.activeLI = this.revertLIs[this.revertLIs.length-1];

		// create mouseover event for all list items
		for (var x = 0; x < this.LIs.length; x++) {
			this.LIs[x].onmouseover = function(e) {
				var event = typeof e != "undefined" ? e : window.event;
				cssMenus.dropit(this, event, this.firstChild.getAttribute("rel"));
			}
		}
	}
}

addEvent(window, 'load', function () {
	cssMenus.init("navigation");
});
