// xs_menu.js

// initialize the xirasol "package"
if (typeof(xirasol) == "undefined") {
	eval("var xirasol = new Object()");
}

var isMSIE = navigator.userAgent.indexOf("MSIE") > -1;

/*******************************************************************/

xirasol.MenuCommon = function(parent, label, action) {
	this.parent = parent;
	this.label = label;
	this.action = action;
	this.children = new Array();
	this.selected = false;
	this.mouseOver = false;
}

xirasol.MenuCommon.prototype.menuDeselectDelay = 50;
xirasol.MenuCommon.prototype.popupMenuZIndex = 1000;

xirasol.MenuCommon.prototype.getParent = function() { 
	return this.parent;
}

xirasol.MenuCommon.prototype.getLabel = function() {
	return this.label;
}

xirasol.MenuCommon.prototype.getAction = function() {
	return this.action;
}

xirasol.MenuCommon.prototype.doAction = function() {
	if (this.action) {
		if (typeof(this.action) == "function") {
			this.action();
		} else if (typeof(this.action) == "string") {
			location.href = this.action;
		} else if (typeof(this.action) == "object") {
			// not sure what to do yet
		}
	}
}

xirasol.MenuCommon.prototype.getChildCount = function() {
	return this.children.length;
}

xirasol.MenuCommon.prototype.getChildAt = function(i) {
	return this.children[i];
}

xirasol.MenuCommon.prototype.addChild = function(child) {
	this.children.push(child);
}

xirasol.MenuCommon.prototype.setSelected = function(selected) {
	this.selected = selected;
}

xirasol.MenuCommon.prototype.isSelected = function() {
	return this.selected;
}

xirasol.MenuCommon.prototype.checkDeselect = function() {
	if (!this.mouseOver) {
		for(var i = 0; i < this.children.length; i++) {
			if (this.children[i].isMouseOver() || this.children[i].isDescendentMouseOver()) {
				return;
			}
		}
		for(var i = 0; i < this.children.length; i++) {
			this.children[i].setSelected(false);
		}
		if (this.parent != null) {
			this.parent.checkDeselect();
		}
	}
}

xirasol.MenuCommon.prototype.isDescendentMouseOver = function() {
	for(var i = 0; i < this.children.length; i++) {
		if (this.children[i].isMouseOver()) {
			return true;
		} else {
			var descMouseOver = this.children[i].isDescendentMouseOver();
			if (descMouseOver) {
				return true;
			}
		}
	}
	return false;
}

xirasol.MenuCommon.prototype.setMouseOver = function(mouseOver) {
	this.mouseOver = mouseOver;
}

xirasol.MenuCommon.prototype.isMouseOver = function() {
	return this.mouseOver;
}

xirasol.MenuCommon.prototype.getContainer = function() {
	return null;
}

xirasol.MenuCommon.prototype.render = function() {
	for(var i = 0; i < this.children.length; i++) {
		this.children[i].render();
	}
}

xirasol.MenuCommon.prototype.getSelectedChild = function() {
	for(var i = 0; i < this.children.length; i++) {
		if (this.children[i].isSelected()) {
			return this.children[i];
		}
	}
	return null;
}

xirasol.MenuCommon.prototype.deselectSiblings = function() {
	if (this.getParent() != null) {
		for(var i = 0; i < this.getParent().getChildCount(); i++) {
			if (this.getParent().getChildAt(i) != this) {
				this.getParent().getChildAt(i).setSelected(false);
			}
		}
	}
}

/*******************************************************************/

xirasol.MenuBar = function(rootName) {
	xirasol.MenuCommon.call(this, null, null, null);
	this.rootName = rootName;
	this.root = document.getElementById(rootName);
	
	this.div = document.createElement("DIV");
	this.div.className = this.barClassName;
	
	this.root.appendChild(this.div);
}

xirasol.MenuBar.prototype = new xirasol.MenuCommon();

xirasol.MenuBar.prototype.barClassName = "xirasol_menubar";

xirasol.MenuBar.prototype.getContainer = function() {
	return this.div;
}

/*******************************************************************/

xirasol.Menu = function(menuBar, label, action) {
	xirasol.MenuCommon.call(this, menuBar, label, action);
	
	// create the "button" on the menu bar
	this.button = document.createElement("SPAN");
	this.button.className = this.buttonNormalClassName;
	this.button.innerHTML = label;
	
	// create event handlers
	var self = this;
	this.button.onmouseover = function() {
		self.setSelected(true);
		self.setMouseOver(true);
	}
	this.button.onmouseout = function() {
		self.setMouseOver(false);
		setTimeout(function() {
			if (self.getParent() != null) {
				self.getParent().checkDeselect();
			}
		}, this.menuDeselectDelay);
	}
	this.button.onclick = function() {
		self.doAction();
	}
	
	// create the popup div
	this.popup = document.createElement("DIV");
	this.popup.className = this.popupMenuClassName;
	this.popup.style.display = "none";
	this.popup.style.position = "absolute";
	this.popup.style.zIndex = this.popupMenuZIndex;
	this.popup.onmouseover = function() {
		self.setSelected(true);
		self.setMouseOver(true);
	}
	this.popup.onmouseout = function() {
		self.setMouseOver(false);
		setTimeout(function() {
			if (self.getParent() != null) {
				self.getParent().checkDeselect();
			}
		}, this.menuDeselectDelay);
	}
	this.getParent().getContainer().appendChild(this.popup);
	
	this.getParent().addChild(this);
}

xirasol.Menu.prototype = new xirasol.MenuCommon();

xirasol.Menu.prototype.buttonNormalClassName = "xirasol_menu_normal";
xirasol.Menu.prototype.buttonSelectedClassName = "xirasol_menu_selected";
xirasol.Menu.prototype.popupMenuClassName = "xirasol_menu_popup";

xirasol.Menu.prototype.render = function() {
	this.getParent().getContainer().appendChild(this.button);
	xirasol.MenuCommon.prototype.render.call(this);
}

xirasol.Menu.prototype.getContainer = function() {
	return this.popup;
}

xirasol.Menu.prototype.setSelected = function(selected) {
	xirasol.MenuCommon.prototype.setSelected.call(this, selected);
	if (selected) {
		this.button.className = this.buttonSelectedClassName;
		if (this.children.length > 0) {
			this.popup.style.display = "block";
			this.popup.style.left = this.button.offsetLeft;
			this.popup.style.top = this.button.offsetTop + this.button.offsetHeight - 1;
			var targetWidth = this.button.offsetWidth;
			var padding = this.popup.clientLeft * 2;
			if (!isMSIE) { targetWidth -= padding; }
			if (this.popup.offsetWidth < targetWidth) {
				this.popup.style.width = targetWidth;
			}
		}
		this.deselectSiblings();
	} else {
		this.button.className = this.buttonNormalClassName;
		this.popup.style.display = "none";
	}
}

/*******************************************************************/

xirasol.MenuItem = function(menu, label, action) {
	xirasol.MenuCommon.call(this, menu, label, action);

	// create the button on the menu
	this.button = document.createElement("DIV");
	this.button.className = this.buttonNormalClassName;
	this.button.innerHTML = label;
	
	var self = this;
	this.button.onmouseover = function() {
		self.setSelected(true);
		self.setMouseOver(true);
	}
	this.button.onmouseout = function() {
		self.setMouseOver(false);
		setTimeout(function() {
			if (self.getParent() != null) {
				self.getParent().checkDeselect();
			}
		}, this.menuDeselectDelay);
	}
	this.button.onclick = function() {
		self.doAction();
	}
	
	this.getParent().addChild(this);
}

xirasol.MenuItem.prototype = new xirasol.MenuCommon();

xirasol.MenuItem.prototype.buttonNormalClassName = "xirasol_menuitem_normal";
xirasol.MenuItem.prototype.buttonSelectedClassName = "xirasol_menuitem_selected";

xirasol.MenuItem.prototype.render = function() {
	this.getParent().getContainer().appendChild(this.button);
}

xirasol.MenuItem.prototype.setSelected = function(selected) {
	xirasol.MenuCommon.prototype.setSelected.call(selected);
	if (selected) {
		this.button.className = this.buttonSelectedClassName;
		this.deselectSiblings();
	} else {
		this.button.className = this.buttonNormalClassName;
	}
}


