function FruitMenu( sID )
{
	this._container = document.getElementById( sID );
	if ( this._container )
		this._configure();
};
FruitMenu.prototype._configure = function()
{
	/*  'Declare' variables  */
	this._item        = new Array();
	this._currentstep = 0;
	this._totalstep   = 15;
	this._path        = Array();
	this._timer       = null;
	this._direction   = 0;

	/** interchange delay  **/
	this._eventdelay  = 250;
	this._eventtimer  = null;
	this._labeldisplaystate = false;


	/*  Gather Menu Items  */
	var aItem  = this._container.getElementsByTagName( "span" );
	for ( var i = 0; i < aItem.length; ++i )
		if ( aItem[ i ].className.match( /fruitmenuitem/gi ) && !aItem[ i ].className.match( /active/gi ) )
			this._item[ this._item.length ] = new FruitMenuItem( aItem[ i ], this );

	/*  Calculate the animation path  */
	this._path = this._calculatePath( 0, this._item[ 0 ]._width * .5, this._totalstep );

	/*  Attach the mouseout event  */
	this._container._control   = this;
	this._container.onmouseout = this.__mouseOutHandler;
	window[ ( this._self = "FruitMenuMasterControlObject" ) ] = this;
};
FruitMenu.prototype._calculatePath = function( nFrom, nTo, nStep )
{
	var aReturn = new Array();
	for ( var i = 0; i < nStep; ++i )
		aReturn[ aReturn.length ] = Math.round( klib3.movement.smooth( nFrom, nTo, i, nStep ) );
	return aReturn;
};
FruitMenu.prototype.__mouseOutHandler = function()
{
	this._direction = -1;
	this._control._update();
};
FruitMenu.prototype.focus = function()
{
	clearTimeout( this._eventtimer );
	this._direction = 1;
	this._eventtimer = setTimeout( this._self + "._update();", this._eventdelay );
};
FruitMenu.prototype._update = function()
{
	clearTimeout( this._timer );
	for ( var i = 0; i < this._item.length; ++i )
	{
		if ( this._item[ i ].focus )
		{
			if ( this._item[ i ].step + 1 < this._path.length )
				++this._item[ i ].step;
			else
			{
				if ( !this._item[ i ]._control._labeldisplaystate )
				{
					this._item[ i ]._label.style.visibility = "visible";
					this._item[ i ]._control._labeldisplaystate = true;
				}
				clearTimeout( this._timer );
			}
		}
		else
		{
			if ( this._item[ i ].step - 1 > 0 )
				--this._item[ i ].step;
			else
				clearTimeout( this._timer );
		}
		var nWidth = this._item[ i ].offsetWidth + ( this._path[ this._item[ i ].step ] * 2 );
		this._item[ i ].update( nWidth );
	}
	this._timer = setTimeout( this._self + "._update();", 30 );
};

function FruitMenuItem( oItem, oControl )
{
	this._configure( oItem, oControl );
};
FruitMenuItem.prototype = new klib3.dynamic;
FruitMenuItem.prototype._configure = function( oItem, oControl )
{
	this.init( oItem );

	this.step        = 0;
	this.focus       = false;
	this._control    = oControl;
	this._image      = null;
	this._label      = null;
	this.offsetWidth = this._width;

	if ( oItem.firstChild && oItem.firstChild.nodeType == 1 ) // The image must be the first child of the span... yes must... no whitespace in the XHTML
	{
		this._image      = new FruitMenuItemImage( oItem.firstChild );
		this.offsetWidth = this._image._width;
	}
	
	if ( oItem.lastChild && oItem.lastChild.nodeType == 1 ) // The image must be the last child of the span... yes must... no whitespace in the XHTML
		this._label     = oItem.lastChild;

	this.attachMethod( "handleMouseOver", this.__mouseOverHandler );
	this.attachMethod( "handleMouseOut", this.__mouseOutHandler );
	this.attachEvent( "mouseover", "handleMouseOver" );
	this.attachEvent( "mouseout", "handleMouseOut" );
};
FruitMenuItem.prototype.__mouseOverHandler = function()
{
	this.focus = true;
	this._control.focus();
};
FruitMenuItem.prototype.__mouseOutHandler = function()
{
	if ( this._control && this._control._labeldisplaystate )
	{
		this._label.style.visibility = "hidden";
		this._control._labeldisplaystate = false;
	}
	this.focus = false;
	
	if ( typeof document.defaultView == "object" && this._control && this._control.blur )
		this._control.blur();
};
FruitMenuItem.prototype.update = function( nWidth )
{
	this._image.update( nWidth );
};

function FruitMenuItemImage( oImage )
{
	this.init( oImage );

	this._source    = oImage.src;
	this._sourceOn  = this._source.replace( /small/gi, "on" );
	this._sourceOff = this._source.replace( /small/gi, "large" );
	oImage.src      = this._source.replace( /[a-zA-Z0-9_\.-]+$/gi, "clear.gif" );

	this.preload( this._sourceOn, this._sourceOff );

	this.setWidth( this._width );
	this.hover( false );
};
FruitMenuItemImage.prototype = new klib3.dynamic;
FruitMenuItemImage.prototype.preload = function()
{
	for ( var i = 0; i < arguments.length; ++i )
		( new Image() ).src = arguments[ i ];
};
FruitMenuItemImage.prototype.hover = function( bHover )
{
	this.setStyle( "background-image", "url(" + ( bHover ? this._sourceOn : this._sourceOff ) + ")" );
};
FruitMenuItemImage.prototype.update = function( nWidth )
{
	this.resize( nWidth, this._height );
};