if (typeof(bsn) == "undefined"){ wm = bsn = {}; } if (typeof(wm.Autosuggest) == "undefined"){ wm.Autosuggest = {}; } wm.AutoSuggest = function (id, param){ if (!document.getElementById){ return 0; } this.fld = wm.DOM.gE(id); if (!this.fld){ return 0; } this.sInp = ""; this.nInpC = 0; this.aSug = []; this.iHigh = 0; this.oP = param ? param : {}; var k, def = {minchars:3, meth:"get", varname:"input", className:"autosuggest", timeout:360000, delay:0, offsety:-5, shownoresults: true, noresults: "No results found", maxheight: 250, cache: false, maxentries: 25}; for (k in def){ if (typeof(this.oP[k]) != typeof(def[k])){ this.oP[k] = def[k]; } } var p = this; this.fld.onkeypress = function(ev){ return p.onKeyPress(ev); }; this.fld.onkeyup = function(ev){ return p.onKeyUp(ev); }; this.fld.setAttribute("autocomplete","off"); }; wm.AutoSuggest.prototype.onKeyPress = function(ev){ var key = (window.event) ? window.event.keyCode : ev.keyCode; var RETURN = 13; var TAB = 9; var ESC = 27; var bubble = 1; switch(key){ case RETURN: this.setHighlightedValue(); bubble = 0; break; case ESC: this.clearSuggestions(); break; } return bubble; }; wm.AutoSuggest.prototype.onKeyUp = function(ev){ var key = (window.event) ? window.event.keyCode : ev.keyCode; var ARRUP = 38; var ARRDN = 40; var bubble = 1; switch(key){ case ARRUP: this.changeHighlight(key); bubble = 0; break; case ARRDN: this.changeHighlight(key); bubble = 0; break; default: this.getSuggestions(this.fld.value); } return bubble; }; wm.AutoSuggest.prototype.getSuggestions = function (val){ if (val == this.sInp){ return 0; } wm.DOM.remE(this.idAs); this.sInp = val; if (val.length < this.oP.minchars){ this.aSug = []; this.nInpC = val.length; return 0; } var ol = this.nInpC; this.nInpC = val.length ? val.length : 0; var l = this.aSug.length; var pointer = this; var input = this.sInp; clearTimeout(this.ajID); this.ajID = setTimeout( function() { pointer.doAjaxRequest(input) }, this.oP.delay ); return false; }; wm.AutoSuggest.prototype.doAjaxRequest = function (input){ if (input != this.fld.value){ return false; } var pointer = this; if (typeof(this.oP.script) == "function"){ var url = this.oP.script(encodeURIComponent(this.sInp)); } else { var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp); } if (!url){ return false; } var meth = this.oP.meth; var input = this.sInp; var onSuccessFunc = function (req) { pointer.setSuggestions(req, input) }; var onErrorFunc = function (status) { }; var myAjax = new wm.Ajax(); myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc ); }; wm.AutoSuggest.prototype.setSuggestions = function (req, input){ if (input != this.fld.value){ return false; } this.aSug = []; var xml = req.responseXML; var results = xml.getElementsByTagName('results')[0].childNodes; for (var i=0;i" + val.substring(st, st+this.sInp.length) + "" + val.substring(st+this.sInp.length); var span = wm.DOM.cE("span", {}, output, true); var a = wm.DOM.cE("a", { href: arr[i].url }); var pic = wm.DOM.cE("img",{src: arr[i].logo, className:"img img-thumbnail"}," "); var tl = wm.DOM.cE("span", {className:"tl"}, " "); var tr = wm.DOM.cE("span", {className:"tr"}, " "); a.appendChild(pic); a.appendChild(tl); a.appendChild(tr); a.appendChild(span); a.name = i+1; a.onmouseover = function () { pointer.setHighlight(this.name); }; var li = wm.DOM.cE( "li", {}, a ); ul.appendChild( li ); } if (arr.length == 0 && this.oP.shownoresults){ var li = wm.DOM.cE( "li", {className:"as_warning"}, this.oP.noresults ); ul.appendChild( li ); } div.appendChild( ul ); var pos = wm.DOM.getPos(this.fld); if(navigator.appName=='Microsoft Internet Explorer'){ div.style.left = ( pos.x - 0 ) + "px"; } else { div.style.left = ( pos.x - 0 ) + "px"; } div.style.top = ( ( pos.y + this.fld.offsetHeight + this.oP.offsety ) - 2 ) + "px"; div.style.width = (this.fld.offsetWidth + 38 ) + "px"; div.onmouseover = function(){ pointer.killTimeout() }; div.onmouseout = function(){ pointer.resetTimeout() }; document.getElementsByTagName("body")[0].appendChild(div); this.iHigh = 0; var pointer = this; this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout); }; wm.AutoSuggest.prototype.changeHighlight = function(key){ var list = wm.DOM.gE("as_ul"); if (!list){ return false; } var n; if (key == 40){ n = this.iHigh + 1; } else if (key == 38){ n = this.iHigh - 1; } if (n > list.childNodes.length){ n = list.childNodes.length; } if (n < 1){ n = 1; } this.setHighlight(n); }; wm.AutoSuggest.prototype.setHighlight = function(n){ var list = wm.DOM.gE("as_ul"); if (!list){ return false; } if (this.iHigh > 0){ this.clearHighlight(); } this.iHigh = Number(n); list.childNodes[this.iHigh-1].className = "as_highlight"; this.killTimeout(); }; wm.AutoSuggest.prototype.clearHighlight = function(){ var list = wm.DOM.gE("as_ul"); if (!list){ return false; } if (this.iHigh > 0){ list.childNodes[this.iHigh-1].className = ""; this.iHigh = 0; } }; wm.AutoSuggest.prototype.setHighlightedValue = function (){ if (this.iHigh){ this.sInp = this.fld.value = this.aSug[ this.iHigh-1 ].value; this.fld.focus(); if (this.fld.selectionStart){ this.fld.setSelectionRange(this.sInp.length, this.sInp.length); } this.clearSuggestions(); if (typeof(this.oP.callback) == "function"){ this.oP.callback( this.aSug[this.iHigh-1] ); } } }; wm.AutoSuggest.prototype.killTimeout = function(){ clearTimeout(this.toID); }; wm.AutoSuggest.prototype.resetTimeout = function(){ clearTimeout(this.toID); var pointer = this; this.toID = setTimeout(function () { pointer.clearSuggestions() }, 1000); }; wm.AutoSuggest.prototype.clearSuggestions = function (){ this.killTimeout(); var ele = wm.DOM.gE(this.idAs); var pointer = this; if (ele){ var fade = new wm.Fader(ele,1,0,250,function () { wm.DOM.remE(pointer.idAs) }); } }; if (typeof(wm.Ajax) == "undefined"){ wm.Ajax = {}; } wm.Ajax = function () { this.req = {}; this.isIE = false; }; wm.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr){ if (meth != "POST"){ meth = "GET"; } this.onComplete = onComp; this.onError = onErr; var pointer = this; if (window.XMLHttpRequest){ this.req = new XMLHttpRequest(); this.req.onreadystatechange = function () { pointer.processReqChange() }; this.req.open("GET", url, true); // this.req.send(null); } else if (window.ActiveXObject){ this.req = new ActiveXObject("Microsoft.XMLHTTP"); if (this.req){ this.req.onreadystatechange = function () { pointer.processReqChange() }; this.req.open(meth, url, true); this.req.send(); } } }; wm.Ajax.prototype.processReqChange = function(){ if (this.req.readyState == 4) { if (this.req.status == 200){ this.onComplete( this.req ); } else { this.onError( this.req.status ); } } }; if (typeof(wm.DOM) == "undefined"){ wm.DOM = {}; } wm.DOM.cE = function ( type, attr, cont, html ){ var ne = document.createElement( type ); if (!ne){ return 0; } for (var a in attr){ ne[a] = attr[a]; } var t = typeof(cont); if (t == "string" && !html){ ne.appendChild( document.createTextNode(cont) ); } else if (t == "string" && html){ ne.innerHTML = cont; } else if (t == "object"){ ne.appendChild( cont ); } return ne; }; wm.DOM.gE = function ( e ){ var t=typeof(e); if (t == "undefined"){ return 0; } else if (t == "string"){ var re = document.getElementById( e ); if (!re){ return 0; } else if (typeof(re.appendChild) != "undefined" ){ return re; } else { return 0; } } else if (typeof(e.appendChild) != "undefined"){ return e; } else { return 0; } }; wm.DOM.remE = function ( ele ){ var e = this.gE(ele); if (!e){ return 0; } else if (e.parentNode.removeChild(e)){ return true; } else { return 0; } }; wm.DOM.getPos = function ( e ){ var e = this.gE(e); var obj = e; var curleft = 0; if (obj.offsetParent){ while (obj.offsetParent){ curleft += obj.offsetLeft; obj = obj.offsetParent; } } else if (obj.x){ curleft += obj.x; } var obj = e; var curtop = 0; if (obj.offsetParent){ while (obj.offsetParent){ curtop += obj.offsetTop; obj = obj.offsetParent; } } else if (obj.y){ curtop += obj.y; } return {x:curleft, y:curtop}; }; if (typeof(wm.Fader) == "undefined"){ wm.Fader = {}; } wm.Fader = function (ele, from, to, fadetime, callback){ if (!ele){ return 0; } this.e = ele; this.from = from; this.to = to; this.cb = callback; this.nDur = fadetime; this.nInt = 50; this.nTime = 0; var p = this; this.nID = setInterval(function() { p._fade() }, this.nInt); }; wm.Fader.prototype._fade = function(){ this.nTime += this.nInt; var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 ); var op = ieop / 100; if (this.e.filters) { try { this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop; } catch (e) { this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')'; } } else { this.e.style.opacity = op; } if (this.nTime == this.nDur){ clearInterval( this.nID ); if (this.cb != undefined){ this.cb(); } } }; wm.Fader.prototype._tween = function(t,b,c,d){ return b + ( (c-b) * (t/d) ); }; var options_xml = { script: function (input) { return "https://www.worldcyclingstats.com/site_nav_search.php?input="+input; }, varname:"input" }; var as_xml = new bsn.AutoSuggest('input_xml', options_xml);