/* ------------------------------------------------------------------------
SelectBox
------------------------------------------------------------------------- */
var SelectBox = Class.create( {
	id:'',
	_options:[],
	_selectedcontent:'',
	_selectedInput:null,
	_selectedIndex:0,
	_selectedvalue:null,
	_length:0,
	_select:null,
	_selectOptsWrap:null,
	_selectBtn:null,
	_mouseuplock:true,
	initialize : function(config) {
		this.id=config.id;
		
		this._select=$('#'+this.id);
		this._selectedvalue=config.selectedValue;
		this._selectedcontent=$('#'+this.id+' .selectbox_opts_selected a');
		this._selectOptsWrap=$('#'+this.id+' .selectbox_opts');
		this._selectBtn=$('#'+this.id+' .selectbox_btn_wrap a');
		this._options=$('#'+this.id+' .selectbox_opts a');
		this._length=this._options._length;
		this._selectedInput=$('#'+this.id+' .selectbox_opts_selected input');
		if(config.onchange!=null)this.onchange=config.onchange;
		
		var owner=this;
		
		this._selectBtn.bind("mousedown", function(){
			owner._mouseuplock=true;
			owner.toggleList();
		});
		this._selectedcontent.bind("mousedown", function(){
			owner._mouseuplock=true;
			owner.toggleList();
		});
		this._selectBtn.bind("mouseup", function(){
			owner._mouseuplock=false;
		});
		this._selectedcontent.bind("mouseup", function(){
			owner._mouseuplock=false;
		});
		
		this._options.each(function(i){
			if(owner._selectedvalue==$(this).attr('alt'))
				owner._selectedIndex=i;
			$(this).attr('title',i);
			$(this).bind("mousedown", function(){
				owner._mouseuplock=true;
				owner._selectedIndex=$(this).attr('title');
	    		owner.select();
	    		owner.onchange(owner._selectedIndex,$(this).attr('alt'));
	    		owner.hideList();
			});
		});
		$('body').bind("mousedown", function(){
			if(!owner._mouseuplock) owner.hideList();
		});
		
		this.select();
	},
	toggleList:function(){
		if(this._selectOptsWrap.css('display')=='none'){
			this._selectOptsWrap.show();
		}else{
			this._selectOptsWrap.hide();
		}
	},
	hideList:function(){
			this._selectOptsWrap.hide();
	},
	select:function(){
		selectOption=$(this._options[this._selectedIndex]);
		selectedValue=selectOption.attr('alt');
		selectedContent=selectOption.html();
		
		this._selectedcontent.attr('alt',selectedValue);
		this._selectedcontent.html(selectedContent);
		this._selectedInput.attr('value',selectedValue);
	},
	onchange:function(index,value){
		
	}
});