	
/*****************************************************
 * msSlideOutMenu
 * 09/09/2006
 * 
 * a nice little script to create exclusive, slide-out menus
 *
 * Based on work done by youngpup's ypSlideOutMenu
 *****************************************************/

msSlideOutMenu.Registry = []
msSlideOutMenu.hideDelay = 250
msSlideOutMenu.minCPUResolution = 10

// constructor
function msSlideOutMenu(id, dir, left, top, width, height, showTime, showCode, hideTime, hideCode) {
	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.isLoaded	 = false;

	this.showTime	 = (showTime==0) ? 1 : showTime;
	this.showTimer	 = false

	this.hideTimer	 = false
	this.hideTime	 = (hideTime==0) ? 1 : hideTime;

	this.open	 = false
	this.over	 = false
	this.startTime	 = 0

	// global reference to this object
	this.gRef = "msSlideOutMenu_" + this.id
	eval(this.gRef + "=this")

	// add this menu object to an internal list of all menus
	msSlideOutMenu.Registry[this.id] = this

	this.load(left, top, width, height, showCode, hideCode)
}

msSlideOutMenu.prototype.load = function(left, top, width, height, showCode, hideCode) {
	var d = document
	var lyrId = this.id
	var obj2 = d.getElementById(lyrId)

	if (!obj2)
		window.setTimeout(this.gRef + ".load(" + left + ", " + top + ", " + width + ", " + height + ", " + showCode + ", " + hideCode + ")", 100)
	else {
		var obj1 = d.createElement("div");

		obj1.style.visibility = "hidden"
		obj1.style.display = "none"
		obj1.style.position = "absolute"
		obj1.style.margin = "0px"
		obj1.style.padding = "0px"
		obj1.style.overflow = "hidden"
		obj1.style.zIndex = "99"

		obj1.style.left = left + 'px'
		obj1.style.top = top + 'px'
		obj1.style.width = width + 'px'
		obj1.style.height = height + 'px'
		obj1.style.clip = "rect(1px " + width + "px " + height + "px 0)"


		obj2.parentNode.insertBefore(obj1,obj2);
		obj1.appendChild(obj2)

		this.container	= obj1
		this.style	= obj2.style
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos	= 0
		this.showStep = (this.outPos - this.homePos) / this.showTime / this.showTime
		this.hideStep = (this.outPos - this.homePos) / this.hideTime / this.hideTime

		// set event handlers
		obj2.onmouseover = new Function("msSlideOutMenu.showMenu('" + lyrId + "'); " + ((showCode!=undefined) ? showCode : ""))
		obj2.onmouseout = new Function("msSlideOutMenu.hideMenu('" + lyrId + "'); " + ((hideCode!=undefined) ? hideCode : ""))


		//set initial state
		this.endSlide()
		this.isLoaded = true;
		obj2.style.visibility = "visible"
		obj2.style.display = "block"
	}
}

msSlideOutMenu.showMenu = function(id, left, top) {
	var reg = msSlideOutMenu.Registry
	var obj = msSlideOutMenu.Registry[id]

	if (obj.isLoaded) {
		obj.over = true

		// close other menus.
//		for (menu in reg) if (id != menu) msSlideOutMenu.hide(menu)

		// if this menu is scheduled to close, cancel it.
		if (obj.hideTimer) obj.hideTimer = window.clearTimeout(obj.hideTimer)

		// if this menu is closed, open it.
		if (left) obj.container.style.left = left + 'px';
		if (top) obj.container.style.top = top + 'px'
		if (!obj.open && !obj.showTimer) obj.startSlide(true)
	}
}

msSlideOutMenu.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 = msSlideOutMenu.Registry[id]

	if (obj.isLoaded) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("msSlideOutMenu.hide('" + id + "')", msSlideOutMenu.hideDelay);
	}
}

msSlideOutMenu.hide = function(id) {
	var obj = msSlideOutMenu.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.showTimer) obj.startSlide(false)
}

msSlideOutMenu.prototype.startSlide = function(open) {
	this.open = open
	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime()
	this.showTimer = window.setInterval(this.gRef + ".slide(" + (open? (this.showTime + ", " + this.showStep) : (this.hideTime + ", " + this.hideStep)) + ")", msSlideOutMenu.minCPUResolution)
}

msSlideOutMenu.prototype.slide = function(animTime, step) {
	var elapsed = (new Date()).getTime() - this.startTime

	if (elapsed > animTime)
		this.endSlide()
	else {
		var d = Math.round(Math.pow(animTime-elapsed, 2) * step)

		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)
	}
}

msSlideOutMenu.prototype.endSlide = function() {
	this.showTimer = window.clearTimeout(this.showTimer)
	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)
}

msSlideOutMenu.prototype.setVisibility = function(bShow) { this.container.style.visibility = bShow ? "visible" : "hidden"; this.container.style.display = bShow ? "block" : "none"; }
msSlideOutMenu.prototype.moveTo = function(p) { eval("this.style." + ((this.orientation == "h") ? "left" : "top") + " = p + \"px\"") }

