
Menu.objects = [];


/**
 * constructor
 */

function Menu(id, x, y, width, height)
{
	// properties
	this.id 		= id;
	
	this.delay		= 1000;
	this.duration	= 300;
	
	this.offset		= 0;
	this.acc		= 1;
	this.curDY		= 0;
	
	this.moveTo(x, y);
	this.sizeTo(width, height);

	this.hideTimer		= false;
	this.animTimer		= false;
	
	this.animStart		= false;
	
	this.isSlidingIn	= false;
	this.isSlidingOut	= false;
	this.isOpen			= false;
	
	Menu.objects[id] = this;
	
	document.getElementById(id).onmouseover	= new Function("{ Menu.objects['"+ id +"'].open() }");
	document.getElementById(id).onmouseout	= new Function("{ Menu.objects['"+ id +"'].close() }");
}


/**
 *	miscellaneous method definitions
 *	such as hide, show, clip, move, ...
 */

Menu.prototype.show = function()
{
	document.getElementById(this.id).style.visibility = "visible";
}


Menu.prototype.hide = function()
{
	document.getElementById(this.id).style.visibility = "hidden";
}


Menu.prototype.moveTo = function(x, y)
{
	this.x = x;
	this.y = y;		

	document.getElementById(this.id).style.left	= x + "px";
	document.getElementById(this.id).style.top	= (y - this.offset) + "px";
}


Menu.prototype.sizeTo = function(width, height)
{
	this.width	= width;
	this.height	= height;	
}


Menu.prototype.setOffset = function(offset)
{
	this.offset = offset;		
	document.getElementById(this.id).style.top	= (this.y - this.offset) + "px";
}


Menu.prototype.setSlideLevel = function(level)
{
	this.setOffset(level);
	this.clipTo(level, this.width + 2, this.height + 2, 0);
}


Menu.prototype.clipTo = function(top, right, bottom, left)
{
	document.getElementById(this.id).style.clip
		= "rect("+ top +"px, "+ right +"px, "+ bottom +"px, "+ left +"px)";
}


/**
 *	method definitions for animation
 */

Menu.prototype.open = function()
{
	if(this.hideTimer) {
		window.clearTimeout(this.hideTimer);
	}

	for(menu in Menu.objects) {
		if(menu != this.id) {
			Menu.objects[menu].closeNow();
		}
	}
	
	if(this.isOpen) {
		return;
	}

	this.setSlideLevel(this.height);
	this.show();

	this.animStart = (new Date()).getTime();
	this.isSlidingIn	= true;
	this.isOpen			= true;

	this.slideIn();	
}


Menu.prototype.slideIn = function()
{
	var animTime = (new Date()).getTime() - this.animStart;
	
	if(animTime > this.duration) {
		// done, the menu is open now
		this.setSlideLevel(0);
	} else {		
		d = Math.round(((this.duration - animTime) * (this.duration - animTime)) * (this.height / (this.duration * this.duration)))
		this.setSlideLevel(d);
	
		if(this.isSlidingIn) {
			this.animTimer = window.setTimeout(
				"Menu.objects['"+ this.id +"'].slideIn()", 10);
		}
	}
}


Menu.prototype.close = function()
{
	this.hideTimer
		= window.setTimeout(
			"Menu.objects['"+ this.id +"'].closeNow()",
			this.delay);
}


Menu.prototype.closeNow = function()
{
	if(!this.isOpen) {
		return;
	}
	
	this.isSlidingIn	= false;
	
	if(this.isSlidingOut) {
		return
	}
	
	window.clearTimeout(this.animTimer);
	
	this.isSlidingOut	= true;	
	this.animStart = (new Date()).getTime();
	this.slideOut();
}


Menu.prototype.slideOut = function()
{
	var animTime = (new Date()).getTime() - this.animStart;
	
	if(animTime > this.duration) {
		// done
		this.setSlideLevel(this.height);
		this.hide();
		this.isSlidingOut = false;
		this.isOpen = false;
		
	} else {		
		d = this.height - Math.round(((this.duration - animTime) * (this.duration - animTime)) * (this.height / (this.duration * this.duration)))
		this.setSlideLevel(d);
	
		this.animTimer = window.setTimeout(
			"Menu.objects['"+ this.id +"'].slideOut()", 10);
	}
}

