/**
 * TreeExpander jQuery Extension
 *
 * Used for implementing an expandable AJAX treeview
 *
 * @package pmweb
 * @subpackage layout
 * @since 20/12/2007
 * @copyright Profitmaster Systems Ltd., www.profitmaster.co.uk
 * @author Pete Hurst, <peterh@profitmaster.co.uk>
 * @version $Id: treeExpander.js 2017 2008-10-31 17:28:32Z pete $ v1.0.0.1
 */
jQuery.fn.extend({
	
	/**
	 * Expand or collapse an individual node, passed reference to top-level tree so that
	 * 
	 */
	toggleNode : function(openClass,closedClass,callback,tree) {
		if ($(this).parent().hasClass(openClass)) {
			if (jQuery.browser.msie && (parseInt(jQuery.browser.version)<7)) {
				// Get reference to top-level tree(s) to show/hide to fix document flow
				tree.hide();
				// Just hide in old browsers (animation caused too many glitches)
				$(this).hide();
				$(this).parent().removeClass(openClass).addClass(closedClass);
				tree.show();
				if (callback!=undefined) callback();
			}
			else {
				// Collapse then change class
				$(this).slideToggle("fast",function(){
					$(this).parent().removeClass(openClass).addClass(closedClass);
					if (callback!=undefined) callback();
				});
			}
		}
		else {
			// More basic behaviour in IE6 to prevent problems		
			if (jQuery.browser.msie && (parseInt(jQuery.browser.version)<7)) {
				// Sub-tree instantly appears
				var me = $(this);
				me.show();
				
				// For some reason IE6 needs a fractional amount
				// of time for the document to reflow, otherwise it
				// glitches horribly, causing elements to appear all over the place.
				// So set a timeout for this code to run in 1ms:
				setTimeout(function(){
					me.parent().addClass(openClass).removeClass(closedClass);
					
					// Show and hide the whole tree instantaneously, causing
					// a final re-flow which corrects everything.
					tree.hide();
					tree.show();
				},1);
				if (callback!=undefined) callback();
			}
			else {
				// Change class immediately, then expand
				// (it looks slightly better when the two states work slightly
				// differently like this)
				$(this).parent().addClass(openClass).removeClass(closedClass);
				if (jQuery.browser.msie) {
					// use show() in IE to avoid problems with slideDown()
					$(this).show("fast",function(){
						if (callback!=undefined) callback();
					});
				} else {
					$(this).slideToggle("fast",function(){
						if (callback!=undefined) callback();
					});
				}
			}				
		}
		
	},

	/**
	 * Apply the tree expander/collapsers
	 *
	 * closedClass can be blank in which case openClass will just toggle on/off. 
	 */
	treeExpander : function(openClass,closedClass,callback,tree) {
		if (openClass==undefined) openClass="current";
		if (closedClass==undefined) closedClass="";
		// If top-level, retain a reference to #tree or whatever
		// object has treeExpander called
		if (tree==undefined) tree = $(this);
		// Attach to li's that are not empty
		$(this).find("li").not(".empty")
			// Filter and tag ones that we haven't already plumbed events into
			.not(".uiExpander").addClass("uiExpander")
			// Get child span and attach the click event for expanding/collapsing
			.children("span").click(function(evt){
				evt.preventDefault();	
				// Collapse
				node = $(this).siblings("ul");
				if (node.is("ul")) {
					node.toggleNode(openClass,closedClass,callback,tree);
				}
				else {
					// Check it's not already fetching AJAX data
					if (!$(this).hasClass("uiExpandFetch")) {
						$(this).addClass("uiExpandFetch");
						// Use AJAX to populate the node
						var targetUrl = 
							"/core/TreeNode.php?parentPath="
							+$(this).siblings("a").attr("href");
						var parentNode = $(this).parent();
						$.get(targetUrl,null,function(data){
							// This will run on callback, allowing us to inject
							// the new data
							parentNode.append(data);
							newList = parentNode.children("ul");
							// Attach events to new elements
							newList.hide().treeExpander(openClass,closedClass,callback,tree);
							// Now expand it
							newList.toggleNode(openClass,closedClass,callback,tree);
						});
					}
				}
			});
	}
});
