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

var Warp=Warp||{};
Warp.Search=new Class({options:{url:document.location.href,param:"search",method:"post",minLength:1,delay:200,match:":not(li.skip)",skipClass:"skip",loadingClass:"loading",filledClass:"filled",resultClass:"result",resultsHeaderClass:"results-header",moreResultsClass:"more-results",noResultsClass:"no-results",listClass:"results",hoverClass:"selected",msgResultsHeader:"Search Results",msgMoreResults:"More Results",msgNoResults:"No results found"},initialize:function(b,c){this.setOptions(c);this.value=
this.timer=null;this.form=$(b).getParent("form");this.input=$(b);this.input.setProperty("autocomplete","off");this.input.addEvent("keyup",this.keypressed.bind(this));this.input.addEvent("blur",this.hide.bind(this));var a=this;this.input.addEvent("keyup",function(){a.form[a.input.value?"addClass":"removeClass"](a.options.filledClass)});this.form.getElement("button[type=reset]").addEvent("click",function(){a.form.removeClass(a.options.filledClass);a.value=null;a.input.focus()});c={method:this.options.method,
onRequest:function(){a.form.addClass(a.options.loadingClass)},onComplete:function(){a.form.removeClass(a.options.loadingClass)}};this.request=(new Ajax(this.options.url,c)).addEvent("onComplete",this.suggest.bind(this));this.choices=(new Element("ul",{"class":this.options.listClass})).inject(this.input,"after");this.fx=(new Fx.Style(this.choices,"opacity",{duration:200})).set(0)},keypressed:function(b){if((b=new Event(b))&&b.key&&!b.shift)switch(b.key){case "enter":this.done(this.selected);return false;
case "up":this.pick("previous");return false;case "down":this.pick("next");return false;case "esc":case "tab":this.hide()}this.trigger();return true},pick:function(b){var c=null;if($type(b)!="string"&&!b.hasClass(this.options.skipClass))c=b;if(b=="next"){var a=this.selected;for(a||(a=this.choices.getFirst());a;)if((a=a.getNext())&&!a.hasClass(this.options.skipClass)){c=a;break}}if(b=="previous")for((a=this.selected)||(a=this.choices.getLast());a;)if((a=a.getPrevious())&&!a.hasClass(this.options.skipClass)){c=
a;break}if(c!=null&&c!=this.selected){this.selected=c;this.choices.getChildren().removeClass(this.options.hoverClass);this.selected.addClass(this.options.hoverClass)}},done:function(b){if(b){if(b.hasClass(this.options.moreResultsClass))this.input.getParent("form").submit();else if(b.inputData)window.location=b.inputData.url;this.hide()}},trigger:function(){var b=this.value;this.value=this.input.value;if(this.value.length<this.options.minLength)return this.hide();if(this.value!=b){this.timer&&$clear(this.timer);
this.timer=function(){var c={};c[this.options.param]=this.value;this.request.request(Object.toQueryString(c))}.delay(this.options.delay,this)}},suggest:function(b){if(b){b=Json.evaluate(b,true);var c=function(a){return a.addEvents({mouseover:this.pick.bind(this,[a]),click:this.done.bind(this,[a])})}.bind(this);if(b===false)this.hide();else{this.selected=null;this.choices.empty();c((new Element("li",{"class":this.options.resultsHeaderClass+" "+this.options.skipClass})).setHTML(this.options.msgResultsHeader)).inject(this.choices);
if(b.results&&b.results.length>0){b.results.each(function(a){var d=new Element("li",{"class":this.options.resultClass});(new Element("h3")).setHTML(a.title).inject(d);(new Element("div")).setHTML(a.text).inject(d);d.inputData=a;c(d).inject(this.choices)},this);c((new Element("li",{"class":this.options.moreResultsClass+" "+this.options.skipClass})).setHTML(this.options.msgMoreResults)).inject(this.choices)}else c((new Element("li",{"class":this.options.resultClass+" "+this.options.noResultsClass+" "+
this.options.skipClass})).setHTML(this.options.msgNoResults)).inject(this.choices);this.show()}}},show:function(){if(!this.visible){this.visible=true;this.choices.setStyle("display","");this.fx.start(1)}},hide:function(){if(this.visible){this.visible=false;this.choices.getChildren().removeClass(this.options.hoverClass);var b=function(){this.choices.setStyle("display","none")}.bind(this);this.fx.start(0).chain(b)}}});Warp.Search.implement(new Options);
Warp.Placeholder=new Class({options:{"class":"placeholder"},initialize:function(b){this.setOptions(b);"placeholder"in document.createElement("input")||document.getElements("input[type=text]").each(function(c){var a=c.getProperty("placeholder"),d=c.getParent("form");if(a){this.toggle(c,false);c.addEvents({focus:function(){this.toggle(c,true)}.bind(this),blur:function(){this.toggle(c,false)}.bind(this)});d&&d.addEvent("submit",function(){c.value==a&&c.set("value","")})}}.bind(this))},toggle:function(b,
c){var a=b.getProperty("placeholder"),d=b.getProperty("value");if(d==""||d==a){if(this.options["class"])b[c?"removeClass":"addClass"](this.options["class"]);b.setProperty("value",c?"":a)}}});Warp.Placeholder.implement(new Options);window.addEvent("domready",function(){new Warp.Placeholder});
