// treeview.js v0.5, Friday 23 Jan 2009

// Copyright (c) 2009 Mayuresh Phadke (http://www.opelitservices.com, mayuresh at gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

treeviewJar = new CookieJar({
	expires:3600,   // seconds
	path: ''
});

function collectListNodes(e) {
	return e.childElements().collect(function(s) {
		var h = new Array();
		h[0] = s.hasClassName('expanded');
                h[1] = s.hasClassName('current');
                var x = collectListNodes(s);
                h[2] = x;

                return h;
	});
}

function traverse(e, a) {
	e.childElements().map(function(s, i) {
		var h = $A(a[i]);
		if (h[0] == true) {
			s.down('ul').show(); // Toggle show or hide the nested list
			s.addClassName('expanded').removeClassName('collapsed'); //Toggle the class names to show '+' or '-' sign
		}
		if (h[1] == true) {
			s.addClassName('current');
		}
		var c = $A(h[2]);
		if (c.size() > 0) {
			traverse(s, c);
		}
	});
}

function respondToClick(ev) {
	var e = ev.element();
	if (e.tagName == "A") { // Treat HTML A tags specially
		e = e.up('li');	
	}
		
	//
	// Manage different classname for the currently clicked node
	//
	var containerUL = e.up('.treeroot');
	containerUL.select('.current').each(function(z) { // remove previously assigned classname 'current'
		z.removeClassName('current');
	});
	e.addClassName('current'); // Add classname 'current' to list item which was clicked

	//
	// Toggle expansion of tree node
	//
	if (e.down('ul') != null) {
		e.down('ul').toggle(); // Toggle show or hide the nested list
		e.toggleClassName('expanded').toggleClassName('collapsed'); //Toggle the class names to show '+' or '-' sign
		e.removeClassName('current'); // expandable list items are not assigned 'current'
	}
	
	//
	// Store current tree state in cookie
	//
	var a = $A(collectListNodes(containerUL)); // Create a nested array storing the classnames assigned to list items
	treeviewJar.put(containerUL.identify(), $A(a)); // Store the array into a cookie

	ev.stop();  // stop event propogation

	//
	// If click actually occured on an anchor, go to the target
	//
	e = ev.element();
	if (e.tagName == "A") {
		document.location.href = e.readAttribute('href');	
	}
}

function setupTreeView(z) {
	//
	// Setup treeview of nested lists
	//
	$(z).select('li').each(function(s) {
		s.observe('click', respondToClick); 
		if (s.down('ul') != null) {
			s.addClassName('collapsed');	
			s.down('ul').hide();
		}
	});

	// 
	// The following lines of code are used to retrive the state of the treeview on page refresh
	//
	var a = treeviewJar.get(z); // get array which stores classnames for list items from cookie
	if (a != null) { 
		traverse($(z), $A(a)); // Set class names for list items
	}
}
