// <![CDATA[

var Prj;
if ( !Prj ) Prj = {};

Prj.MainNav = Class.create();

Prj.MainNav.prototype =
{
	initialize: function( id, subNavItems, opts ) 
	{
		this.options = 
		{
			tag:				'li',
			addToTop:			0,
			addToLeft:			0,
			closeLayers:		new Array( 'closelayer1', 'closelayer2' )
		}
		Object.extend(this.options, opts || {});
		
		this.mainnav = $(id);
		this.subNavItems = subNavItems;
		this.mainnavItems = $A(this.mainnav.getElementsByTagName( this.options.tag) );
		this.visibleSubNavItem = null;
		
		this.runningUpEffect = null;
		this.runningDownEffect = null;
		
		this.activSelectedItem = null;
		
		this.funcHandleMouseOverCloseLayer = this._handleMouseOverCloseLayer;
		
		for ( var i = 0; i < this.subNavItems.length; i++ )
		{
			if ( Element.myIsExisting( this.subNavItems[i] ) )
			{
				this.subNavItems[i] = $(this.subNavItems[i]);
				this.subNavItems[i].hide();
			}
			else
			{
				this.subNavItems[i] = null;
			}
		}
		
		for ( var i = 0; i < this.mainnavItems.length; i++ )
		{
			Event.observe( this.mainnavItems[i], 'mouseover', this._handleMouseOver.bind( this ) );
		}
	},
	
	_handleMouseOver: function ( event )
	{
		this._handleMouseOverCloseLayer();
		
		this.selectedItem = $(Event.element(event));
		this.selectedItem.addClassName( 'hover' );
		
		for ( var i = 0; i < this.mainnavItems.length; i++ )
		{
			if ( this.selectedItem.descendantOf(this.mainnavItems[i]) )
			{
				if ( this.activSelectedItem == null || this.mainnavItems[i].identify() != this.activSelectedItem.identify() )
				{
					if ( !this.mainnavItems[i].hasClassName( 'activeNav' ) && !this.mainnavItems[i].hasClassName( 'currentActiveNav' ) )
					{
						this.mainnavItems[i].addClassName( 'activeNav' );
						this.activSelectedItem = this.mainnavItems[i];
						
						if ( this.runningUpEffect != null )
						{
							this.runningUpEffect.finish();
						}
						this.runningUpEffect = new Effect.Parallel(
							[
								new Effect.Move( this.mainnavItems[i], { y: -7, x: 0, mode: 'absolute' } )
							],
							{
								duration: 0.2
							}
						);
					}
				}
				
				this._showCloseLayers();
				
				if ( this.subNavItems[i] != null )
				{
					var cumOffset = $(this.mainnavItems[i]).cumulativeOffset();
					this.subNavItems[i].setStyle(
						{
							left: cumOffset.left + this.options.addToLeft + 'px'/*,
							top: cumOffset.top + this.options.addToTop + 'px'*/
						}
					);
					this.visibleSubNavItem = this.subNavItems[i];
					new Effect.Appear( this.subNavItems[i], { to: 1, duration: EFFECT_APPEAR_DURATION } );
				}
			}
		}
	},
	
	_showCloseLayers: function ()
	{
		for ( var i = 0; this.options.closeLayers != null && i < this.options.closeLayers.length; i++ )
		{
			var closeLayer = $(this.options.closeLayers[i]);
			closeLayer.setStyle(
				{
					visibility: 'visible'
				}
			);
			Event.observe( closeLayer, 'mouseover', this.funcHandleMouseOverCloseLayer.bind( this ) );
		}
	},
	
	_handleMouseOverCloseLayer: function ()
	{
		for ( var i = 0; this.options.closeLayers != null && i < this.options.closeLayers.length; i++ )
		{
			var closeLayer = $(this.options.closeLayers[i]);
			closeLayer.setStyle(
				{
					visibility: 'hidden'
				}
			);
			Event.stopObserving( closeLayer, 'mouseover', this.funcHandleMouseOverCloseLayer.bind( this ) );
		}
		
		if ( this.selectedItem != null )
		{
			if ( this.activSelectedItem != null && this.activSelectedItem.hasClassName( 'activeNav' ) )
			{
				this.activSelectedItem.removeClassName( 'activeNav' );
				if ( this.runningDownEffect != null )
				{
					this.runningDownEffect.finish();
				}
				this.runningDownEffect = new Effect.Parallel(
					[
						new Effect.Move( this.activSelectedItem, { y: 0, x: 0, mode: 'absolute' } )
					],
					{
						duration: 0.2
					}
				);
			}
			this.activSelectedItem = null;
			this.selectedItem.removeClassName( 'hover' );
		}
		
		if ( this.visibleSubNavItem != null )
		{
			this._hideSubNav( this.visibleSubNavItem );
		}
	},
	
	_hideSubNav: function ( subNavItem )
	{
		if ( this.visibleSubNavItem != null && this.visibleSubNavItem.identify() == subNavItem.identify() )
		{
			this.visibleSubNavItem = null;
		}
		new Effect.Fade( subNavItem, { to: 0, duration: EFFECT_FADE_DURATION } );
	},
	
	closeActSubNav: function ()
	{
		this._handleMouseOverCloseLayer();
	}
};

var prjMainNav = null;
Event.observe( window, 'load', function()
	{
		prjMainNav = new Prj.MainNav( 'ulMainNav', mainNavSubNavItems );
	}
);

// ]]>