
/*****************************************************
 * ypSlideOutMenu
 * 3/04/2001
 * 
 * a nice little script to create exclusive, slide-out menus
 *
 * --youngpup--
 *****************************************************/

ypSlideOutMenu.Registry = []
ypSlideOutMenu.hideDelay = 250
ypSlideOutMenu.minCPUResolution = 10

// constructor
function ypSlideOutMenu(id, dir, left, top, width, height, showTime, hideTime) {
	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 = "ypSlideOutMenu_" + this.id
	eval(this.gRef + "=this")

	// add this menu object to an internal list of all menus
	ypSlideOutMenu.Registry[this.id] = this

	this.load(left, top, width, height)
}

ypSlideOutMenu.prototype.load = function(left, top, width, height) {
	var d = document
	var lyrId = this.id
	var obj2 = d.getElementById(lyrId)

	if (!obj2)
		window.setTimeout(this.gRef + ".load(" + left + ", " + top + ", " + width + ", " + height + ")", 100)
	else {
		var obj1 = d.createElement("div");

		obj1.style.visibility = "hidden"
		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(0 " + 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("ypSlideOutMenu.showMenu('" + lyrId + "')")
		obj2.onmouseout = new Function("ypSlideOutMenu.hideMenu('" + lyrId + "')")

		//set initial state
		this.endSlide()
		this.isLoaded = true;
		obj2.style.visibility = "visible"
	}
}

ypSlideOutMenu.showMenu = function(id, left, top) {
	var reg = ypSlideOutMenu.Registry
	var obj = ypSlideOutMenu.Registry[id]

	if (obj.isLoaded) {
		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) 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)
	}
}

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.isLoaded) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("ypSlideOutMenu.hide('" + id + "')", ypSlideOutMenu.hideDelay);
	}
}

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.showTimer) obj.startSlide(false)
}

ypSlideOutMenu.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)) + ")", ypSlideOutMenu.minCPUResolution)
}

ypSlideOutMenu.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)
	}
}

ypSlideOutMenu.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)
}

ypSlideOutMenu.prototype.setVisibility = function(bShow) { this.container.style.visibility = bShow ? "visible" : "hidden"; this.container.style.display = bShow ? "block" : "none"; }
ypSlideOutMenu.prototype.moveTo = function(p) { this.style[this.orientation == "h" ? "left" : "top"] = p }
