// JavaScript Document
function MenuItem(itemName, itemId) {
	this.id = itemId;
	this.name = itemName;
	this.link = "#";
	this.children = new Array();
}

MenuItem.prototype.id;
MenuItem.prototype.name;
MenuItem.prototype.link;
MenuItem.prototype.children;
MenuItem.prototype.type = "MenuItem";

MenuItem.prototype.setLink = function(itemLink) {
	this.link = itemLink;
}

MenuItem.prototype.appendChild = function(child) {
	if (child.type == "MenuItem") {
		this.children.push(child);
		return this.children;
	}
	else {
		throw("Parameter is not MenuItem type");
	}
}

MenuItem.prototype.hasDescendant = function(filelink) {
	if (this.link == filelink) return true;
	if (this.children.length > 0) {
		for (var i=0; i < this.children.length; i++) {
			if (this.children[i].link == filelink) return true;
		}
	}
	return false;
}

MenuItem.prototype.toDOMObject = function() {
	var itemDOM = document.createElement("span");
	var linkItem = document.createElement("a");
	linkItem.setAttribute("id", this.id);
	linkItem.setAttribute("href", this.link);
	linkItem.style.textDecoration = "none";
	linkItem.onmouseover = function() {
		eval("showMenu(" + this.id + ")");
	}
	if (!this.hasDescendant(getBasename())) linkItem.onmouseout = function() {
		if (!isActive(this)) this.style.color = "#FFFFFF";
	}
	var textItem = document.createTextNode(this.name);
	linkItem.appendChild(textItem);
	if (this.hasDescendant(getBasename())) linkItem.style.color = "#EEB211";
	itemDOM.appendChild(linkItem);
	return itemDOM;
}

function isActive(menuDOMObject) {
	for ( i=0; i<activeMenu.length; i++) {
		if (activeMenu[i] == menuDOMObject) return true;
	}
	return false;
}

function showMenu(menuItem) {
	if (!menuItem) return;
	if (menuItem.type != "MenuItem") throw("Parameter is not MenuItem type");
	var obj = document.getElementById(menuItem.id);
	obj.style.color = "#EEB211";
	clearTimeout(timer);
	if (!objSubMenuContainer.contains(menuItem) && !isActive(obj)) {
		objSubMenuContainer.initialize();
		for (var i=0; i<menuItem.children.length; i++) {
			objSubMenuContainer.appendChild(menuItem.children[i]);
		}
		if (activeMenu.length > 0) {
			var lastPop = activeMenu.pop();
			if (!eval(lastPop.id + ".hasDescendant(getBasename())")) lastPop.style.color = "#FFFFFF";
			else lastPop.style.color = "#EEB211";
		}
		activeMenu.push(obj);
		objSubMenuContainer.getObject().style.visibility = "hidden";
		if (menuItem.children.length > 0) objSubMenuContainer.getObject().innerHTML = "&nbsp;";	
		var container = objSubMenuContainer.draw();
		if (menuItem.children.length > 0) objSubMenuContainer.getObject().innerHTML += "&nbsp;";
		var objArrow = document.getElementById("arrow");
		showArrowUnder(menuItem, obj, container);
		objSubMenuContainer.getObject().style.visibility = "visible";
	}
}

function hideMenu() {
	clearTimeout(timer);
	timer = setTimeout(function() {
									var objArrow = document.getElementById("arrow");
									objArrow.style.display = "none";
									objSubMenuContainer.initialize();
									if (activeMenu.length > 0) {
										var lastPop = activeMenu.pop();
										if (!eval(lastPop.id + ".hasDescendant(getBasename())")) lastPop.style.color = "#FFFFFF";
										else lastPop.style.color = "#EEB211";
									}
									showMenu(defaultOpenedMenu);
								}, 400);
}

function showArrowUnder(menuItem, obj, container) {
	if (menuItem.type != "MenuItem") throw("Parameter is not MenuItem type");
	var objArrow = document.getElementById("arrow");
	
	var objLeft = findPos(obj)[0];
	var objTop = findPos(obj)[1];
	var objWidth = findSize(obj)[0];
	var objHeight = findSize(obj)[1];
	
	var objMenuContainerLeft = findPos(objMenuContainer.getObject())[0];
	var objMenuContainerTop = findPos(objMenuContainer.getObject())[1];
	var objMenuContainerWidth = findSize(objMenuContainer.getObject())[0];
	var objMenuContainerHeight = findSize(objMenuContainer.getObject())[1];
	
	var objSubMenuContainerWidth = findSize(container)[0];
	var objSubMenuContainerHeight = findSize(container)[1];
	
	if (menuItem.children.length > 0) {
		objArrow.style.left =  objLeft + Math.round(objWidth/2) + "px";
		objArrow.style.top = objTop + objHeight + 5 + "px";
		objArrow.style.display = "block";
		if (objLeft + objWidth/2 > objMenuContainerLeft - (objSubMenuContainerWidth - objMenuContainerWidth)) {
			objSubMenuContainer.getObject().style.left = objMenuContainerLeft - (objSubMenuContainerWidth - objMenuContainerWidth) + "px"; 
		}
		else if (objMenuContainerLeft < objLeft - (objSubMenuContainerWidth - objWidth)/2) {
			objSubMenuContainer.getObject().style.left = objLeft - (objSubMenuContainerWidth - objWidth)/2 + "px";
		}
		else {
			objSubMenuContainer.getObject().style.left = objLeft + "px";
		}
			
		objSubMenuContainer.getObject().style.top = findPos(objArrow)[1] + findSize(objArrow)[1] + "px";
	}
	else objArrow.style.display = "none";
}

/*******************************************************/

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function findSize(obj) {
	return [obj.offsetWidth, obj.offsetHeight];
}

function getBasename() {
	return location.href.substring(location.href.lastIndexOf('/')+1);
}

function getObjectByFilename() {
	for (var i=0; i < objMenuContainer.children.length; i++) {
		if (objMenuContainer.children[i].hasDescendant(getBasename())) return objMenuContainer.children[i];
	}
	return false;
}

