/**
 * dropDownMenu v0.4 sw edition
 * An easy to implement dropDown Menu for Websites, that may be based on styled list tags
 *
 * Works for IE 5.5+ PC, Mozilla 1+ all Plattforms, Opera 7+
 *
 * Copyright (c) 2004 Knallgrau New Medias Solutions GmbH, Vienna - Austria
 *
 * Original written by Matthias Platzer at http://knallgrau.at
 *
 * Modified by Sven Wappler http://www.wappler.eu
 *
 * Use it as you need it
 * It is distributed under a BSD style license
 */


/**
 * Container Class (Prototype) for the dropDownMenu
 *
 * @param idOrElement     String|HTMLElement  root Node of the menu (ul)
 * @param name            String              name of the variable that stores the result
 *                                            of this constructor function
 * @param customConfigFunction  Function      optional config function to override the default settings
 *                                            for an example see Menu.prototype.config
 */
var Menu = Class.create();
Menu.prototype = {

	initialize: function(idOrElement, name, options) {

		this.name = name;
		this.type = "menu";
		this.closeDelayTimer = null;
		this.closingMenuItem = null;
		this.activeItem = null;
		
		
		this.options = {
			collapseBorders: true,
			quickCollapse: true,
	 		closeDelayTime: 500,
	  		menuOpenEvent: 'mouseover',
	  		menuCloseEvent: 'mouseout'
		}
		
		Object.extend(this.options,options || {});
		
		this.rootContainer = new MenuContainer(idOrElement, this, true);
		
		
	}

}

var MenuContainer = Class.create();
MenuContainer.prototype = {
	initialize: function(idOrElement, parent, isRoot) {
		this.type = "menuContainer";
  		this.menuItems = [];
		this.init(idOrElement, parent, isRoot);
	},

	init: function(idOrElement, parent, isRoot) {
	  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;
	  this.isRoot = isRoot;
	  if(!this.isRoot) {
		  if (this.type == "menuContainer"  ) {
		    if (this.element.hasClassName("dropdown")) {
		    	this.menuType = "dropdown";
		    } else if (this.element.hasClassName("flyout")) {
		    	this.menuType = "flyout";
		    } else if (this.element.hasClassName("horizontal")) {
		    	this.menuType = "horizontal";
		    } else if (this.element.hasClassName("harmonica")) {
		    	this.menuType = "harmonica";
		    } else {
		    	this.menuType = "standard";
		    }
		    if(this.menuType == "flyout" || this.menuType == "dropdown" || this.menuType == "horizontal" ) {
		      this.isOpen = false;
			  Element.setStyle(this.element,{
		      	position: "absolute",
		      	top: "0px",
		      	left: "0px",
		      	display:"none"});
		    } 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") {
	          var item = new MenuItem(node, this);
	          this.menuItems.push(item);
	          if(this.isRoot) {
		          if(node.hasClassName("active")) {
		          	this.root.activeItem = item;
		          }
	          }
	        }
	      } else {
	        if (node.tagName.toLowerCase() == "ul") {
	          this.subMenu = new MenuContainer(node, this, false);
	        }
	      }
	    }
	  }
	},

	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.options.closeDelayTimer) 
	  	window.clearTimeout(this.root.options.closeDelayTimer);
	  
	  this.parentMenu.closeAll(this);
	  /*
	  if(Prototype.Browser.IE &&
			typeof document.body.style.maxHeight == "undefined"
	  ) {
		  $$('SELECT').each(function(e){
		 	if(e.style.display != 'none') {
		 		e.setAttribute('display' , e.style.display);
		 		e.style.display = 'none';
		 	}
		  }); 
	  }
	  */
	  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"
		});

	  } else if (this.menuType == "flyout") {
	    var parentMenuBorders = this.parentMenu ? this.parentMenu.getBorders() : new Object();
	    var thisBorders = this.getBorders();
	    if (
	      (Position.positionedOffset(this.parentMenu.element)[0] + this.parentMenu.element.offsetWidth + this.element.offsetWidth + 20) >
	      (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
	    ) {
			Element.setStyle(this.element,{
	      		left: (- this.element.offsetWidth - (this.root.options.collapseBorders ?  0 : parentMenuBorders["left"])) + "px"
			});
	    } else {
			Element.setStyle(this.element,{
	    		left: (this.parentMenu.element.offsetWidth - parentMenuBorders["left"] - (this.root.options.collapseBorders ?  Math.min(parentMenuBorders["right"], thisBorders["left"]) : 0)) + "px"
			});
	    }
		Element.setStyle(this.element,{
	    	top: (this.parent.element.offsetTop - parentMenuBorders["top"] - this.menuItems[0].element.offsetTop) + "px"
		});
	  } else if (this.menuType == "horizontal") {
	  	
	  	
	  	Element.setStyle(this.element,{
			left: parseInt(Position.positionedOffset($('root'))) + "px",
			top: (Position.positionedOffset(this.parent.element)[1] + Element.getHeight(this.parent.element)) + "px"
		});
	  	
	  	
	  }
	  Element.setStyle(this.element,{display:"block"});
	},

	close: function() {
		Element.setStyle(this.element,{display: "none"});
		this.isOpen = false;
		this.closeAll();
		/*
		if(Prototype.Browser.IE &&
			typeof document.body.style.maxHeight == "undefined"
		) {
			$$('SELECT').each(function(e){
			 	try {
			 		e.style.display = e.display;
			 	} catch(e){};
			}); 
		}
		*/
	},

	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);
		var linkTag = this.element.getElementsByTagName("A")[0];
		
		
		if (this.subMenu) {
			this.element.observe(this.root.options.menuOpenEvent, function() {
				this.subMenu.open();
			}.bind(this));
			
			if (linkTag) {
			 	try {
				 	linkTag.observe("focus" , function() {
						this.subMenu.open();
					}.bind(this));
			 	} catch(err){}
			 	
			 	this.link = linkTag;
			 	this.text = linkTag.text;
			}
			
		} else {
			if (this.root.options.quickCollapse) {
			  	this.element.observe(this.root.options.menuOpenEvent, function() {
					this.parentMenu.closeAll();
			  	}.bind(this));
			}
		}
		
		if (this.subMenu) {
			if(this.root.options.menuCloseEvent != '') {
				this.element.observe(this.root.options.menuCloseEvent, function() {
			  		var menuItem = this;
			  		if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
			  		if (menuItem.root.options.closeDelayTimer) window.clearTimeout(menuItem.root.options.closeDelayTimer);
			  		eval(menuItem.root.name + ".closingMenuItem = menuItem");
			  		if(menuItem.root.activeItem && menuItem.parent.isRoot) {
			  			menuItem.root.options.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close();" + menuItem.root.name + ".activeItem.subMenu.open();", menuItem.root.options.closeDelayTime);
			  		} else {
			  			menuItem.root.options.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close();", menuItem.root.options.closeDelayTime);
			  		}
				}.bind(this));
			} 
		} else {
			if(this.root.options.menuCloseEvent != '') {
				this.element.observe(this.root.options.menuCloseEvent, function() {
			  		var menuItem = this;
			  		if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
			  		if (menuItem.root.options.closeDelayTimer) window.clearTimeout(menuItem.root.options.closeDelayTimer);
			  		if(menuItem.root.activeItem && menuItem.parent.isRoot) {
			  			menuItem.root.options.closeDelayTimer = window.setTimeout(menuItem.root.name + ".activeItem.subMenu.open();", menuItem.root.options.closeDelayTime);
			  		}
				}.bind(this));
			}
		}
	},

	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();
		    }
	  	}
	  	
	}
});


