(function(f){f.dropdownReplacement={defaults:{options:null,selectClass:null,optionsClass:"dropdownOpts",optionsDisplayNum:25,optionClass:"dropdownOpt",optionSelectedClass:"selectedOpt",resizeSelectToFitOptions:false,useHiddenInput:true,resizeOptionsToFitSelect:true,selectCssWidth:null,optionsWidthOffset:3,debounceLookupMs:200,debounceArrowsMs:50,lookupMaxWordLength:3,scrollWidth:17,ellipsisSelectText:true,ellipsisText:"...",charWidth:null,onInit:function(){},onSelect:function(){}}};f.expr[":"].dr= function(c,x,A){x=A[3];return(c=f(c).dropdownReplacement("option"))&&(c.getSelect().is(x)||c.getHidden()?c.getHidden().is(x):false)};f.fn.dropdownReplacement=function(c,x){if(typeof c==="string"&&c==="option"){var A=f(this).data("dropdownReplacement");return arguments.length==2?A[x]():A}c=f.extend({},f.dropdownReplacement.defaults,f.isFunction(c)?{onSelect:c}:c);var B=this,Q=f("body"),C=f(window),R=f.browser.msie&&parseInt(f.browser.version,10)==8,$=f.browser.msie&&parseInt(f.browser.version,10)<= 7,m=[],o={},r={},u={},D={},E,F,G,I,S,n,i,s=[],t=f([]),p=f([]),k=null,T=[],J=true,K=[],H=null,U=null,L={},M=[],V=-1,aa={selectClass:"dropdown"},N=f([]),l={lastLookupWord:null,lastLookupIndex:0,options:function(a){var b=f(a.target);if(b.is("a")){v(false);y(b);z();a.stopPropagation();return false}},focus:function(a){p=f(this);k=a.data.index;a=O();y(o[a]);this.selectionStart=this.selectionEnd=-1},select:function(a){p=f(this);this.selectionStart=this.selectionEnd=-1;k=a.data.index;if(K[k])v(false);else{a= O();a=o[a];v(true);y(a)}},unselect:function(){J&&v(false)},optionsOver:function(){J=false;t.removeClass(c.optionSelectedClass)},optionsOut:function(){J=true},selectLookup:function(a){for(var b=null,d=c.lookupMaxWordLength>a.length?c.lookupMaxWordLength:a.length,e=0;!b&&e<d;e++){a=a.substring(0,c.lookupMaxWordLength-e);b=D[a]}if(b){if(l.lastLookupWord===a)l.lastLookupIndex+=1;else{l.lastLookupIndex=0;l.lastLookupWord=a}if(b&&b.length){if(b.length<=l.lastLookupIndex)l.lastLookupIndex=0;y(b[l.lastLookupIndex]); z()}}}},P=function(a){if(c.charWidth)return c.charWidth;a=a||"a b c d e f 1 2 3 4 5 6 A B C D E F ! ! %";if(!L[a]){var b=f("<span>",{text:a,"class":c.selectClass,css:{background:"none",margin:0,padding:0,overflow:"visible",width:"auto",color:"#FFF"}});Q.append(b);L[a]=b.width()/a.length;b.remove()}return L[a]},v=function(a){if(K[k]=a){var b=p.offset(),d=b.top,e=b.left;d=d+G>E+C.scrollTop()&&b.top-G>0?b.top-G:d+S;if(!c.resizeSelectToFitOptions&&e+I>F)e-=I-n;i.css({top:d,left:e})}i[a?"show":"hide"](); a&&W(p)},z=function(a){a=a||t.text();var b=u[a];X(p,a);c.useHiddenInput&&s[k].val(b);c.onSelect.apply(p,[b,a,k])},O=function(a){return T[arguments.length?a:k]},X=function(a,b){T[k]=b;if(c.ellipsisSelectText){H=P(b);var d=~~(a.width()/H);if(d<b.length){d-=~~((U+5)/H);b=b.substring(0,d)+c.ellipsisText}}a.val(b)},y=function(a){t.removeClass(c.optionSelectedClass);if(a){t=a;t.addClass(c.optionSelectedClass)}K[k]&&jQuery.scrollTo&&a&&i.scrollTo(a)},ca=function(a){k=0;var b=function(){i=c.options;for(var g= [],h=i.find("a"),j=0;j<h.length;j++){var q=f(h[j]);g[j]={t:q.text(),v:q.attr("name")};q.addClass(c.optionClass);o[g[j].t]=q;r[g[j].v]=g[j].t}c.options=g},d=function(){var g=c.options;g.length==0&&w("options list must contain values, ex: [{'t':'text', 'v':'value'}]");if(typeof g[0].t=="undefined"||typeof g[0].v=="undefined")w("options json list must contain a list of objects with 2 keys: 't', and 'v'. ex: [{'t':'text', 'v':'value'}]");i=f("<div>");for(var h=0;h<g.length;h++){var j=f("<a>",{href:"#", name:g[h].v,text:g[h].t,"class":c.optionClass});i.append(j);o[g[h].t]=j;r[g[h].v]=g[h].t}Q.append(i)},e=function(){if(B.length>1)throw w("trying to widgetize more then ONE 'select' at a time is not supported. You can widgetize multiple 'input' elements.");var g=a.find("option");if(g.length===0)throw w("'select' must have ONE or more options elements as children in order to widgetize the select");c.options=[];for(var h=f("<input>",{css:{width:a.width()}}),j=0;j<g.length;j++){var q=f(g[j]),ba=q.val(), Y=q.text();c.options[j]={t:Y,v:ba};q.is(":selected")&&h.val(Y)}a.after(h);if(c.useHiddenInput){s[0]=a;a.hide()}else{h.attr("name",a.attr("name"));a.remove()}a=B=h;d()};if(a.is("select"))e();else c.options instanceof jQuery?b():d();b=c.options;for(e=0;e<b.length;e++){m[m.length]=b[e].t;u[b[e].t]=b[e].v}i.addClass(c.optionsClass);i.click(l.options);i.mouseover(l.optionsOver);i.mouseleave(l.optionsOut);f.fn.bgiframe&&i.bgiframe();V=i.width()},da=function(){for(var a=0;a<m.length;a++)for(var b=1;b<c.lookupMaxWordLength+ 1;b++)if(m[a].length>=b){var d=m[a].substring(0,b).toUpperCase();D[d]||(D[d]=[]);D[d].push(o[m[a]])}},ea=function(a){var b=o[m[0]];i.show();var d=a.is(":visible");d||a.show();b=b.outerHeight(true);b=c.optionsDisplayNum*b;var e=i.height();W(a);d||a.hide();i.hide();i.css({height:(e<b?e:b)+(R?2:0)})},W=function(a){var b=M[k];n=a.outerWidth(true);var d=i.width();if(!(d==b&&n==d)){if(c.resizeSelectToFitOptions&&d>n){a.width(d+c.optionsWidthOffset);n=d}if(c.resizeOptionsToFitSelect&&d<n)n-=c.optionsWidthOffset; else if(c.resizeOptionsToFitSelect&&d>n&&d>V)d=n-c.optionsWidthOffset;b=d>n?d:n;a.data("dropdownReplancement.width",b);M[k]=b;i.width(b)}},fa=function(){var a,b=[];return function(d){b.push(String.fromCharCode(d.keyCode));clearTimeout(a);a=setTimeout(function(){l.selectLookup(b.join(""));a=null;b=[]},c.debounceLookupMs)}},ga=function(a){a=a!=="first"&&a!=="last"?t.length>0?t[a]("a"):o[m[0]]:a==="first"?o[m[0]]:o[m[m.length-1]];if(a.length===0)return false;else{y(a);return true}},ha=function(){var a, b=[];b[38]="prev";b[40]="next";b[33]="first";b[34]="last";return function(d){if(d=b[d.keyCode])if(ga(d)){clearTimeout(a);a=setTimeout(function(){z();a=null},c.debounceArrowsMs)}}},Z=function(){var a=F,b=E;F=C.width();E=C.height();a=Math.abs(a-F);b=Math.abs(b-E);return a>c.scrollWidth&&b>c.scrollWidth},ia=function(a){var b=a.is(":visible");b||a.show();n=a.outerWidth(true);S=a.outerHeight(true);G=i.outerHeight(true);I=i.outerWidth(true);b||a.hide()},ja=function(a){a.addClass(aa.selectClass);c.selectClass&& a.addClass(c.selectClass);if(f.support.windowsTheme&&f.support.windowsTheme.name){a.addClass("dd-theme-"+f.support.windowsTheme.name);i.addClass("opt-theme-"+f.support.windowsTheme.name)}else a.addClass("dd-all");$&&a.addClass("dd-oldIE")},w=function(a){return"jquery.dropdownReplacement exception: "+a},ka=function(a,b,d){var e={val:function(g){if(arguments.length==1){var h=g=r[g]?g:u[m[0]];k=d;p=a;z(r[h])}else return u[a.val()]},text:function(g){if(g){g=u[g];k=d;p=a;z(r[g])}else return O(d)},getSelect:function(){return a}, getHidden:function(){return b}};a.data("dropdownReplacement",e);b&&b.data("dropdownReplacement",e)};(function(){if(!jQuery.scrollTo)throw w("jquery.scrollTo plugin is required for this plugin. http://plugins.jquery.com/project/ScrollTo");if(R)c.optionsWidthOffset-=3;var a=f(B[0]);if(c.ellipsisSelectText){H=P();U=P(c.ellipsisText)*c.ellipsisText.length}ca(a);da();Z();C.resize(function(){Z()&&v(false)});B.each(function(b){var d=f(this),e,g=d.is("select");M[b]=-1;if(!d.is("input")&&!g)throw w("root element must be an 'input' or 'select'"); k=b;if(c.useHiddenInput&&!g&&!s[b]){e=f("<input>");e.val(d.val());d.after(e)}else e=d;N=N.add(e);c.selectCssWidth&&e.css({width:c.selectCssWidth});ja(e);if(b===0){ea(e);ia(e)}var h=e.val();if(c.useHiddenInput&&!g&&r[h])h=r[h];else u[h]||(h=m[0]);X(e,h);if(c.useHiddenInput&&!s[b]){d.hide();s[b]=d}ka(e,s[b],b);e.attr("readonly","true");e.bind("click",{index:b},l.select).bind("blur",{index:b},l.unselect).bind("focus",{index:b},l.focus).keyup(fa()).keydown(ha()).keydown(function(j){if(j.keyCode==13|| j.keyCode==27){v(false);e.blur()}});c.onInit(e,s[b])})})();return N}})(jQuery);
