if(typeof Protoplasm=="undefined"){throw ("protoplasm.js not loaded, could not intitialize treelist")}if(window.Control==undefined){Control={}}Protoplasm.loadStylesheet("treelist.css","treelist");Control.TreeList=Class.create({initialize:function(b,a){b=$(b);if(c=b.retrieve("treelist")){c.destroy()}a=Object.extend({selectable:true},a||{});var d=Protoplasm.base("treelist");if(!a.collapsedIcon){a.collapsedIcon=d+"collapsed.png"}if(!a.collapsedHoverIcon){a.collapsedHoverIcon=d+"collapsed-hover.png"}if(!a.expandedIcon){a.expandedIcon=d+"expanded.png"}if(!a.expandedHoverIcon){a.expandedHoverIcon=d+"expanded-hover.png"}this.element=b;this.options=a;this.listeners=[b.on("selectstart",function(f){Event.stop(f)}.bindAsEventListener(this)),b.on("mousedown",function(f){Event.stop(f)}.bindAsEventListener(this)),Event.on(window,"unload",this.destroy.bindAsEventListener(this))];this.selected=null;b.addClassName("_pp_treelist");this.extended=[Protoplasm.extend(b,{destroy:this.destroy.bind(this)})];b.select("li").each(this.initListItem.bind(this));b.store("treelist",this)},destroy:function(){this.extended.each(Protoplasm.revert);this.listeners.invoke("stop");var a=this.element;a.select("._pp_treelist_icon").invoke("remove");a.select("ul").invoke("show");a.select("._pp_treelist_selectable").each(function(b){while(b.firstChild){b.parentNode.insertBefore(b.firstChild,b.parentNode.firstChild)}b.remove()});a.select("._pp_highlight").invoke("removeClassName","_pp_highlight");a.removeClassName("_pp_treelist");a.store("treelist",null)},initListItem:function(b){var e=b.next();var h=new Element("span",{"class":"_pp_treelist_selectable"});while(b.firstChild){h.appendChild(b.firstChild)}b.appendChild(h);if(e&&e.tagName=="UL"){e.hide();var f=new Element("div",{"class":"_pp_treelist_icon"});b.insert({top:f});var d=this.open(b,f,e);var g=this.close(b,f,e);var a=this.toggle(f,d,g);this.listeners.push(f.on("click",a));this.listeners.push(b.on("dblclick",a));this.extended.push(Protoplasm.extend(b,{open:d,close:g,toggle:a}))}if(this.options.onOpen){this.listeners.push(b.on("dblclick",d))}if(this.options.selectable){this.listeners.push(b.on("click",function(i){if(this.selected){this.selected.removeClassName("_pp_highlight")}h.addClassName("_pp_highlight");this.selected=h;this.select(b)}.bindAsEventListener(this)))}},close:function(a,d,b){return function(f){if(d.hasClassName("_pp_treelist_icon_expanded")){d.removeClassName("_pp_treelist_icon_expanded");b.select("ul").invoke("hide");b.select("._pp_treelist_icon_expanded").invoke("removeClassName","_pp_treelist_icon_expanded");b.hide()}if(f){Event.stop(f)}}.bindAsEventListener(this)},open:function(a,d,b){return function(f){if(!d.hasClassName("_pp_treelist_icon_expanded")){d.addClassName("_pp_treelist_icon_expanded");if(b.childElements().length==0){this.loadChildItems(b)}b.show()}if(this.options.onOpen){this.options.onOpen(a.id?a.id:a.textContent,a)}if(f){Event.stop(f)}}.bindAsEventListener(this)},toggle:function(b,a,d){return function(f){if(b.hasClassName("_pp_treelist_icon_expanded")){d()}else{a()}}.bindAsEventListener(this)},loadChildItems:function(b){if(b.id&&this.options.childLoader){var d=this.listUpdater(b);b.update(new Element("li",{"class":"_pp_treelist_loading"}).update("Loading..."));var a=this.options.childLoader(b.id,d);if(a){d(a)}}},listUpdater:function(a){return function(b){a.update();b.each(a.insert.bind(a));a.select("li").each(this.initListItem.bind(this))}.bind(this)},select:function(a){if(this.options.onSelect){this.options.onSelect(a.id?a.id:a.textContent||a.innerText,a)}}});Protoplasm.register("treelist",Control.TreeList);