// ######################################################################

// ## flipMenu 4.7.8 (c) J. Reijers
// ## Last modifications: 3 Oct 2006

// ######################################################################

// ## Degree of indentation from the left.
	flipIndentation = "5px";

// ## Padding inbetween menu items.
	flipVerticalPadding = "2px";

// ## Margin between the left of the browser and the menu.
	flipLeftMargin = "12px";

// ## Margin between the top of the browser and the menu.
	flipTopMargin = "16px";

// ## Allow multiple menus to fold out without closing all the other open ones.
	flipOpenMultipleMenus = false;

// ## Preserve the current state of the menu (requires cookies).
	flipSaveMenuState = true;

// ## Use custom images for bullets
	flipImages = true;

// ## Images to use
	flipImg_open   = "img/flip_open.gif";
	flipImg_closed = "img/flip_closed.gif";
	flipImg_static = "img/flip_static1.gif";

// ## Initialise all flipMenus onload
	flipInitOnLoad = true;

// ######################################################################

function alterSize(someSize, alterAmount) {
	someSize = String(someSize);
	var tmpNr = parseFloat(someSize.replace(/\D/g, ""));
	var tmpChar = someSize.replace(/\d/g, "");
	return isNaN(tmpNr) ? someSize : ((tmpNr + alterAmount) + tmpChar);
}

isIE = (String(navigator.appVersion).indexOf("MSIE") > -1);
if (!isIE) flipIndentation = alterSize(flipIndentation, -16);
if (!isIE) flipLeftMargin = alterSize(flipLeftMargin, -16);

document.write(
	"<style type=\"text/css\">" +

	"ul.flipMenu { margin-top: " + flipTopMargin + "; margin-left: " + flipLeftMargin + "; " + (flipImages ? "" : "list-style-type: none;") + " }" +
	"ul.flipMenu ul, ul.flipMenu li { padding-top: " + flipVerticalPadding + "; margin-left: " + flipIndentation + "; margin_right: 0px; " + (flipImages ? "" : "list-style-type: none;") + " }" +

	"li.flipFolder { cursor: pointer; " + (flipImages ? "list-style-image: url(" + flipImg_closed + ");" : "") + " }" +
	"ul.flipList { " + (flipImages ? "list-style-image: url(" + flipImg_static + ");" : "") + " }" +

	"</style>"
);

if (flipImages) {
	aFlipPreloads = [];
	aFlipPreloads[0] = new Image;
	aFlipPreloads[0].src = flipImg_open;
	aFlipPreloads[1] = new Image;
	aFlipPreloads[1].src = flipImg_closed;
	aFlipPreloads[2] = new Image;
	aFlipPreloads[2].src = flipImg_static;
}

function addEvent(someObj, someEvent, someFunction) {
	if (someObj.addEventListener) { someObj.addEventListener(someEvent, someFunction, true); return true; } else if (someObj.attachEvent) return someObj.attachEvent("on" + someEvent, someFunction); else return false;
}

function openCloseFlip(theItem, newSetting, openParents) {
	if (theItem.flipID) {
		if (openParents) {
			var tmpItem = theItem;
			while (tmpItem.parentElement || tmpItem.parentNode) {
				tmpItem = (tmpItem.parentElement) ? tmpItem.parentElement : tmpItem.parentNode;
				openCloseFlip(tmpItem, newSetting);
			}
		}
		if (theItem.className == "flipFolder") {
			theItem.getElementsByTagName("UL")[0].style.display = (newSetting == "open") ? "" : "none";
			if (flipImages) theItem.style.listStyleImage = "url(" + (newSetting == "open" ? theItem.flipOpen : theItem.flipClosed) + ")";
		}
	}
}

function openFlip(theItem, openParents) {
	openCloseFlip(theItem, "open", openParents);
}

function closeFlip(theItem, closeParents) {
	openCloseFlip(theItem, "closed", closeParents);
}

function toggleFlip(theElement) {
	if (theElement.flipID) {
		var theItem = theElement;
		var isContained = true;
	} else {
		if (theElement && theElement.button > 0) return false;
		var theItem = (isIE) ? event.srcElement : theElement.target;

		var isContained = false;
		if (theItem.className == "flipFolder") isContained = true; else while (theItem.parentElement || theItem.parentNode) {
			if (theItem.className == "flipStatic" || theItem.className == "flipFolder" || theItem.className == "flipList") {
				isContained = (theItem.className == "flipFolder");
				break;
			}
			theItem = (theItem.parentElement) ? theItem.parentElement : theItem.parentNode;
		}
	}

	var toOpenFlip = (isContained && theItem.getElementsByTagName("UL")[0].style.display == "none");

	if ((theItem.className == "flipStatic" || toOpenFlip) && !flipOpenMultipleMenus) {
		if (theItem.parentElement || theItem.parentNode) {
			var parentUL = (theItem.parentElement) ? theItem.parentElement : theItem.parentNode;
			for (var aCur = 0; aCur < parentUL.childNodes.length; aCur++) closeFlip(parentUL.childNodes[aCur]);
		}
	}

	if (isContained) {
		if (toOpenFlip) openFlip(theItem); else closeFlip(theItem);
	}
}

function setAllFlips(newSetting) {
	if (typeof newSetting == "undefined") var newSetting = "closed";

	var aUL = document.getElementsByTagName("UL");
	for (var aCur = 0; aCur < aUL.length; aCur++) if (aUL[aCur].className == "flipList") {
		var parentFlip = (aUL[aCur].parentElement) ? aUL[aCur].parentElement : aUL[aCur].parentNode;
		if (newSetting == "closed") closeFlip(parentFlip); else openFlip(parentFlip);
	}
}

function openAllFlips() {
	setAllFlips("open");
}

function closeAllFlips() {
	setAllFlips("closed");
}

function initFlip(startElement) {
	if (!document.createElement) return false;

	if (!startElement || !startElement.nodeName) {
		var aUL = document.getElementsByTagName("UL");
		for (var aCur = 0; aCur < aUL.length; aCur++) {
			var curUL = aUL[aCur];
			if (curUL.className == "flipMenu") {
				initFlip(curUL);

				// ## Fix text selecting problem in Mozilla
				curUL.onselectstart = new Function("return false");
				curUL.onmousedown = new Function("return false");
				curUL.onclick = new Function("return true");
			}
		}

		if (flipSaveMenuState) loadMenuState();

		return true;
	}

	if (startElement.className == "flipMenu") startElement.style.display = "";

	if (!startElement.childNodes || startElement.childNodes.length == 0) return false;

	if (typeof flipIDCur == "undefined") flipIDCur = 0;
	var aUL = startElement.getElementsByTagName("UL");
	for (var aCur = 0; aCur < aUL.length; aCur++) {
		var curUL = aUL[aCur];
		if (curUL.className == null || curUL.className == "") curUL.className = "flipList";
		curUL.style.display = "none";
	}

	for (var aCur = 0; aCur < startElement.childNodes.length; aCur++) {
		var curNode = startElement.childNodes[aCur];
		if (curNode.nodeName == "LI") {
			flipIDCur++;
			curNode.flipID = flipIDCur;

			var nodeHasChildren = false;
			var bCur = 0;
			while (!nodeHasChildren && bCur < curNode.childNodes.length) {
				nodeHasChildren = (curNode.childNodes[bCur].nodeName == "UL");
				bCur++;
			}

			if (nodeHasChildren) {
				if (flipImages && curNode.flipClosed) curNode.style.listStyleImage = "url(" + curNode.flipClosed + ")";

				if (curNode.className == null || curNode.className == "") curNode.className = "flipFolder";
				for (var bCur = 0; bCur < curNode.childNodes.length; bCur++) if (curNode.childNodes[bCur].nodeName == "UL") initFlip(curNode.childNodes[bCur]);
			} else {
				curNode.className = "flipStatic";
				if (flipImages && !curNode.style.listStyleImage) {
					if (!curNode.flipStatic) curNode.flipStatic = flipImg_static;
					curNode.style.listStyleImage = "url(" + curNode.flipStatic + ")";
				}
			}

			if (!curNode.flipOpen) curNode.flipOpen = flipImg_open;
			if (!curNode.flipClosed) curNode.flipClosed = flipImg_closed;

			if (curNode.flipIsOpen) openFlip(curNode);
		}
	}
}

function rootOfFlip(flipID, startElement) {

	function containsFlip(startElement, flipID) {
		var flipFound = false;
		var aCur = 0;
		while (aCur < startElement.childNodes.length && !flipFound) {
			var curNode = startElement.childNodes[aCur];
			flipFound = (curNode.flipID == flipID) ? true : containsFlip(curNode, flipID);
			aCur++;
		}
		return flipFound;
	}

	var rootFlip = null;

	if (!startElement || !startElement.nodeName) {
		var aUL = document.getElementsByTagName("UL");
		var aCur = 0;
		while (rootFlip == null && aCur < aUL.length) {
			var curUL = aUL[aCur];
			if (curUL.nodeName == "UL" && curUL.className == "flipMenu") rootFlip = rootOfFlip(flipID, curUL);
			aCur++;
		}
		return rootFlip;
	}

	if (startElement.childNodes) for (var aCur = 0; aCur < startElement.childNodes.length; aCur++) {
		var curNode = startElement.childNodes[aCur];
		if (curNode.flipID == flipID || containsFlip(curNode, flipID)) rootFlip = curNode;
	}

	return rootFlip;
}

function getCookie(cookieName) {
	var allCookies = document.cookie;
	var indexStr = allCookies.indexOf(cookieName + "=");
	if (indexStr == -1) return "";
	indexStr = allCookies.indexOf("=", indexStr) + 1;
	var endStr = allCookies.indexOf(";", indexStr);
	if (endStr == -1) endStr = allCookies.length;
	return unescape(allCookies.substring(indexStr, endStr));
}

function inArray(someID, someArray) {
	for (var aCur = 0; aCur < someArray.length; aCur++) if (someArray[aCur] == someID) return true;
	return false;
}

function getMenuState(startElement) {
	if (!startElement.childNodes || startElement.childNodes.length == 0) return "";

	var openItems = "";
	var aUL = startElement.getElementsByTagName("UL");
	for (var aCur = 0; aCur < aUL.length; aCur++) {
		var curNode = aUL[aCur];
		var parentFlip = (curNode.parentElement) ? curNode.parentElement : curNode.parentNode;
		if (curNode.style.display == "" && parentFlip.flipID) openItems += " " + parentFlip.flipID;		
	}
	return openItems;
}

function putMenuState(startElement) {
	if (!startElement.childNodes || startElement.childNodes.length == 0) return false;

	var aUL = startElement.getElementsByTagName("UL");
	for (var aCur = 0; aCur < aUL.length; aCur++) {
		var curNode = aUL[aCur];
		var parentFlip = (curNode.parentElement) ? curNode.parentElement : curNode.parentNode;
		if (inArray(parentFlip.flipID, aOpenItems)) {
			openFlip(parentFlip);
			if (typeof prevFlipRoot == "undefined") {
				var testRoot = rootOfFlip(parentFlip.flipID);
				if (testRoot.flipID == parentFlip.flipID) prevFlipRoot = testRoot;
			}
		}
	}
}

function saveMenuState() {
	if (flipSaveMenuState) {
		var aUL = document.getElementsByTagName("UL");
		for (var aCur = 0; aCur < aUL.length; aCur++) {
			var curUL = aUL[aCur];
			var curID = curUL.id ? curUL.id : aCur;
			if (curUL.className == "flipMenu") document.cookie = cookiePrefix + curID + "=" + getMenuState(curUL) + ";";
		}
	}
}

function loadMenuState() {
	var aUL = document.getElementsByTagName("UL");
	for (var aCur = 0; aCur < aUL.length; aCur++) {
		var curUL = aUL[aCur];
		var curID = curUL.id ? curUL.id : aCur;
		if (curUL.className == "flipMenu") {
			var savedState = String(getCookie(cookiePrefix + curID));
			if (savedState != "") {
				aOpenItems = savedState.split(" ");
				putMenuState(curUL);
			}
		}
	}

	addEvent(window, "unload", saveMenuState);
}

function clearMenuState(flipMenuID) {
	if (typeof flipMenuID == "undefined") {
		var aUL = document.getElementsByTagName("UL");
		for (var aCur = 0; aCur < aUL.length; aCur++) {
			var curUL = aUL[aCur];
			var curID = curUL.id ? curUL.id : aCur;
			if (curUL.className == "flipMenu") document.cookie = cookiePrefix + curID + "=;";
		}
	} else document.cookie = cookiePrefix + flipMenuID + "=;";
}

cookiePrefix = document.location.pathname + "_";

addEvent(document, "click", toggleFlip);
if (flipInitOnLoad) addEvent(window, "load", initFlip);
