/* Modified for us with skymobile and DWR - Caleb Lyness
 *
 * Ajax.Menu JavaScript framework, version 1.0
 *  Copyright (c) 2005, Glenn Nilsson <glenn.nilsson@gmail.com>
 *
 *  Ajax.Menu is distributed under the terms of an Creative Commons
 *  Attribution license. In short words, you can use this however you like
 *  as long as you give me and the code credit. Read more at:
 *  http://creativecommons.org/licenses/by/2.5/
 *
 *  Requirements: Prototype framework <http://prototype.conio.net/>
 *
 *  For details, see: http://wailqill.com/code/ajax-menu/
 *
/*--------------------------------------------------------------------------*/

Ajax.Menu = Class.create();
Object.extend(Object.extend(Ajax.Menu.prototype), {
	initialize: function(id, submenuCallback) {
		this.id = id;
		this.submenuCallback = submenuCallback;
		this.addObservers();
	},
	addObservers: function() {
		var menu = $(this.id);
		if (menu) {
			var lis = menu.select('li');
			for (var i=0; li=lis[i]; i++) {
				Event.observe(lis[i], "click", this.onClick.bindAsEventListener(this));
			}
		}
	},
	onClick: function( event ) {
		var li = Event.findElement(event, 'LI');
		if (li) {
			switch (li.className) {
				case "collapsed":
				  Event.stop(event);
					var ul = null;
					for (var i=0; node=li.childNodes[i]; i++) {
						if (node.nodeName.toLowerCase() == "ul") {
							ul = node;
						}
					}
					if (ul) {
						ul.style.display = "block";
						li.className = li.className.replace(/collapsed/, "expanded");
					} else {
						var submenu = this.submenuCallback(li.id);
						li.className = li.className.replace(/collapsed/, "expanded");															
					}
					break;
				case "expanded":
				  Event.stop(event);
					var uls = li.getElementsByTagName("ul");
					for (var i=0; ul=uls[i]; i++) {
						ul.style.display = "none";
						ul.parentNode.className = ul.parentNode.className.replace(/expanded/, "collapsed");
					}
					li.className = li.className.replace(/expanded/, "collapsed");
					break;
			}
		} 
	}
});