/*
	Name: k_menu.js
	Description: Dynamic cascade menu.
		Tested in FireFox 2.0.0.11, IE6, IE7, Opera 9.24
	Date: 7 january 2008
	Version: 1.2
	Author: Alexander Koleda
	All right reserved.
*/
function KAIDropDownMenu(oArray, tContainer) {
	if(!tContainer) {
		alert("KAIDropDownMenu error:\nNo container name.");
		return;
	}
	// Setup block
	this.timer = 30;
	this.TopHeight = 25;
	this.TopOffset = 0;
	this.SubMenuChar = "&raquo;";
	this.CurrentClassName = "current";
	this.aClassName = "kaimenu";
	this.tAutoExtension = ".html";
	// End Setup block
	var oThis = this;
	this.BoxName = tContainer;
	this.ParentName = tContainer + "_cnt_";
	this.DivName = tContainer + "_div_";
	var parentBox = document.getElementById(tContainer);
	for(var i = 0; i < oArray.length; i++) {
		if(!oArray[i][0]) {
			var new_a = document.createElement("a");			
			new_a.href = this.SmartHref(oArray[i][2]);
			new_a.appendChild(document.createTextNode(oArray[i][1]));
			new_a.className = this.aClassName;
			if(oArray[i][4]) {
				new_a.className += " " + oThis.CurrentClassName;
			}			
			var subMenuId = oArray[i][3];
			parentBox.appendChild(new_a);
			if(subMenuId) {
				new_a.href = "#";
				new_a.onclick = function() {return false;}
				oThis.BuildSubMenu(new_a, oArray, subMenuId);
			}
		}
	}
	this.CorrectHorizontalPositions();
}

KAIDropDownMenu.prototype.BuildSubMenu = function(oAppendTo, oArray, iStartID) {
	var oThis = this;
	oAppendTo.id = this.ParentName + iStartID;
	oAppendTo.onmouseover = function() {oThis.ShowSubMenu(oThis.GetID(this));}
	oAppendTo.onmouseout = function() {
		var localThis = this;
		setTimeout(function() {oThis.HideSubMenu(oThis.GetID(localThis));}, oThis.timer);
		}
	var menuContainer = document.createElement("div");
	menuContainer.id = this.DivName + iStartID;
	menuContainer.className = "k_menu";
	menuContainer.style.top = this.getTop(oAppendTo) + oAppendTo.offsetHeight + this.TopOffset + "px";
	menuContainer.appendChild(this.AddSubMenu(oArray, iStartID));
	menuContainer.firstChild.style.left = "0px";
	menuContainer.firstChild.style.top = "0px";	
	menuContainer.firstChild.onmouseover = function() {
			this.style.visibility = "visible";
			this.style.display = "block";
		}
	menuContainer.firstChild.onmouseout = function() {
			this.style.visibility = "";
			oThis.HideSubMenu(this.parentNode);
		}
	document.body.appendChild(menuContainer);
}

KAIDropDownMenu.prototype.AddSubMenu = function(oArr, iDeep) {	
	var oUl = null;	
	var oThis = this;
	var iCount = -1;
	for(var i = 0; i < oArr.length; i++) {
		if(oArr[i][0] == iDeep) {
			if(!oUl) {
				oUl = document.createElement("ul");
				oUl.className = "dropdownmenu";
			}
			iCount++;
			var new_a = document.createElement("a");
			new_a.href = this.SmartHref(oArr[i][2]);
			new_a.appendChild(document.createTextNode(oArr[i][1]));
			if(oArr[i][4]) {
				new_a.className += " " + oThis.CurrentClassName;
			}
			var new_li = document.createElement("li");
			new_li.style.top = (iCount * oThis.TopHeight) + "px";
			if(oArr[i][3]) {
				new_li.className = "drop";
				var new_span = document.createElement("span");
				new_span.className = "subchar";
				new_span.innerHTML = oThis.SubMenuChar;
				new_a.appendChild(new_span);
			}
			new_li.appendChild(new_a);
			if(oArr[i][3]) {
				new_li.appendChild(this.AddSubMenu(oArr, oArr[i][3]));
				new_li.onmouseover = function() {oThis.ShowSubMenu(this);}
				new_li.onmouseout = function() {oThis.HideSubMenu(this);}
			}
			oUl.appendChild(new_li);			
		}
	}
	if(!oUl) { return null; }
	oUl.lastChild.className += " last";
	oUl.style.display = "none";
		
	return oUl;	
}

KAIDropDownMenu.prototype.CorrectHorizontalPositions = function() {
	var parentBox = document.getElementById(this.BoxName);
	var oThis = this;
	for(var i = 0; i < parentBox.childNodes.length; i++) {
		var currID = parentBox.childNodes[i];
		if(currID.id.length > 0) {
			oThis.GetID(currID).style.left = oThis.getLeft(document.getElementById(currID.id)) + "px";					
		}
	}
}

KAIDropDownMenu.prototype.SmartHref = function(h) {
	if(h == "") h = "#";
	if((h != "#") && !h.match(/\./)) {
		h += this.tAutoExtension;
	}
	
	return h;
}

KAIDropDownMenu.prototype.GetID = function(ID) {
	var oThis = this;
	return document.getElementById(ID.id.replace(oThis.ParentName, oThis.DivName));
}

KAIDropDownMenu.prototype.ShowSubMenu = function(ID) {
	var oUl = null;
	for(var i = 0; i < ID.childNodes.length; i++) {
		oUl = ID.childNodes[i];
		if(oUl.tagName == "UL") {
			oUl.style.display = "block";
			return;
		}
	}
}

KAIDropDownMenu.prototype.HideSubMenu = function(ID) {
	var oUl = null;	
	for(var i = 0; i < ID.childNodes.length; i++) {
		oUl = ID.childNodes[i];
		if(oUl.tagName == "UL") {
			if(oUl.style.visibility != "visible") {
				oUl.style.display = "none";
			}			
			return;	
		}
	}	
}

KAIDropDownMenu.prototype.getLeft = function(oNode) {
    var iLeft = 0;
    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;
    }
    return iLeft;
}

KAIDropDownMenu.prototype.getTop = function(oNode) {
    var iTop = 0;
    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
    return iTop;
}