(function($){var nsp='AddIncSearch';$[nsp]={defaultOptions:{maxListSize:20,maxMultiMatch:50,warnMultiMatch:'top {0} matches ...',warnNoMatch:'no matches ...',selectBoxHeight:'230px',zIndex:'200'}};var _={moveInputFocus:function(jq,dist){var $fields=jq.parents('form').eq(0)
.find('button,input[type!=hidden],textarea,select')
.filter(':not(:hidden)');var index=$fields.index(jq);if(index>-1
&&index+dist<$fields.length
&&index+dist>=0){$fields.eq(index+dist).focus();return true;}
else{return false;}},reEscape:function(text){return text.replace(/([.*+?^${}()|[\]\/\\])/g,'\\$1');},action:function(options){if(this.nodeName!='SELECT'||this.size>1){return this;}
var select_tag=this;var $select_tag=$(select_tag);if($select_tag.data('AICelements')){return this;};var $parent=$select_tag.parent();var idKey='AIS_'+Math.floor(1e10*Math.random()).toString(36);var meta_opts=options;if($.meta){meta_opts=$.extend({},options,$select_tag.data());}
var $empty_opt=$('<option value="_E_M_P_T_Y_"></option>');$select_tag.append($empty_opt);var $top_match_div=$('<div>'+meta_opts.warnMultiMatch.replace(/\{0\}/g,meta_opts.maxMultiMatch)+'</div>')
.css({color:'#bbb'});var $no_match_div=$('<div>'+meta_opts.warnNoMatch+'</div>')
.css({color:'#bbb'});var $blocker=$('<div/>')
.css({position:'absolute',width:$select_tag.outerWidth(),height:$select_tag.outerHeight(),backgroundColor:'#FFFFFF',opacity:'0.01',filter:'Alpha(opacity=1)'})
.appendTo($parent);var $input=$('<input type="text"/>')
.hide()
.addClass('addIncSearch')
.css({position:'absolute',backgroundColor:'transparent',outlineStyle:'none',borderColor:'transparent',borderStyle:'solid',borderBottomWidth:$select_tag.css('border-bottom-width'),borderLeftWidth:$select_tag.css('border-left-width'),borderRightWidth:$select_tag.css('border-right-width'),borderTopWidth:$select_tag.css('border-top-width'),marginBottom:$select_tag.css('margin-bottom'),marginLeft:$select_tag.css('margin-left'),marginRight:$select_tag.css('margin-right'),marginTop:$select_tag.css('margin-top'),paddingBottom:$select_tag.css('padding-bottom'),paddingLeft:$select_tag.css('padding-left'),paddingRight:$select_tag.css('padding-right'),paddingTop:$select_tag.css('padding-top')})
.width($select_tag.innerWidth())
.height($select_tag.outerHeight())
.appendTo($parent);var $chooser=$('<div/>')
.addClass('AddIncSearchChooser')
.hide()
.css({position:'absolute',height:meta_opts.selectBoxHeight.toString(),width:$select_tag.outerWidth()-6,overflow:'auto',borderColor:$select_tag.css('border-color'),borderStyle:'solid',borderWidth:'1px',padding:'2px',backgroundColor:$select_tag.css('background-color'),fontFamily:$select_tag.css('font-family'),fontSize:$select_tag.css('font-size'),cursor:'pointer',MozUserSelect:'none',WebkitUserSelect:'none',userSelect:'none',boxShadow:'3px 3px 5px #bbb',MozBoxShadow:'3px 3px 5px #bbb',WebkitBoxShadow:'3px 3px 5px #bbb',});$chooser.xClear=function(){this.xIdArr=[];this.xCurrentRow=null;};$chooser.xHiLite=function(row){if(this.xCurrentRow!=null){$('#'+idKey+this.xIdArr[this.xCurrentRow].toString(36)).css({color:$select_tag.css('color'),backgroundColor:'transparent'})}
if(row>=this.xIdArr.length){row=this.xIdArr.length-1;}
else if(row<0){row=0;}
var $el=$('#'+idKey+this.xIdArr[row].toString(36)).css({color:'#fff',backgroundColor:'#444'})
var el=$el.get(0);if(el){var top=$el.position().top;var scroll=$chooser.scrollTop();var elheight=$el.height();var cheight=$chooser.height()-elheight;if(top>=cheight){$chooser.scrollTop(scroll+top-cheight+elheight);}
else if(top<0){$chooser.scrollTop(scroll+top);}}
this.xCurrentRow=row;};$chooser.xNextRow=function(){if(this.xCurrentRow<this.xIdArr.length-1){this.xHiLite(this.xCurrentRow+1);}};$chooser.xPrevRow=function(){if(this.xCurrentRow>0){this.xHiLite(this.xCurrentRow-1);}};$chooser.xNextPage=function(){if(this.xCurrentRow<this.xIdArr.length-1){this.xHiLite(this.xCurrentRow+5);}};$chooser.xPrevPage=function(){if(this.xCurrentRow>0){this.xHiLite(this.xCurrentRow-5);}};$chooser.xClear();$chooser.xClickify=function(){for(var i=0;i<this.xIdArr.length;i++){var id='#'+idKey+this.xIdArr[i].toString(36);(function(){var ii=i;$(id).click(function(){$chooser.xHiLite(ii);input_hide();})})();}};var chooser=$chooser.get(0);var zIndex=/^\d+$/.test($select_tag.css("z-index"))?$select_tag.css("z-index"):1;if(meta_opts.zIndex&&/^\d+$/.test(meta_opts.zIndex))
zIndex=meta_opts.zIndex;$blocker.css("z-index",zIndex.toString(10));$input.css("z-index",(zIndex+1).toString(10));$chooser.css("z-index",(zIndex+1).toString(10));$chooser.appendTo($parent);function position_update(){var position=$select_tag.position();$chooser.css({top:position.top+$select_tag.outerHeight()+2,left:position.left+2});$input.css({top:position.top,left:position.left+2});$blocker.css({top:position.top,left:position.left});};$select_tag.resize(position_update);$(window).resize(position_update);position_update();var over_blocker=false;$blocker.mouseover(function(){over_blocker=true;});$blocker.mouseout(function(){over_blocker=false;});var over_chooser=false;$chooser.mouseover(function(){over_chooser=true;});$chooser.mouseout(function(){over_chooser=false;});$chooser.click(function(e){$input.focus();e.stopPropagation();});var last_selected;var search;var search_cache;var timer=null;function searcher(){if(search_cache==search){timer=null;return true;}
$chooser.xClear();search_cache=search;search=_.reEscape(search);var match_id;var matches=0;var opt_cnt=select_tag.length;var opt_arr=select_tag.options;var matcher_quick=new RegExp(search,'i');var matcher=new RegExp('(.*?)('+search+')(.*)','i');var new_opts='';var cap;var last_match;for(var i=0;i<opt_cnt&&matches<meta_opts.maxMultiMatch;i++){if(matcher_quick.test(opt_arr[i].text)){cap=matcher.exec(opt_arr[i].text)
matches++;$chooser.xIdArr.push(i);last_match='<div id="'+idKey+i.toString(36)+'">'+cap[1]+'<span style="background-color: #8f8; color: #000;">'+cap[2]+'</span>'+cap[3]+'</div>';new_opts+=last_match;match_id=i;}};if(matches==1&&opt_cnt<meta_opts.maxListSize){new_opts='';$chooser.xClear();for(var i=0;i<opt_cnt;i++){$chooser.xIdArr.push(i);if(i==match_id){new_opts+=last_match;}
else{new_opts+='<div id="'+idKey+i.toString(36)+'">'+opt_arr[i].text+'</div>';}}
$chooser.html(new_opts);$chooser.xHiLite(match_id);}
else if(matches>=1){$chooser.html(new_opts);$chooser.xHiLite(0);}
else{$chooser.empty();$chooser.append($no_match_div);}
if(matches>=meta_opts.maxMultiMatch){$chooser.append($top_match_div);}
$chooser.xClickify();timer=null;};function input_show(){last_selected=select_tag.selectedIndex;select_tag.selectedIndex=select_tag.length;search='';search_cache='dymmy';if(last_selected){search=select_tag.options[last_selected].text;$input.val(search);}
$input.show();$input.focus();$input.select();$chooser.show();timer=setTimeout(searcher,100);};function input_hide(){$input.hide();if($chooser.xCurrentRow!=null){select_tag.selectedIndex=$chooser.xIdArr[$chooser.xCurrentRow]
$select_tag.change();}
else{select_tag.selectedIndex=last_selected;}
$chooser.hide();$chooser.empty();};$blocker.click(function(e){if($select_tag.attr("disabled"))
return false;input_show();e.stopPropagation();});$select_tag.bind('focus.AIC',function(e){e.stopPropagation();input_show();});$select_tag.bind('click.AIC',function(e){e.stopPropagation();input_show();});$input.blur(function(e){if(!over_blocker&&!over_chooser){input_hide();return true;}
return false;});$input.keyup(function(e){if($.inArray(e.keyCode,new Array(9,13,40,38,34,33))>0)
return true;search=$.trim($input.val());if(timer!=null)
clearTimeout(timer);timer=setTimeout(searcher,100);});var pg_step=chooser.size;function handleKeyDown(e){switch(e.keyCode){case 9:input_hide();_.moveInputFocus($select_tag,e.shiftKey?-1:1);break;case 13:input_hide();_.moveInputFocus($select_tag,1);break;case 40:$chooser.xNextRow();break;case 38:$chooser.xPrevRow();break;case 34:$chooser.xNextPage();break;case 33:$chooser.xPrevPage();break;default:return true;}
e.stopPropagation();return false;};$input.keydown(handleKeyDown);$select_tag.data('AICelements',[$chooser,$blocker,$input,$empty_opt]);return this;}};$.fn[nsp]=function(options){if($.browser.msie){var bvers=(parseInt($.browser.version));if(bvers<7){return this;}}
var localOpts=$.extend({},$[nsp].defaultOptions,options);return this.each(function(){_.action.call(this,localOpts)});};$.fn.RemoveIncSearch=function(){return this.each(function(){var $this=$(this);var helpers=$this.data('AICelements');if(helpers){for(var i=0;i<helpers.length;i++){helpers[i].empty().remove();}
$this.removeData('AICelements');$this.unbind('click.AIC');$this.unbind('focus.AIC');}});};})(jQuery);
