﻿/**
 * JQuery Searchable DropDown Plugin
 * 
 * @required jQuery 1.3.x
 * @author Sascha Wolski <hagman@gmx.de>
 * 
 * Based up on the AddIncSearch plugin published by Tobias Oetiker
 * http://plugins.jquery.com/project/AddIncSearch
 */
(function($){var z=register("searchable");z.defaults={maxListSize:100,maxMultiMatch:50,exactMatch:false,wildcards:true,ignoreCase:true,warnMultiMatch:"top {0} matches ...",warnNoMatch:"Không tìm thấy ...",latency:200,zIndex:"auto"};z.execute=function(g){if($.browser.msie&&parseInt(jQuery.browser.version)<7)return this;if(this.nodeName!="SELECT"||this.size>1)return this;var h=$(this);var j={index:-1,options:null};var k="lang";var l=false;$.browser.chrome=/chrome/.test(navigator.userAgent.toLowerCase());if($.browser.chrome)$.browser.safari=false;if($.meta){g=$.extend({},options,h.data())}var m=$("<div/>");var n=$("<div/>");var o=$("<input/>");var p=$("<select/>");var q=$("<option>"+g.warnMultiMatch.replace(/\{0\}/g,g.maxMultiMatch)+"</option>").attr("disabled","true");var r=$("<option>"+g.warnNoMatch+"</option>").attr("disabled","true");draw();var t=false;n.mouseover(function(){t=true});n.mouseout(function(){t=false});p.mouseover(function(){t=true});p.mouseout(function(){t=false});o.click(function(e){if(!l)enable(e,true);else disable(e,true)});o.blur(function(e){if(!t&&l)disable(e,true)});h.keydown(function(e){if(e.keyCode!=9)enable(e,false,true)});h.click(function(e){p.focus()});p.click(function(e){if(p.selectedIndex()<0)return;disable(e)});p.focus(function(e){o.focus()});p.blur(function(e){if(!t)disable(e,true)});p.mousemove(function(e){if($.browser.opera&&parseFloat(jQuery.browser.version)>=9.8)return true;var a=Math.floor(parseFloat(/([0-9\.]+)px/.exec(p.option(0).css("font-size"))));var b=4;if($.browser.opera)b=2.5;if($.browser.safari||$.browser.chrome)b=3;a+=Math.round(a/b);p.selectedIndex(Math.floor((e.pageY-p.offset().top+this.scrollTop)/a))});n.click(function(e){o.click()});o.keyup(function(e){if(jQuery.inArray(e.keyCode,new Array(9,13,16,33,34,35,36,38,40))>-1)return true;y=$.trim(o.val().toLowerCase());clearSearchTimer();u=setTimeout(searching,g.latency)});o.keydown(function(e){if(e.shiftKey||e.ctrlKey||e.altKey)return;switch(e.keyCode){case 9:disable(e);moveTab(h,e.shiftKey?-1:1);break;case 13:disable(e);h.focus();break;case 27:disable(e,true);h.focus();break;case 33:if(p.selectedIndex()-p.attr("size")>0){p.selectedIndex(p.selectedIndex()-=p.attr("size"))}else{p.selectedIndex(0)}synchronize();break;case 34:if(p.selectedIndex()+p.attr("size")<p.get(0).options.length-1){p.selectedIndex(p.selectedIndex()+=p.attr("size"))}else{p.selectedIndex(p.get(0).options.length-1)}synchronize();break;case 38:if(p.selectedIndex()>0){p.selectedIndex(p.selectedIndex()-1);synchronize()}break;case 40:if(p.selectedIndex()<p.get(0).options.length-1){p.selectedIndex(p.selectedIndex()+1);synchronize()}break;default:return true}return false});function draw(){h.css("text-decoration","none");h.width(h.outerWidth());h.height(h.outerHeight());m.css("position","relative");m.css("width",h.outerWidth());n.css({"position":"absolute","top":0,"left":0,"width":h.outerWidth(),"height":h.outerHeight(),"background-color":"#FFFFFF","opacity":"0.01"});o.attr("type","text");o.hide();o.height(h.outerHeight());o.css({"position":"absolute","top":0,"left":0,"margin":"0px","padding":"0px","outline-style":"none","border-style":"solid","border-bottom-style":"none","border-color":"transparent","background-color":"transparent"});var a=new Array();a.push("border-left-width");a.push("border-top-width");a.push("font-size");a.push("font-stretch");a.push("font-variant");a.push("font-weight");a.push("color");a.push("text-align");a.push("text-indent");a.push("text-shadow");a.push("text-transform");a.push("padding-left");a.push("padding-top");for(var i=0;i<a.length;i++)o.css(a[i],h.css(a[i]));if($.browser.msie&&parseInt(jQuery.browser.version)<8){o.css("padding","0px");o.css("padding-left","3px");o.css("border-left-width","2px");o.css("border-top-width","3px")}else if($.browser.chrome){o.height(h.innerHeight());o.css("text-transform","none");o.css("padding-left",parseFloatPx(o.css("padding-left"))+3);o.css("padding-top",2)}else if($.browser.safari){o.height(h.innerHeight());o.css("padding-top",2);o.css("padding-left",3);o.css("text-transform","none")}else if($.browser.opera){o.height(h.innerHeight());var b=parseFloatPx(h.css("padding-left"));o.css("padding-left",b==1?b+1:b);o.css("padding-top",0)}else if($.browser.mozilla){o.css("padding-top","0px");o.css("border-top","0px");o.css("padding-left",parseFloatPx(h.css("padding-left"))+3)}else{o.css("padding-left",parseFloatPx(h.css("padding-left"))+3);o.css("padding-top",parseFloatPx(h.css("padding-top"))+1)}var c=parseFloatPx(h.css("padding-left"))+parseFloatPx(h.css("padding-right"))+parseFloatPx(h.css("border-left-width"))+parseFloatPx(h.css("border-left-width"))+23;o.width(h.outerWidth()-c);var w=h.css("width");var d=h.outerWidth();h.css("width","auto");var d=d>h.outerWidth()?d:h.outerWidth();h.css("width",w);p.resize(h.get(0).length);p.hide();p.css({"position":"absolute","top":h.outerHeight(),"left":0,"width":d,"border":"1px solid #333","font-weight":"normal","padding":0,"background-color":h.css("background-color"),"text-transform":h.css("text-transform")});var e=/^\d+$/.test(h.css("z-index"))?h.css("z-index"):1;if(g.zIndex&&/^\d+$/.test(g.zIndex))e=g.zIndex;n.css("z-index",(e).toString(10));o.css("z-index",(e+1).toString(10));p.css("z-index",(e+2).toString(10));h.wrap(m);h.after(n);h.after(o);h.after(p)};function enable(e,s,v){if(h.attr("disabled"))return false;h.prepend("<option />");if(typeof v=="undefined")l=!l;p.reset();synchronize();store();o.show();o.focus();o.select();if(s)p.show();h.get(0).selectedIndex=0;if(typeof e!="undefined")e.stopPropagation()};function disable(e,a){l=false;h.find(":first").remove();clearSearchTimer();o.hide();p.hide();if(typeof a!="undefined")restore();populate();if(typeof e!="undefined")e.stopPropagation()};function clearSearchTimer(){if(u!=null)clearTimeout(u)};function populate(){if(p.selectedIndex()<0||p.selected().get(0).disabled)return;h.get(0).selectedIndex=parseInt(p.find(":selected").attr(k));h.change();h.data("index",new Number(h.get(0).selectedIndex))};function synchronize(){if(p.selectedIndex()>-1&&!p.selected().get(0).disabled)o.val(p.find(":selected").text());else o.val(h.find(":selected").text())};function store(){j.index=p.selectedIndex();j.options=new Array();for(var i=0;i<p.get(0).options.length;i++)j.options.push(p.get(0).options[i])};function restore(){p.empty();p.resize(j.options.length);for(var i=0;i<j.options.length;i++)p.append(j.options[i]);p.selectedIndex(j.index)};p.option=function(a){return $(this.get(0).options[a])};p.selected=function(){return this.find(":selected")};p.selectedIndex=function(a){if(a>-1)this.get(0).selectedIndex=a;return this.get(0).selectedIndex};p.resize=function(a){this.attr("size",Math.max(2,Math.min(a,20)))};p.reset=function(){if((h.get(0).selectedIndex-1)==h.data("index"))return;var a=h.get(0).selectedIndex;var b=h.get(0).length;var c=Math.floor(g.maxMultiMatch/2);var d=Math.max(1,(a-c));var e=Math.min(b,Math.max(g.maxMultiMatch,(a+c)));var f=a-d;this.empty();this.resize(e-d);for(var i=d;i<e;i++)this.append($(h.get(0).options[i]).clone().attr(k,i-1));if(e>g.maxMultiMatch)this.append(q);this.get(0).selectedIndex=f};function moveTab(a,b){var c=a.parents("form,body").eq(0).find("button,input[type!=hidden],textarea,select");var d=c.index(a);if(d>-1&&d+b<c.length&&d+b>=0){c.eq(d+b).focus();return true}return false};function escapeRegExp(a){var b=["/",".","*","+","?","|","(",")","[","]","{","}","\\","^","$"];var c=new RegExp("(\\"+b.join("|\\")+")","g");return a.replace(c,"\\$1")};var u=null;var x;var y;function searching(){if(x==y){u=null;return}var a=0;x=y;p.hide();p.empty();var b=escapeRegExp(y);if(g.exactMatch)b="^"+b;if(g.wildcards){b=b.replace(/\\\*/g,".*");b=b.replace(/\\\?/g,".")}var c;if(g.ignoreCase)c="i";y=new RegExp(b,c);for(var i=1;i<h.get(0).length&&a<g.maxMultiMatch;i++){if(y.length==0||y.test(h.get(0).options[i].text)){var d=$(h.get(0).options[i]).clone().attr(k,i-1);if(h.data("index")==i)d.text(h.data("text"));p.append(d);a++}}if(a>=1){p.selectedIndex(0)}else if(a==0){p.append(r)}if(a>=g.maxMultiMatch){p.append(q)}p.resize(a);p.show();u=null};function parseFloatPx(a){try{a=parseFloat(a.replace(/[\s]*px/,""));if(!isNaN(a))return a}catch(e){}return 0};return};function register(b){var c=$[b]={};$.fn[b]=function(a){a=$.extend(c.defaults,a);return this.each(function(){c.execute.call(this,a)})};return c}})(jQuery);
