/*
 *	Decode.UK eDropZone Controller - December 2007
 *	
 *  Controls hide/show behaviour of eDropZones. Instantiate for each dropZone eg
 *
 *	 	new eDropZoneController( myDropZone );
 *
 *  myDropZone can be the dropZone's id (string) or a handle to the object.
 *
 *  You may also pass through additonal optional arguments:
 *
 *		new eDropZoneController( myDropZone, animate ); - where animate is true or false
 *
 *		new eDropZoneController( myDropZone, animate, callback ); - where callback is a function
 *	
 */

var eDropZoneController = Class.create();

eDropZoneController.prototype = {
	
	initialize: function (){
		
		this.animate = true;
		
		var dropZone = arguments[0];

		if( arguments.length > 1 )
			this.animate = arguments[1];
		
		if( arguments.length > 2 )
			this.startingFolder = arguments[2];
		else
			this.startingFolder = false;
		
		if( arguments.length > 3 )
			this.callback = arguments[3];
		else
			this.callback = false;
		
		if( typeof( dropZone ) == 'object' )
			var dropZoneHandle = dropZone;
		else
			var dropZoneHandle = $( dropZone );
		
		this.dropZone = dropZoneHandle;
		
		this.initFolders();
					
		this.setNotBusy();
		
		dropZoneHandle.style.display = "block";
			
	},
	
	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( this.startingFolder == false || folderHandle.firstChild.nodeValue != this.startingFolder ) {
				
					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' )
				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();
		
		}	
		
	},
		
	closeFolder: function ( folder ){
		
		this.setBusy();
		var setInactiveBINDED = this.setNotBusy.bind(this);
		
		if( this.animate == true )
			new Effect.SlideUp( folder, { scaleFrom:100, scaleTo:0, afterFinish:setInactiveBINDED } );
		else {
		
			folder.style.display = "none";
		
			this.setNotBusy();
		
		}	
		
	},
		
	setNotBusy: function (){
		
		this.busy = false;
		
	},

	setBusy: function (){
		
		this.busy = true;
		
	}
	
};
