// jQuery File Tree Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=58 for more information
//
// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options:  root           - root folder to display; default = /
//           script         - location of the serverside AJAX file to use; default = jqueryFileTree.php
//           folderEvent    - event to trigger expand/collapse; default = click
//           expandSpeed    - default = 500 (ms); use -1 for no animation
//           collapseSpeed  - default = 500 (ms); use -1 for no animation
//           expandEasing   - easing function to use on expand (optional)
//           collapseEasing - easing function to use on collapse (optional)
//           multiFolder    - whether or not to limit the browser to one subfolder at a time
//           loadMessage    - Message to display while initial tree loads (can be HTML)
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// TERMS OF USE
// 
// jQuery File Tree is licensed under a Creative Commons License and is copyrighted (C)2008 by Cory S.N. LaViska.
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//
if(jQuery) (function($){
	
	jQuery.extend(jQuery.fn, {
		fileTree: function(o, h) {
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '/';
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 500;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
			if( o.expandEasing == undefined ) o.expandEasing = null;
			if( o.collapseEasing == undefined ) o.collapseEasing = null;
			if( o.multiFolder == undefined ) o.multiFolder = true;
			if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
			
			o.staticMode = o.script == null;
			
			jQuery(this).each( function() {
				
				function showTree(c, t) {
					if ( o.staticMode ) {
						jQuery(c).removeClass('wait');
						if( o.root == t ) jQuery(c).children('UL:hidden').show();
						else jQuery(c).children('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
					} else {
						jQuery(c).addClass('wait');
						jQuery(".jqueryFileTree.start").remove();
						jQuery.post(o.script, { dir: t }, function(data) {
							jQuery(c).find('.start').html('');
							jQuery(c).removeClass('wait').append(data);
							
							if( o.root == t ) jQuery(c).find('UL:hidden').show();
							else jQuery(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
							
							bindTree(c);
						});
					}
				}
				
				function bindTree(t) {
					jQuery("LI A", t).each(function() {
						var li = jQuery(this).parent();
						
						if( li.hasClass('directory') ) {
							li.get(0).collapseDirectory = function() {
								// Collapse
								li.find('UL').hide();
								li.removeClass('expanded').addClass('collapsed');
							};
							li.get(0).expandDirectory = function() {
								if( !o.multiFolder ) {
									li.parent().find('UL').hide();
									li.parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
								}
								if ( !o.staticMode ) {
									li.find('UL').remove(); // cleanup
								}
								//showTree( li, jQuery(this).children('a').eq(0).attr('rel').match( /.*\// ) );
								showTree( li, jQuery(this).children('a').eq(0).attr('rel') );
								li.removeClass('collapsed').addClass('expanded');
							};
						}

						jQuery(this).bind(o.folderEvent, function() {
							var li = jQuery(this).parent(); 
							if( li.hasClass('directory') ) {
								if( li.hasClass('expanded') ) {
									li.get(0).collapseDirectory();
								} else {
									li.get(0).expandDirectory();
								}
							} else {
								h(jQuery(this).attr('rel'));
							}
							return false;
						});
					});
					
					// Prevent A from triggering the # on non-click events
					if( o.folderEvent.toLowerCase != 'click' )
						jQuery(t).find('LI A').bind('click', function() { return false; });
				}
				
				if ( this.nodeName.toUpperCase() == 'UL' ) {
					// <ul>
					bindTree(this);
				} else {
					// Anything else. Normally a <div>
					
					if ( o.staticMode ) {
						jQuery("UL UL", this).hide();
						jQuery("LI", this).removeClass('expanded').addClass('collapsed');
						bindTree(this);
					} else {
						// Loading message
						jQuery(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
					}
					
					// Get the initial file list
					showTree( jQuery(this), o.root );
				}
			});
		}
	});
	
})(jQuery);
