ypSlideOutMenuInfo.Registry = []
ypSlideOutMenuInfo.aniLen = 800
ypSlideOutMenuInfo.hideDelay = 1000
ypSlideOutMenuInfo.minCPUResolution = 1

// constructor
function ypSlideOutMenuInfo(id)
{
	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.width = 210
		this.height = 162

		this.id			 = id
		this.orientation = "v"
		this.dir		 = "up"
		this.dirType	 = "+"
		this.dim		 = this.height
		this.hideTimer	 = false
		this.aniTimer	 = false
		this.open		 = false
		this.over		 = false
		this.startTime	 = 0

		// global reference to this object
		this.gRef = "ypSlideOutMenuInfo_"+id
		eval(this.gRef+"=this")

		// add this menu object to an internal list of all menus
		ypSlideOutMenuInfo.Registry[id] = this

		/*// overwrite main menu showMenu function to make it not functional
		ypSlideOutMenuInfo.showMenu = function(id)
		{}

		ypSlideOutMenuInfo.prototype.startSlide= function(id)
		{}*/

		var strCSS = "";
		strCSS += '#' + this.id + 'Container { visibility:hidden; '
		strCSS += 'width:' + this.width + 'px; '
		//strCSS += 'height:' + this.height + 'px; '
		strCSS += 'top: 0px; '
		strCSS += 'left: 0px; '
		strCSS += 'overflow:hidden; z-index:10000; }'
		strCSS += '#' + this.id + 'Content { position:absolute; '
		strCSS += 'width:' + this.width + 'px; '
		//strCSS += 'height:' + this.height + 'px; '
		strCSS += 'clip:rect(0 ' + this.width + ' ' + this.height + ' 0); '
		strCSS += '}'
		
		this.css = strCSS

		this.load()
	}
}

ypSlideOutMenuInfo.writeCSS = function() {

		document.writeln('<style type="text/css">');
		for (var id in ypSlideOutMenuInfo.Registry) {
			document.writeln(ypSlideOutMenuInfo.Registry[id].css);
		}
		document.writeln('</style>');
}

ypSlideOutMenuInfo.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 oAnchor = this.dom ? document.getElementById(this.id + 'Anchor') : this.ie ? document.all[this.id + 'Anchor'] : document.layers[this.id + 'Anchor']

	if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 1000)
	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) / ypSlideOutMenuInfo.aniLen / ypSlideOutMenuInfo.aniLen
		this.anchor     = oAnchor

		///////////////////////////////////
		var containerObj = this.ns4 ? this.container : this.container.style
		containerObj.height = this.menu.offsetHeight
		containerObj.width = this.menu.offsetWidth
		//////////////////////////////////

        // set event handlers for apropriate menu
		if (this.ns4) this.anchor.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.anchor.onmouseover = new Function("ypSlideOutMenuInfo.showMenu('" + this.id + "')")
		this.anchor.onmouseout = new Function("ypSlideOutMenuInfo.hideMenu('" + this.id + "')")

		// set event handlers.
		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("ypSlideOutMenuInfo.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("ypSlideOutMenuInfo.hideMenu('" + this.id + "')")

		//set initial state
		this.endSlide()

		return true;
	}
	return false;
}

ypSlideOutMenuInfo.showMenu = function(id)
{
	var reg = ypSlideOutMenuInfo.Registry
	var obj = ypSlideOutMenuInfo.Registry[id]

	if (obj.container) {

		obj.over = true

		// close other menus.
		for (menu in reg) if (id != menu) ypSlideOutMenuInfo.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)
	}
}

ypSlideOutMenuInfo.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 = ypSlideOutMenuInfo.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("ypSlideOutMenuInfo.hide('" + id + "')", ypSlideOutMenuInfo.hideDelay);
	}
}

ypSlideOutMenuInfo.hideAll = function()
{
	var reg = ypSlideOutMenuInfo.Registry
	for (menu in reg) {
		ypSlideOutMenuInfo.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

ypSlideOutMenuInfo.hide = function(id)
{
	var obj = ypSlideOutMenuInfo.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)
}

ypSlideOutMenuInfo.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()", ypSlideOutMenuInfo.minCPUResolution)
}

ypSlideOutMenuInfo.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > ypSlideOutMenuInfo.aniLen) this.endSlide()
	else {
		var d = Math.round(Math.pow(ypSlideOutMenuInfo.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)
	}
}

ypSlideOutMenuInfo.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)
	}
}

ypSlideOutMenuInfo.prototype.setVisibility = function(bShow) {
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"

	if (bShow) {
		var el = this.anchor

		var minX = 0
        var maxX = document.body.clientWidth - this.menu.offsetWidth
        var minY = 0
        var maxY = document.body.clientHeight - this.menu.offsetHeight

		var x = 0
		var y = 0

		x -= el.offsetLeft

		while (el.offsetParent) {

			x += el.offsetLeft
			y += el.offsetTop

			if (el.scrollLeft) x -= el.scrollLeft
			if (el.scrollTop) y -= el.scrollTop

			el = el.offsetParent
		}

		//little moving by corregation
		me = this.menu
		y -= me.offsetHeight
		an = this.anchor
		y += an.offsetHeight
		y += 6
		//x -= 12

		x = Math.max(Math.min(x, maxX), minX)
    y = Math.max(Math.min(y, maxY), minY)

		s.left = x + "px";
		s.top = y + "px";
	}
}
ypSlideOutMenuInfo.prototype.moveTo = function(p) {
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
ypSlideOutMenuInfo.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}

// events
ypSlideOutMenuInfo.prototype.onactivate		= function() { }
ypSlideOutMenuInfo.prototype.ondeactivate	= function() { }

