/* -------------------------------------------------------------------- */
/* -------- Floating Menu System -------------------------------------- */
/* -------------------------------------------------------------------- */
/* -------- Heikki Gruner ---- 21 August, 2008 ------------------------ */
/* -------------------------------------------------------------------- */

function Menu( menuInstanceName, menuID, buttonID, openDirection ) {
	this.owner = null;
	this.items = new Array();
	
	this.instanceName = menuInstanceName;
	this.menuID = menuID;
	this.buttonID = buttonID;
	this.direction = openDirection;
	
	this.closeTimeout = null;
	
	
	this.AddItem = function( item ) {
		item.owner = this;
		this.items.push( item );
	}
	
	
	this.Activate = function() {
		if( this.owner == null ) return;
		this.owner.ActivateThroughChild( this );
		menuReference = document.getElementById( this.menuID );
		buttonReference = document.getElementById( this.buttonID );
		buttonPosition = GetAbsolutePosition( buttonReference );
		
		switch( this.direction ) {
			case 1:
				menuReference.style.left = buttonPosition.x + "px";
				menuReference.style.top = buttonPosition.y + buttonReference.offsetHeight + "px";
				break;
				
			case 2:
				menuReference.style.left = buttonPosition.x + buttonReference.offsetWidth + "px";
				menuReference.style.top = buttonPosition.y + "px";
				break;
				
			case 3:
				menuReference.style.left = buttonPosition.x - menuReference.offsetWidth + "px";
				menuReference.style.top = buttonPosition.y + "px";
				break;
				
			case 4:
				menuReference.style.left = buttonPosition.x + "px";
				menuReference.style.top = buttonPosition.y - menuReference.offsetHeight + "px";
				break;
		}
		menuReference.style.display = "block";
		AppendClassName( buttonReference, "hover" );
	}
	
	this.ActivateThroughChild = function( child ) {
		for( var counter in this.items ) {
			if( this.items[ counter ] == child ) continue;
			this.items[ counter ].Deactivate();
		}
		if( this.owner != null ) this.owner.ActivateThroughChild( this );
	}
	
	this.ActivateNonFloat = function() {
		for( var counter in this.items )
			this.items[ counter ].Deactivate();
	}
	
	this.Deactivate = function() {
		if( this.owner == null ) return;
		
		for( var counter in this.items ) {
			this.items[ counter ].Deactivate();
		}
		
		this.closeTimeout = null;
		
		menuReference = document.getElementById( this.menuID );
		//menuReference.style.visibility = "hidden";
		menuReference.style.display = "none";
		buttonReference = document.getElementById( this.buttonID );
		RemoveClassName( buttonReference, "hover" );
	}
	
	
	this.ButtonMouseOver = function() {
		if( this.closeTimeout != null ) {
			clearTimeout( this.closeTimeout );
			this.closeTimeout = null;
		} else {
			this.Activate();
		}
	}
	
	this.MenuMouseOver = function() {
		if( this.owner == null ) return;
		
		if( this.closeTimeout != null ) {
			clearTimeout( this.closeTimeout );
			this.closeTimeout = null;
		}
		
		this.owner.MenuMouseOver();
	}
	
	this.MouseOut = function() {
		if( this.owner == null ) return;
		this.closeTimeout = setTimeout( this.instanceName + ".Deactivate()", 250 );
		this.owner.MouseOut();
	}
}
