// SpryAutoSuggest.js - version 0.91 - Spry Pre-Release 1.6.1
//
// Copyright (c) 2007. Adobe Systems Incorporated.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
var Spry;if(!Spry){Spry={}}if(!Spry.Widget){Spry.Widget={}}Spry.Widget.BrowserSniff=function(){var H=navigator.appName.toString();var D=navigator.platform.toString();var B=navigator.userAgent.toString();this.mozilla=this.ie=this.opera=this.safari=false;var F=/Opera.([0-9\.]*)/i;var C=/MSIE.([0-9\.]*)/i;var E=/gecko/i;var G=/(applewebkit|safari)\/([\d\.]*)/i;var A=false;if((A=B.match(F))){this.opera=true;this.version=parseFloat(A[1])}else{if((A=B.match(C))){this.ie=true;this.version=parseFloat(A[1])}else{if((A=B.match(G))){this.safari=true;this.version=parseFloat(A[2])}else{if(B.match(E)){var I=/rv:\s*([0-9\.]+)/i;A=B.match(I);this.mozilla=true;this.version=parseFloat(A[1])}}}}this.windows=this.mac=this.linux=false;this.Platform=B.match(/windows/i)?"windows":(B.match(/linux/i)?"linux":(B.match(/mac/i)?"mac":B.match(/unix/i)?"unix":"unknown"));this[this.Platform]=true;this.v=this.version;if(this.safari&&this.mac&&this.mozilla){this.mozilla=false}};Spry.is=new Spry.Widget.BrowserSniff();Spry.Widget.AutoSuggest=function(F,A,E,D,C){if(!this.isBrowserSupported()){return }C=C||{};this.init(F,A,E,D);Spry.Widget.Utils.setOptions(this,C);if(Spry.Widget.AutoSuggest.onloadDidFire){this.attachBehaviors()}else{Spry.Widget.AutoSuggest.loadQueue.push(this)}this.dataset.addObserver(this);var G=Spry.Widget.Utils.getElementID(A);var B=this;this._notifyDataset={onPostUpdate:function(){B.attachClickBehaviors()},onPreUpdate:function(){B.removeClickBehaviours()}};Spry.Data.Region.addObserver(G,this._notifyDataset);Spry.Widget.Utils.addEventListener(window,"unload",function(){B.destroy()},false);this.attachClickBehaviors();this.handleKeyUp(null);this.showSuggestions(false)};Spry.Widget.AutoSuggest.prototype.init=function(D,A,C,B){this.region=Spry.Widget.Utils.getElement(D);if(!this.region){return }this.minCharsType=false;this.containsString=false;this.loadFromServer=false;this.urlParam="";this.suggestionIsVisible=false;this.stopFocus=false;this.hasFocus=false;this.showSuggestClass="showSuggestClass";this.hideSuggestClass="hideSuggestClass";this.hoverSuggestClass="hoverSuggestClass";this.movePrevKeyCode=Spry.Widget.AutoSuggest.KEY_UP;this.moveNextKeyCode=Spry.Widget.AutoSuggest.KEY_DOWN;this.textElement=Spry.Widget.Utils.getFirstChildWithNodeNameAtAnyLevel(this.region,"INPUT");this.textElement.setAttribute("AutoComplete","off");this.suggestRegion=Spry.Widget.Utils.getElement(A);Spry.Widget.Utils.makePositioned(this.suggestRegion);Spry.Widget.Utils.addClassName(this.suggestRegion,this.hideSuggestClass);this.timerID=null;if(typeof C=="string"){this.dataset=window[C]}else{this.dataset=C}this.field=B;if(typeof B=="string"&&B.indexOf(",")!=-1){B=B.replace(/\s*,\s*/ig,",");this.field=B.split(",")}};Spry.Widget.AutoSuggest.prototype.isBrowserSupported=function(){return Spry.is.ie&&Spry.is.v>=5&&Spry.is.windows||Spry.is.mozilla&&Spry.is.v>=1.4||Spry.is.safari||Spry.is.opera&&Spry.is.v>=9};Spry.Widget.AutoSuggest.prototype.getValue=function(){if(!this.textElement){return""}return this.textElement.value};Spry.Widget.AutoSuggest.prototype.setValue=function(A){if(!this.textElement){return }this.textElement.value=A;this.showSuggestions(false)};Spry.Widget.AutoSuggest.prototype.focus=function(){if(!this.textElement){return }this.textElement.focus()};Spry.Widget.AutoSuggest.prototype.showSuggestions=function(A){if(this.textElement.value==""){Spry.Widget.Utils.removeClassName(this.region,this.showSuggestClass);return }if(this.region&&this.isVisibleSuggestion()!=A){if(A){Spry.Widget.Utils.addClassName(this.region,this.showSuggestClass);if(Spry.is.ie&&Spry.is.version<7){this.createIframeLayer(this.suggestRegion)}}else{if(Spry.is.ie&&Spry.is.version<7){this.removeIframeLayer()}Spry.Widget.Utils.removeClassName(this.region,this.showSuggestClass)}}this.suggestionIsVisible=Spry.Widget.Utils.hasClassName(this.region,this.showSuggestClass)};Spry.Widget.AutoSuggest.prototype.isVisibleSuggestion=function(){return this.suggestionIsVisible};Spry.Widget.AutoSuggest.prototype.onDataChanged=function(A){var B=A.getData(true);var C=this.getValue();this.showSuggestions(B&&(!this.minCharsType||C.length>=this.minCharsType)&&(B.length>1||(B.length==1&&this.childs[0]&&this.childs[0].attributes.getNamedItem("spry:suggest").value!=this.getValue())))};Spry.Widget.AutoSuggest.prototype.nodeMouseOver=function(D,C){var A=this.childs.length;for(var B=0;B<A;B++){if(this.childs[B]!=C&&Spry.Widget.Utils.hasClassName(this.childs[B],this.hoverSuggestClass)){Spry.Widget.Utils.removeClassName(this.childs[B],this.hoverSuggestClass);break}}};Spry.Widget.AutoSuggest.prototype.nodeClick=function(B,A){if(A){this.setValue(A)}};Spry.Widget.AutoSuggest.prototype.handleKeyUp=function(C){if(this.timerID){clearTimeout(this.timerID);this.timerID=null}if(C&&this.isSpecialKey(C)){this.handleSpecialKeys(C);return }var A=this;var B=function(){A.timerID=null;A.loadDataSet()};if(!this.loadFromServer){B=function(){A.timerID=null;A.filterDataSet()}}this.timerID=setTimeout(B,200)};Spry.Widget.AutoSuggest.prototype.scrollVisible=function(C){if(typeof this.scrolParent=="undefined"){var A=C;this.scrolParent=false;while(!this.scrolParent){var D=Spry.Widget.Utils.getStyleProp(A,"overflow");if(!D||D.toLowerCase()=="scroll"){this.scrolParent=A;break}if(A==this.region){break}A=A.parentNode}}if(this.scrolParent!=false){var B=parseInt(Spry.Widget.Utils.getStyleProp(this.scrolParent,"height"),10);if(C.offsetTop<this.scrolParent.scrollTop){this.scrolParent.scrollTop=C.offsetTop}else{if(C.offsetTop+C.offsetHeight>this.scrolParent.scrollTop+B){this.scrolParent.scrollTop=C.offsetTop+C.offsetHeight-B+5;if(this.scrolParent.scrollTop<0){this.scrolParent.scrollTop=0}}}}};Spry.Widget.AutoSuggest.KEY_UP=38;Spry.Widget.AutoSuggest.KEY_DOWN=40;Spry.Widget.AutoSuggest.prototype.handleSpecialKeys=function(H){switch(H.keyCode){case this.moveNextKeyCode:case this.movePrevKeyCode:if(!(this.childs.length>0)||!this.getValue()){return }var E=this.childs.length-1;var D=false;var G=false;var F=this.dataset.getData();if(this.childs.length>1||(F&&F.length==1&&this.childs[0]&&this.childs[0].attributes.getNamedItem("spry:suggest").value!=this.getValue())){this.showSuggestions(true)}else{return }var B=Spry.Widget.Utils;for(var C=0;C<this.childs.length;C++){if(D){B.addClassName(this.childs[C],this.hoverSuggestClass);this.scrollVisible(this.childs[C]);break}if(B.hasClassName(this.childs[C],this.hoverSuggestClass)){B.removeClassName(this.childs[C],this.hoverSuggestClass);G=true;if(H.keyCode==this.moveNextKeyCode){D=true;continue}else{B.addClassName(this.childs[E],this.hoverSuggestClass);this.scrollVisible(this.childs[E]);break}}E=C}if(!G||(D&&C==this.childs.length)){B.addClassName(this.childs[0],this.hoverSuggestClass);this.scrollVisible(this.childs[0])}B.stopEvent(H);break;case 27:this.showSuggestions(false);break;case 13:if(!this.isVisibleSuggestion()){return }for(var C=0;C<this.childs.length;C++){if(Spry.Widget.Utils.hasClassName(this.childs[C],this.hoverSuggestClass)){var A=this.childs[C].attributes.getNamedItem("spry:suggest");if(A){this.setValue(A.value);this.handleKeyUp(null)}this.destroy();this.dataset=null;return false}}break;case 9:for(var C=0;C<this.childs.length;C++){if(Spry.Widget.Utils.hasClassName(this.childs[C],this.hoverSuggestClass)){var A=this.childs[C].attributes.getNamedItem("spry:suggest");if(A){this.textElement.value=A.value}return false}}this.showSuggestions(false);break}return };Spry.Widget.AutoSuggest.prototype.filterDataSet=function(){var D=this.containsString;var B=this.field;var G=this.getValue();if(this.previousString&&this.previousString==G){return }this.previousString=G;if(!G||(this.minCharsType&&this.minCharsType>G.length)){this.dataset.filter(function(I,J,H){return null});this.showSuggestions(false);return }var A=Spry.Widget.Utils.escapeRegExp(G);if(!D){A="^"+A}var C=new RegExp(A,"ig");if(this.maxListItems>0){this.dataset.maxItems=this.maxListItems}var F=function(K,M,I){if(K.maxItems>0&&K.maxItems<=K.data.length){return null}if(typeof B=="object"){var H=B.length;for(var J=0;J<H;J++){var L=M[B[J]];if(L&&L.search(C)!=-1){return M}}}else{var L=M[B];if(L&&L.search(C)!=-1){return M}}return null};this.dataset.filter(F);var E=this.dataset.getData();this.showSuggestions(E&&(!this.minCharsType||G.length>=this.minCharsType)&&(E.length>1||(E.length==1&&this.childs[0]&&this.childs[0].attributes.getNamedItem("spry:suggest").value!=G)))};Spry.Widget.AutoSuggest.prototype.loadDataSet=function(){var D=this.getValue();var C=this.dataset;if(C==null){return }C.cancelLoadData();C.useCache=false;if(!D||(this.minCharsType&&this.minCharsType>D.length)){this.showSuggestions(false);return }if(this.previousString&&this.previousString==D){var B=C.getData();this.showSuggestions(B&&(B.length>1||(B.length==1&&this.childs[0].attributes.getNamedItem("spry:suggest").value!=D)));return }this.previousString=D;var A=Spry.Widget.Utils.addReplaceParam(C.url,this.urlParam,D);C.setURL(A);C.loadData()};Spry.Widget.AutoSuggest.prototype.addMouseListener=function(C,D){var A=this;var B=Spry.Widget.Utils.addEventListener;B(C,"click",function(E){return A.nodeClick(E,D);A.handleKeyUp(null)},false);B(C,"mouseover",function(E){Spry.Widget.Utils.addClassName(C,A.hoverSuggestClass);A.nodeMouseOver(E,C)},false);B(C,"mouseout",function(E){Spry.Widget.Utils.removeClassName(C,A.hoverSuggestClass);A.nodeMouseOver(E,C)},false)};Spry.Widget.AutoSuggest.prototype.removeMouseListener=function(C,D){var A=this;var B=Spry.Widget.Utils.removeEventListener;B(C,"click",function(E){A.nodeClick(E,D);A.handleKeyUp(null)},false);B(C,"mouseover",function(E){Spry.Widget.Utils.addClassName(C,A.hoverSuggestClass);A.nodeMouseOver(E,C)},false);B(C,"mouseout",function(E){Spry.Widget.Utils.removeClassName(C,A.hoverSuggestClass);A.nodeMouseOver(E,C)},false)};Spry.Widget.AutoSuggest.prototype.attachClickBehaviors=function(){var B=this;var A=Spry.Utils.getNodesByFunc(this.region,function(D){if(D.nodeType==1){var C=D.attributes.getNamedItem("spry:suggest");if(C){B.addMouseListener(D,C.value);return true}}return false});this.childs=A};Spry.Widget.AutoSuggest.prototype.removeClickBehaviours=function(){var B=this;var A=Spry.Utils.getNodesByFunc(this.region,function(D){if(D.nodeType==1){var C=D.attributes.getNamedItem("spry:suggest");if(C){B.removeMouseListener(D,C.value);return true}}return false})};Spry.Widget.AutoSuggest.prototype.destroy=function(){this.removeClickBehaviours();Spry.Data.Region.removeObserver(Spry.Widget.Utils.getElementID(this.suggestRegion),this._notifyDataset);if(this.event_handlers){for(var B=0;B<this.event_handlers.length;B++){Spry.Widget.Utils.removeEventListener(this.event_handlers[B][0],this.event_handlers[B][1],this.event_handlers[B][2],false)}}for(var A in this){if(typeof this[A]!="function"){try{delete this[A]}catch(C){}}}};Spry.Widget.AutoSuggest.onloadDidFire=false;Spry.Widget.AutoSuggest.loadQueue=[];Spry.Widget.AutoSuggest.processLoadQueue=function(C){Spry.Widget.AutoSuggest.onloadDidFire=true;var D=Spry.Widget.AutoSuggest.loadQueue;var A=D.length;for(var B=0;B<A;B++){D[B].attachBehaviors()}};Spry.Widget.AutoSuggest.addLoadListener=function(A){if(typeof window.addEventListener!="undefined"){window.addEventListener("load",A,false)}else{if(typeof document.addEventListener!="undefined"){document.addEventListener("load",A,false)}else{if(typeof window.attachEvent!="undefined"){window.attachEvent("onload",A)}}}};Spry.Widget.AutoSuggest.addLoadListener(Spry.Widget.AutoSuggest.processLoadQueue);Spry.Widget.AutoSuggest.prototype.attachBehaviors=function(){this.event_handlers=[];var B=this;var A=function(E){B.handleKeyUp(E)};this.event_handlers.push([this.textElement,"keydown",A]);this.event_handlers.push([this.textElement,"focus",function(E){if(B.stopFocus){B.handleKeyUp(E)}B.hasFocus=true;B.stopFocus=false}]);this.event_handlers.push([this.textElement,"drop",A]);this.event_handlers.push([this.textElement,"dragdrop",A]);var D=false;if(Spry.is.opera){D=function(E){setTimeout(function(){if(!B.clickInList){B.showSuggestions(false)}else{B.stopFocus=true;B.textElement.focus()}B.clickInList=false;B.hasFocus=false},100)}}else{D=function(E){if(!B.clickInList){B.showSuggestions(false)}else{B.stopFocus=true;B.textElement.focus()}B.clickInList=false;B.hasFocus=false}}this.event_handlers.push([this.textElement,"blur",D]);this.event_handlers.push([this.suggestRegion,"mousedown",function(E){B.clickInList=true}]);for(var C=0;C<this.event_handlers.length;C++){Spry.Widget.Utils.addEventListener(this.event_handlers[C][0],this.event_handlers[C][1],this.event_handlers[C][2],false)}};Spry.Widget.AutoSuggest.prototype.createIframeLayer=function(B){if(typeof this.iframeLayer=="undefined"){var A=document.createElement("iframe");A.tabIndex="-1";A.src='javascript:"";';A.scrolling="no";A.frameBorder="0";A.className="iframeSuggest";document.appendChild(A);this.iframeLayer=A}this.iframeLayer.style.left=B.offsetLeft+"px";this.iframeLayer.style.top=B.offsetTop+"px";this.iframeLayer.style.width=B.offsetWidth+"px";this.iframeLayer.style.height=B.offsetHeight+"px";this.iframeLayer.style.display="block"};Spry.Widget.AutoSuggest.prototype.removeIframeLayer=function(){if(this.iframeLayer){this.iframeLayer.style.display="none"}};if(!Spry.Widget.Utils){Spry.Widget.Utils={}}Spry.Widget.Utils.specialSafariNavKeys=",63232,63233,63234,63235,63272,63273,63275,63276,63277,63289,";Spry.Widget.Utils.specialCharacters=",9,13,27,38,40,";Spry.Widget.Utils.specialCharacters+=",33,34,35,36,37,39,45,46,";Spry.Widget.Utils.specialCharacters+=",16,17,18,19,20,144,145,";Spry.Widget.Utils.specialCharacters+=",112,113,114,115,116,117,118,119,120,121,122,123,";Spry.Widget.Utils.specialCharacters+=Spry.Widget.Utils.specialSafariNavKeys;Spry.Widget.AutoSuggest.prototype.isSpecialKey=function(A){return Spry.Widget.Utils.specialCharacters.indexOf(","+A.keyCode+",")!=-1||this.moveNextKeyCode==A.keyCode||this.movePrevKeyCode==A.keyCode};Spry.Widget.Utils.getElementID=function(A){if(typeof A=="string"&&A){return A}if(!A){return }return A.getAttribute("id")};Spry.Widget.Utils.getElement=function(A){if(Object.isUndefined(A)){return }if(A&&typeof A=="string"){return document.getElementById(A)}return A};Spry.Widget.Utils.addReplaceParam=function(C,H,G){if(C==null){return }var F="";var E="";var D=C.indexOf("?");if(D!=-1){F=C.slice(0,D);E=C.slice(D+1)}else{F=C}E=E.replace("?","");var A=E.split("&");if(H.lastIndexOf("/")!=-1){H=H.slice(H.lastIndexOf("/")+1)}for(D=0;D<A.length;D++){var B=A[D].split("=");if((B[0]&&B[0]==decodeURI(H))||A[D]==decodeURI(H)){A[D]=null}}A[A.length]=encodeURIComponent(H)+"="+encodeURIComponent(G);E="";for(D=0;D<A.length;D++){if(A[D]){E+="&"+A[D]}}E=E.slice(1);C=F+"?"+E;return C};Spry.Widget.Utils.addClassName=function(A,B){if(!A){return }if(!A.className){A.className=""}if(!A||A.className.search(new RegExp("\\b"+B+"\\b"))!=-1){return }A.className+=" "+B};Spry.Widget.Utils.removeClassName=function(B,A){if(!B){return }if(!B.className){B.className="";return }B.className=B.className.replace(new RegExp("\\s*\\b"+A+"\\b","g"),"")};Spry.Widget.Utils.hasClassName=function(B,A){if(!B||!A){return false}if(!B.className){B.className=""}return B.className.search(new RegExp("\\s*\\b"+A+"\\b"))!=-1};Spry.Widget.Utils.addEventListener=function(D,B,C,A){try{if(D.addEventListener){D.addEventListener(B,C,A)}else{if(D.attachEvent){D.attachEvent("on"+B,C,A)}}}catch(E){}};Spry.Widget.Utils.removeEventListener=function(D,B,C,A){try{if(D.removeEventListener){D.removeEventListener(B,C,A)}else{if(D.detachEvent){D.detachEvent("on"+B,C,A)}}}catch(E){}};Spry.Widget.Utils.stopEvent=function(A){A.cancelBubble=true;A.returnValue=false;try{this.stopPropagation(A)}catch(B){}try{this.preventDefault(A)}catch(B){}};Spry.Widget.Utils.stopPropagation=function(A){if(A.stopPropagation){A.stopPropagation()}else{A.cancelBubble=true}};Spry.Widget.Utils.preventDefault=function(A){if(A.preventDefault){A.preventDefault()}else{A.returnValue=false}};Spry.Widget.Utils.setOptions=function(D,C,A){if(!C){return }for(var B in C){if(typeof A!="undefined"&&A&&typeof C[B]=="undefined"){continue}D[B]=C[B]}};Spry.Widget.Utils.firstValid=function(){var A=null;for(var B=0;B<Spry.Widget.Utils.firstValid.arguments.length;B++){if(typeof Spry.Widget.Utils.firstValid.arguments[B]!="undefined"){A=Spry.Widget.Utils.firstValid.arguments[B];break}}return A};Spry.Widget.Utils.camelize=function(A){var E=A.split("-");var B=true;var C="";for(var D=0;D<E.length;D++){if(E[D].length>0){if(B){C=E[D];B=false}else{var F=E[D];C+=F.charAt(0).toUpperCase()+F.substring(1)}}}return C};Spry.Widget.Utils.getStyleProp=function(C,F){var D;var B=Spry.Widget.Utils.camelize(F);try{D=C.style[B];if(!D){if(document.defaultView&&document.defaultView.getComputedStyle){var A=document.defaultView.getComputedStyle(C,null);D=A?A.getPropertyValue(F):null}else{if(C.currentStyle){D=C.currentStyle[B]}}}}catch(E){}return D=="auto"?null:D};Spry.Widget.Utils.makePositioned=function(A){var B=Spry.Widget.Utils.getStyleProp(A,"position");if(!B||B=="static"){A.style.position="relative";if(window.opera){A.style.top=0;A.style.left=0}}};Spry.Widget.Utils.escapeRegExp=function(A){return A.replace(/([\.\/\]\[\{\}\(\)\\\$\^\?\*\|\!\=\+\-])/g,"\\$1")};Spry.Widget.Utils.getFirstChildWithNodeNameAtAnyLevel=function(A,C){var B=A.getElementsByTagName(C);if(B){return B[0]}return null};