/*
 *	Decode.UK Menu Slider - March 2008
 *	
 *  Controls hide/show behaviour of navigation menus. Instantiate for each menu eg
 *
 *	 	new navSlider( myNav );
 *
 *  myNav can be the nav's id (string) or a handle to the object.
 *
 *  You may also pass through additonal optional arguments:
 *
 *		new navSlider( myNav, animate ); - where animate is true or false
 *
 *		new navSlider( myNav, animate, callback ); - where callback is a function
 *	
 */

var navSlider = Class.create();

navSlider.prototype = {
	
	initialize: function (){
		
		this.animate = true;
		
		this.initializing = true;
		
		this.openHandle = false;

		var dropZone = arguments[0];

		if( arguments.length > 1 )
			this.animate = arguments[1];

		if( arguments.length > 2 )
			this.keepOpen = arguments[2];

		if( arguments.length > 3 )
			this.autoClose = arguments[3];
		else
			this.autoClose = true;
		
		if( arguments.length > 4 )
			this.callback = arguments[4];
		else
			this.callback = false;
		
		if( typeof( dropZone ) == 'object' )
			var dropZoneHandle = dropZone;
		else
			var dropZoneHandle = $( dropZone );
				
		
		this.dropZone = dropZoneHandle;
		
		this.initFolders();
					
		this.setNotBusy();
		
		this.initializing = false;
			
	},
	
	initFolders: function () {
	
		var contents = this.dropZone.getElementsByTagName( 'li' );
		
		for( var i = 0; i < contents.length; i++ ) {
		
			var fileContainer = contents[i].getElementsByTagName( 'ul' );
			
			var folderHandle = contents[i].firstChild;
			
			if( fileContainer.length > 0 ) { 
			
				this.addOnclick( folderHandle );
				
				var folderContainer = fileContainer[0].parentNode;
			
				if( contents[i].getAttribute( 'id' ) != this.keepOpen ) this.closeFolder( folderContainer );
							
			}
		
		}
			
	},
		
	addOnclick: function ( folderName ){
	
		var clickEvent = this.folderClick.bind( this );
		
		folderName.onclick = function() { clickEvent( folderName.parentNode ); };  
			
	},
		
	folderClick: function ( folder ){
				
		if( !this.busy ){
						
			var folderContents = folder.getElementsByTagName( 'ul' );
			
			var folderContainer = folderContents[0].parentNode;
			
			if( folderContainer.style.display == 'none' ) {
			
				if( this.openHandle && this.autoClose ) this.closeFolder( this.openHandle );
			
				this.openFolder( folderContainer );
			} else
				this.closeFolder( folderContainer );
			
			if( this.callback )
				this.callback();
					
		}
				
	},
		
	openFolder: function ( folder ){
		
		this.setBusy();
		var setInactiveBINDED = this.setNotBusy.bind(this);
		
		if( this.animate == true )
			new Effect.SlideDown( folder, { scaleFrom:0, scaleTo:100, afterFinish:setInactiveBINDED } );
		else {
		
			folder.style.display = "block";
		
			this.setNotBusy();
		
		}	
		
		this.openHandle = folder;
		
	},
		
	closeFolder: function ( folder ){
		
		this.setBusy();
		var setInactiveBINDED = this.setNotBusy.bind(this);
		
		if( this.animate == true && this.initializing == false )
			new Effect.SlideUp( folder, { scaleFrom:100, scaleTo:0, queue:'end', afterFinish:setInactiveBINDED } );
		else {
		
			folder.style.display = "none";
		
			if( this.initializing ) this.unhideSubFolders( folder );
		
			this.setNotBusy();
		
		}	
		
		this.openHandle = false;
		
	},
		
	setNotBusy: function (){
		
		this.busy = false;
		
	},

	setBusy: function (){
		
		this.busy = true;
		
	},
	
	unhideSubFolders: function ( folder ) {
	
		var subUls = folder.getElementsByTagName( 'ul' );
	
		for( var i = 0; i < subUls.length; i++ ) {
				
			subUls[i].style.display = "block";
				
		}
	
	}
	
};
