/*
 * JS code:
 * @author Alastair Pursch [alastair < at > pursch < dot > co < dot > za]
 * @copyright 2009
 * @package inc
 * @subpackage js
 */

//THIS CODE HAS BEEN HEAVILY MODIFIED AND CUSTOMISED BUT WAS ORIGINALLY BASED ON:  
//
//jQuery File Tree Plugin (Version 1.01)
// Cory S.N. LaViska
// 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)
//
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 

if(jQuery) (function($){
	
	$.extend($.fn, {
		fileTree: function(treeType, o, dirCallback, fileCallback) {
			// Defaults
			if( !o ) var o = {};
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 250;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 250;
			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() {
				
				//Show the (sub) tree [regular repository page OR admin page]
				function showTree(c, parent_rc_id) {
					
					//If this LI (or root div) has a UL sub-folder/file list,
					//i.e. it's child files/folders have already been loaded on a previous AJAX call,
					//then we want to avoid another call and just display!
					//[Ammendment:  For now, we WON'T do this and rather comment it out and go back to doing 
					//an AJAX call everytime, since there are potential problems with sub-sub-folders.]
					
					//Child files/folders already loaded
					//if($(c).find('ul').length > 0){
					//	if( parent_rc_id == '0' ) $(c).find('ul:hidden').show(); else $(c).find('ul:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
					
					//Need to obtain child files/folders via AJAX call
					//}else{
						//Clear out sub-contents just in case (safeguard)					
						$(c).find('ul').remove();
						
						$(c).addClass('wait');
						
						var requestData = "";
						if(treeType == "admin"){
							requestData = "action=getRepositoryDirectoriesAndFiles&parent_rc_id=" + parent_rc_id;
						}else{
							requestData = "action=getRepositoryDirectories&parent_rc_id=" + parent_rc_id;
						}
											
						$.ajax({
							url: "repository",
						    data: requestData,
						    success: function(html){
															
								if(hasError(html)){
									data = getMessage(html);
									$(c).removeClass('wait').append(data);
								}else{
									data = getData(html);
									$(c).removeClass('wait').append(data);
									
									//Show or 'slidedown' the tree
									if( parent_rc_id == '0' ) $(c).find('ul:hidden').show(); else $(c).find('ul:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
									//Bind ajax calls to tree click events
									bindTree(c);
								}							
							}
						});
					//}
				}
				
				function bindTree(t) {
					$(t).find('li a').bind(o.folderEvent, function() {
						//DIRECTORY NODE
						if( $(this).parent().hasClass('directory') ) {
							//If currently collapsed, expand!
							if( $(this).parent().hasClass('collapsed') ) {								
								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');
								}
								
								showTree( $(this).parent(), escape($(this).attr('rel')));
								$(this).parent().removeClass('collapsed').addClass('expanded');
							
							//If currently expanded, collapse!
							} else {
								$(this).parent().find('ul').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
								$(this).parent().removeClass('expanded').addClass('collapsed');
							}
							
							dirCallback(escape($(this).attr('rel')));							
						
						//FILE NODE
						} else {
							fileCallback(escape($(this).attr('rel')));
						}
						return false;
					});
					// Prevent anchor from triggering the # on non-click events
					if( o.folderEvent.toLowerCase != 'click' ) $(t).find('li a').bind('click', function() { return false; });
				}
				
				// Get the initial root directory listing
				$(this).html('');
				showTree( $(this), '0');
			});
		}
	});
	
})(jQuery);
