/*global ActiveA, InactiveA, getHeight, hideAllSubmenus, hideMenu, hideObject4Menu, hideSubmenus, menubtnActive, menubtnInactive, showObjectPdf, toggle, topActiveA, topInactiveA */

var topActiveA = "url(http://indu-sol.com/pics/indusol/menu2.jpg) no-repeat -35px 0px";
var topInactiveA = "url(http://indu-sol.com/pics/indusol/menu2.jpg) no-repeat -35px -28px";
var ActiveA = "url(http://indu-sol.com/pics/indusol/menu2.jpg) no-repeat -35px 0px";
var InactiveA = "url(http://indu-sol.com/pics/indusol/menu2.jpg) no-repeat -35px -28px";

function menubtnActive(obj) {
	obj.style.background = topActiveA;
}

function menubtnInactive(obj) {
	obj.style.background = topInactiveA;
}

function getHeight(obj) {
	var iHeight = 0;
	var childs = obj.childNodes;
	for (var i = 0; i < childs.length; i++) {
		if (typeof(childs[i].tagName) !== 'undefined' && childs[i].tagName === "LI") {
			iHeight += ((isNaN(parseInt(childs[i].offsetHeight,10))) ? 0 : parseInt(childs[i].offsetHeight,10));
		}
	}
	return iHeight;
}

function hideSubmenus(obj) {
	obj.style.visibility = "hidden";		// sub is visible, hide it
	var objChilds = obj.childNodes;		// get subs ChildNodes
	var grandChilds;
	var oTop;
	var iTop;
	var currentObj;
	for (var i = 0; i < objChilds.length; i++) {	// hide subs of childnodes if any
		if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "LI") {
			grandChilds = objChilds[i].childNodes;
			for (var j = 0; j < grandChilds.length; j++) {
				if (typeof(grandChilds[j].tagName) !== 'undefined' && grandChilds[j].tagName === "A") {
					if (obj.className === "" && objChilds[i].className === "") {	// set background-image of non-hovering A's
						grandChilds[j].style.background = InactiveA;
					}
				}
				if (typeof(grandChilds[j].tagName) !== 'undefined' && grandChilds[j].tagName === "UL") {
					oTop = parseInt(getHeight(grandChilds[j]),10);		// get ULs height
					currentObj = grandChilds[j].parentNode;				// get reference of parent LI
					while (currentObj.nextSibling !== null) {				// and move sibling-LIs of parent-LI further up
						currentObj = currentObj.nextSibling;
						if (typeof(currentObj.tagName) !== 'undefined' && currentObj.tagName === "LI") {
							iTop = ((currentObj.style.top.length > 0) ? parseInt(currentObj.style.top,10) : 0) - oTop;
							currentObj.style.top = ((iTop>0) ? iTop : 0) + "px";
						}
					}
 					hideSubmenus(grandChilds[j]);
				}
			}
		}
	}
}

function hideAllSubmenus(obj, isTop) {
	var oTop;
	var iTop;
	hideSubmenus(obj);				// hide subs if any (recursive)
	oTop = parseInt(getHeight(obj),10);		// get its height
	obj = obj.parentNode;				// get reference of parent LI
	while (obj.nextSibling !== null) {		// and move sibling-LIs of parent-LI further up
		obj = obj.nextSibling;
		if (!isTop && typeof(obj.tagName) !== 'undefined' && obj.tagName === "LI") {
			iTop = ((obj.style.top.length > 0) ? parseInt(obj.style.top,10) : 0) - oTop;
			obj.style.top = iTop + "px";
		}
	}
}

function hideMenu() {
	var currentObj = document.getElementById("menutop").childNodes;
	var objChilds;
	for (var j = 0; j < currentObj.length; j++) {
		if (typeof(currentObj[j].tagName) !== 'undefined' && currentObj[j].tagName==="LI") {
			objChilds= currentObj[j].childNodes;
			for (var i=0; i < objChilds.length; i++) {
				if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "A") {
					objChilds[i].style.background = topInactiveA;
				}
				if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "UL") {
					hideAllSubmenus(objChilds[i], true);
				}
			}
		}
	}
}

function hideObject4Menu() {
	var opdf = document.getElementById("opdf");
	opdf.style.display = "none";
	opdf.style.visibility = "hidden";
}

function showObjectPdf(thisobj) {
	var opdf;
	if (typeof(opdf = document.getElementById("opdf")) !== 'undefined' && opdf !== null && opdf.style) {
		if (typeof(opdf.data) !== 'undefined') {
			opdf.data = thisobj.href;
		} else if (typeof(opdf.src) !== 'undefined') {
			opdf.src = thisobj.href;
		}
		opdf.style.display = "block";
		opdf.style.visibility = "visible";
	}
}

function toggle(thisobj) {
	// get the parent UL and determine if it is the topmost UL (with cssclass="inhaltmenu-top")
	var isTop = false;
	var obj = thisobj;
	var oTop;
	var currentObj;
	var iTop;
	var objChilds;
	var i;
	var opdf;
	while (obj.parentNode.tagName !== "UL") {
		obj = obj.parentNode;
	}
	obj = obj.parentNode;
	isTop = (typeof(obj.className) !== 'undefined' && obj.className === "inhaltmenu-top") ? true : false;

	// get the following UL if any for changing visiblity
	obj = thisobj;
	while (obj.nextSibling !== null && obj.tagName !== "UL") {
		obj = obj.nextSibling;
	}
	if (obj.tagName === "UL" && obj.style.visibility === "visible") { // sub is visible, hide it and all submenus
		if (isTop) {
			thisobj.style.background = topInactiveA;
			if (typeof(opdf = document.getElementById("opdf")) !== 'undefined' && opdf !== null && opdf.style && opdf.style.visibility === "visible") {
				hideObject4Menu();
			} else {
				hideAllSubmenus(obj, isTop);
			}
		} else {
			thisobj.style.background = InactiveA;
			hideAllSubmenus(obj, isTop);
		}
	} else { 							// sub isnt visible, show it
		if (obj.tagName === "UL" && obj.style) { 
			obj.style.visibility = "visible";
		}
		if (isTop) {
			thisobj.style.background = topActiveA;
			if (typeof(opdf = document.getElementById("opdf")) !== 'undefined' && opdf !== null && opdf.style && opdf.style.visibility === "visible") {
				hideObject4Menu();
			} 
		} else {
			thisobj.style.background = ActiveA;
		}
		oTop = parseInt(getHeight(obj),10);	// get its height
		currentObj=obj.parentNode;		// get reference of parent LI
		if (!isTop) {					// if not in top UL
			while (currentObj.nextSibling !== null) {	// move nextSibling-LIs of parent-LI further down
				currentObj = currentObj.nextSibling;			
				if (typeof(currentObj.tagName) !== 'undefined' && currentObj.tagName === "LI") {
					iTop = oTop + ((currentObj.style.top.length > 0) ? parseInt(currentObj.style.top,10) : 0);
					currentObj.style.top = iTop + "px";
				}
			}
		} else {							// in top UL hide siblings
			currentObj = obj.parentNode;		// get reference of parent LI
			while (currentObj.nextSibling !== null) {	// hide subs of nextSiblings
				currentObj = currentObj.nextSibling;			
				if (typeof(currentObj.tagName) !== 'undefined' && currentObj.tagName === "LI") {
					objChilds = currentObj.childNodes;
					for (i = 0; i < objChilds.length; i++) {
						if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "A") {
							objChilds[i].style.background = topInactiveA;
						}
						if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "UL") {
							hideAllSubmenus(objChilds[i],true);
						}
					}
				}
			}
			currentObj=obj.parentNode;			// get reference of parent LI
			while (currentObj.previousSibling !== null) {	// hide subs of previousSiblings
				currentObj = currentObj.previousSibling;			
				if (typeof(currentObj.tagName) !== 'undefined' && currentObj.tagName === "LI") {
					objChilds = currentObj.childNodes;
					for (i = 0; i < objChilds.length; i++) {
						if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "A") {
							objChilds[i].style.background = topInactiveA;
						}
						if (typeof(objChilds[i].tagName) !== 'undefined' && objChilds[i].tagName === "UL") {
							hideAllSubmenus(objChilds[i],true);
						}
					}
				}
			}

		}
	}
	if (document.getElementById("opdf")) {
		document.getElementById("opdf").style.display = "none";
	}
}

/*members background, childNodes, className, data, display, 
    getElementById, href, length, nextSibling, offsetHeight, parentNode, 
    previousSibling, src, style, tagName, top, visibility
*/