/*
 * jQuery superhero tree control plugin 0.1
 *
 * http://www.ontecnologia.info/superhero/ui/shtree
 * http://docs.jquery.com/Plugins/shtree
 *
 * Copyright (c) 2009 Oscar Emilio Antolinez Collazos
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function($) {
	$.widget("ui.shtree", {
		_init: function() {
			var o = this.options;
			this.element.get(0).onselectstart = function () { return false; };//Prevent user selection
			if(o.url.length > 0)
				this._getData();
			this._bindEvents();
		},
		_bindEvents: function() {
			var self = this;
		  
			this.element.find("a").bind("click", function(){//ojo se quito live por problemas en chrome
				self.element.find("a").removeClass("selected");
				$(this).addClass("selected");
				eval($(this).attr("action"));
				return false;
			});
			this.element.find("li").live("click", function(event){
				if($(this).children("ul").length==1){
					if(event.target.tagName=="LI"){
						if(event.pageY - $(event.target).offset().top > $(event.target).children("a").innerHeight()) return false;
						if(event.pageX - $(event.target).offset().left > parseInt($(event.target).css("padding-left"))) return false;
					
						$(this).children("ul").toggle();//Revisar easing
						$(this).toggleClass("close").toggleClass("open");
						event.preventDefault();	event.stopPropagation(); event.stopImmediatePropagation();
						return false;
					}
				}
			});
			this.element.find("a").live("dblclick", function(event){
				if($(this).siblings("ul").length==1){
					$(this).siblings("ul").toggle();
					$(this).parent().toggleClass("close").toggleClass("open");
				}
				return false;
			});
			
		},
		_createHTML: function(response, target) {
			var element=this.element; var o = this.options;	var self=this;
			var u=$("<ul>");
			
			$(target).append(u);
			
			if(o.type=="xml"){
				$(">item", response).each(function(i, node){
					var l=$("<li>").addClass($(node).attr("status"));
					if($(node).parent().attr("status")=="close")
						u.hide();
					
					u.append(l);
					
					l.append("<a class='"+$(node).attr("clase")+"' action="+$(node).attr("action")+" >"+$(node).attr("title")+"</a>");
					if($("item", node).length > 0)
						self._createHTML(node, l);
				});
			}

			if(o.type=="json"){
				$(response.item).each(function(i, node){
					var l=$("<li>").addClass(node.status);
					if(response.status=="close")
						u.hide();
						
					u.append(l);
					//alert($(node).attr("class")+" - "+$(node).attr("title"));
					l.append("<a class='"+node.clase+"' action="+node.action+" >"+node.title+"</a>");
					
					var it=node.item;
					if(it)
						self._createHTML(node, l);
				});
			}

		},
		_getData: function() {
			var element=this.element;
			var o = this.options;
			var self=this;
			
			$.ajax({
				type: "POST",
				url: o.url,
				data: o.data,
				dataType: o.type,
				async: false,
				success: function(response){
					if(o.type=="html"){
						element.append(response);
					}else{
						if(o.type=="json") var nodo=$(response.menu);
						if(o.type=="xml") var nodo=$("menu", response);
						nodo.each(
							function(){
								self._createHTML(this, element);
							}
						);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
				}
			});			
		},
		collapseAll: function() {
			this.element.find("li").each(function(i, node){
				if($(node).children("ul").length==1){
					$(node).children("ul").hide();
					$(node).removeClass("open").addClass("close");
				}
			});
			return false;
		},		
		expandAll: function() {
			this.element.find("li").each(function(i, node){
				if($(node).children("ul").length==1){
					$(node).children("ul").show();
					$(node).removeClass("close").addClass("open");
				}
			});
			return false;
		},		
		length: function() {
			return this.element.find("li").length;
		},
		destroy: function() {
			$.widget.prototype.apply(this, arguments); // default destroy
			// now do other stuff particular to this widget
		}
	});
	$.extend($.ui.shtree, {
		getter: "length collapseAll expandAll",
		defaults: {
			url: "",
			data: "", 
			type: "html"
		}
	});
})(jQuery);