/***
*	A huge thanks to Dean Parkinson for the help with this widget
*/
/************************************************************************************************************
@fileoverview
Slide out menu
Copyright (C) 2007  Dean Parkinson

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Alf Magne Kalleland, 2007
Owner of DHTMLgoodies.com


************************************************************************************************************/	
var isMSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
var browserVersion = parseInt(navigator.userAgent.replace(/.*?MSIE ([0-9]+?)[^0-9].*/g,'$1'));
if(!browserVersion)browserVersion=1;
var currentMenu = null;

function slideChildMenu(aId) {
	currentMenu.slideChildMenu(aId);
}

function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}
function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

function DHTML_MENU(MENU_DIV) {
	this.MENUDIV_ID = MENU_DIV;
	this.SUBMENU_CLASS = MENU_DIV+'_subMenu';
	this.menuItems;
	this.slideSpeed_out = 100;	// Steps to move sub menu at a time ( higher = faster)
	this.slideSpeed_in = 100;
	this.delayMenuClose = 150;	// Microseconds from mouseout to close of menu
	this.slideTimeout_out = 0;	// Microseconds between slide steps ( lower = faster)
	this.slideTimeout_in = 0;	// Microseconds between slide steps ( lower = faster)
	this.xOffsetSubMenu = 0; 	// Offset x-position of sub menu items - use negative value if you want the sub menu to overlap main menu
	this.yOffsetSubMenu = 0;
	this.display = 'horizontal';
	this.subMenuWidth = 120;
	
	/* Don't change anything below here */
	
	this.indeces = new Array();
	this.indeces[0] = 0;
	
	this.mouseOn = function(obj) {
		var mi = this.findNode(this.getSearchIdFromObj(obj));
		if (mi) mi.mouseOn();
	}
	
	this.mouseOff = function(obj) {
		var mi = this.findNode(this.getSearchIdFromObj(obj));
		if (mi) mi.mouseOff();
	}
	
	this.getSearchIdFromObj = function(obj) {
		// pull the postfix off the A link or LI tag id and return the menu item ID
		var objId = obj.id;
		var idx = objId.indexOf('_');
		if (idx>=0) {
			return "MenuItem" + objId.substring(idx);
		}
		return null;
	}
	
	this.slideChildMenu = function(aId) {
		var mi = this.findNode(aId);
		if (mi) mi.slideChildMenu();
	}
	
	this.findNode = function(searchId) {
		var result;
		for (var no=0;no<this.menuItems.length;no++) {
			result = this.menuItems[no].findNode(searchId);
			if (result) return result;
		}
		return null;
	}
	
	this.getNextIndex = function(lvl) {
		var result = 0;
		if (this.indeces.length<=lvl) {
			this.indeces[lvl] = 1;
		} else {
			result = this.indeces[lvl];
			this.indeces[lvl]++;
		}
		return result;
	}
	
	this.collectMenuNodes = function(menuObj) {
		 if (!menuObj) return null;
	
		 var results = new Array();
		 var menuUL = menuObj.getElementsByTagName('UL')[0];
		 if (!menuUL) return null;
		 var menuLI = menuUL.getElementsByTagName('LI')[0];
		if (!menuLI) return null;
		while(menuLI) {
			if(menuLI.tagName && menuLI.tagName.toLowerCase()=='li') {
				  results[results.length] = new MenuItem(menuObj, menuUL, menuLI, 0, null,this);
			}
			menuLI = menuLI.nextSibling;
		 }
		 return results;
	}
	
	this.init = function() {
		var mainDiv = document.getElementById(this.MENUDIV_ID);
		this.menuItems = this.collectMenuNodes(mainDiv);
		if (this.menuItems) {
			for (var no=0;no<this.menuItems.length;no++) {
				var mi = this.menuItems[no];
				mi.renderNode();
			}
			mainDiv.style.visibility = 'visible';
		}
		// window.onresize = resetPosition;
	}
}

function MenuItem(divref, ulref, liref, lvlnum, parentref,menu) {
	this.menu = menu;
	this.parent = parentref;
	this.div = divref;
	this.ul = ulref;
	this.width = this.ul.offsetWidth;
	this.height = this.ul.offsetHeight;
	// this.left = div.style.left.replace(/[^0-9]/g,'');
	this.li = liref;
	this.alink = this.li.getElementsByTagName('A')[0];
	this.lvl = lvlnum;
	this.idx = this.menu.getNextIndex(this.lvl);
	this.children;
	this.subUL = this.li.getElementsByTagName('UL')[0];
	this.children;
	this.isMouseOnMe = false;
	// note: if !isOpen && !isClosed then I am animating a slide
	this.isChildMenuOpen = false;
	this.isChildMenuClosed = true;

	// Constructor
	// if a node does not have an A tag but it's children do then we need
	// null out this node's alink field...
	if (this.alink) {
		if (this.alink.parentNode!=this.li) this.alink = null;
	}
	if (this.subUL) {
		this.children = new Array();
		var subLI = this.subUL.getElementsByTagName('LI')[0];
		while(subLI) {
			if(subLI.tagName && subLI.tagName.toLowerCase()=='li') {
				this.children[this.children.length] = new MenuItem(null, this.subUL, subLI, this.lvl + 1, this,this.menu);
			}
			subLI = subLI.nextSibling;
		}
	}

	this.getPostfix = function() {
		return '_'+this.menu.MENUDIV_ID+'_' + this.idx + '_' + this.lvl;
	}
	
	this.getId = function() {
		return "MenuItem" + this.getPostfix();
	}

	this.hasChildren = function() {
		return (this.children!=null);
	}

	this.getTopPos = function() {
		var origDisp = this.div.style.display;
		this.div.style.display = "";
		var obj = this.li;
		var result = obj.offsetTop;
		while((obj = obj.offsetParent) != null) result += obj.offsetTop;
		this.div.style.display = origDisp;
		return result;
	}

	this.getLeftPos = function() {
		var origDisp = this.div.style.display;
		this.div.style.display = "";
		var obj = this.li;
		var result = obj.offsetLeft;
		while((obj = obj.offsetParent) != null) result += obj.offsetLeft;
		this.div.style.display = origDisp;
		return result;
		
	}

	this.renderNode = function() {
		// set node properties
		this.li.id = "menuItemLI" + this.getPostfix();
		this.ul.style.position = "relative";
		if (this.alink) {
			this.alink.id = "menuItemA" + this.getPostfix();
			this.alink.onmouseover = function() {menu.mouseOn(this);};
			this.alink.onmouseout = function() { menu.mouseOff(this);};
		} else {
			this.li.onmouseover = function() {menu.mouseOn(this);};
			this.li.onmouseout = function() { menu.mouseOff(this);};
		}

		// set sub-menu nodes
		if (this.hasChildren()) {
			var mi = this.children[0];
			var subdiv = document.createElement('DIV');
			subdiv.className=this.menu.SUBMENU_CLASS;
			document.body.appendChild(subdiv);
			subdiv.id = "menuItemDIV" + mi.getPostfix();
			this.subUL.id = "menuItemUL" + mi.getPostfix();
			subdiv.appendChild(this.subUL);
			if (this.menu.display != "vertical") {
				subdiv.style.left = this.getLeftPos() + this.width + this.menu.xOffsetSubMenu + 'px';
				subdiv.style.top = this.getTopPos() + this.menu.yOffsetSubMenu + 'px';
			}
			else {
				subdiv.style.left = this.getLeftPos() + this.menu.xOffsetSubMenu + 'px';
				subdiv.style.top = this.getTopPos() + this.menu.yOffsetSubMenu + 'px';
			}
			subdiv.style.visibility = "hidden";
			subdiv.style.display = "none";
			subdiv.style.zindex = "1000";
			for (var no=0;no<this.children.length;no++) {
				var mi = this.children[no];
				mi.div = subdiv;
				mi.renderNode();
			}
		}
		return this.li;
	}

	this.findNode = function(searchId) {
		var result;
		if (this.getId() == searchId) {
			result = this;
		} else {
			if (this.hasChildren()) {
				for (var no=0;no<this.children.length;no++) {
					var mi = this.children[no];
					result = mi.findNode(searchId);
					if (result!=null) break;
				}
			}
		}
		return result;
	}

	this.mouseOn = function() {
		this.isMouseOnMe = true;
		if (this.hasChildren() && this.isChildMenuClosed) {
			this.initiateChildMenuOpen();
		}
	}

	this.mouseOff = function() {
		this.isMouseOnMe = false;
		
		if (this.hasChildren() && !this.isChildMenuClosed) {
			this.initiateChildMenuClose();
		} else if (this.parent) {
			this.parent.mouseOff();
		}
	}

	this.isMouseOnChild = function() {
		if (this.isMouseOnMe) return true;
		if (this.hasChildren()) {
			for (var no=0;no<this.children.length;no++) {
				if (this.children[no].isMouseOnChild()) return true;
			}
		}
		return false;
	}

	this.initiateChildMenuOpen = function() {
		this.isChildMenuClosed = false;
		var childDiv = this.children[0].div;
		childDiv.style.width = "0px";
		childDiv.style.visibility = "visible";
		childDiv.style.display = "";
		this.slideChildMenu();
	}

	this.initiateChildMenuClose = function() {
		this.isChildMenuOpen = false;
		// we have to wait to close the menu
		// allow the mouse to navigate over the child menu
		currentMenu = this.menu;
		setTimeout("slideChildMenu('" + this.getId() + "')", this.menu.delayMenuClose);
	}

	this.slideChildMenu = function() {
		var divref = this.children[0].div;
		var ulref = this.children[0].ul;
		var maxwidth = this.menu.subMenuWidth;//this.children[0].width;
		var nextWidth;
		if (this.isMouseOnMe  || this.isMouseOnChild()) {
			nextWidth = divref.offsetWidth + this.menu.slideSpeed_out;
			if (nextWidth >= maxwidth) {
				this.finishOpeningChild(divref, ulref, maxwidth);
			} else {
				ulref.style.left = nextWidth - maxwidth + "px";
				divref.style.width = nextWidth + "px";
				//this.menu.slideChildMenu(this.getId());
				currentMenu = this.menu;
				setTimeout("slideChildMenu('" + this.getId() + "')", this.menu.slideTimeout_out);
			}
		} else {
			nextWidth = divref.offsetWidth - this.menu.slideSpeed_in;
			if (nextWidth <= 0) {
				this.finishClosingChild(divref, ulref, maxwidth);
			} else {
				ulref.style.left = nextWidth - maxwidth + "px";
				divref.style.width = nextWidth + "px";
				//this.menu.slideChildMenu(this.getId());
				currentMenu = this.menu;
				setTimeout("slideChildMenu('" + this.getId() + "')", this.menu.slideTimeout_out);
			}
		}
	}

	this.finishOpeningChild = function(divref, ulref, maxwidth) {
		this.isChildMenuOpen = true;
		this.isChildMenuClosed = false;
		ulref.style.left = "0px";
		divref.style.width = maxwidth + "px";
	}

	this.finishClosingChild = function(divref, ulref, maxwidth) {
		this.isChildMenuOpen = false;
		this.isChildMenuClosed = true;
		divref.style.visibility = "hidden";
		divref.style.display = "none";
		divref.style.width = maxwidth + "px";
		if (this.parent) this.parent.mouseOff();
	}

}

function initMenu() {
	var menu2 = new DHTML_MENU('top_menu');
	menu2.display = "vertical";
	menu2.yOffsetSubMenu = 20;
	menu2.init();
	var menu = new DHTML_MENU('dhtmlgoodies_menu');
	menu.xOffsetSubMenu = -2;
	menu.init();
}


window.onload = initMenu;