var CustomSelectbox=$Class({tagName:"select",_agent:$Agent().navigator(),_object:null,_options:null,_focusTimer:null,_comboTimer:null,_customTimer:null,_simfocus:null,_items:null,_customs:null,_title:"",_selectedIndex:-1,_hoveredIndex:-1,_heightFitted:false,_heightOverflowed:false,_comboChanged:false,_needle:"",_handlers:{box:{focus:function(c){var d=this._object;if(d.source.disabled){return}var a=this.option("classPrefix");this._focusTimer.stop();var b=$Element(d.selectbox);if(b.hasClass(a+"focused")){return}b.addClass(a+"focused");this.fireEvent("focus")},blur:function(b){var c=this._object;var a=this.option("classPrefix");this._focusTimer.start(50,function(){$Element(c.selectbox).removeClass(a+"focused");this._showLayer(false);this.fireEvent("blur")},this)},keydown:function(b){var h=this._object;var f=b.key().keyCode;if(h.source.disabled){return}var d=b.element;if(this.fireEvent("keydown",b)){switch(f){case 38:this.selectAbove(1);this._paintLabel();b.stop();break;case 40:this.selectBelow(1);this._paintLabel();b.stop();break;case 33:this.selectAbove(Infinity);this._paintLabel();b.stop();break;case 34:this.selectBelow(Infinity);this._paintLabel();b.stop();break;case 13:if(this._isVisibleLayer()){this._showLayer(false)}b.stop();break}var e=this._items[this._selectedIndex];if(d==h.combobox){var c=h.combobox.value;this._comboTimer.start(0,function(){var i=h.combobox.value;if(c==i){return}this._comboChanged=true;h.source.selectedIndex=this._pseudoIndex;this._pseudo.value=i;this._paintItem()},this)}else{if(e&&!e.custom){switch(f){case 37:this.selectAbove(1);this._paintLabel();b.stop();break;case 39:this.selectBelow(1);this._paintLabel();b.stop();break;default:var g=String.fromCharCode(f);if(/[a-z0-9 ]/i.test(g)){this._needle+=g.toUpperCase();var a=this._findMatchItem();if(a!=-1){this._setSelectedIndex(a)}b.stop()}this._paintLabel();break}}else{if(this.option("customValue")){var c=this.option("customValue").call(this,e.custom,e.object);this._comboTimer.start(0,function(){e=this._setCustomValue(e);if(c==e.source.value){return}this._comboChanged=true;this._paintLabel()},this)}}}}this._paintItem()},mousedown:function(a){var b=this._object;if(b.source.disabled){return}if(a.element==b.combobox){this._showLayer(false);return}this._showLayer();if(b.combobox){setTimeout(function(){b.combobox.focus()},10)}}},combobox:{keyup:function(a){}},layer:{mousedown:function(b){var d=this._object;if(d.source.disabled){return}var c=cssquery.test(b.element,"input, textarea, button, a");var a=this._getItemIndexFromEvent(b);if(a!=-1&&this._items[a].custom&&c){}else{this._delayCall(0,function(){try{d.box.focus()}catch(f){}})}b.stop()}},list:{mouseover:function(b){var a=this._getItemIndexFromEvent(b);if(a==-1){return}var c=this._items[a];if(c.disable){return}this._addHoveredClass(a)},mousedown:function(b){var a=this._getItemIndexFromEvent(b);if(a==-1){return}if(!this.fireEvent("itemclick",{itemIndex:a,itemObject:c})){return}var c=this._items[a];if(c.disable){return}this._setSelectedIndex(a);this._paintLabel();this._paintItem();if(this.option("closeOnClickItem")&&!c.custom){this._showLayer(false)}}},field:{focus:function(b){var c=b.element;var a=this.option("classPrefix");this._customTimer.start(10,function(){for(;c&&c.tagName;c=c.parentNode){var d=$Element(c);if(d.hasClass(a+"item-selected")){return}if(d.hasClass(a+"list")){return this._showLayer(false)}}},this)}}},$init:function(e,b){var g=this._object={};var f=Math.round(new Date().getTime()*10000)+Math.random()*10000;var d="selectbox("+f+")";this.option({useHtml:false,closeOnClickItem:true,classPrefix:"selectbox-"});this.option(b||{});var a=this.option("classPrefix");g.selectbox=$(e);if(cssquery.test(g.selectbox,"select")){g.selectbox=cssquery("! ."+a+"naked",g.selectbox)[0]}g.source=cssquery("select."+a+"source",g.selectbox)[0];g.box=cssquery("."+a+"box",g.selectbox)[0];g.label=cssquery("."+a+"label",g.selectbox)[0];g.combobox=cssquery("input."+a+"combobox",g.selectbox)[0];var c=this.option("layer");if(c){g.layer=$(c);document.body.insertBefore(g.layer,document.body.firstChild)}else{g.layer=cssquery("."+a+"layer",g.selectbox)[0]}g.list=cssquery.test(g.layer,"ul."+a+"list")?g.layer:cssquery("ul."+a+"list",g.layer)[0];if(g.combobox){g.label.appendChild(g.combobox)}$Element(g.selectbox).removeClass(a+"naked");$Element(g.selectbox).addClass(a+"applied");$Element(g.source).css({position:"absolute",visibility:"hidden",display:"block"});this._processCustoms();this._showLayer(false);this._focusTimer=new Timer("timeout");this._comboTimer=new Timer("timeout");this._customTimer=new Timer("timeout");this._simfocus=new SimulateFocus(g.box);this._bindEvents();this.paint();$Element(g.selectbox).addClass(d);$Element(g.layer).addClass(d);CustomSelectbox._instanceKeys[f]=this},_toRegExp:function(a){return a.replace(/\-/g,"\\-")},_getCustom:function(b){var a=this.option("classPrefix");if(new RegExp("\\b"+this._toRegExp(a)+"item\\-custom\\(([^\\)]+)\\)").test(b.className)){return RegExp.$1}},_processCustoms:function(){var f=this._object;var b=cssquery("> li",f.list);this._custom=[];var e;for(var c=0,a;a=b[c];c++){if(e=this._getCustom(a)){var d=cssquery("input",a)[0];this._custom[e]=a.cloneNode(true)}}},_paintPseudoItem:function(){var a=this.option("classPrefix");var c=this._object;if(!c.combobox){return}var b=cssquery("option."+a+"item-pseudo",c.source)[0];if(b){this._pseudo=b;this._pseudoIndex=b.index;return}var b=this._pseudo=$("<option>");this._pseudoIndex=c.source.options.length;b.className=a+"item-pseudo "+a+"invisible";b.innerHTML="콤보박스 항목";c.source.appendChild(b)},_findMatchItem:function(d){var b=this._items.length;var h=this._object.source.selectedIndex;var f=this._needle;var g=-1;if(typeof d=="undefined"){d=0}for(var c=h+d;c<h+b;c++){var a=c%b;var e=this._items[a].found;if(e.indexOf(f)==0){g=a;break}}if(g==-1&&f.length>1){this._needle=f=f.substr(f.length-1);g=this._findMatchItem(1)}return g},_delayCall:function(a,b){setTimeout($Fn(b,this).bind(),a)},_text:function(b){var a=this._options;return a.useHtml?(b.textContent||b.innerText):b.innerHTML},_value:function(a){return a.getAttribute("value")||""},_isVisibleLayer:function(){var a=this._object.layer;return $Element(a).visible()},_showLayer:function(b){var a=this.option("classPrefix");var c=$Element(this._object.layer);var d=c.visible();if(typeof b=="undefined"){b=!d}$Element(this._object.selectbox)[b?"addClass":"removeClass"](a+"expanded");if(b!=d&&!this.fireEvent(b?"open":"close")){return}c[b?"show":"hide"]();if(b){this._paintHeight();this._paintItem();this._paintLayer()}else{this._fireChangeEvent()}},_fireChangeEvent:function(){var b=this._object;if(this._comboChanged){this._comboChanged=false;this._onCustomSelect();var a=b.source.selectedIndex;$Element(b.source).fireEvent("change");this.fireEvent("change",{custom:this._items[a].custom})}},_onCustomSelect:function(){var d=this._object;var a=this.option("classPrefix");if(!$Element(d.selectbox).hasClass(a+"focused")){return}var c=this._setCustomValue();if(!c){return}if(!this._isVisibleLayer()){this._showLayer(true)}var b=this;if(this.option("customSelect")){setTimeout(function(){b.option("customSelect").call(b,c.custom,c.object)},0)}},_setCustomValue:function(a){var c=this._object;if(!a){a=this._items[c.source.selectedIndex]}if(!a.custom){return}var b=this.option("customValue").call(this,a.custom,a.object);a.source.value=b;return a},_getItemIndexFromEvent:function(a){var b=a.element;if(b.tagName.toLowerCase()!="li"){b=cssquery("! li",b)[0]}return this._getItemIndex(b)},_getItemIndex:function(c){var a=this.option("classPrefix");if(c&&new RegExp("\\b"+this._toRegExp(a)+"item\\(([0-9]+)\\)").test(c.className)){var b=parseInt(RegExp.$1);return isNaN(b)?-1:b}return -1},selectAbove:function(a){return this._addSelectedIndex(-a)},selectBelow:function(a){return this._addSelectedIndex(a)},_addSelectedIndex:function(f){var e=this._object;var d=-1;var b=this._selectedIndex;var c=b+f;var a=this._items.length;if(c<0){c=0}if(c>a-1){c=a-1}if(b<c){for(d=c;d<a&&this._items[d].disable;d++){}if(d>=a){for(d=c;d>=b&&this._items[d].disable;d--){}}}else{for(d=c;d>=0&&this._items[d].disable;d--){}if(d<0){for(d=c;d<=b&&this._items[d].disable;d++){}}}this._setSelectedIndex(d)},_setSelectedIndex:function(a){var b=this._object;if(b.source.selectedIndex==a){return}b.source.selectedIndex=a;this._comboChanged=true;this._fireChangeEvent()},_bindEvents:function(){var c=this._object;this._fnHandlers={};for(var b in this._handlers){for(var a in this._handlers[b]){if(typeof this._fnHandlers[b]=="undefined"){this._fnHandlers[b]={}}this._fnHandlers[b][a]=$Fn(this._handlers[b][a],this)}}this._simfocus.attach({focus:this._fnHandlers.box.focus.bind(),blur:this._fnHandlers.box.blur.bind(),fieldfocus:this._fnHandlers.field.focus.bind()});this._simfocus.attach(this._agent.opera?"keypress":"keydown",this._fnHandlers.box.keydown.bind());this._fnHandlers.box.mousedown.attach(c.box,"mousedown");this._fnHandlers.combobox.keyup.attach(c.box,"keyup");this._fnHandlers.layer.mousedown.attach(c.layer,"mousedown");this._fnHandlers.list.mouseover.attach(c.list,"mouseover");this._fnHandlers.list.mousedown.attach(c.list,"mousedown")},destroy:function(){var a=this._object;this._simfocus.destroy();this._fnHandlers.box.mousedown.detach(a.box,"mousedown");this._fnHandlers.combobox.keyup.detach(a.box,"keyup");this._fnHandlers.layer.mousedown.detach(a.layer,"mousedown");this._fnHandlers.list.mouseover.detach(a.list,"mouseover");this._fnHandlers.list.mousedown.detach(a.list,"mousedown")},_removeSelectedClass:function(){var b=this.option("classPrefix");var c;var a=this._selectedIndex;if(a==-1){return}if((c=this._items[a])&&c.object){$Element(c.object).removeClass(b+"item-selected")}this._selectedIndex=-1},_addSelectedClass:function(b){var a=this.option("classPrefix");this._removeSelectedClass();var c;if(b==-1){return}if((c=this._items[b])&&c.object){$Element(c.object).addClass(a+"item-selected");this._selectedIndex=b}},_removeHoveredClass:function(){var b=this.option("classPrefix");var c;var a=this._hoveredIndex;if(a==-1){return}if((c=this._items[a])&&c.object){$Element(c.object).removeClass(b+"item-hovered")}this._hoveredIndex=-1},_addHoveredClass:function(b){var a=this.option("classPrefix");this._removeHoveredClass();var c;if(b==-1){return}if((c=this._items[b])&&c.object){$Element(c.object).addClass(a+"item-hovered");this._hoveredIndex=b}},paint:function(){var c=this._object;var a=this.option("classPrefix");this._paintWidth();this._paintPseudoItem();this._paintItems();this._paintLabel();this._paintItem();var b=c.source.disabled;this._simfocus._object.disabled=b;$Element(c.selectbox)[b?"addClass":"removeClass"](a+"disabled");if(b){this._showLayer(false);this._handlers.box.blur.call(this)}},paintValue:function(){this._setCustomValue();this._paintLabel()},_paintWidth:function(){var e=this._object;var a=this._options;var d=function(f){if(typeof f=="string"){if(/^[0-9]+%$/.test(f)){f=Math.round(parseInt(f)*e.source.offsetWidth/100)}else{f=parseInt(f)}}return f};$Element(e.source).show();var c=d(a.boxWidth||a.width);var b=d(a.layerWidth||a.width);$Element(e.source).hide();if(!isNaN(c)){$Element(e.box).css({width:c+"px"})}if(!isNaN(b)){$Element(e.layer).css({width:b+"px"})}},_paintHeight:function(){var a=this.option("height");if(!a||this._heightFitted){return}var b=this._object;if(b.list.offsetHeight>a){b.list.style.height=a+"px";if(this._agent.safari){b.list.style.overflow="scroll";b.list.style.overflowX="hidden"}else{b.list.style.overflow="auto"}this._heightOverflowed=true}this._heightFitted=true},_paintLayer:function(){var b=this._object;var a=this.option("classPrefix");var d=$Element(b.layer);var c=$Element(b.box);var h=c.offset();var e=d.height("margin");var f=c.height("margin");var g=h.top+b.box.offsetHeight+e;var j=(document.documentElement.scrollTop||document.body.scrollTop)+(window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight);var i=g>j;d[i?"addClass":"removeClass"](a+"layer-above");if(this.option("layer")){if(i){b.layer.style.top=h.top-e+"px";b.layer.style.left=h.left+"px"}else{b.layer.style.top=h.top+f+"px";b.layer.style.left=h.left+"px"}}},_paintItems:function(){var d=this._object;var a=this.option("classPrefix");var c=cssquery("option",d.source);this._items=[];d.list.innerHTML="";d.list.style.height="auto";for(var f=0,e;e=c[f];f++){var h=$Element(e);var l=h.hasClass(a+"invisible");var g=l||h.hasClass(a+"item-pseudo")||e.disabled;var b=this._getCustom(e);var j=null;var k=this._text(e)||"";if(!l){if(b&&this._custom[b]){j=this._custom[b].cloneNode(true)}else{j=$("<li>");j.style.cssText=e.style.cssText;j.innerHTML=k}j.className=(e.className||"")+" "+a+"item("+f+")";d.list.appendChild(j)}this._items[f]={index:f,source:e,object:j,text:k,found:k.toUpperCase(),invisible:l,disable:g,custom:b}}this._simfocus.setFriend(d.list);this._heightFitted=false;this._heightOverflowed=false},_paintLabel:function(){var d=this._object;var a=d.source.selectedIndex;if(a==-1){return}var c=this._items[a];if(d.combobox){if(c.custom){d.combobox.value=this._value(c.source)}else{d.combobox.value=this._value(c.source);d.combobox.select()}return}var b=d.source.title;if(!b){b=c.text}$Element(d.label).html(b)},_paintItem:function(){var g=this._object;var b=g.source.selectedIndex;this._customTimer.stop();this._addSelectedClass(b);this._addHoveredClass(b);var e=this._items[b];if(!e){return}var a=e.object;if(a&&this._isVisibleLayer()&&this._heightOverflowed){var h=a.offsetParent;var f=a.offsetTop-a.parentNode.offsetTop;var d=[g.list.scrollTop,g.list.scrollTop+g.list.clientHeight];var c=[f,f+a.offsetHeight];if(d[0]>c[0]){g.list.scrollTop=c[0]}if(d[1]<c[1]){g.list.scrollTop+=c[1]-d[1]}}},focus:function(){this._simfocus.focus()},blur:function(){this._simfocus.blur()}}).extend(nhn.HTMLComponent);CustomSelectbox._instanceKeys={};CustomSelectbox.getInstance=function(c){var b=/\bselectbox\(([0-9]+)\)/;for(var a;c&&(a=c.className);c=c.parentNode){if(b.test(a)){return CustomSelectbox._instanceKeys[RegExp.$1]}}return null};