/** menus **/

function Smsm(caller, sub, shadeElement)
{
	this.caller = caller;
	this.sub = sub;
	this.shadeElement = shadeElement;
	this.state = "";
	this.opint = null;
	this.horzSpan = 450;
	this.topLeft = getPageCoords( caller );
	this.topLeft.x -= this.horzSpan;
	this.currentPos = { x: -1, y: -1, width: 208 };
	this.lastWidth = 0;
}

Smsm.currentInstance = null;

Smsm.prototype.open = function()
{
	this.currentPos.x = this.topLeft.x + this.horzSpan;
	this.currentPos.y = this.topLeft.y;
	this.sub.style.display = "block";
	this.sub.style.left = this.currentPos.x + "px";
	this.sub.style.top = this.currentPos.y + "px";
	this.sub.style.width = this.currentPos.width + "px";
	this.sub.style.height = "40px";
	this.state = "rollout";
	Smsm.currentInstance = this;
	this.opint = window.setInterval( function() { if( Smsm.currentInstance != null ) Smsm.currentInstance.onRollout(); }, 20 );
	this.updateShade();
}

Smsm.prototype.updateShade = function()
{
	this.shadeElement.style.left = (this.currentPos.x - 15) + "px";
	this.shadeElement.style.top = (this.currentPos.y - 15) + "px";
	this.shadeElement.style.height = "70px";
	this.shadeElement.style.width = (this.currentPos.width + 30) + "px";
}

Smsm.prototype.close = function()
{
	this.sub.style.display = "none";
	this.sub.style.left = "-1px";
	this.sub.style.top = "-1px";
	this.sub.style.height = "0px";
	Smsm.currentInstance = null;
}

Smsm.prototype.onRollout = function()
{
	this.currentPos.x -= 10;
	this.currentPos.width += 10;
		
	if( this.currentPos.x <= this.topLeft.x ) {
		this.currentPos.x = this.topLeft.x;
		Smsm.currentInstance = null;
		window.clearInterval( this.opint );
		this.state = "ready";
	}
	
	this.sub.style.left = this.currentPos.x + "px";
	this.sub.style.width = this.currentPos.width + "px";
	this.sub.style.display = "block";
	this.updateShade();
	
	var items = this.sub.getElementsByTagName( "li" );
	var itemsWidth = 0;
	
	for( var j = 0; j < items.length; j ++ ) {
		if( items[ j ].offsetWidth <= 0 ) {
			return;
		}
			
		itemsWidth += items[ j ].offsetWidth;
	}

	if( itemsWidth < this.lastWidth ) {
		return;
	}

	if( (this.lastWidth == itemsWidth) && (this.currentPos.width >= itemsWidth) ) {
		this.topLeft.x = this.currentPos.x;
	}
	
	this.lastWidth = itemsWidth;
}

function TopMenus()
{
	this.currentlyOpen = null;
	this.currentSub = null;
	this.currentState = null;
	this.inCleanup = null;
	this.shadeElement = null;
	this.currentSubId = 0;
}

TopMenus.currentState = null;

TopMenus.prototype.mouseOver = function(element)
{
	if( (element == null) || (element == this.currentlyOpen) ) {
		this.ignoreFirst = false;
		if( this.inCleanup != null ) {
			window.clearTimeout( this.inCleanup );
			this.inCleanup = null;
		}
		return;
	}

	if( !element.getAttribute ) return;
	var subid = element.getAttribute( "subid" );
	if( !subid ) return;
	var submenu = document.getElementById( "sub_" + subid );
	if( !submenu ) return;
	
	if( subid != this.currentSubId ) {
		if( this.inCleanup != null ) {
			window.clearTimeout( this.inCleanup );
			this.inCleanup = null;
		}
		this.finalCleanup();
	} else if( submenu == this.currentSub ) {
		return;
	}
	
	submenu.style.backgroundColor = element.style.backgroundColor;
	
	this.shadeElement = document.createElement( "DIV" );
	this.shadeElement.style.position = "absolute";
	this.shadeElement.style.display = "block";
	this.shadeElement.style.zIndex = 98;
	
	setEventHandler( this.shadeElement, "mouseover", function() { topmenu.mouseOver(null); } );
	setEventHandler( this.shadeElement, "mouseout", function() { topmenu.mouseOut(null); } );
	
	document.body.appendChild( this.shadeElement );

	this.currentlyOpen = element;
	this.currentSub = submenu;
	this.currentSubId = subid;
	this.currentState = new Smsm( element, submenu, this.shadeElement );
	this.currentState.open();
}

TopMenus.prototype.mouseOut = function(element)
{
	if( this.inCleanup == null ) {
		this.inCleanup = window.setTimeout( function() { topmenu.finalCleanup(); }, 800 );
	}
}

TopMenus.prototype.finalCleanup = function()
{
	if( this.currentState != null ) {
		this.currentState.close();
		this.currentState = null;
		this.currentlyOpen = null;
		this.currentSub = null;
		this.inCleanup = null;
		this.currentSubId = 0;

		document.body.removeChild( this.shadeElement );
		this.shadeElement = null;
	}
}
