/* Copyright (C) 2007 - 2010 YOOtheme GmbH, YOOtheme License (http://www.yootheme.com/license) */

Zoo.BrowseTags=new Class({initialize:function(a){this.setOptions({msgSave:"Save",msgCancel:"Cancel"},a);var b=this;$$("span.edit-tag").each(function(d){d.getElement("a").addEvent("click",function(){b.remove();this.setStyle("display","none");var c=(new Element("span",{"class":"edit-tag-panel"})).injectAfter(this),f=(new Element("input",{"class":"text",type:"text",name:"new",value:this.getText()})).injectInside(c);(new Element("input",{type:"hidden",name:"old",value:this.getText()})).injectInside(c);
var g=(new Element("button",{"class":"save"})).setText(b.options.msgSave).injectInside(c);c=(new Element("a",{"class":"cancel"})).setText(b.options.msgCancel).injectInside(c);f.focus();f.addEvent("keydown",function(e){e=new Event(e);if(e.key=="enter"){e.stop();b.submit()}if(e.key=="esc"){e.stop();b.remove()}});g.addEvent("click",b.submit);c.addEvent("click",b.remove)})})},remove:function(){$$("span.edit-tag-panel").each(function(a){a.getParent().getElement("a").setStyle("display","");a.empty().remove()})},
submit:function(){var a=$("tags-default");a.getElement('input[name="task"]').setProperty("value","update");a.submit()}});Zoo.BrowseTags.implement(new Options);
Zoo.Tag=new Class({initialize:function(a){this.setOptions({url:"index.php?option=com_zoo&controller=item"},a);this.tagCompleter=null;this.tagArea=$("tag-area");this.tagInput=this.tagArea.getElement('input[type="text"]');this.attachEvents();this.attachTagCompleter();this.indicator=this.tagArea.getElement("div.icon")},attachEvents:function(){var a=this;this.tagArea.getElement("div.hint").addEvent("click",function(){a.tagInput.focus()});this.tagArea.getElements("div.tag-list a").each(function(b){b.addEvent("click",
function(){b.getParent().remove()})});this.tagArea.getElement("button").addEvent("click",function(b){(new Event(b)).stop();a.pressAddButton()});this.tagInput.addEvent("focus",function(){a.tagArea.getElement("div.hint").setStyle("display","none")});this.tagInput.addEvent("blur",function(){this.getValue()||a.tagArea.getElement("div.hint").setStyle("display",null)});this.tagInput.addEvent("keypress",function(b){b=new Event(b);if((!a.tagCompleter.selected||!a.tagCompleter.visible)&&b.key=="enter"){a.pressAddButton();
b.stop()}});this.tagArea.getElements("div.tag-cloud a").each(function(b){b.addEvent("click",function(){a.addTag(this.getText())})})},pressAddButton:function(){var a=this;tags=a.tagInput.getValue().split(",");tags.each(function(b){b=b.trim();a.addTag(b);a.tagInput.value=""})},attachTagCompleter:function(){var a=this;this.tagCompleter=new Autocompleter.Ajax.Tag(a.tagInput,this.options.url+"&format=raw&task=loadtags",{postVar:"tag",maxChoices:20,onRequest:function(){a.indicator.addClass("loader")},onComplete:function(){a.indicator.removeClass("loader")}})},
addTag:function(a){if(!this.tagExists(a)&&a!=""){var b=new Element("div"),d=new Element("a"),c=new Element("input",{type:"hidden",value:a,name:"tags[]"});b.setText(a);d.injectTop(b);d.addEvent("click",function(){b.remove()});c.injectInside(b);b.injectInside(this.tagArea.getElement("div.tag-list"))}},tagExists:function(a){var b=false;this.tagArea.getElements('input[name^="tags"]').each(function(d){if(a==d.getValue())b=true});return b}});Zoo.Tag.implement(new Options);
if(window.Autocompleter)Autocompleter.Ajax.Tag=Autocompleter.Ajax.Base.extend({query:function(){var a=$extend({},this.options.postData);a[this.options.postVar]=this.getLastTag(this.element.getValue());this.fireEvent("onRequest",[this.element,this.ajax]);this.ajax.request(a)},choiceSelect:function(a){var b=this.element.value.substring(0,this.element.value.lastIndexOf(this.getLastTag(this.element.value)));b+=a.inputValue;this.observer.value=this.element.value=b;this.hideChoices();this.fireEvent("onSelect",
[this.element],20)},updateChoices:function(a){var b=$("tag-area").getElement('input[type="text"]').getValue();b=b.trim();$("tag-area").getElements('input[name^="tags"]').each(function(d){var c=d.getValue();b&&c.contains(b)&&a.include(d.getValue())});if(a.length){this.choices.empty();this.selected=null;if(a&&a.length){if(this.options.maxChoices<a.length)a.length=this.options.maxChoices;a.each(this.options.injectChoice||function(d){var c=(new Element("li")).setHTML(this.markQueryValue(d));c.inputValue=
d;this.addChoiceEvents(c).injectInside(this.choices)},this);this.showChoices()}}},queryResponse:function(a){this.parent(a);(a=Json.evaluate(a||false))&&this.updateChoices(a)},markQueryValue:function(a){value=this.getLastTag(this.queryValue);return this.options.markQuery&&value?a.replace(new RegExp("("+value.escapeRegExp()+")","i"),'<span class="autocompleter-queried">$1</span>'):a},getLastTag:function(a){a=a.split(",");return a[a.length-1].trim()},build:function(){this.parent();window.webkit&&this.element.addEvent("keydown",
this.onCommand.bindWithEvent(this))}}); 
