var Menu = Class.create();

Menu.prototype = {
	
	initialize: function(idOrElement, name, customConfigFunction) {
		this.name = name;
		this.type = "menu";
		this.closeDelayTimer = null;
		this.closingMenuItem = null;

		this.config();
		if (typeof customconfigFunction == "function") {
			this.customConfig = customConfigFunction;
			this.customConfig();

		}
		this.rootcontainer = new MenuContainer(idOrElement,this);
	},

	config: function() {
		this.collapseBorder = true;
		this.quickCollapse = true;
		this.closeDelayTime = 500;

	}
}


var MenuContainer = Class.create();
MenuContainer.prototype = {

	initialize: function(idOrElement,parent) {

		this.type = "menuContainer";
		this.menuItems = [];
		this.init(idOrElement,parent);

	},

	init: function(idOrElement,parent) {
		this.element = $(idOrElement);
		this.parent = parent;
		this.parentMenu = (this.type == "menuContainer") ? ((parent) ? parent.parent : null) : parent;
		this.root = parent instanceof Menu ? parent : parent.root;
		this.id = this.element.id;

		if (this.type == "menuContainer") 
		{
	  		if (this.element.hasClassName("nav_container")) this.menuType = "horizontal";
			else if (this.element.hasClassName("navDropdown")) this.menuType = "dropdown";
			else
				this.menuType="flyout";
				
			if (this.menutype == "dropdown"){
			  this.isopen = false;
			}
			else {
				this.isOpen = true;
			}
		}
		else
		{
		  this.isOpen = this.parentMenu.isOpen;	
		}
		
		
		var childNodes = this.element.childNodes;
		if (childNodes == null) return;
		for (var i=0; i< childNodes.length; i++) {
			var node = childNodes[i];
			if (node.nodeType == 1) 
			{
			 if (this.type == "menuContainer") 
			 {
				if (node.tagName.toLowerCase() == "li") {
					this.menuItems.push(new MenuItem(node,this));
				}
			 }
				else {
					if (node.tagName.toLowerCase() == "ul") {
						this.subMenu = new MenuContainer(node,this);
					}
				}				
			}
		}	
			
	},

	getBorders: function(element) {
		var ltrb = ["Left","Top","Right","Bottom"];
		var result = {};
		for (var i=0; i < ltrb.length; ++i)	{
		
			if (this.element.currentStyle)
				var value = parseInt(this.element.currentStyle["border"+ltrb[i]+"Width"]);
			else if (window.getComputedStyle)
				var value = parseInt(window.getComputedStyle(this.element,"")/getPropertyValue("border-"+ltrb[i].toLowerCase()+"-width"));
			else
				var value = parseInt(this.element.style["border"+ltrb[i]]);
			result[ltrb[i].toLowerCase()] = isNaN(value) ? 0 : value;

		}
		return result;	

	},


	open: function() {

		if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
		this.parentMenu.closeAll(this);
		this.isOpen = true;
		if (this.menuType == "dropdown")
		{
			Element.setStyle(this.element,{
				left: (Position.positionedOffset(this.parent.element)[0]) + "px",
				top: (Position.positionedOffset(this.parent.element)[1] + Element.getHeight(this.parent.element)) + "px"
			});
	
		}

		Element.setStyle(this.element,{visibility: "visible"});
	},


	close: function() {
		Element.setStyle(this.element,{visibility:"hidden"});
		this.parent.element.removeClassName('activeItem');

		if (Element.hasClassName(this.parent.element,'currentItem'))
			Element.setStyle(Element.firstDescendant(this.parent.element),{color:'#1966AE'});
		else
		  {			
			if (Element.hasClassName(Element.firstDescendant(this.parent.element),'grayed'))
				Element.setStyle(Element.firstDescendant(this.parent.element),{color:'#CCCCCC'});
			else
				Element.setStyle(Element.firstDescendant(this.parent.element),{color:'#FFFFFF'});
			
		  }
		this.isOpen = false;
		this.closeAll();
	},

	closeAll: function(trigger) {
		for (var i=0; i < this.menuItems.length; ++i)
		{
			this.menuItems[i].closeItem(trigger);
		}
	}
}

var MenuItem = Class.create();

Object.extend(Object.extend(MenuItem.prototype,MenuContainer.prototype), {
	initialize: function(idOrElement,parent){
	
		var menuItem = this;
		this.type = "menuItem";
		this.subMenu;
		this.init(idOrElement,parent);

		if (this.subMenu) {
			this.element.onmouseover = function() {
				this.addClassName('activeItem');
				Element.setStyle(this.childNodes[0],{color:'#1966AE'});
				menuItem.subMenu.open();
			}
		}else {
			if (this.root.quickCollapse) {
				this.element.onmouseover = function() {
					menuItem.parentMenu.closeAll();
				}
			}
		}
		var linkTag = this.element.getElementsByTagName("A")[0];
		if (linkTag) {
			linkTag.onfocus = this.element.onmouseover;
			this.link = linkTag;
			this.text = linkTag.text;
		}
		if (this.subMenu) {
			this.element.onmouseout = function() 
			{
			if (menuItem.root.openDelayTimer) window.clearTimeout (menuItem.root.openDelayTimer);
			if (menuItem.root.closeDelayTimer) window.clearTimeout(menuItem.root.closeDelayTimer);
			//eval(menuItem.root.name + ".closingMenuItem = menuItem");
			$(menuItem.root.name).closingMenuItem = menuItem;
			menuItem.root.closeDelayTimer = window.setTimeout("document.getElementById('" + menuItem.root.name + "').closingMenuItem.subMenu.close()",menuItem.root.closeDelayTime);
			}
		}
	},

	openItem: function() {
		this.isOpen = true;
		if (this.subMenu) {this.subMenu.open();}
	},

	closeItem: function(trigger) {
		this.isOpen = false;
		if (this.subMenu) {
			if (this.subMenu != trigger) this.subMenu.close();
		}
	}

});


var menu;

function configMenu() {
	this.closeDelayTime = 300;
}


function initMenu() {
  menu = new Menu('root', 'fpi_navigation', configMenu);
}


Event.observe(window, 'load', initMenu, false);









