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

var Warp=Warp||{};
Warp.Search=new Class({Implements:[Options],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(a,b){this.setOptions(b);
this.value=this.timer=null;this.form=$(a).getParent("form");this.input=$(a);this.input.setProperty("autocomplete","off");this.input.addEvent("keyup",this.keypressed.bind(this));this.input.addEvent("blur",this.hide.bind(this));var c=this;this.input.addEvent("keyup",function(){c.form[c.input.value?"addClass":"removeClass"](c.options.filledClass)});this.form.getElement("button[type=reset]").addEvent("click",function(){c.form.removeClass(c.options.filledClass);c.value=null;c.input.focus()});b={url:this.options.url,
method:this.options.method,onRequest:function(){c.form.addClass(c.options.loadingClass)},onComplete:function(){c.form.removeClass(c.options.loadingClass)}};this.request=(new Request(b)).addEvent("onComplete",this.suggest.bind(this));this.choices=(new Element("ul",{"class":this.options.listClass})).inject(this.input,"after");this.fx=(new Fx.Tween(this.choices,{property:"opacity",link:"cancel",duration:200})).set(0)},keypressed:function(a){if(a&&a.key&&!a.shift)switch(a.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(a){var b=null;if($type(a)!="string"&&!a.hasClass(this.options.skipClass))b=a;if(a=="next")b=(this.selected||this.choices)[this.selected?"getNext":"getFirst"](this.options.match);if(a=="previous")b=(this.selected||this.choices)[this.selected?"getPrevious":"getLast"](this.options.match);if(b!=null&&b!=this.selected){this.selected=
b;this.choices.getChildren().removeClass(this.options.hoverClass);this.selected.addClass(this.options.hoverClass)}},done:function(a){if(a){if(a.hasClass(this.options.moreResultsClass))this.input.getParent("form").submit();else if(a.inputData)window.location=a.inputData.url;this.hide()}},trigger:function(){var a=this.value;this.value=this.input.value;if(this.value.length<this.options.minLength)return this.hide();if(this.value!=a){this.timer&&$clear(this.timer);this.timer=function(){var b={};b[this.options.param]=
this.value;this.request.send({data:b})}.delay(this.options.delay,this)}},suggest:function(a){if(a){a=JSON.decode(a,true);var b=function(c){return c.addEvents({mouseover:this.pick.bind(this,[c]),click:this.done.bind(this,[c])})}.bind(this);if(a===false)this.hide();else{this.selected=null;this.choices.empty();b(new Element("li",{"class":this.options.resultsHeaderClass+" "+this.options.skipClass,html:this.options.msgResultsHeader})).inject(this.choices);if(a.results&&a.results.length>0){a.results.each(function(c){var d=
new Element("li",{"class":this.options.resultClass});(new Element("h3",{html:c.title})).inject(d);(new Element("div",{html:c.text})).inject(d);d.inputData=c;b(d).inject(this.choices)},this);b(new Element("li",{"class":this.options.moreResultsClass+" "+this.options.skipClass,html:this.options.msgMoreResults})).inject(this.choices)}else b(new Element("li",{"class":this.options.resultClass+" "+this.options.noResultsClass+" "+this.options.skipClass,html: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 a=function(){this.choices.setStyle("display","none")}.bind(this);this.fx.start(0).chain(a)}}});
Warp.Placeholder=new Class({Implements:[Options],options:{"class":"placeholder"},initialize:function(a){this.setOptions(a);"placeholder"in document.createElement("input")||document.getElements("input[type=text]").each(function(b){var c=b.get("placeholder"),d=b.getParent("form");if(c){this.toggle(b,false);b.addEvents({focus:function(){this.toggle(b,true)}.bind(this),blur:function(){this.toggle(b,false)}.bind(this)});d&&d.addEvent("submit",function(){b.value==c&&b.set("value","")})}}.bind(this))},toggle:function(a,
b){var c=a.get("placeholder"),d=a.get("value");if(d==""||d==c){if(this.options["class"])a[b?"removeClass":"addClass"](this.options["class"]);a.set("value",b?"":c)}}});window.addEvent("domready",function(){new Warp.Placeholder});
