// treeview_moo.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.
//

function collectListNodes(e) {
	var u = e.getChildren().map(function(s) {
		var h = new Array();
		h[0] = s.hasClass('expanded');
		h[1] = s.hasClass('current');
		var x = collectListNodes(s);
		h[2] = x;
		
		return h;
	});
	return(u);
}

function traverse(e, a) {
	e.getChildren().map(function(s, i) {
		var h = $A(a[i]);
		if (h[0] == true) {
			var u = s.getElement('ul'); // Toggle show or hide the nested list
			u.setStyle('display', 'inline');
			s.addClass('expanded'); //Toggle the class names to show '+' or '-' sign
			s.removeClass('collapsed');
		}
		if (h[1] == true) {
			s.addClass('current');
		}
		var c = $A(h[2]);
		if (c.length > 0) {
			traverse(s, c);
		}
	});
}

function respondToClick(event) {
	var ev = new Event(event);
	var e = $(ev.target);
	ev.stopPropagation();  // stop event propogation

	if (e.tagName == "A") { // Treat HTML A tags specially
		e = e.getParent();	
	}
	//
	// Manage different classname for the currently clicked node
	//
	var containerUL = e.getParent();
	while (containerUL.hasClass('treeroot') == false) {
		containerUL = containerUL.getParent();	
	}
	containerUL.getElementsBySelector('.current').each(function(z) { // remove previously assigned classname 'current'
		z.removeClass('current');
	});
	e.addClass('current'); // Add classname 'current' to list item which was clicked

	//
	// Toggle expansion of tree node
	//
	var u = e.getElement('ul');
	if (u != null) {
		if (e.hasClass('expanded')) {
			e.removeClass('expanded');
			e.addClass('collapsed');
			u.setStyle('display', 'none');
		} else if (e.hasClass('collapsed')) {
			e.removeClass('collapsed');
			e.addClass('expanded');
			u.setStyle('display', 'inline');
		}
		e.removeClass('current'); // expandable list items are not assigned 'current'
	}
	
	//
	// Store current tree state in cookie
	//
	var a = collectListNodes(containerUL); // Create a nested array storing the classnames assigned to list items
	var z = JSON.stringify(a);
	Cookie.set('__CJ_' + containerUL.getProperty('id'), JSON.stringify(a), {path: '/'}); // Store the array into a cookie
}

function setupTreeView(z) {
	//
	// Setup treeview of nested lists
	//
	$(z).getElements('li').each(function(s) {
		$(s).addEvent('click', respondToClick); 
		if (s.getElement('ul') != null) {
			s.addClass('collapsed');	
			s.getElement('ul').setStyle('display', 'none');
		}
	});

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