/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MenuBar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	/**
	 *	MenuBar
	 *	This class provides the ability to manage the buttons in the menu bar, as well as their associated drop downs.
	 *
	 *	@constructor
	 */
	function MenuBar() {
		this.i_buttons = Array();
		
		MenuBar.i_listeners = Array();
		MenuBar.addListener = function(callback, scope) {
			var o = new Object();
			o.callback = callback;
			o.scope = scope;
			MenuBar.i_listeners[MenuBar.i_listeners.length] = o;	
			return o;
		}
		MenuBar.removeListener = function(o) {
			for (var x = 0; x < MenuBar.i_listeners.length; x++) {
				if (MenuBar.i_listeners[x] == o) {
					MenuBar.i_listeners.splice(x, 1);
					return true;
				}
			}
			return false;
		}


		MenuBar.getX = function() {
			return MenuBar.mouseX;;
		}

		MenuBar.getY = function() {
			return MenuBar.mouseY;
		}

		document.onmousemove = MenuBar.setXY;
	}
	
	/**
	 *	Get an array of all buttons in this bar
	 *
	 *	@return an array of all buttons
	 */
	MenuBar.prototype.buttons = function() {
		return this.i_buttons;
	}
	
	/**
	 *	Add a new button to this bar
	 *
	 *	@param button The button to add
	 *
	 *	@return the button which was just added
	 */
	MenuBar.prototype.addButton = function(button) {
		this.i_buttons[this.i_buttons.length] = button;
		return button;
	}
	
	/**
	 *	Remove a button from the bar
	 *
	 *	@param button The button to remove
	 *
	 *	@return true if the button was removed, false otherwise
	 */
	MenuBar.prototype.removeButton = function(button) {
		for (var x = 0; x < this.i_buttons.length; x++) {
			if (this.i_buttons[x] == button) {
				this.i_buttons.splice(x, 1);
				return true;
			}
		}
		return false;
	}
	
	/**
	 *	Get the DIV that contains this menu
	 *
	 *	@return the DIV that contains this menu
	 */
	MenuBar.prototype.getMenu = function() {
		if (this.i_menu == undefined) {
			this.i_menu = document.createElement('DIV');
			this.i_menu.className = "MenuBar";
			
			for (var x = 0; x < this.i_buttons.length; x++) {
				this.i_menu.appendChild(this.i_buttons[x].getButton());
			}
		}
		return this.i_menu;
	}
	
MenuBar.setXY = function(e) {
	if (document.all) {
		MenuBar.mouseX = event.clientX + document.body.scrollLeft;
		MenuBar.mouseY = event.clientY + document.body.scrollTop;
	}
	else {
		MenuBar.mouseX = e.pageX;
		MenuBar.mouseY = e.pageY;
	}
	for (var x = 0; x < MenuBar.i_listeners.length; x++) {
		MenuBar.i_listeners[x].callback.call(MenuBar.i_listeners[x].scope, MenuBar.mouseX, MenuBar.mouseY);
	}
}




/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MenuButton %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	/**
	 *	MenuButton
	 *	This class provides a button to be inserted into a menu
	 *
	 *	@param text The text to display for this button
	 *	@param url The url to load if this button is selected
	 *
	 *	@constructor
	 */
	function MenuBarButton(text, url) {
		this.i_text = text;
		this.i_url = url;
		this.i_open = false;
		
		this.i_children = Array();
	}
	
	
	/**
	 *	Get the left position of this item
	 *
	 *	@return the left position of this menu item
	 */
	MenuBarButton.prototype.left = function() {
		var lf = 0;
		var nx = this.i_button;
		while (nx != null) {
			lf+=parseInt(nx.offsetLeft);
			nx = nx.offsetParent;
		}
		return lf;
	}
	
	/**
	 *	Get the left position of this item
	 *
	 *	@return the left position of this menu item
	 */
	MenuBarButton.prototype.top = function() {
		var tp = 0;
		var nx = this.i_button;
		while (nx != null) {
			tp+=parseInt(nx.offsetTop);
			nx = nx.offsetParent;
		}
		return tp;
	}
	
	
	/**
	 *	Get/Set the text to dispay inside this button
	 *
	 *	@param text (Optional) The new text to display in this button
	 *
	 *	@return the text which is currently in the button
	 */
	MenuBarButton.prototype.text = function(text) {
		if (text != undefined) {
			this.i_text = text;
		}
		return this.i_text;
	}
	
	/**
	 *	Get/Set the url to open when this menu item is selected
	 *
	 *	@param url (Optional) the new url to execute
	 *
	 *	@return the current url that will be opened if this menu item is selected
	 */
	MenuBarButton.prototype.url = function(url) {
		if (url != undefined) {
			this.i_url = url;
		}
		return this.i_url;
	}
	
	/**
	 *	Get the height of this button
	 *
	 *	@return the height of this and all other buttons
	 */
	MenuBarButton.prototype.height = function() {
		return 22;
	}
	
	/**
	 *	Get an array of all child items for this button
	 *
	 *	@return an array of all child items
	 */
	MenuBarButton.prototype.children = function() {
		return this.i_children;
	}
	
	/**
	 *	Add a new child to this button
	 *
	 *	@param item the child item to add to this button
	 *
	 *	@return the child that was just added
	 */
	MenuBarButton.prototype.addChild = function(child) {
		this.i_children[this.i_children.length] = child;
		return child;
	}
	
	/**
	 *	Remove a child from this button
	 *
	 *	@param button The child to remove
	 *
	 *	@return true if the child was removed, false otherwise
	 */
	MenuBarButton.prototype.removeChild = function(child) {
		for (var x = 0; x < this.i_children.length; x++) {
			if (this.i_children[x] == child) {
				this.i_children.splice(x, 1);
				return true;
			}
		}
		return false;
	}
	
	/**
	 *	Get/Set if this button is currently open (in a hover state)
	 *
	 *	@param state (Optional) the new hover state of this button
	 *
	 *	@return the current hover state of this button
	 */
	MenuBarButton.prototype.isOpen = function(state) {
		if (state != undefined) {
			this.i_open = state;
			if (this.i_button != undefined) {
				this.i_button.className = "MenuBarButton" + (this.isOpen() ? "_open" : "");
				this.i_button_inner.className = "MenuBarButton2" + (this.isOpen() ? "_open" : "");
			}
			if (state) {
				if (this.i_children.length > 0) {
					document.body.appendChild(this.getSubMenu());
				}
				this.getSubMenu().style.left = this.left() +  "px";
				this.getSubMenu().style.top = (this.top() + this.height()) + "px";
				
			}
			else {
				try {
					document.body.removeChild(this.getSubMenu());
				} catch (e) { }
			}	
		}
		return this.i_open;
	}
	
	/**
	 *	Handle when the mouse is moved over a button
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 *
	 *	@return true
	 */
	MenuBarButton.handleMouseOver = function(e) {
		var ev = (document.all ? event : e);
		
		if (MenuBarButton.i_active == this.pObj) {
			return true;
		}
		
		if (MenuBarButton.i_active != undefined) {
			MenuBarButton.handleMouseOut();
		}

		this.pObj.isOpen(true);
		var ev = new Object();

		var me = this.pObj;	


		ev.button_width = parseInt(me.i_button.offsetWidth);
		ev.button_height = me.height();
		ev.button_top = me.top();
		ev.button_left = me.left();
		ev.menu_top = ev.button_top + ev.button_height;
		ev.menu_left = ev.button_left;
		ev.menu_width = parseInt(me.i_sub.offsetWidth);
		ev.menu_height = parseInt(me.i_sub.offsetHeight);

		MenuBarButton.i_active = this.pObj;
		MenuBarButton.i_ev = ev;
		MenuBarButton.i_cur_mon = MenuBar.addListener(MenuBarButton.handleMouseMove, this);

		
		
	}
	
	/**
	 *	Handle when the mouse is moved while a sub menu is open
	 *
	 *	@private
	 *
	 *	@param x the x position of the cursor
	 *	@param y the y position of the cursor
	 *
	 *	@return true
	 */
	MenuBarButton.handleMouseMove = function(x, y) {
				
		var me = MenuBarButton.i_active;
		
				
		if ((x >= MenuBarButton.i_ev.button_left && x <= MenuBarButton.i_ev.button_left + MenuBarButton.i_ev.button_width &&
		     y > MenuBarButton.i_ev.button_top && y <= MenuBarButton.i_ev.button_top + MenuBarButton.i_ev.button_height)) {
		     
		     	
			if (me.actTimer != undefined) {
				clearTimeout(me.actTimer);
				me.actTimer = null;
			}
		     
		    return true;
		     
		}
		else if (me.i_children.length > 0 && 
		    	x >= MenuBarButton.i_ev.button_left && x <= MenuBarButton.i_ev.button_left + MenuBarButton.i_ev.menu_width &&
		    	y >= MenuBarButton.i_ev.menu_top && y <= MenuBarButton.i_ev.menu_top + MenuBarButton.i_ev.menu_height) {
			
			
			if (me.actTimer != undefined) {
				clearTimeout(me.actTimer);
				me.actTimer = null;
			}
		    
		    return true;
		}
		else {
			if (me.actTimer == null) {
				me.actTimer = setTimeout(function() {
					MenuBarButton.handleMouseOut();
					me.actTimer = null;
				}, 500);
			}
		}
	}
	
	/**
	 *	Handle when the mouse is moved away form a button
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 *
	 *	@return true
	 */
	MenuBarButton.handleMouseOut = function(e) {
		if (MenuBarButton.i_active != undefined) {
			if (MenuBarButton.i_active.actTimer != null) {
				clearTimeout(MenuBarButton.i_active.actTimer);
			}
			MenuBarButton.i_active.isOpen(false);
			if (MenuBarButton.i_cur_mon != null) {
				MenuBar.removeListener(MenuBarButton.i_cur_mon);
				MenuBarButton.i_cur_mon = null;
			}
			MenuBarButton.i_active = null;
		}
	}
	
	/**
	 *	Handle when the mouse is depressed on a button
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 *
	 *	@return true
	 */
	MenuBarButton.handleMouseDown = function(e) {
		var ev = (document.all ? event : e);
		if (this.pObj.url() != undefined && this.pObj.url() != "") {
			self.location = this.pObj.url();
		}
	}	
	
	
	/**
	 *	Get the DIV that contains this items sub menu
	 *
	 *	@return the DIV that contains this items sub menu
	 */
	MenuBarButton.prototype.getSubMenu = function() {
		if (this.i_sub == undefined) {
			this.i_sub = document.createElement('DIV');
			this.i_sub.className = "MenuBarButton_submenu";
			
			for (var x = 0; x < this.i_children.length; x++) {
				this.i_sub.appendChild(this.i_children[x].getItem());
			}
		}
		return this.i_sub;
	}
	
	
	/**
	 *	Get the DIV that contains this button
	 *
	 *	@return the DIV that contains this button
	 */
	MenuBarButton.prototype.getButton = function() {
		if (this.i_button == undefined) {
			this.i_button = document.createElement('DIV');
			this.i_button.className = "MenuBarButton" + (this.isOpen() ? "_open" : "");
			this.i_button.pObj = this;
			this.i_button.onmouseover = MenuBarButton.handleMouseOver;
			//this.i_button.onmouseout = MenuBarButton.handleMouseOut;
			this.i_button.onmousedown = MenuBarButton.handleMouseDown;
			
				this.i_button_inner = document.createElement('DIV');
				this.i_button_inner.className = "MenuBarButton2" + (this.isOpen() ? "_open" : "");
				this.i_button.appendChild(this.i_button_inner);
				
					this.i_button_inner.innerHTML = this.text();

		}
		return this.i_button;
	}
	
/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MenuBarButtonChild %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	/**
	 *	MenuBarButtonChild
	 *	This class represents a child item in a menu button
	 *
	 *	@param text The text to display for this item
	 *	@param url The url to open when this item is selected
	 *
	 *	@constructor
	 */
	function MenuBarButtonChild(text, url) {
		this.i_text = text;
		this.i_url = url;
		this.i_hover = false;
	}
	
	/**
	 *	Get/Set the text to dispay inside this button
	 *
	 *	@param text (Optional) The new text to display in this button
	 *
	 *	@return the text which is currently in the button
	 */
	MenuBarButtonChild.prototype.text = function(text) {
		if (text != undefined) {
			this.i_text = text;
		}
		return this.i_text;
	}
	
	/**
	 *	Get/Set the url to open when this menu item is selected
	 *
	 *	@param url (Optional) the new url to execute
	 *
	 *	@return the current url that will be opened if this menu item is selected
	 */
	MenuBarButtonChild.prototype.url = function(url) {
		if (url != undefined) {
			this.i_url = url;
		}
		return this.i_url;
	}
	
	/**
	 *	Get/Set if this menu item is currently being hovered over
	 *
	 *	@param state (Optional) The new hover state
	 *
	 *	@return the current hover state
	 */
	MenuBarButtonChild.prototype.hover = function(state) {
		if (state != undefined) {
			this.i_hover = state;
			this.i_item.className = "MenuBarButtonChild" + (this.hover() ? "_hover" : "");
		}
		return this.i_hover;
	}
	
	/**
	 *	Handle when the user mouses over this item
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 *
	 *	@return true
	 */
	MenuBarButtonChild.handleMouseOver = function(e) {
		this.pObj.hover(true);
	}
	
	/**
	 *	Handle when the user mouses away from this item
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 *
	 *	@return true
	 */
	MenuBarButtonChild.handleMouseOut = function(e) {
		this.pObj.hover(false);
	}
	 
	/**
	 *	Get the DIV that contains this menu item
	 *
	 *	@return the DIV that contains this menu item
	 */
	MenuBarButtonChild.prototype.getItem = function() {
		if (this.i_item == undefined) {
			this.i_item = document.createElement('DIV');
			this.i_item.className = "MenuBarButtonChild" + (this.hover() ? "_hover" : "");
			this.i_item.pObj = this;
			this.i_item.onmouseover = MenuBarButtonChild.handleMouseOver;
			this.i_item.onmouseout = MenuBarButtonChild.handleMouseOut;
			this.i_item.onmousedown = MenuBarButton.handleMouseDown;
			this.i_item.innerHTML = this.text();
		}
		return this.i_item;
	}