﻿function selectJSDDItem(param){var focusedElem=focusedJSDD;if(param!=undefined)
focusedElem=param;var select_name=focusedElem;var selectItems=$("#"+focusedElem+"-selectshow").children('.selectitem');if(!select_name.indexOf('cust_'))
{select_name=select_name.substr(5,select_name.length);}
srchStr=srchStr.toString();if($("#"+select_name).hasClass("sorted"))
var index=BinSearch(srchStr,selectItems,-1,1);else
var index=LinSearch(srchStr,selectItems);if(index==-1){return 1;}
while(lastFocusedJSDDIndex>=index&&($(selectItems[lastFocusedJSDDIndex+1]).children(".selecttext").children(".thistext").text().toLowerCase().substring(0,srchStr.length)==srchStr.toLowerCase()))
{index++;}
var test=document.getElementById(focusedElem+"-selectshow");if(test.style.display!='none')
{$('.selectitem').css({'background':'none','color':'#7a7a7a','left':'0','padding':'0'});$(selectItems[index]).css({'background-color':'#BBB','color':'#fff','left':'-5px','padding':'0 2px 0 5px','width':'100%','position':'relative'});lastFocusedJSDDIndex=index;focusedJSDDElem=selectItems[index];if(document.getElementById(focusedElem+'-selectshow').scrollTop+141<focusedJSDDElem.offsetTop)
{var topPos=selectItems[lastFocusedJSDDIndex].offsetTop-141;document.getElementById(focusedElem+'-selectshow').scrollTop=topPos;}
else if(document.getElementById(focusedElem+'-selectshow').scrollTop>focusedJSDDElem.offsetTop)
{var topPos=selectItems[lastFocusedJSDDIndex].offsetTop;document.getElementById(focusedElem+'-selectshow').scrollTop=topPos;}}
else
{$(selectItems[index]).click();}}
function theResult(a,b)
{var result=0;for(var i=0;i<a.length;i++)
{result=a[i].localeCompare(b[i]);if(result!=0)return result;}
return 0;}
function LinSearch(val,TheArr){for(var i=0;i<TheArr.length;i++)
{x=$(TheArr[i]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);if(val==x)
return i;}
return-1;}
function BinSearch(val,TheArr,col,series){val=val.toLowerCase();var index=-2;var Lo=ans=mid=0;var x='';var Hi=TheArr.length;while(index==-2){mid=Math.floor((Lo+Hi)/2);if(col==-1){x=$(TheArr[mid]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}
else{x=$(TheArr[mid][col]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}
if(theResult(val,x)==0){index=mid;}
else{if((mid==Hi)||(mid==Lo)){index=-1;}
else{if((isNaN(val))&&(isNaN(x))){ans=((val.localeCompare(x)<0)?true:false);}
else{ans=(isNaN(val)?false:true);}
ans=(theResult(val,x)<0)?true:false;if(ans==true){Hi=mid;}
else{Lo=mid;}}}}
if(index==0){return index;}
if((series==1)&&(index>=0)){if(col==-1){x=$(TheArr[index]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}
else{x=$(TheArr[index][col]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}
while((theResult(val,x)==0)&&(index>0)){index--;if(col==-1){x=$(TheArr[index]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}
else{x=$(TheArr[index][col]).children(".selecttext").children(".thistext").text().toLocaleLowerCase().substring(0,val.length);}}
if(theResult(val,x)==0){return index;}
index++;}
return index;};(function($){$.fn.extend({getByteLength:function(strIN){var i,cnt=0;for(i=0;i<strIN.length;i++){if(escape(strIN.charAt(i)).length>=4)cnt+=2;else cnt++;}
return cnt;}});$.fn.extend({getIndexByByte:function(strIN,limit){var i,cnt=0;for(i=0;i<strIN.length;i++){if(escape(strIN.charAt(i)).length>=4)cnt+=2;else cnt++;if(cnt>limit)
{return[true,i];}}
return false;}});$.fn.extend({finalselect:function(options){var settings={id:null,animalSpeed:100,selectWidth:"175px",selectImage:"",selectText:"",zIndex:0,viewHeight:"150px",viewWidth:"175px",viewMouseoverColor:"#cfdfff",viewTop:"20px",viewLeft:" 2px",cClass:null};if(typeof(options)!='undefined')
{jQuery.extend(settings,options);}
var expTopSh1;var expTopSh2;if(settings.cClass=="cust_wh")
{expTopSh1='';expTopSh2='';}
else
{expTopSh1="";expTopSh2="";}
if(parseInt(settings.selectWidth.substr(0,settings.selectWidth.indexOf("px",0)))>400)
settings.selectWidth="400px";var tmp='<input type="text" style="background: none; background-color: none; border: none; color: none; z-index: 0; position: absolute; top: 7px; right: 8px; width: 1px; height: 1px;" class="focusDJS" id="djs_'+settings.id+'">';if(settings.cClass=="cust_wh")
tmp+='<div style="float: right; clear: both; display: block;">';else
tmp+='';tmp+='<div id="'+settings.id+'-select" style="cursor:default;font-size:11px;text-align:left;z-index:'+settings.zIndex+';border: solid 0px #999; padding: 0; width: '+(parseInt(settings.selectWidth.substr(0,settings.selectWidth.indexOf("px",0)))+17)+'; position: relative; margin-right: 10px; ';if(settings.cClass!="cust_wh")
tmp+='left: 0px">';else
tmp+='left: 0px; margin-bottom: 4px;">';tmp+='<div id="'+settings.id+'-Text" class="'+settings.cClass+'" style="background: url('+settings.selectImage+') no-repeat 0 0; width: '+(parseInt(settings.selectWidth.substr(0,settings.selectWidth.indexOf("px",0)))+17)+'px; height: 25px; color: White; font-weight:bold;padding: 0  0 0 0px;position:relative; text-align:left; z-index: 3;"><div class="custSelArr"></div>';tmp+='<div class="textshow" style="padding: 4px 0 0 11px; _padding: 3px 0 0 11px;">'+settings.selectText+'</div><div class="valueshow" style="display:none;"></div></div><div style="position:absolute; margin-top:-13px; margin-left:5px;$left:0" class="exp'+settings.cClass+'">';tmp+='<div class="cSelectTollUp" id="'+settings.id+'-cSelectTollUp">';tmp+='<div class="cSelectTollLSh" id="'+settings.id+'-expcont">'+expTopSh1+expTopSh2+'<div class="cSelectTollRSh" id="'+settings.id+'-TollRSh"><div class="cSelectTollBSh"><div class="cSelectTollBg" id="'+settings.id+'-selectshow" style="overflow-y:auto; overflow-x:hidden; height:auto; _height:'+settings.viewHeight+'; max-height:'+settings.viewHeight+'; width:'+(parseInt(settings.selectWidth.substr(0,settings.selectWidth.indexOf("px",0)))+11)+'px; display:none; position: relative; left:'+settings.viewLeft+'; top:0;';if(settings.cClass!="cust_wh")
tmp+='">';else
tmp+='padding-right: 14px">';tmp+='</div>';tmp+='</div></div></div></div></div>';if(settings.cClass=="cust_wh")
tmp+='</div>';var _handler=function(){$(this).html(tmp);bindInputEvents();bindArrowClick();bindSelectMouseover();bindSelectMouseleave();};var bindInputEvents=function(){var homeland=$('#djs_'+settings.id);var selectId=settings.id;var isSelected=true;homeland.bind("keypress",function(e){var charCode=(e.which)?e.which:e.keyCode;if(charCode==9||charCode==8)
return true;if(focusedJSDD!="none")
{if(charCode==40||charCode==34||charCode==33||charCode==38)
return $(document).trigger(e);else
return true;}
var optIndex=0;var select_name=settings.id;if(!select_name.indexOf('cust_'))
{select_name=select_name.substr(5,select_name.length);}
var select=document.getElementById(select_name);for(var i=0;i<select.length;i++)
{if(select.options[i].selected)
{optIndex=i;break;}}
var selectItems=$("#"+settings.id+"-selectshow").children('.selectitem');if(charCode==40||charCode==34)
{var counter=0;for(var i=0;i<selectItems.length;i++)
{if(counter==optIndex){$(selectItems[i+1]).click();break;}
counter++;}}
else if(charCode==38||charCode==33)
{var counter=0;for(var i=0;i<selectItems.length;i++)
{if(optIndex==0)
break;if(counter==optIndex){$(selectItems[i-1]).click();break;}
counter++;}}
else if(charCode==35)
{var counter=0;$(selectItems[selectItems.length-1]).click();}
else if(charCode==36)
{var counter=0;var selectItems=$("#"+settings.id+"-selectshow").children('.selectitem');$(selectItems[0]).click();}
else if((charCode>=49&&charCode<=57)||(charCode>=65&&charCode<=122)||(charCode>=192&&charCode<=255))
{srchStr+=String.fromCharCode(charCode);clearTimeout(t);t=setTimeout("srchStr = ''; ",1000);selectJSDDItem(settings.id);if(eventTracker=="mouse")
{eventTracker="key";}
return true;}
if(eventTracker=="mouse")
{eventTracker="key";}
return false;});};var bindArrowClick=function(){var tmp=$('#'+settings.id+'-Text');var expand=$("#"+settings.id+'-select');var arr=$('#'+settings.id+'-Text');$("#"+settings.id+'-Text').bind("click",function(e){var obj=$('#'+settings.id+'-selectshow');var optIndex=0;$('.selectitem').css({'background':'none','color':'#7a7a7a','left':'0','padding':'0'});var select_name=settings.id;if(!select_name.indexOf('cust_'))
{select_name=select_name.substr(5,select_name.length);}
var select=document.getElementById(select_name);for(var i=0;i<select.length;i++)
{if(select.options[i].selected)
{optIndex=i;break;}}
var scrollingElem=document.getElementById(settings.id+'-selectshow');var selectItems=$(".selectitem");var counter=0;for(var i=0;i<selectItems.length;i++)
{if(selectItems[i].parentNode.id==settings.id+'-selectshow')
{if(counter==optIndex){$(selectItems[i]).css({'background-color':'#BBB','color':'#fff','left':'-5px','padding':'0 2px 0 5px','width':'100%','position':'relative'});focusedJSDDElem=selectItems[i];break;}
counter++;}}
if(obj.css('display')=='none')
{var test=$('.cSelectTollBg');test.slideUp(settings.animalSpeed,function(){test.hide();});var tollUp=$('.cSelectTollUp');tollUp.css({'display':'none'});var hidee=$('.cSelectTollRSh');hidee.css({'display':'none'});var tollRSh=$('#'+settings.id+'-TollRSh');tollRSh.css({'display':'block'});obj.slideDown(settings.animalSpeed,function(){var expander=$('#'+settings.id+'-expcont');expander.css({'display':'block'});var expanderTop=$('#'+settings.id+'-cSelectTollUp');expanderTop.css({'display':'block'});var topSh=$('#'+settings.id+'-topSh');topSh.css({'display':'block'});obj.show();obj.css('overflow','auto');obj.css('overflow-x','hidden');var topPos=selectItems[counter].offsetTop-75;scrollingElem.scrollTop=topPos;$('body').addClass('changed');});expand.addClass("cSelectExp");$('.cSelectClick').toggleClass('cSelectClick');focusedJSDD=settings.id;}
else
{obj.slideUp(settings.animalSpeed,function(){obj.hide();var cSelectTollUp=$('#'+settings.id+'-cSelectTollUp');cSelectTollUp.css({'display':'none'});});if(eventTracker=="mouse")
{eventTracker="key";}
focusedJSDD="none";document.getElementById('djs_'+settings.id).focus();}
$('.cSelectClick').addClass('cSelectClick');arr.toggleClass("cSelectClick");if(arr.is('.cSelectClick')&&obj.css('display')=='none')
{var test=$('.cSelectTollBg');test.slideUp(settings.animalSpeed,function(){});obj.slideDown(settings.animalSpeed,function(){var expander=$('#'+settings.id+'-expcont');expander.css({'display':'block'});var topSh=$('#'+settings.id+'-topSh');topSh.css({'display':'block'});obj.show();obj.css('overflow','auto');obj.css('overflow-x','hidden');});var topPos=selectItems[counter].offsetTop-75;scrollingElem.scrollTop=topPos;expand.addClass("cSelectExp");$('.cSelectClick').removeClass('cSelectClick');arr.toggleClass("cSelectClick");focusedJSDD=settings.id;}});};var bindItemMouseover=function(){var expand=$("#"+settings.id+'-select');var inx=0;while($(".selectitem",$("#"+settings.id+"-selectshow")).get(inx)!=null)
{var item=$(".selectitem",$("#"+settings.id+"-selectshow")).get(inx);$(item).bind("mouseover",function(e){if(eventTracker=="key")
{eventTracker="mouse";$(focusedJSDDElem).css({'background':'none','color':'#7a7a7a','left':'0','padding':'0'});}
$('body').addClass('changed');focusedJSDDElem=this;});$(item).bind("click",function(e){var obj=$('#'+settings.id+'-selectshow');if(obj.css('display')=='block'){obj.slideUp(settings.animalSpeed,function(){obj.hide();var cSelectTollUp=$('#'+settings.id+'-cSelectTollUp');cSelectTollUp.css({'display':'none'});});var arr=$('#'+settings.id+'-Text');arr.toggleClass("cSelectClick");}
if(eventTracker=="mouse")
{eventTracker="key";}
focusedJSDD="none";document.getElementById('djs_'+settings.id).focus();var tmpstr=$(".thistext",$(this)).html();var arr=$().getIndexByByte(tmpstr,settings.selectWidth/5);if(arr[0]==true)
tmpstr=tmpstr.substring(0,arr[1])+'...';$(".textshow",$("#"+settings.id+"-Text")).html(tmpstr);$(".valueshow",$("#"+settings.id+"-Text")).html($(".selectvalue",$(this)).html());var cV=$(".valueshow",$("#"+settings.id+"-Text")).html();var cF="<script>function setValueSelect_"+settings.id+"(v){\n var select_name = '"+settings.id+"';\n if(!select_name.indexOf('cust_')){ select_name = select_name.substr(5, select_name.length);}\nvar select = document.getElementById(select_name);\nfor(i=0;i<select.length;i++){\nif(select.options[i].value==v){\nselect.options[i].selected = true;\nbreak;\n\n}\n}\nif(select.id == 'select_case_categories_id') select.onchange();\n}\nsetValueSelect_"+settings.id+"('"+cV+"')</script>";$(".valueshow",$("#"+settings.id+"-Text")).innerHTML=$(".valueshow",$("#"+settings.id+"-Text")).html(cV+cF);});inx++;}}
var bindSelectMouseover=function(){$('#'+settings.id+'-Text').bind("mouseover",function(){if($.browser.msie==false)
$('#'+settings.id+'-Text').css("background-position","0 0");});}
var bindSelectMouseleave=function(){$('#'+settings.id+'-Text').bind("mouseout",function(){if($.browser.msie==false)
$('#'+settings.id+'-Text').css("background-position","0 0px");});}
this.setViewTop=function(top){$('#'+settings.id+'-selectshow').css('top',top+'px');}
this.setViewLeft=function(left){$('#'+settings.id+'-selectshow').css('left',left+'px');}
this.getLength=function(){return $('.selectitem',$('#'+settings.id+'-selectshow')).length;}
this.addItem=function(itemhtml){$("#"+settings.id+'-selectshow').html($("#"+settings.id+'-selectshow').html()+itemhtml);bindItemMouseover();};this.removeItem=function(index){if($('.selectitem',$('#'+settings.id+'-selectshow')).length>index)
$($('.selectitem',$('#'+settings.id+'-selectshow')).get(index)).remove();if($('.selectborder',$('#'+settings.id+'-selectshow')).length>index)
$($('.selectborder',$('#'+settings.id+'-selectshow')).get(index)).remove();}
this.getValue=function(){var text='romay';return $('.valueshow',$('#'+settings.id+'-Text')).html();}
this.getText=function(){return $('.textshow',$('#'+settings.id+'-Text')).html();}
return this.each(_handler);}});})(jQuery);
