var MavSelectBox=new Class({Implements:[Options,Events],options:{allowSplit:true,altClass:"select-box-alt",alternate:false,alternateOdd:false,attachResize:true,container:null,disableClass:"disabled",elem:null,filter:null,fxOptions:{},groupClass:"select-box-options-group",maxShow:null,minShow:3,optionClass:"select-box-opt",selectboxClass:"select-box",selectClass:"selected",selectmenuClass:"select-box-options",separator:"--",showStyles:false,size:1,template:"<span>%s</span>",tmplt_regex:null,useFx:true,onHide:$empty(),onOver:$empty(),onSelect:$empty(),onShow:$empty()},isCrolling:false,container:null,element:null,focused:null,length:0,selected:null,selectedIndex:0,showing:false,textSearch:"",version:"0.5.5",initialize:function(a){this.boundShow=this.show.bind(this);this.boundKey_option=this.key_option.bind(this);this.boundHide=this.hide.bind(this);var b=($type(a)!="object"?{elem:a}:a);if(!$defined(b.elem)){return}this.setOptions(b);if(this.options.filter){this.filter=this.options.filter}this.element=$(this.options.elem);this.elementCopy=this.element.clone().set({id:this.element.get("id"),name:this.element.get("name"),"class":this.element.get("class")});if(!$defined(this.options.container)||(this.container=$(this.options.container))==null){this.container=null}this.optClass="li[class*="+this.options.optionClass+"]";this.create_select();if(this.options.attachResize){window.addEvent("resize",function(){if(this.showing){this.show()}}.bind(this))}},destroy:function(a){if(a){if(this.selected&&$defined(this.selected.retrieve("value"))){$each(this.elementCopy.options,function(b){if(b.value==this.selected.retrieve("value")){b.selected=true}},this)}this.elementCopy.replaces($(this.element.get("id")))}this.remove_events();this.elementSelect.destroy()},ieFocus:function(){this.isCrolling=true},create_select:function(){var a=this.element.getSize();this.eid=this.element.get("id");this.elementSelect=new Element("div",{"class":this.options.selectboxClass,styles:{width:a.x,height:a.y}}).inject(this.element,"after");this.elementDisplay=new Element("a",{href:"javascript:void(0)"}).inject(this.elementSelect,"top");if(this.elementCopy.get("tabindex")!=0){this.elementDisplay.set("tabindex",this.elementCopy.get("tabindex"))}this.elementDisplay.setStyles({height:(a.y-5),"line-height":(a.y-5)});this.add_events();this.elementOptions=new Element("ul",{styles:{width:a.x},opacity:(this.options.useFx?0:1),"class":this.options.selectmenuClass}).inject(this.elementSelect);if(Browser.Engine.trident){this.boundIeFocus=this.ieFocus.bind(this);this.elementOptions.addEvent("mousedown",this.boundIeFocus)}this.fx=this.options.useFx?new Fx.Tween(this.elementOptions,$merge({duration:"200",link:"cancel"},this.options.fxOptions)):null;$each(this.element.getChildren(),this.create_option.bind(this));if(this.options.alternate){this.elementOptions.getElements(this.optClass+":"+(this.options.alternateOdd?"odd":"even")).addClass(this.options.altClass)}(this.selected=this.elementOptions.getElement("li."+this.options.selectClass)).removeClass(this.options.selectClass);this.selectedIndex=this.selected.retrieve("idx");this.elementDisplay.set({html:this.selected.get("html"),"class":(this.options.showStyles?this.selected.get("class"):""),style:(this.options.showStyles?this.selected.get("style"):"")});this.elementOptions.store("coords",this.elementOptions.getCoordinates()).setStyles({visibility:"",display:"none"});this.element=new Element("input",{type:"hidden",value:this.element.get("value"),id:this.element.get("id"),name:this.element.get("name"),"class":this.element.get("class")}).replaces(this.element).set("id",this.eid)},create_option:function(f,b,a){var d=f.get("value"),g=!!(f.selected);var i=(f.get("label")?f.get("label"):(f.get("text")||"&nbsp;"));var e=(f.get("tag")=="optgroup"?" optgroup unselectable":" "+this.options.optionClass)+(g&&!f.disabled?" "+this.options.selectClass:"")+(f.disabled?" "+this.options.disableClass:"");var h=new Element("li",{id:this.eid+"_opt"+b,html:this.filter(i,this.options.tmplt_regex,this.options.template),style:f.get("style"),"class":f.get("class")+e}).store("value",f.get("value")).store("idx",(f.get("tag")!="optgroup"?(++this.length):"")).addEvents({mouseover:this.over.bind(this),mousedown:this.select.bind(this)}).inject(($(a)||this.elementOptions));h.store("coords",h.getCoordinates());if(f.get("tag")=="optgroup"){var c=new Element("ul",{"class":this.options.groupClass}).inject(h);$each(f.getChildren(),function(k,j){this.create_option(k,(b+""+j),c)},this)}if(this.options.allowSplit&&i.match(new RegExp(this.options.separator))){i=i.split(this.options.separator);this.elementOptions.lastChild.set("html",'<span><span class="goleft">'+i[0].trim()+'</span><span class="goright">'+i[1].trim()+'</span><br style="clear:both" /></span>')}},filter:function(c,b,a){return a.replace(/\%s/i,c)},inject:function(b,a){},dispose:function(a){},add_events:function(){this.elementDisplay.addEvents({click:this.boundShow,keydown:this.boundKey_option})},remove_events:function(){this.elementOptions.removeEvent(this.boundIeFocus);this.elementDisplay.removeEvents({click:this.boundShow,keydown:this.boundKey_option})},key_option:function(a){a=new Event(a);if(a.key!="tab"){a.stop();switch(a.key){case"esc":this.hide();break;case"enter":this.select(this.selected);case"tab":this.hide();break;case"up":case"down":if(a.alt){this.show()}this.select(a.key);break;case"shift":case"control":case"alt":break;default:this.search(a.key)}}},search:function(d,b){this.textSearch+=d;var f=this.get_options(),a=false,e=false;for(var c=0;c<f.length;c++){var e=f[c];if((e.get("text")).match(new RegExp("^"+this.textSearch,"i"))){if(this.selected!=e){this.select(e)}a=true;break}}if(a===false){this.textSearch="";if(!b){this.search(d,true)}}},get_options:function(a){return this.elementOptions.getElements((a||this.optClass))},determine:function(a){var c=($type(a)=="element"?(a.get("tag")!="li"?a.getParent("li"):a):this.get_options());if($type(a)!="element"){var b=((this.focused&&this.focused!=this.selected)?this.focused:this.selected).retrieve("idx");c=c.filter(function(d){return(!$(d).hasClass(this.options.disableClass)&&((a=="up"&&$(d).retrieve("idx")<b)||(a=="down"&&$(d).retrieve("idx")>b)))},this);c=c[0]?(a=="up"?c.reverse():c)[0]:c}return c},over:function(b){b=new Event(b);var a=($(b.target).get("tag")!="li"?$(b.target).getParent("li"):$(b.target));if(!a.hasClass(this.options.disableClass)&&!a.hasClass("unselectable")){if($type(this.focused)=="element"){this.focused.removeClass(this.options.selectClass)}(this.focused=a).addClass(this.options.selectClass);this.fireEvent("over")}},select:function(a){var b=($type(a)=="event"?new Event(a).target:a);b=this.determine(b);if(b&&!b.hasClass(this.options.disableClass)&&!b.hasClass("unselectable")){if(this.focused){this.focused.removeClass(this.options.selectClass)}if(this.showing===true){(this.focused=this.selected=b).addClass(this.options.selectClass);this.scroll()}else{this.selected=b}this.element.set("value",this.selected.retrieve("value"));this.selectedIndex=this.selected.retrieve("idx");this.elementDisplay.set({html:this.selected.get("html"),"class":(this.options.showStyles?this.selected.get("class"):"")}).removeClass(this.options.selectClass).removeClass(this.options.altClass);this.fireEvent("select",this.selected);this.hide();if(Browser.Engine.trident){this.isCrolling=false;this.hide()}}},scroll:function(){var a=this.elementOptions.getCoordinates();var c=this.selected.getCoordinates();var b=this.elementOptions.scrollTop;if((b+a.height)<(c.top-a.top+5)){this.elementOptions.scrollTop=(c.top-a.top-a.height+c.height)}else{if((c.top-a.top+c.height)<(b+5)){this.elementOptions.scrollTop=(c.top-a.top)}}},show:function(){var c=this.elementOptions.retrieve("coords");var a=this.elementSelect.getCoordinates(),b=(a.top+a.height);if(this.container){b-=this.container.getStyle("top");a.left-=this.container.getStyle("left")}this.elementOptions.setStyles({display:"",height:"200px",top:b,left:a.left,margin:0});this.scroll();this.showing=true;this.focused=this.selected;this.focused.addClass(this.options.selectClass);this.fireEvent("show");if(this.options.useFx){this.fx.start("opacity",0,1)}this.elementDisplay.focus()},hide:function(a){if(this.isCrolling){this.isCrolling=false;this.elementDisplay.focus()}else{if(this.showing){if(this.options.useFx){this.fx.start("opacity",1,0).chain(function(){this.elementOptions.scrollTop=0;this.elementOptions.setStyle("display","none");if(this.focused){this.focused.removeClass(this.options.selectClass)}this.showing=this.focused=false}.bind(this))}else{this.elementOptions.setStyle("display","none");if(this.focused){this.focused.removeClass(this.options.selectClass)}this.showing=this.focused=false}this.fireEvent("hide")}}this.textSearch=""}});
