/**
 * JMU Vertical Navigation
 * Expand/Collapse Class
 */

if( !Object.inspect )				// Prototype JS Framework not detected
{
	throw 'The JMU vertical navigation expand/collapse class requires the Prototype JS Framework (www.prototypejs.org)';

} else { 							// Prototype JS Framework was detected

	if( Element.collectTextNodes )
	{
		var effects	= true;			// Script.aculo.us effects library detected (effects.js)
	}

	var vertNavItem	= Class.create
	({
		initialize: function( element )
		{
			this.element	= $( element );
			
			if( !this.element )
			{
				return;				// ul#vertNav doesn't exist. Exit.
			}
			
			if( this.initDOM() )
			{
				this.initEvents();	// This item requires collapse functionality
				
			} else {
				
				return;				// Functionality not necessary. Exit.
			}
			
			this.loaded	= true;
		},
		
		initDOM: function()
		{
			this.subList	= this.element.down( 'ul' );
			
			if( this.subList )
			{
				this.element.addClassName( 'collapsible' );
				this.collapse();
				
				vertNavItems.push( this );	// Add this to array of collapsible items
				
				this.subListHeight	= this.subList.getHeight(); // Get collapsed height
				this.subList.setStyle( { overflow: 'hidden' } );
				
				return true;
			}
			
			return false;
		},
		
		initEvents: function()
		{
			this.element.observe( 'click', this.eventClick.bind( this ) );
		},
		
		eventClick: function( e )
		{
			e.stopPropagation();
			
			if( this.isCollapsed() )
			{
				this.collapseAll();
				this.expand();
				
			} else {
				
				this.collapse();
			}
		},
		
		collapse: function()
		{
			if( this.isCollapsed() )
			{
				return;
			}
			
			if( effects && this.loaded )
			{
				new Effect.Tween( this.subList, this.subListHeight, 0, { duration: 1, afterFinish: function(){ this.element.addClassName( 'collapsed' ) }.bind( this ) }, function( i )
				{
					this.setStyle( { height: i + 'px' } );
				});
				
			} else {
			
				this.element.addClassName( 'collapsed' );
			}
			
			this.element.title = 'Click to Expand';

			this.collapsed	= true;
		},
		
		expand: function()
		{
			if( !this.isCollapsed() )
			{
				return;
			}
			
			if( effects )	// Do slide down effect
			{
				this.subList.setStyle( { height: '0px' } );
				this.element.removeClassName( 'collapsed' );
				
				new Effect.Tween( this.subList, 0, this.subListHeight+35, { duration: 1 }, function( i )
				{
					this.setStyle( { height: i + 'px' } );
				});
				
			} else {		// Just make sublist items visible
			
				this.element.removeClassName( 'collapsed' );
			}

			this.element.title = 'Click to Collapse';
			this.collapsed	= false;
		},
		
		isCollapsed: function()
		{
			return this.collapsed;
		},
		
		collapseAll: function()
		{
			vertNavItems.each( function( item )
			{
				item.collapse();
			});
		}
	 });
	
	if( !vertNavItems )		// Instantiate an array object if necessary
	{
		var vertNavItems	= $A();
	}
	
	document.observe( 'dom:loaded', function()
	{
		$$( 'ul#vertNav > li' ).each( function( el )
		{
			new vertNavItem( el );
		});
	});
}			
