/*---------------------------------------------------------------------------------------------------
 *
 * File:		menu.js
 * Author:		Peter Quinn
 * Date:		04.23.2008
 * Description:	Provides methods for javascript menus.
 *
 * Usage:		m = new Menu( );
 *				m.add( 'first' );
 *
 *-------------------------------------------------------------------------------------------------*/

var	timer		= 0,
	MENU_DOWN	= 1,
	MENU_UP		= 2,
	MENU_LEFT	= 3,
	MENU_RIGHT	= 4;


/*---------------------------------------------------------------------------------------------------
 *
 * Menu Class
 *
 * @param String path The path that the cookie belongs to
 * @param String domain The domain that the cookie belongs to
 */
function Menu( obj, flag, name, url, parent )
{
	this.obj		= obj;
	this.list		= new Array;
	this.layer		= null;
	this.count		= 0;
	this.name		= name		|| null;
	this.url		= url		|| null;
	this.parent		= parent	|| null;
	this.child		= null;
	this.id			= name + '_' + this.count;
	this.classname	= 'jsMenu';
	this.first		= flag ? false : true;
	this.isVisible	= false;
	this.delay		= 500;
	this.direction	= this.parent ? this.parent.direction : MENU_RIGHT;
	
	
	/**
	 *
	 */
	this.add = function( obj, name, url )
	{
		var tmp;
		
		this.list.push( new Menu( obj, true, name, url, this ) );

		this.count++;		
		tmp = this.list[ this.list.length - 1 ];
		tmp.id = name + '_' + this.count;
		
		return tmp;
	}
	
	/**
	 *
	 */
	this.debug = function( str )
	{
		document.getElementById( 'debug' ).innerHTML = str;
	}

	/**
	 *
	 */
	this.show = function( obj, id, flag )
	{
		var	table;


		if ( ! this.first )
			table = this.parent.layer.layer;
		else
			table = document.getElementById( 'table_' + this.name );
		
		if ( flag )
			obj.className = 'jsMenuItemOver';
		else
			obj.className = 'jsMenuItem';

		if ( this.list[id].list.length > 0 )
			if ( flag )
			{
				clearTimeout( timer );
				if ( this.child )
				{
					this.closeForwards();
					this.child = null;
				}
				
				this.layer = new Layer();
				this.layer.construct();
				this.layer.write( this.list[id].output() );
				this.layer.show( true );
				
				posY = this.layer.getPos( obj );
				
				switch( this.direction )
				{
					case MENU_DOWN:
						if ( this.first )
						{
							posX = this.layer.getPos( obj );
							this.layer.move( posX[0], posY[1] + parseInt( ie ? table.clientHeight : table.offsetHeight ) - 2 );
						} 
						else
						{
							posX = this.layer.getPos( table );
							this.layer.move( posX[0] + parseInt( ie ? table.clientWidth + 2 : table.offsetWidth ), posY[1] - 1 );
						}
						break;
						
					case MENU_UP:
						if ( this.first )
						{
							posX = this.layer.getPos( obj );
							this.layer.move( posX[0], posY[1] - this.layer.getHeight() );
						}
						else
						{
							posX = this.layer.getPos( table );
							this.layer.move( posX[0] + parseInt( ie ? table.clientWidth + 2 : table.offsetWidth ), posY[1] - this.layer.getHeight() + parseInt( ie ? obj.clientHeight : obj.offsetHeight ) );
						}
						break;
						
					case MENU_LEFT:
						posX = this.layer.getPos( table );
						this.layer.move( posX[0] - this.layer.getWidth(), posY[1] - 1 );
						break;
						
					case MENU_RIGHT:
						posX = this.layer.getPos( table );
						this.layer.move( posX[0] + parseInt( ie ? table.clientWidth + 2 : table.offsetWidth ), posY[1] - 1 );
						break;
				}
				
				
				
				this.child = this.list[id];
			}
			else
			{
				clearTimeout( timer );
				timer = setTimeout( this.obj + '.closeAll()', this.delay );
			}
				
		else
		{
			if ( this.child )
			{
				if ( flag )
				{
					clearTimeout( timer );
					timer = setTimeout( this.obj + '.closeForwards()', this.delay );
					return;
				}
				else
				{
					clearTimeout( timer );
					timer = setTimeout( this.obj + '.closeAll()', this.delay );
					return;
				}
			}
			
			if ( this.parent )
				if ( flag )
				{
					clearTimeout( timer );
					timer = 0;
				}
				else
				{
					clearTimeout( timer );
					
					if ( this.child )
						timer = setTimeout( this.child.obj + '.closeBackwards()', this.delay );
					else
						timer = setTimeout( this.obj + '.closeBackwards()', this.delay );
				}
		}
	}

	/**
	 *
	 */
	this.closeAll = function()
	{
		this.closeForwards();
		this.closeBackwards();
	}
	
	/**
	 *
	 */
	this.closeBackwards = function()
	{
		clearTimeout( timer );
		timer = 0;
		
		if ( this.layer )
			this.layer.show( false );
			
		if ( this.parent )
			this.parent.closeBackwards();
	}

	/**
	 *
	 */
	this.closeForwards = function()
	{
		clearTimeout( timer );
		timer = 0;

		if ( this.layer )
			this.layer.show( false );
		
		if ( this.child )
			this.child.closeForwards();
			
		this.child = null;
	}
	
	/**
	 *
	 */
	this.output = function()
	{
		content = '<table class="' + this.classname + '" id="table_' + this.name + '">';
		content += '<tr><td class="jsMenuTop">&nbsp;</td></tr>';
		
		if ( this.first && (this.direction == MENU_UP || this.direction == MENU_DOWN) )
			content += '<tr>';
		
		if ( this.first && (this.direction == MENU_UP || this.direction == MENU_DOWN) )
			for( var i in this.list )
				content += '<td class="jsMenuItem" onmouseover="' + this.obj + '.show( this, ' + i + ', true )" onmouseout="' + this.obj + '.show( this, ' + i + ', false )" onclick="location.href=\'' + this.list[i].url + '\'">' + this.list[i].name + '</td>';
		
		else
			for( var i in this.list )
				content += '<tr><td class="jsMenuItem" onmouseover="' + this.obj + '.show( this, ' + i + ', true )" onmouseout="' + this.obj + '.show( this, ' + i + ', false )" onclick="location.href=\'' + this.list[i].url + '\'">' + this.list[i].name + '</td></tr>';
		
		if ( this.first && (this.direction == MENU_UP || this.direction == MENU_DOWN) )
			content += '</tr>';
		
		content += '<tr><td class="jsMenuBottom">&nbsp;</td></tr>';
		content += '</table>';
		
		return content;
	}
}


/*-------------------------------------------------------------------------------------------------*/
