(function(g){g.fn.addOption=function(){var b=function(b,d,c,a){var e=document.createElement("option");e.value=d;e.text=c;c=b.options;var h=c.length;if(!b.cache){b.cache={};for(var f=0;f<h;f++)b.cache[c[f].value]=f}"undefined"==typeof b.cache[d]&&(b.cache[d]=h);b.options[b.cache[d]]=e;a&&(e.selected=!0)},a=arguments;if(0==a.length)return this;var c=!0,e=!1,f,d,h;"object"==typeof a[0]&&(e=!0,f=a[0]);2<=a.length&&("boolean"==typeof a[1]?c=a[1]:"boolean"==typeof a[2]&&(c=a[2]),e||(d=a[0],h=a[1]));this.each(function(){if("select"==
this.nodeName.toLowerCase())if(e)for(var a in f)b(this,a,f[a],c);else b(this,d,h,c)});return this};g.fn.ajaxAddOption=function(b,a,c,e,f){if("string"!=typeof b)return this;"object"!=typeof a&&(a={});"boolean"!=typeof c&&(c=!0);this.each(function(){var d=this;g.getJSON(b,a,function(b){g(d).addOption(b,c);"function"==typeof e&&("object"==typeof f?e.apply(d,f):e.call(d))})});return this};g.fn.removeOption=function(){var b=arguments;if(0==b.length)return this;var a=typeof b[0],c,e;if("string"==a||"object"==
a||"function"==a){if(c=b[0],c.constructor==Array){for(var a=c.length,f=0;f<a;f++)this.removeOption(c[f],b[1]);return this}}else if("number"==a)e=b[0];else return this;this.each(function(){if("select"==this.nodeName.toLowerCase()){this.cache&&(this.cache=null);var d=!1,a=this.options;if(c)for(var f=a.length-1;0<=f;f--)c.constructor==RegExp?a[f].value.match(c)&&(d=!0):a[f].value==c&&(d=!0),d&&!0===b[1]&&(d=a[f].selected),d&&(a[f]=null),d=!1;else(d=!0===b[1]?a[e].selected:!0)&&this.remove(e)}});return this};
g.fn.sortOptions=function(b){var a=g(this).selectedValues(),c="undefined"==typeof b?!0:!!b;this.each(function(){if("select"==this.nodeName.toLowerCase()){for(var b=this.options,a=b.length,d=[],h=0;h<a;h++)d[h]={v:b[h].value,t:b[h].text};d.sort(function(b,a){o1t=b.t.toLowerCase();o2t=a.t.toLowerCase();return o1t==o2t?0:c?o1t<o2t?-1:1:o1t>o2t?-1:1});for(h=0;h<a;h++)b[h].text=d[h].t,b[h].value=d[h].v}}).selectOptions(a,!0);return this};g.fn.selectOptions=function(b,a){var c=typeof b;if("object"==c&&
b.constructor==Array){var e=this;g.each(b,function(){e.selectOptions(this,a)})}var f=a||!1;if("string"!=c&&"function"!=c&&"object"!=c)return this;this.each(function(){if("select"!=this.nodeName.toLowerCase())return this;for(var a=this.options,c=a.length,e=0;e<c;e++)b.constructor==RegExp?a[e].value.match(b)?a[e].selected=!0:f&&(a[e].selected=!1):a[e].value==b?a[e].selected=!0:f&&(a[e].selected=!1)});return this};g.fn.copyOptions=function(b,a){var c=a||"selected";if(0==g(b).size())return this;this.each(function(){if("select"!=
this.nodeName.toLowerCase())return this;for(var a=this.options,f=a.length,d=0;d<f;d++)("all"==c||"selected"==c&&a[d].selected)&&g(b).addOption(a[d].value,a[d].text)});return this};g.fn.containsOption=function(b,a){var c=!1,e=typeof b,f=typeof a;if("string"!=e&&"function"!=e&&"object"!=e)return"function"==f?this:c;this.each(function(){if("select"!=this.nodeName.toLowerCase())return this;if(c&&"function"!=f)return!1;for(var d=this.options,e=d.length,g=0;g<e;g++)b.constructor==RegExp?d[g].value.match(b)&&
(c=!0,"function"==f&&a.call(d[g],g)):d[g].value==b&&(c=!0,"function"==f&&a.call(d[g],g))});return"function"==f?this:c};g.fn.selectedValues=function(){var b=[];this.selectedOptions().each(function(){b[b.length]=this.value});return b};g.fn.selectedTexts=function(){var b=[];this.selectedOptions().each(function(){b[b.length]=this.text});return b};g.fn.selectedOptions=function(){return this.find("option:selected")}})(jQuery);
