(function($){$.cpSelectBox={currentUrlParams:null,numPerPage:20,activeSelect:null,popupWrapper:null,lockMenu:false,popupMouseOver:false,firstPopupMouseUp:true,scrollBarWidth:null,o:{widthHeightMode:"client",optionBorderHeight:0,additionalWrapperWidth:0,popupStretch:true,popupStretchMaxWidth:500,popupMarginTop:0,popupExtraWidth:0,selectWrapPosition:"select",selectInputParentClass:null},buildSelectBox:function(o){return this.each(function(){var fhWrapper=this;fhWrapper.o=$.extend(true,{},$.cpSelectBox.o);fhWrapper.o=$.extend(fhWrapper.o,o||{});var origSelect=(fhWrapper.tagName=="SELECT"?fhWrapper:$(fhWrapper).find("select").get(0));if(origSelect==undefined){return false}fhWrapper.origSelect=origSelect;fhWrapper.sstatus=0;fhWrapper.inputFocused=false;setTimeout(function(){$.cpSelectBox.chooseOption(fhWrapper,0)},1);var selectWrap;if(fhWrapper.o.selectWrapPosition=="select"){$(origSelect).wrap('<div style="width: '+$.cpSelectBox.getWidth(fhWrapper,origSelect)+'px;" />');selectWrap=$(origSelect).parent()}else if(fhWrapper.o.selectWrapPosition=="fhWrapper"){$(fhWrapper).wrap('<div />');selectWrap=$(fhWrapper).parent()}else{$(fhWrapper).find("."+fhWrapper.o.selectWrapPosition).wrap('<div />');selectWrap=$(fhWrapper).find("."+fhWrapper.o.selectWrapPosition).parent()}selectWrap.css({"position":"relative"});fhWrapper.selectWrap=selectWrap;selectWrap.addClass("sb_selectWrap");selectWrap.wrap('<div />');fhWrapper.allWrap=selectWrap.parent();var selectInput=document.createElement("INPUT");$(selectInput).attr({type:"text",value:"",tabindex:"-1",style:"cursor: default;"}).addClass("fhIgnore");(fhWrapper.o.selectInputParentClass!=null?$(fhWrapper).find("."+fhWrapper.o.selectInputParentClass):selectWrap).append(selectInput);fhWrapper.selectInput=selectInput;var siWrap;if(fhWrapper.o.selectWrapPosition=="select"){$(selectInput).wrap('<div class="cpSB_selectWrapOuter"><div class="cpSB_selectWrapInner" style="position: relative;"></div></div>');siWrap=$(selectInput).parent()}else{siWrap=selectWrap}var siOverlay=document.createElement("DIV");$(siOverlay).html("<div class='cpSB_dropdownIcon'><hr style='display: none;' /></div>");$(siOverlay).attr({style:"position: absolute; width: 100%; left: 0; top: 0;"}).addClass("cpSB_inputOverlay");setTimeout(function(){$(siOverlay).css("height",$.cpSelectBox.getHeight(fhWrapper,selectInput)+"px")},1);siWrap.append(siOverlay);var hControllerInput=document.createElement("INPUT");$(hControllerInput).attr({type:"text",style:"position: absolute; left: -9999px; top: 0; width: 1px; height: 1px; border: 0;"}).addClass("fhIgnore");selectWrap.append(hControllerInput);fhWrapper.hControllerInput=hControllerInput;$(origSelect).css("display","none");$(origSelect).attr({tabindex:"-1"});$(selectInput).focus(function(){setTimeout(function(){$(hControllerInput).focus()},1)});$(siOverlay).mousedown(function(e){if((!$.browser.msie&&e.button==0)||($.browser.msie&&e.button==1)){$.cpSelectBox.openPopup(fhWrapper);$.cpSelectBox.firstPopupMouseUp=false;$('body').unbind('mouseup',$.cpSelectBox.mouseUpHandler).bind('mouseup',$.cpSelectBox.mouseUpHandler);setTimeout(function(){$(hControllerInput).focus()},1)}return false});$(selectInput).mousedown(function(e){if((!$.browser.msie&&e.button==0)||($.browser.msie&&e.button==1)){$.cpSelectBox.openPopup(fhWrapper);$.cpSelectBox.firstPopupMouseUp=false;$('body').unbind('mouseup',$.cpSelectBox.mouseUpHandler).bind('mouseup',$.cpSelectBox.mouseUpHandler)}setTimeout(function(){$(hControllerInput).focus()},1);return false});$(selectInput).keypress(function(){setTimeout(function(){$(hControllerInput).focus()},1)});$(hControllerInput).focus(function(){var oldXScroll=$(window).scrollLeft();fhWrapper.inputFocused=true;clearTimeout(fhWrapper.closePopupTimer);if($.cpSelectBox.activeSelect!=null&&$.cpSelectBox.activeSelect!=selectWrap){$.cpSelectBox.activeSelect.removeClass("cpSB_inputActive");$.cpSelectBox.activeSelect=selectWrap;selectWrap.addClass("cpSB_inputActive")}else if($.cpSelectBox.popupWrapper==null){selectWrap.addClass("cpSB_inputActive")}$(hControllerInput).val("");if(oldXScroll>0){setTimeout(function(){$(window).scrollLeft(oldXScroll)},1)}});$(hControllerInput).blur(function(){fhWrapper.inputFocused=false;if($.cpSelectBox.popupWrapper==null){selectWrap.removeClass("cpSB_inputActive")}fhWrapper.closePopupTimer=setTimeout(function(){$.cpSelectBox.closePopup(fhWrapper)},100)});$(hControllerInput).keydown(function(e){var preventAction=true;if($.cpSelectBox.popupWrapper!=null&&$.cpSelectBox.firstPopupMouseUp&&((e.keyCode>=33&&e.keyCode<=40)||e.keyCode==13)){fhWrapper.chosen=(+$(fhWrapper.sbActive).attr("sbI"))}switch(e.keyCode){case 27:if($.cpSelectBox.popupWrapper!=null){$.cpSelectBox.closePopup(fhWrapper)}break;case 13:if($.cpSelectBox.popupWrapper!=null){$.cpSelectBox.chooseOption(fhWrapper,fhWrapper.chosen);$.cpSelectBox.closePopup(fhWrapper)}break;case 38:case 37:case 40:case 39:$.cpSelectBox.chooseOption(fhWrapper,fhWrapper.chosen+(e.keyCode<=38?-1:1));break;case 36:$.cpSelectBox.chooseOption(fhWrapper,0);break;case 35:$.cpSelectBox.chooseOption(fhWrapper,origSelect.length-1);break;case 33:$.cpSelectBox.chooseOption(fhWrapper,(fhWrapper.chosen>($.cpSelectBox.numPerPage-1)?(fhWrapper.chosen-($.cpSelectBox.numPerPage-1)):0));break;case 34:$.cpSelectBox.chooseOption(fhWrapper,(fhWrapper.chosen<(origSelect.length-($.cpSelectBox.numPerPage-1))?(fhWrapper.chosen+($.cpSelectBox.numPerPage-1)):(origSelect.length-1)));break;default:var val=$(this);if(fhWrapper.sstatus<3){timeout=setTimeout(function(){var valText=val.val().toLowerCase();if(valText!=""){if(fhWrapper.sstatus==0){fhWrapper.sstatus=1;if($.cpSelectBox.popupWrapper!=null){fhWrapper.origChosen=(+$(fhWrapper.sbActive).attr("sbI"))}else{fhWrapper.origChosen=fhWrapper.chosen}fhWrapper.firstAlphaPos=-1;var matches=$(origSelect).find("option:isw('"+valText.charAt(0)+"')");fhWrapper.alphaPos=new Array();if(matches.length>0){matches.each(function(i){var ti=$(this).prevAll().size();fhWrapper.alphaPos.push(ti);if(fhWrapper.firstAlphaPos==-1&&ti>fhWrapper.origChosen){fhWrapper.firstAlphaPos=i}});if(fhWrapper.firstAlphaPos==-1){fhWrapper.firstAlphaPos=0}}}if(fhWrapper.sstatus==1&&valText.length>1&&valText.charAt(valText.length-1)!=valText.charAt(valText.length-2)){fhWrapper.sstatus=2}if(fhWrapper.sstatus==1){if(fhWrapper.alphaPos.length>0){$.cpSelectBox.chooseOption(fhWrapper,fhWrapper.alphaPos[((fhWrapper.firstAlphaPos+valText.length-1)%fhWrapper.alphaPos.length)])}else{fhWrapper.sstatus=3}}else{var matches=$(origSelect).find("option:isw('"+valText+"')");if(matches.length>0){var matchPos;if(matches.length>1){matches.each(function(i){var ti=$(this).prevAll().size();if(i==0){matchPos=ti}if(ti>fhWrapper.origChosen){matchPos=ti;return false}})}else{matchPos=matches.prevAll().size();fhWrapper.sstatus=3}$.cpSelectBox.chooseOption(fhWrapper,matchPos)}else{fhWrapper.sstatus=3}}}},1)}clearTimeout(fhWrapper.resetTypeTimer);fhWrapper.resetTypeTimer=setTimeout(function(){val.val("");fhWrapper.sstatus=0},1000);preventAction=false;break}if(preventAction){e.returnValue=false;e.cancelBubble=true;return false}})})},chooseOption:function(fhWrapper,index,updatePopup){if($.cpSelectBox.popupWrapper==null||$.cpSelectBox.firstPopupMouseUp){if(index<0){index=0}if(index>(fhWrapper.origSelect.length-1)){index=(fhWrapper.origSelect.length-1)}fhWrapper.chosen=index;fhWrapper.origSelect.selectedIndex=index;$(fhWrapper.selectInput).val(fhWrapper.origSelect[fhWrapper.origSelect.selectedIndex].text);if($.cpSelectBox.popupWrapper!=null&&updatePopup!==false){var puDiv=$(fhWrapper.popupBoxInner).find("div:eq("+index+")");$(fhWrapper.sbActive).removeClass("cpSB_puActive");puDiv.addClass("cpSB_puActive");fhWrapper.sbActive=puDiv.get(0);var docViewTop=$(fhWrapper.popupBoxInner).scrollTop();var docViewBottom=docViewTop+(fhWrapper.oHeight*$.cpSelectBox.numPerPage)-1;var elemTop=(fhWrapper.oHeight*index);var elemBottom=elemTop+fhWrapper.oHeight-1;if(!((elemTop>=docViewTop)&&(elemBottom<=docViewBottom))){var scrollPos=elemTop;if(elemTop>docViewTop){scrollPos=elemBottom-((fhWrapper.oHeight*$.cpSelectBox.numPerPage)-1);if(index==(fhWrapper.origSelect.length-1)){scrollPos=(fhWrapper.oHeight*fhWrapper.origSelect.length)+5}}else if(index==0){scrollPos=0}$(fhWrapper.popupBoxInner).scrollTop(scrollPos)}}}},closePopup:function(fhWrapper){if($.cpSelectBox.lockMenu!==true){clearTimeout(fhWrapper.closePopupTimer);$(fhWrapper.popupBox).css({"display":"none"});if($.cpSelectBox.activeSelect!=fhWrapper.selectWrap){fhWrapper.selectWrap.removeClass("cpSB_inputActive")}else{$.cpSelectBox.popupWrapper=null;$('body').unbind('mousewheel',$.cpSelectBox.mouseWheelHandler);if(fhWrapper.inputFocused){fhWrapper.selectWrap.addClass("cpSB_inputActive")}}}},openPopup:function(fhWrapper){if($.cpSelectBox.popupWrapper!=null){if($.cpSelectBox.popupWrapper==fhWrapper){$.cpSelectBox.closePopup($.cpSelectBox.popupWrapper);return false}else{$.cpSelectBox.closePopup($.cpSelectBox.popupWrapper)}}if($.cpSelectBox.activeSelect!=null&&$.cpSelectBox.activeSelect!=fhWrapper.selectWrap){$.cpSelectBox.activeSelect.removeClass("cpSB_inputActive")}$.cpSelectBox.activeSelect=fhWrapper.selectWrap;$.cpSelectBox.popupWrapper=fhWrapper;fhWrapper.selectWrap.removeClass("cpSB_inputActive");var popupBox=fhWrapper.popupBox;if(popupBox==undefined){popupBox=document.createElement("DIV");$(popupBox).attr({style:"position: absolute; "+(!fhWrapper.o.popupStretch?("width: "+($.cpSelectBox.getWidth(fhWrapper,fhWrapper)+fhWrapper.o.popupExtraWidth)+"px; "):"")+"margin-top: "+($.cpSelectBox.getHeight(fhWrapper,fhWrapper.selectInput)+fhWrapper.o.popupMarginTop)+"px;"}).addClass("cpSB_popup");var popupBoxInner=document.createElement("DIV");$(popupBoxInner).attr({style:"overflow-x: hidden; overflow-y: auto; white-space: nowrap;"}).addClass("cpSB_popupInner");$(popupBox).append(popupBoxInner);$(fhWrapper.origSelect).find("option").each(function(i){var selOption=document.createElement("DIV");if(fhWrapper.o.optionBorderHeight!=0){var spanWrap=document.createElement("SPAN");$(spanWrap).css({"display":"block"});$(spanWrap).html($(this).text());$(selOption).append(spanWrap)}else{$(selOption).html($(this).text())}if(this.selected){$(selOption).addClass("cpSB_puActive");fhWrapper.sbActive=selOption}$(selOption).mouseover(function(){$(fhWrapper.sbActive).removeClass("cpSB_puActive");$(this).addClass("cpSB_puActive");fhWrapper.sbActive=this});$(selOption).mouseup(function(){if($.cpSelectBox.firstPopupMouseUp){$.cpSelectBox.chooseOption(fhWrapper,i);$.cpSelectBox.lockMenu=false;$.cpSelectBox.closePopup(fhWrapper)}});$(selOption).attr({style:"cursor: default;","sbi":i});$(popupBoxInner).append(selOption)});fhWrapper.selectWrap.before($(popupBox));fhWrapper.oHeight=$.cpSelectBox.getHeight(fhWrapper,$(popupBoxInner).find((fhWrapper.o.optionBorderHeight!=0?"span":"div")+":eq(0)").get(0))+fhWrapper.o.optionBorderHeight;$(popupBoxInner).css({"height":(fhWrapper.oHeight*(fhWrapper.origSelect.length>$.cpSelectBox.numPerPage?$.cpSelectBox.numPerPage:fhWrapper.origSelect.length))+"px"});$(popupBox).mouseover(function(){$.cpSelectBox.popupMouseOver=fhWrapper});$(popupBox).mouseout(function(){$.cpSelectBox.popupMouseOver=false});$(popupBox).mousedown(function(evt){$.cpSelectBox.lockMenu=true;setTimeout(function(){$(fhWrapper.hControllerInput).focus()},1);setTimeout(function(){$.cpSelectBox.lockMenu=false},125);$('body').unbind('mouseup',$.cpSelectBox.mouseUpHandler).bind('mouseup',$.cpSelectBox.mouseUpHandler);return false});fhWrapper.popupBox=popupBox;fhWrapper.popupBoxInner=popupBoxInner;if(fhWrapper.o.popupStretch){var curWidth=$(popupBox).width();var minWidth=($.cpSelectBox.getWidth(fhWrapper,fhWrapper)+fhWrapper.o.popupExtraWidth)-fhWrapper.o.additionalWrapperWidth;var isScroll=(fhWrapper.origSelect.length>$.cpSelectBox.numPerPage);var newWidth=null;if(isScroll){if($.cpSelectBox.scrollBarWidth==null){$.cpSelectBox.scrollBarWidth=getScrollbarWidth()}newWidth=curWidth+$.cpSelectBox.scrollBarWidth;if(newWidth<minWidth){newWidth=minWidth}}else if(curWidth<minWidth){newWidth=minWidth}if(newWidth!=null){$(popupBoxInner).css("width",(fhWrapper.o.popupStretchMaxWidth&&newWidth>fhWrapper.o.popupStretchMaxWidth?fhWrapper.o.popupStretchMaxWidth:newWidth)+"px")}}}else{$(popupBox).css({"display":"block"});popupBoxInner=fhWrapper.popupBoxInner}var container={top:parseInt($(window).scrollTop()),left:parseInt($(window).scrollLeft()),height:$(window).height(),width:$(window).width()};if(popupBox.offHeight==undefined){popupBox.offHeight=parseInt($(popupBox).offset().top+0.5);popupBox.boxHeight=$.cpSelectBox.getHeight(fhWrapper,popupBox);popupBox.boxWidth=$(popupBox).width()}popupBox.offWidth=parseInt($(popupBox).offset().left+0.5);if((popupBox.offHeight+popupBox.boxHeight)>(container.height+container.top)){$(popupBox).css({"margin-top":($.cpSelectBox.getHeight(fhWrapper,fhWrapper.selectInput)+fhWrapper.o.popupMarginTop+((container.height+container.top)-(popupBox.offHeight+popupBox.boxHeight)))+"px"})}else{$(popupBox).css({"margin-top":($.cpSelectBox.getHeight(fhWrapper,fhWrapper.selectInput)+fhWrapper.o.popupMarginTop)+"px"})}if((popupBox.offWidth+popupBox.boxWidth-parseInt($(popupBox).css("margin-left")))>(container.width+container.left)){$(popupBox).css({"margin-left":((container.width+container.left)-(popupBox.offWidth+popupBox.boxWidth-parseInt($(popupBox).css("margin-left"))))+"px"})}else{$(popupBox).css({"margin-left":0})}var puDiv=$(popupBoxInner).find("div:eq("+fhWrapper.origSelect.selectedIndex+")");$(fhWrapper.sbActive).removeClass("cpSB_puActive");$(puDiv).addClass("cpSB_puActive");fhWrapper.sbActive=puDiv;$(popupBoxInner).scrollTop(fhWrapper.oHeight*fhWrapper.chosen);$('body').unbind('mouseup',$.cpSelectBox.mouseUpHandler).bind('mouseup',$.cpSelectBox.mouseUpHandler).unbind('mousewheel',$.cpSelectBox.mouseWheelHandler).bind('mousewheel',$.cpSelectBox.mouseWheelHandler)},mouseWheelHandler:function(event,delta){if($.cpSelectBox.popupMouseOver!==false){var fhWrapper=$.cpSelectBox.popupMouseOver;var scrollPos=$(fhWrapper.popupBoxInner).scrollTop()+(-3*fhWrapper.oHeight*delta);if(scrollPos<5){scrollPos=0}else if(scrollPos>($(fhWrapper.popupBoxInner).scrollTop()+(fhWrapper.oHeight*$.cpSelectBox.numPerPage)-5)){scrollPos=(fhWrapper.oHeight*fhWrapper.origSelect.length)+5}$(fhWrapper.popupBoxInner).scrollTop(scrollPos)}return false},mouseUpHandler:function(){if(!$.cpSelectBox.firstPopupMouseUp){if($.cpSelectBox.popupWrapper!=null){$($.cpSelectBox.popupWrapper.hControllerInput).focus()}setTimeout(function(){$.cpSelectBox.firstPopupMouseUp=true},25)}$.cpSelectBox.lockMenu=false;$('body').unbind('mouseup',$.cpSelectBox.mouseUpHandler)},getWidth:function(fhWrapper,item){return(fhWrapper.o.widthHeightMode=="client"?item.offsetWidth:$(item).width())},getHeight:function(fhWrapper,item){return(fhWrapper.o.widthHeightMode=="client"?item.offsetHeight:$(item).height())}};$.fn.buildSelectBox=$.cpSelectBox.buildSelectBox})(jQuery);jQuery.expr[':'].isw=function(a,i,m){return(a.textContent||a.innerText||"").toLowerCase().indexOf(m[3].toLowerCase())==0};function getScrollbarWidth(){var div=$('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');$('body').append(div);var w1=$('div',div).innerWidth();div.css('overflow-y','scroll');var w2=$('div',div).innerWidth();$(div).remove();return(w1-w2)}(function($){var types=['DOMMouseScroll','mousewheel'];$.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var i=types.length;i;){this.addEventListener(types[--i],handler,false)}}else{this.onmousewheel=handler}},teardown:function(){if(this.removeEventListener){for(var i=types.length;i;){this.removeEventListener(types[--i],handler,false)}}else{this.onmousewheel=null}}};$.fn.extend({mousewheel:function(fn){return fn?this.bind("mousewheel",fn):this.trigger("mousewheel")},unmousewheel:function(fn){return this.unbind("mousewheel",fn)}});function handler(event){var orgEvent=event||window.event,args=[].slice.call(arguments,1),delta=0,returnValue=true,deltaX=0,deltaY=0;event=$.event.fix(orgEvent);event.type="mousewheel";if(event.wheelDelta){delta=event.wheelDelta/120}if(event.detail){delta=-event.detail/3}deltaY=delta;if(orgEvent.axis!==undefined&&orgEvent.axis===orgEvent.HORIZONTAL_AXIS){deltaY=0;deltaX=-1*delta}if(orgEvent.wheelDeltaY!==undefined){deltaY=orgEvent.wheelDeltaY/120}if(orgEvent.wheelDeltaX!==undefined){deltaX=-1*orgEvent.wheelDeltaX/120}args.unshift(event,delta,deltaX,deltaY);return $.event.handle.apply(this,args)}})(jQuery);
