if(jQuery) (function($){
	
	$.extend($.fn, {
		fileSystem: function(o, h , w) {
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined )			o.root				= '0';
			if( o.script == undefined )			o.script			= 'FileSystem.php';
			if( o.folderEvent == undefined )	o.folderEvent		= 'click';
			if( o.location == undefined )		o.location			= 'filesystem';
			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.callback == undefined )		o.callback			= function(){};
			if( o.addclicks == undefined )		o.addclicks			= true;
			if( o.loadMessage == undefined )	o.loadMessage		= 'Loading...';
			if( o.fileClass == undefined )		o.fileClass			= 'FileSystem';
			if( o.emptydir == undefined )		o.emptydir			= '<li class="file ext_info">Empty</li>';
			if( o.fileWrapper == undefined )	o.fileWrapper		= '<ul class="'+o.fileClass+'" style="display: none;"></ul>'
			$(this).each( function() {
				
				function showTree(c, t) {
					$(c).addClass('wait');
					$("."+o.fileClass+".start").remove();
					$.post(o.script, { 'parent': t , 'location': o.location}, function(data) {
						$(c).find('.start').html('');
						$(c).removeClass('wait');
						if(data == '') $(c).append('<ul class="FileSystem" style="display: none;">'+o.emptydir+'</ul>');
						else $(c).append(data);
						if( o.root == t ) $(c).find('UL:hidden').show();
						else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
						if(o.addclicks) bindTree(c);
						else $(c).find('li a.nameLink').click(function(){$(this).blur(); return false;});
						$(c).find("li.collapsed").find("."+o.fileClass).hide();
						w($(c));
					});
				};
				
				function bindTree(t) {
					$(t).find('li a.nameLink').bind(o.folderEvent, function() {
						if( $(this).parent().hasClass('directory') ) {
							if( $(this).parent().hasClass('collapsed') ) {
								// Expand
								if( !o.multiFolder ) {
									$(this).parent().parent().find('ul').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
									$(this).parent().parent().find('li.directory').removeClass('expanded').addClass('collapsed');
								}
								$(this).parent().removeClass('collapsed').addClass('expanded');
								showTree( $(this).parent(), $(this).attr('rel') );
								$(this).parent().find('ul.'+o.fileClass+':first:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
							} else {
								// Collapse
								$(this).parent().find('ul.'+o.fileClass+':first:visible').slideUp({
									duration: o.collapseSpeed,
									easing: o.collapseEasing,
									complete:function(){
										$(this).siblings('.content').hide();
										$(this).remove();
									}
								});
								$(this).parent().removeClass('expanded').addClass('collapsed');
							}
						}else{
							h(this,true);
						}
						return false;
					});
					// Prevent A from triggering the # on non-click events
					if( o.folderEvent.toLowerCase() != 'click' ) $(t).find('li a.nameLink, li a.button').bind('click', function() { return false; });
				};
				function doempty($this){
					var fileUL = $this.parent().find('UL.'+o.fileClass+':first:hidden');
					if(fileUL.length==0){
						$this.parent().append(o.fileWrapper).find('UL.'+o.fileClass+':first').append(o.emptydir);
					}else{
						if(fileUL.find("li").length==0) fileUL.append(o.emptydir);
					}
				};
				// Loading message
				$(this).html('<ul class="'+o.fileClass+' start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), o.root );
			});
			// starting callback
			o.callback($(this));
		}
	});
	
})(jQuery);
