var XWiki=(function(b){var a=b.widgets=b.widgets||{};
if(typeof a.XList=="undefined"){if(typeof console!="undefined"&&typeof console.warn=="function"){console.warn("[Suggest widget] Required class missing: XWiki.widgets.XList")
}}else{a.Suggest=Class.create({options:{minchars:1,method:"get",varname:"input",className:"ajaxsuggest",timeout:2500,delay:500,offsety:0,shownoresults:true,noresults:"No results!",maxheight:250,cache:false,seps:"",icon:null,resultsParameter:"results",resultId:"id",resultValue:"value",resultInfo:"info",parentContainer:"body",highlight:true,fadeOnClear:true,insertBeforeSuggestions:null,displayValue:false,displayValueText:"Value :",align:"left"},sInput:"",nInputChars:0,aSuggestions:[],iHighlighted:null,isActive:false,initialize:function(c,d){if(!c){return false
}this.setInputField(c);
this.options=Object.extend(Object.clone(this.options),d||{});
if(typeof this.options.sources=="object"&&this.options.sources.length>1){this.sources=this.options.sources
}else{this.sources=this.options
}this.sources=[this.sources].flatten().compact();
if(!$(this.options.parentContainer)){this.options.parentContainer=$(document.body)
}if(this.options.seps){this.seps=this.options.seps
}else{this.seps=""
}},setInputField:function(c){if(this.fld){this.fld.stopObserving()
}this.fld=$(c);
this.fld.observe("keyup",this.onKeyUp.bindAsEventListener(this));
if(Prototype.Browser.IE||Prototype.Browser.WebKit){this.fld.observe("keydown",this.onKeyPress.bindAsEventListener(this))
}else{this.fld.observe("keypress",this.onKeyPress.bindAsEventListener(this))
}this.fld.setAttribute("autocomplete","off")
},onKeyUp:function(f){var d=f.keyCode;
switch(d){case Event.KEY_RETURN:case Event.KEY_ESC:case Event.KEY_UP:case Event.KEY_DOWN:break;
default:if(this.seps){var e=-1;
for(var c=0;
c<this.seps.length;
c++){if(this.fld.value.lastIndexOf(this.seps.charAt(c))>e){e=this.fld.value.lastIndexOf(this.seps.charAt(c))
}}if(e==-1){this.getSuggestions(this.fld.value)
}else{this.getSuggestions(this.fld.value.substring(e+1))
}}else{this.getSuggestions(this.fld.value)
}}},onKeyPress:function(d){if(!$(this.isActive)){return
}var c=d.keyCode;
switch(c){case Event.KEY_RETURN:if(this.aSuggestions.length==1){this.highlightFirst()
}this.setHighlightedValue();
Event.stop(d);
break;
case Event.KEY_ESC:this.clearSuggestions();
Event.stop(d);
break;
case Event.KEY_UP:this.changeHighlight(c);
Event.stop(d);
break;
case Event.KEY_DOWN:this.changeHighlight(c);
Event.stop(d);
break;
default:break
}},getSuggestions:function(f){f=f.strip();
if(f==this.sInput){return false
}if(f.length<this.options.minchars){this.sInput="";
return false
}if(f.length>this.nInputChars&&this.aSuggestions.length&&this.options.cache){var c=[];
for(var d=0;
d<this.aSuggestions.length;
d++){if(this.aSuggestions[d].value.substr(0,f.length).toLowerCase()==f.toLowerCase()){c.push(this.aSuggestions[d])
}}this.sInput=f;
this.nInputChars=f.length;
this.aSuggestions=c;
this.createList(this.aSuggestions);
return false
}else{this.sInput=f;
this.nInputChars=f.length;
this.prepareContainer();
var e=this;
clearTimeout(this.ajID);
this.ajID=setTimeout(function(){e.doAjaxRequests()
},this.options.delay)
}return false
},doAjaxRequests:function(){for(var e=0;
e<this.sources.length;
e++){var f=this.sources[e];
var d=f.script+f.varname+"="+encodeURIComponent(this.fld.value.strip());
var h=f.method||"get";
var g={};
if(f.json){g.Accept="application/json"
}else{g.Accept="application/xml"
}var c=new Ajax.Request(d,{method:h,requestHeaders:g,onSuccess:this.setSuggestions.bindAsEventListener(this,f),onFailure:function(i){new b.widgets.Notification("Failed to retrieve suggestions : ')"+i.statusText,"error",{timeout:5})
}})
}},setSuggestions:function(f,g){this.aSuggestions=[];
if(g.json){var h=f.responseJSON;
if(!h){return false
}var e=h[g.resultsParameter||this.options.resultsParameter];
for(var d=0;
d<e.length;
d++){this.aSuggestions.push({id:e[d][g.resultId||this.options.resultId],value:e[d][g.resultValue||this.options.resultValue],info:e[d][g.resultInfo||this.options.resultInfo]})
}}else{var c=f.responseXML;
var e=c.getElementsByTagName(g.resultsParameter||this.options.resultsParameter)[0].childNodes;
for(var d=0;
d<e.length;
d++){if(e[d].hasChildNodes()){this.aSuggestions.push({id:e[d].getAttribute("id"),value:e[d].childNodes[0].nodeValue,info:e[d].getAttribute("info")})
}}}this.createList(this.aSuggestions,g)
},prepareContainer:function(){if(!$(this.options.parentContainer).down(".suggestItems")){var e=new Element("div",{"class":"suggestItems "+this.options.className});
var j=this.fld.cumulativeOffset();
var k=this.options.width?this.options.width:(this.fld.offsetWidth-2);
if(this.options.align=="left"){e.style.left=j.left+"px"
}else{e.style.left=(j.left-k+this.fld.offsetWidth-2)+"px"
}e.style.top=(j.top+this.fld.offsetHeight+this.options.offsety)+"px";
e.style.width=k+"px";
var c=this;
e.onmouseover=function(){c.killTimeout()
};
e.onmouseout=function(){c.resetTimeout()
};
this.resultContainer=new Element("div",{"class":"resultContainer"});
e.appendChild(this.resultContainer);
$(this.options.parentContainer).insert(e);
this.container=e;
if(this.options.insertBeforeSuggestions){this.resultContainer.insert(this.options.insertBeforeSuggestions)
}document.fire("xwiki:suggest:containerCreated",{container:this.container,suggest:this})
}if(this.sources.length>1){for(var f=0;
f<this.sources.length;
f++){var d=this.sources[f];
d.id=f;
if(this.resultContainer.down(".results"+d.id)){if(this.resultContainer.down(".results"+d.id).down("ul")){this.resultContainer.down(".results"+d.id).down("ul").remove()
}this.resultContainer.down(".results"+d.id).down(".sourceContent").addClassName("loading")
}else{var m=new Element("div",{"class":"results results"+d.id}),l=new Element("div",{"class":"sourceName"});
if(typeof d.icon!="undefined"){var h=new Image();
h.onload=function(){this.sourceHeader.setStyle({backgroundImage:"url("+this.iconImage.src+")"});
this.sourceHeader.setStyle({textIndent:(this.iconImage.width+6)+"px"})
}.bind({sourceHeader:l,iconImage:h});
h.src=d.icon
}l.insert(d.name);
m.insert(l);
m.insert(new Element("div",{"class":"sourceContent loading"}));
this.resultContainer.insert(m)
}}}else{if(this.resultContainer.down("ul")){this.resultContainer.down("ul").remove()
}}var g=this.container.fire("xwiki:suggest:containerPrepared",{container:this.container,suggest:this});
return this.container
},createList:function(j,d){this.isActive=true;
var c=this;
this.killTimeout();
if(j.length==0&&!this.options.shownoresults){return false
}if(this.sources.length>1){var e=this.resultContainer.down(".results"+d.id);
e.down(".sourceContent").removeClassName("loading")
}else{var e=this.resultContainer
}var k=new b.widgets.XList([],{icon:this.options.icon,classes:"suggestList",eventListeners:{click:function(){c.setHighlightedValue();
return false
},mouseover:function(){c.setHighlight(this.getElement())
}}});
for(var h=0;
h<j.length;
h++){if(d.highlight){var g=j[h].value;
var o=g.toLowerCase().indexOf(this.sInput.toLowerCase());
var f=g.substring(0,o)+"<em>"+g.substring(o,o+this.sInput.length)+"</em>"+g.substring(o+this.sInput.length)
}else{var f=j[h].value
}if(!this.options.displayValue){var n=new Element("span",{"class":"info"}).update(f)
}else{var n=new Element("div").insert(new Element("div",{"class":"value"}).update(f)).insert(new Element("div",{"class":"info"}).update("<span class='legend'>"+this.options.displayValueText+"</span>"+j[h].info))
}var l=new Element("div").insert(new Element("span",{"class":"suggestId"}).update(j[h].id)).insert(new Element("span",{"class":"suggestValue"}).update(j[h].value)).insert(new Element("span",{"class":"suggestInfo"}).update(j[h].info));
var m=new b.widgets.XListItem(n,{containerClasses:"suggestItem",value:l,noHighlight:true});
k.addItem(m)
}if(j.length==0){k.addItem(new b.widgets.XListItem(this.options.noresults,{classes:"noSuggestion",noHighlight:true}))
}e.appendChild(k.getElement());
this.suggest=e;
var c=this;
if(this.options.timeout>0){this.toID=setTimeout(function(){c.clearSuggestions()
},this.options.timeout)
}},changeHighlight:function(c){var e=this.resultContainer;
if(!e){return false
}var f,d;
if(this.iHighlighted){if(c==40){d=this.iHighlighted.next()||((this.iHighlighted.up("div.results")&&this.iHighlighted.up("div.results").next())?this.iHighlighted.up("div.results").next().down("li"):e.down("li"))
}else{if(c==38){if(this.iHighlighted.previous()){d=this.iHighlighted.previous()
}else{if((this.iHighlighted.up("div.results")&&this.iHighlighted.up("div.results").previous())){d=this.iHighlighted.up("div.results").previous().down("li:last-child")
}else{d=e.select("ul")[e.select("ul").length-1].down("li:last-child")
}}}}}else{if(c==40){d=e.down("li")
}else{if(c==38){if(e.select("li")>0){d=e.select("li")[e.select("li").length-1]
}}}}if(d){this.setHighlight(d)
}},setHighlight:function(c){if(this.iHighlighted){this.clearHighlight()
}c.addClassName("xhighlight");
this.iHighlighted=c;
this.killTimeout()
},clearHighlight:function(){if(this.iHighlighted){this.iHighlighted.removeClassName("xhighlight");
delete this.iHighlighted
}},highlightFirst:function(){if(this.suggest&&this.suggest.down("ul")){var c=this.suggest.down("ul").down("li");
if(c){this.setHighlight(c)
}}},hasActiveSelection:function(){return this.iHighlighted
},setHighlightedValue:function(){if(this.iHighlighted&&!this.iHighlighted.hasClassName("noSuggestion")){var g,e;
if(this.sInput==""&&this.fld.value==""){g=e=this.iHighlighted.down(".suggestValue").innerHTML
}else{if(this.seps){var j=-1;
for(var f=0;
f<this.seps.length;
f++){if(this.fld.value.lastIndexOf(this.seps.charAt(f))>j){j=this.fld.value.lastIndexOf(this.seps.charAt(f))
}}if(j==-1){g=e=this.iHighlighted.down(".suggestValue").innerHTML
}else{e=this.fld.value.substring(0,j+1)+this.iHighlighted.down(".suggestValue").innerHTML;
g=e.substring(j+1)
}}else{g=e=this.iHighlighted.down(".suggestValue").innerHTML
}}var h=Event.fire(this.fld,"xwiki:suggest:selected",{suggest:this,id:this.iHighlighted.down(".suggestId").innerHTML,value:this.iHighlighted.down(".suggestValue").innerHTML,info:this.iHighlighted.down(".suggestInfo").innerHTML});
if(!h.stopped){this.sInput=g;
this.fld.value=e;
this.fld.focus();
this.clearSuggestions();
if(typeof(this.options.callback)=="function"){this.options.callback({id:this.iHighlighted.down(".suggestId").innerHTML,value:this.iHighlighted.down(".suggestValue").innerHTML,info:this.iHighlighted.down(".suggestInfo").innerHTML})
}if(this.fld.id.indexOf("_suggest")>0){var d=this.fld.id.substring(0,this.fld.id.indexOf("_suggest"));
var c=$(d);
if(c){c.value=this.iHighlighted.down(".suggestInfo").innerHTML
}}}}},killTimeout:function(){clearTimeout(this.toID)
},resetTimeout:function(){clearTimeout(this.toID);
var c=this;
this.toID=setTimeout(function(){c.clearSuggestions()
},1000)
},clearSuggestions:function(){this.killTimeout();
this.isActive=false;
var c=$(this.container);
var e=this;
if(c&&c.parentNode){if(this.options.fadeOnClear){var d=new Effect.Fade(c,{duration:"0.25",afterFinish:function(){if($(e.container)){$(e.container).remove()
}}})
}else{$(this.container).remove()
}document.fire("xwiki:suggest:clearSuggestions",{suggest:this})
}}})
}return b
})(XWiki||{});