// 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/
//

// global variables since IE6 won't pass params in settimeout
dblClkd = '';
_elm = '';


if(jQuery) (function($){
	
	$.extend($.fn, {
		fileTree: function(o, h) {
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '0';
			if( o.script == undefined ) o.script = '/organizer/json.php';
			if( o.snd_var == undefined ) o.snd_var = 'display';
			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...';
			
			$(this).each( function() {
				var timeLmt, clickTimer, id;

				function showTree(c, t, rt) {
					$(c).addClass('wait');
					
					var tmp_arr = new Object();
					
					if(t){
						tmp_arr[o.snd_var] = t;
					}
					if(rt){
						tmp_arr['rt_tree'] = rt;
					}					
					
					
					
					$.getJSON(o.script, tmp_arr, function(data) {
						var tmp = '<ul class="jqueryFileTree" style="display: none;">';
						
						if(data){
						
							if(data.data_home){
								$.each(data.data_home, function(i,item){
									id = (item.item_id) ? item.item_id : ((item.box_id) ? item.box_id : item.unique_id);
									item.file_type_css = (item.file_type_css=='box') ? 'lightbox' : item.file_type_css;
									tmp += '<li class="'+ item.file_type_css +' collapsed"><a href=""#" rel="' + id + '" file_type="'+ item.file_type_css +'">' +  item.title + '</a></li>';
								});
							}
							
							if(data.folder){
								$.each(data.folder, function(i,item){
									id = (item.item_id) ? item.item_id : ((item.box_id) ? item.box_id : item.unique_id);
									tmp += '<li class="directory collapsed"><a href=""#" rel="' + id + '" file_type="folder">' +  item.title + '</a></li>';
								});
							}
							if(data.slide){
								$.each(data.slide, function(i,item){
									id = (item.item_id) ? item.item_id : ((item.box_id) ? item.box_id : item.unique_id);
									tmp += '<li class="slide"><a href=""#" rel="' + id + '" file_type="slide">' +  item.title + '</a></li>';
								});
							}
							if(data.roll){
								$.each(data.roll, function(i,item){
									id = (item.item_id) ? item.item_id : ((item.box_id) ? item.box_id : item.unique_id);
									tmp += '<li class="roll"><a href=""#" rel="' + id + '" file_type="roll">' +  item.title + '</a></li>';
								});
							}
							if(data.box){
								$.each(data.box, function(i,item){
									id = (item.item_id) ? item.item_id : ((item.box_id) ? item.box_id : item.unique_id);
									tmp += '<li class="lightbox"><a href=""#" rel="' + id + '" file_type="lightbox">' +  item.title + '</a></li>';
								});
							}
							tmp += '</ul>';						
							$(c).find('.start').html('');
							$(c).removeClass('wait').append(tmp);
							if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
							bindTree(c);
						
						} else {
						
							tmp = '<div>[ No results ]</div>';
							$(c).find('.start').html('');
							$(c).removeClass('wait').append(tmp);
						}
					});
				}
				
				function bindTree(t) {
					$(t).find('LI A').bind(o.folderEvent, function() {
						timeLmt = (!o.useClckSlct) ? 0 : 300;

					   _elm = $(this);
			
						if( $(this).parent().hasClass('directory') ) {
						
							clickTimer = setTimeout(function(){

								if(dblClkd!=true){
									if( $((_elm)).parent().hasClass('collapsed') ) {
										// Expand
										if( !o.multiFolder ) {
											$((_elm)).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
											$((_elm)).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
										}
										$((_elm)).parent().find('UL').remove(); // cleanup
										showTree( $((_elm)).parent(), $((_elm)).attr('rel') );
										$((_elm)).parent().removeClass('collapsed').addClass('expanded');
									} else {
										// Collapse
										$((_elm)).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
										$((_elm)).parent().removeClass('expanded').addClass('collapsed');
									}
								}
								dblClkd = false;
                                _elm = '';
							}, timeLmt);
							
						} else {
							if(!o.useClckSlct){
								h($(this).attr('rel'), $(this).text());
							}
						}
						return false;
					});
					
					if(o.useClckSlct){
						$(t).find('LI A').bind('dblclick', function() {
							dblClkd = true;
							clearTimeout(clickTimer);
							if(!o.useClckSlctFunc){
								h($(this).attr('rel'), $(this).text(), $(this).attr('file_type'));
							} else {
								o.useClckSlctFunc($(this).attr('rel'), $(this).text(), $(this).attr('file_type'));
							}


						});
					}					
					
					$(t).find('LI').droppable({
						accept:'.edtr_brwse_span',
						drop:function(ev, ui) {
							alert('dropped');
						}
					});				
					
								
					
					// Prevent A from triggering the # on non-click events
					if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });	
				}
				// Loading message
				$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), null, escape(o.root));
			});
		}
	});
	
})(jQuery);
