offsetLeft = 0;
offsetTop = 1;
deltaWidth = 2;

SlideOutMenu.Registry = []
SlideOutMenu.aniLen = 400
SlideOutMenu.hideDelay = 800
SlideOutMenu.minCPUResolution = 10

var opera = (navigator.userAgent.indexOf("Opera")!= -1) ? true : false;
var firefox = (navigator.userAgent.indexOf("Firefox")!= -1) ? true : false;

function getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while ((inputObj = inputObj.offsetParent) != null) returnValue += inputObj.offsetTop;
  return returnValue;
}

function getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while ((inputObj = inputObj.offsetParent) != null) returnValue += inputObj.offsetLeft;
  return returnValue;
}

function SetPosMenu(id, left, top, width, height)
{
	var d = document;
	var obj1 = d.getElementById(id + "Container")
	if (obj1) var obj2 = d.getElementById(id + "Content")
	obj1.style.position = 'absolute';
	obj1.style.visibility = 'hidden';
	obj1.style.display = 'block';
	obj1.style.overflow = 'hidden';
	obj1.style.left = left + 'px';
	obj1.style.top = top + 'px'
	obj1.style.width = width + 'px';
	obj1.style.height = height + 'px';

	obj2.style.position = 'relative';
	obj2.style.left = '0px';
	obj2.style.top = '0px';
	obj2.style.width = width + 'px';
	obj2.style.height = height + 'px';
}

function BodyLoaded()
{
  var corr = 1;
  if (opera) corr = 2;
	SetPosMenu("menu1", getLeftPos(menu1Top) + offsetLeft - corr, getTopPos(menu1Top) + offsetTop + menu1Top.offsetHeight, 121 + deltaWidth, menu1Content.offsetHeight);
	SetPosMenu("menu2", getLeftPos(menu2Top) + offsetLeft - corr, getTopPos(menu2Top) + offsetTop + menu2Top.offsetHeight, 121 + deltaWidth, menu2Content.offsetHeight);
	SetPosMenu("menu3", getLeftPos(menu3Top) + offsetLeft - corr, getTopPos(menu3Top) + offsetTop + menu3Top.offsetHeight, 121 + deltaWidth, menu3Content.offsetHeight);
}

function BodyResize()
{
  var corr = 1;
  if (opera) corr = 2;
	SetPosMenu("menu1", getLeftPos(menu1Top) + offsetLeft - corr, getTopPos(menu1Top) + offsetTop + menu1Top.offsetHeight, 121 + deltaWidth, menu1Content.offsetHeight);
	SetPosMenu("menu2", getLeftPos(menu2Top) + offsetLeft - corr, getTopPos(menu2Top) + offsetTop + menu2Top.offsetHeight, 121 + deltaWidth, menu2Content.offsetHeight);
	SetPosMenu("menu3", getLeftPos(menu3Top) + offsetLeft - corr, getTopPos(menu3Top) + offsetTop + menu3Top.offsetHeight, 121 + deltaWidth, menu3Content.offsetHeight);
}

// constructor
function SlideOutMenu(id, left, top, width, height, over)
{
	dir = "down";
  this.id			 = id
	this.dir		 = "down"
	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		 = over
	this.startTime	 = 0

	// global reference to this object
	this.gRef = "SlideOutMenu_"+id
	eval(this.gRef+"=this")

	// add this menu object to an internal list of all menus
	SlideOutMenu.Registry[id] = this

	var d = document

	var obj1 = d.getElementById(this.id + "Container")
	if (obj1) var obj2 = d.getElementById(this.id + "Content")
	obj1.style.position = 'absolute';
	obj1.style.visibility = 'hidden';
	obj1.style.display = 'block';
	obj1.style.overflow = 'hidden';
	obj1.style.left = left + 'px';
	obj1.style.top = top + 'px'
	obj1.style.width = width + 'px';
	obj1.style.height = height + 'px';
	

	obj2.style.position = 'relative';
	obj2.style.left = '0px';
	obj2.style.top = '0px';
	obj2.style.width = width + 'px';
	obj2.style.height = height + 'px';


	var strCSS = '<style type="text/css">';
	strCSS += '#' + this.id + 'Container { visibility: hidden; display: block;'
	strCSS += 'left:' + left + 'px; '
	strCSS += 'top:' + top + 'px; '
	strCSS += 'overflow:hidden; z-index:2000; }'
	strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position: absolute; '
	strCSS += 'width:' + width + 'px; '
	strCSS += 'height:' + height + 'px; '
	strCSS += 'clip:rect(0 ' + width + ' ' + height + ' 0); '
	strCSS += '}'
	strCSS += '</style>';
//	d.write(strCSS)

	this.load()
}

SlideOutMenu.prototype.load = function() {
	var trigger = document.getElementById(this.id + "Trigger");
	var obj1 = document.getElementById(this.id + "Container")
	if (obj1) var obj2 = document.getElementById(this.id + "Content")
	var temp

	if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)
	else {
		this.trigger	= trigger
		this.container	= obj1
		this.menu		= obj2
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos		= 0
		this.accelConst	= (this.outPos - this.homePos) / SlideOutMenu.aniLen / SlideOutMenu.aniLen 

		// set event handlers.
		this.trigger.onmouseover = new Function("SlideOutMenu.showMenu('" + this.id + "')");
		this.trigger.onmouseout = new Function("SlideOutMenu.hideMenu('" + this.id + "')");
		this.menu.onmouseover = new Function("SlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("SlideOutMenu.hideMenu('" + this.id + "')")

		//set initial state
		this.endSlide()
	}
}
	
SlideOutMenu.showMenu = function(id)
{
	var reg = SlideOutMenu.Registry
	var obj = SlideOutMenu.Registry[id]
	
	if (obj.container) {
		obj.over = true

		// close other menus.
		for (menu in reg) if (id != menu) SlideOutMenu.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)
	}
}

SlideOutMenu.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 = SlideOutMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("SlideOutMenu.hide('" + id + "')", SlideOutMenu.hideDelay);
	}
}

SlideOutMenu.hideAll = function()
{
	var reg = SlideOutMenu.Registry
	for (menu in reg) {
		SlideOutMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

SlideOutMenu.hide = function(id)
{
	var obj = SlideOutMenu.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)
}

SlideOutMenu.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()", SlideOutMenu.minCPUResolution)
}

SlideOutMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > SlideOutMenu.aniLen) this.endSlide()
	else {
		var d = Math.round(Math.pow(SlideOutMenu.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)
	}
}

SlideOutMenu.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)
	}
}

SlideOutMenu.prototype.setVisibility = function(bShow) { 
	var s = this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}
SlideOutMenu.prototype.moveTo = function(p) { 
	this.menu.style[this.orientation == "h" ? "left" : "top"] = p + "px"
}
SlideOutMenu.prototype.getPos = function(c) {
	return parseInt(this.menu.style[c])
}

// events
SlideOutMenu.prototype.onactivate		= function()
{
//  this.trigger.parentNode.style.backgroundColor = '#cc3300';
  this.trigger.parentNode.parentNode.style.backgroundColor = '#809dc9';
}
SlideOutMenu.prototype.ondeactivate	= function()
{
//  this.trigger.parentNode.style.backgroundColor = '#57646d';
  this.trigger.parentNode.parentNode.style.backgroundColor = '#57646d';
}

