/*  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, Ajax.Request.prototype), {
	initialize: function(id, url, options) {
		this.id = id;
		this.url = url;
		
		this.setOptions(Object.extend( {
				nodeIdentType: "path"
			}, options));

		this.options.nodeIdentType = this.options.nodeIdentType || "id";

		this.transport = Ajax.getTransport();
	
		var onComplete = this.options.onComplete || Prototype.emptyFunction;
		this.options.onComplete = (function(transport, object) {
			this.li.className = this.li.className.replace(/collapsed/, "expanded");
			new Insertion.Bottom( this.li, transport.responseText );
			onComplete(transport, object);
		}).bind(this);
		
		this.addObservers();
	},
	addObservers: function() {
		var menu = $(this.id);
		if (menu) {
			var lis = document.getElementsByTagName("li");
			for (var i=0; li=lis[i]; i++) {
				Event.observe(lis[i], "click", this.onClick.bindAsEventListener(this));
			}
		}
	},
	setIdentData: function() {
		this.options.nodeIdentKey = this.options.nodeIdentKey || this.options.nodeIdentType;
		var params = this.options.parameters || '';
		if (params.length > 0)
			params += "&";
		switch (this.options.nodeIdentType) {
			case "path":
				var re = new RegExp(this.options.nodeIdentKey + "=.*?(&|$)", "g");
				params = params.replace(re, "");
				var a = (as = this.li.getElementsByTagName("a")).length > 0 ? as[0] : null;
				if (a) {
					/*
					Thanks to:
					http://www.quirksmode.org/bugreports/archives/2005/02/getAttributeHREF_is_always_absolute.html
					*/
					var path = a.getAttribute("href", 2).replace(/^[hf]t?tps?:\/\/[^\/]+/, '');
					params += this.options.nodeIdentKey + "=" + path;
				}
				break;
			case "id":
				params += this.options.nodeIdentKey + "=" + li.id;
				break;
		}
		this.options.parameters = params;
	},
	onClick: function( event ) {
		var li = Event.findElement(event, 'LI');
		Event.stop(event);
		switch (li.className) {
			case "collapsed":
				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 {
					this.li = li;
					this.setIdentData();
					this.request(this.url);
				}
				break;
			case "expanded":
				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;
		}
	}
});