ypSlideOutMenu.Registry = []
ypSlideOutMenu.aniLen = 650
ypSlideOutMenu.hideDelay = 325
ypSlideOutMenu.minCPUResolution = 10

// constructor
function ypSlideOutMenu(id, dir, left, top, width, height)
{
	this.ie  = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0
	this.css = "";

	if (this.ie || this.ns4 || this.dom) {
		this.id			 = id
		this.dir		 = dir
		this.orientation = dir == "left" || dir == "right" ? "h" : "v"
		this.dirType	 = dir == "right" || dir == "down" ? "-" : "+"
		this.dim		 = this.orientation == "h" ? width : height
		this.hideTimer	 = false
		this.aniTimer	 = false
		this.open		 = false
		this.over		 = false
		this.startTime	 = 0

		// global reference to this object
		this.gRef = "ypSlideOutMenu_"+id
		eval(this.gRef+"=this")

		// add this menu object to an internal list of all menus
		ypSlideOutMenu.Registry[id] = this

	
		var d = document
		d.write('<style type="text/css">')
		d.write('#' + this.id + 'Container { visibility:hidden; ')
		d.write('left:' + left + 'px; ')
		d.write('top:' + top + 'px; ')
		d.write('overflow:hidden; z-index:10000;}')
		d.write('#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; ')
		d.write('width:' + width + 'px; ')
		d.write('height:' + height + 'px; ')
		d.write('clip:rect(0 ' + width + ' ' + height + ' 0); ')
		d.write('}')
		d.write('</style>')
//alert(left);
		this.load()
	}
}

ypSlideOutMenu.writeCSS = function() {
	document.writeln('<style type="text/css">');

	for (var id in ypSlideOutMenu.Registry) {
		document.writeln(ypSlideOutMenu.Registry[id].css);
	}

	document.writeln('</style>');
}

ypSlideOutMenu.prototype.load = function() {
	var d = document
	var lyrId1 = this.id + "Container"
	var lyrId2 = this.id + "Content"
	var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	var temp

	if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)
	else {
		this.container	= obj1
		this.menu		= obj2
		this.style		= this.ns4 ? this.menu : this.menu.style
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos		= 0
		this.accelConst	= (this.outPos - this.homePos) / ypSlideOutMenu.aniLen / ypSlideOutMenu.aniLen 

		// set event handlers.
		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("ypSlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("ypSlideOutMenu.hideMenu('" + this.id + "')")

		//set initial state
		this.endSlide()
	}
}
	
ypSlideOutMenu.showMenu = function(id)
{
	var reg = ypSlideOutMenu.Registry
	var obj = ypSlideOutMenu.Registry[id]
	
	if (obj.container) {
		obj.over = true

		// close other menus.
		for (menu in reg) if (id != menu) ypSlideOutMenu.hide(menu)

		// if this menu is scheduled to close, cancel it.
		if (obj.hideTimer) { reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }

		// if this menu is closed, open it.
		if (!obj.open && !obj.aniTimer) reg[id].startSlide(true)
	}
}

ypSlideOutMenu.hideMenu = function(id)
{
	// schedules the menu to close after <hideDelay> ms, which
	// gives the user time to cancel the action if they accidentally moused out
	var obj = ypSlideOutMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("ypSlideOutMenu.hide('" + id + "')", ypSlideOutMenu.hideDelay);
	}
}

ypSlideOutMenu.hideAll = function()
{
	var reg = ypSlideOutMenu.Registry
	for (menu in reg) {
		ypSlideOutMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

ypSlideOutMenu.hide = function(id)
{
	var obj = ypSlideOutMenu.Registry[id]
	obj.over = false

	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	
	// flag that this scheduled event has occured.
	obj.hideTimer = 0

	// if this menu is open, close it.
	if (obj.open && !obj.aniTimer) obj.startSlide(false)
}

ypSlideOutMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]()
	this.open = open

	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime()	
	this.aniTimer = window.setInterval(this.gRef + ".slide()", ypSlideOutMenu.minCPUResolution)
}

ypSlideOutMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > ypSlideOutMenu.aniLen) this.endSlide()
	else {
		var d = Math.round(Math.pow(ypSlideOutMenu.aniLen-elapsed, 2) * this.accelConst)
		if (this.open && this.dirType == "-")		d = -d
		else if (this.open && this.dirType == "+")	d = -d
		else if (!this.open && this.dirType == "-")	d = -this.dim + d
		else										d = this.dim + d

		this.moveTo(d)
	}
}

ypSlideOutMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer)
	this.moveTo(this.open ? this.outPos : this.homePos)
	if (!this.open) this.setVisibility(false)
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over)
	}
}

ypSlideOutMenu.prototype.setVisibility = function(bShow) { 
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}
ypSlideOutMenu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
ypSlideOutMenu.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}

// events
ypSlideOutMenu.prototype.onactivate		= function() { }
ypSlideOutMenu.prototype.ondeactivate	= function() { }





/***
*	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 MENUDIV_ID = "dhtmlgoodies_menu";
var SUBMENU_CLASS = 'dhtmlgoodies_subMenu';
var menuItems;
var slideSpeed_out = 80;	// Steps to move sub menu at a time ( higher = faster)
var slideSpeed_in = 50;
var delayMenuClose = 150;	// Microseconds from mouseout to close of menu
var slideTimeout_out = 5;	// Microseconds between slide steps ( lower = faster)
var slideTimeout_in = 10;	// Microseconds between slide steps ( lower = faster)
var xOffsetSubMenu = 0; 	// Offset x-position of sub menu items - use negative value if you want the sub menu to overlap main menu

/* Don't change anything below here */

var indeces = new Array();
indeces[0] = 0;
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;

function mouseOn(obj) {
	var mi = findNode(getSearchIdFromObj(obj));
	if (mi) mi.mouseOn();
}

function mouseOff(obj) {
	var mi = findNode(getSearchIdFromObj(obj));
	if (mi) mi.mouseOff();
}

function getSearchIdFromObj(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;
}

function slideChildMenu(aId) {
	var mi = findNode(aId);
	if (mi) mi.slideChildMenu();
}

function findNode(searchId) {
	var result;
	for (var no=0;no<menuItems.length;no++) {
		result = menuItems[no].findNode(searchId);
		if (result) return result;
	}
	return null;
}

function getNextIndex(lvl) {
	var result = 0;
	if (indeces.length<=lvl) {
		indeces[lvl] = 1;
	} else {
		result = indeces[lvl];
		indeces[lvl]++;
	}
	return result;
}

function MenuItem(divref, ulref, liref, lvlnum, parentref) {
	this.parent = parentref;
	this.div = divref;
	this.ul = ulref;
	this.width = this.ul.offsetWidth;
	// this.left = div.style.left.replace(/[^0-9]/g,'');
	this.li = liref;
	this.alink = this.li.getElementsByTagName('A')[0];
	this.lvl = lvlnum;
	this.idx = 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);
			}
			subLI = subLI.nextSibling;
		}
	}

	this.getPostfix = function() {
		return '_' + 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() {mouseOn(this);};
			this.alink.onmouseout = function() {mouseOff(this);};
		} else {
			this.li.onmouseover = function() {mouseOn(this);};
			this.li.onmouseout = function() {mouseOff(this);};
		}

		// set sub-menu nodes
		if (this.hasChildren()) {
			var mi = this.children[0];
			var subdiv = document.createElement('DIV');
			subdiv.className=SUBMENU_CLASS;
			document.body.appendChild(subdiv);
			subdiv.id = "menuItemDIV" + mi.getPostfix();
			this.subUL.id = "menuItemUL" + mi.getPostfix();
			subdiv.appendChild(this.subUL);
			subdiv.style.left = this.getLeftPos() + this.width + xOffsetSubMenu + 'px';
			subdiv.style.top = this.getTopPos() + '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
		setTimeout("slideChildMenu('" + this.getId() + "')", delayMenuClose);
	}

	this.slideChildMenu = function() {
		var divref = this.children[0].div;
		var ulref = this.children[0].ul;
		var maxwidth = this.children[0].width;
		var nextWidth;
		if (this.isMouseOnMe  || this.isMouseOnChild()) {
			nextWidth = divref.offsetWidth + slideSpeed_out;
			if (nextWidth >= maxwidth) {
				this.finishOpeningChild(divref, ulref, maxwidth);
			} else {
				ulref.style.left = nextWidth - maxwidth + "px";
				divref.style.width = nextWidth + "px";
				setTimeout("slideChildMenu('" + this.getId() + "')", slideTimeout_out);
			}
		} else {
			nextWidth = divref.offsetWidth - slideSpeed_in;
			if (nextWidth <= 0) {
				this.finishClosingChild(divref, ulref, maxwidth);
			} else {
				ulref.style.left = nextWidth - maxwidth + "px";
				divref.style.width = nextWidth + "px";
				setTimeout("slideChildMenu('" + this.getId() + "')", 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 collectMenuNodes(menuObj) {
     if (!menuObj) return null;

     var results = new Array();
     var menuUL = menuObj.getElementsByTagName('UL')[0];
     var menuLI = menuUL.getElementsByTagName('LI')[0];
     while(menuLI) {
        if(menuLI.tagName && menuLI.tagName.toLowerCase()=='li') {
              results[results.length] = new MenuItem(menuObj, menuUL, menuLI, 0, null);
        }
        menuLI = menuLI.nextSibling;
     }
     return results;
}

function initMenu() {
	var mainDiv = document.getElementById(MENUDIV_ID);
	menuItems = collectMenuNodes(mainDiv);
	if (menuItems) {
		for (var no=0;no<menuItems.length;no++) {
			var mi = menuItems[no];
			mi.renderNode();
		}
		mainDiv.style.visibility = 'visible';
	}
	// window.onresize = resetPosition;
}

window.onload = initMenu;