
function checkParent(t,check){
	while(t.parentNode){
	if(t==check){
	return false
	}
	t=t.parentNode
	}
	return true
} 

function StyledSelect(id,w,val,inputName){

	var id;
	var obj;
	var optionsObj;
	var valueObj;
	var instance;
	var options;
	var w;
	
	this.replaceSelect(id,w);
	this.setup(id);
	this.selectByValue(val);
	
}

StyledSelect.prototype.replaceSelect = function(id,w) {
	
	var s = $(id);
	var options = s.options;
	
	var div = document.createElement('div');
	div.className = 'styledSelect';
	div.id = id;
	
	var valueDiv = document.createElement('div');	valueDiv.className = 'styledSelectValue';	
	var txtDiv = document.createElement('div');	txtDiv.className = 'styledSelectTxt';	
	var btnDiv = document.createElement('div');	btnDiv.className = 'styledSelectBtn';
	
	var input = document.createElement('input'); 
	input.type = 'hidden'; 
	if (s.name) {
		input.name = s.name;
	} else {
		input.name = id;
	}
	input.id = id+'_input';
	
	valueDiv.appendChild(txtDiv);
	valueDiv.appendChild(btnDiv);
	valueDiv.appendChild(input);
		
	valueDiv.onmouseover = function() { $(this).addClassName('hv'); }
	valueDiv.onmouseout = function() { $(this).removeClassName('hv'); }
			
	var optionsDiv = document.createElement('div');
	optionsDiv.className = 'styledSelectOptions';
		
	for(var i = 0; i < options.length; i++) {
		var opt = document.createElement('div'); opt.className = 'styledSelectOption';		
		var txt = document.createElement('div'); txt.className = 'txt';
		var v = document.createElement('div'); v.className = 'v';
		txt.innerHTML = options[i].innerHTML;
		v.innerHTML = options[i].value;
		v.style.display = 'none';
		opt.appendChild(txt);
		opt.appendChild(v);
		
		opt.onmouseover = function() { $(this).addClassName('hv'); }
		opt.onmouseout = function() { $(this).removeClassName('hv'); }
		if(i == 0) { $(opt).addClassName('first'); }
		if(i == (options.length-1)) { $(opt).addClassName('last'); }
		
		optionsDiv.appendChild(opt);
	}
	
	optionsDiv.style.display = 'none';
	
	valueDiv.style.width = w+'px';
	optionsDiv.style.width = w+'px';
	txtDiv.style.width = (w-(30+10))+'px';	
	btnDiv.style.width = '30px';
	
	div.appendChild(valueDiv);
	div.appendChild(optionsDiv);
	s.replace(div);
	
	this.input = input;	
	
}	
	
	StyledSelect.prototype.setup = function(id,name) {	
				
		this.id = id;
		this.obj = $(id);
		
		this.valueObj = this.obj.down('.styledSelectValue');
		this.valueObj.obj = this;
		
		this.optionsObj = this.obj.down('.styledSelectOptions');
		this.options = this.optionsObj.select('.styledSelectOption');
								
		for (var i = 0; i < this.options.length; i++) {
			this.options[i].obj = this;
			this.options[i].onclick = function() {				
				this.obj.select(this);
			}
		}
									
		this.valueObj.onclick = function() {
			this.obj.show();
		}
	}
	
	StyledSelect.prototype.show = function() {
				
		this.optionsObj.show();
		
		var ref = this;
		document.onclick = function(e) {			
			if (e) {
				var p = checkParent(e.target, ref.obj);
				if (p) {
					ref.hide();
					delete document.onclick;
				}
			}
		}
		
	}
	
	StyledSelect.prototype.hide = function() {
		
		this.optionsObj.hide();		
		
	}
	
	StyledSelect.prototype.select = function(opt,noupdate) {
				
				
		var val = this.getOptionValue(opt);		
		var label = this.getOptionLabel(opt);
		
		this.valueObj.down('.styledSelectTxt').innerHTML = label;
		this.input.value = val;
		
		if (!noupdate) {
			Reservation.updateRes();
		}
		
		opt.adjacent('.styledSelectOption').each(function(e){
			e.removeClassName('act');
		});
		opt.addClassName('act');
		
		this.hide();
		
	}
	
	StyledSelect.prototype.selectByValue = function(val) {
			
		for(var i = 0; i < this.options.length; i++) {
			var v = this.getOptionValue(this.options[i]);			
			if(v == val) {
				this.select(this.options[i],true);
			}
		}
		
	}
	
	StyledSelect.prototype.getOptionValue = function(opt) {
		
		var val = $(opt).select('.v');		
		val = val[0].innerHTML;		
		return val;
		
	}
	
	StyledSelect.prototype.getOptionLabel = function(opt) {
		
		var val = $(opt).select('.txt');
		val = val[0].innerHTML;
		return val;
		
	}
	
	StyledSelect.prototype.resize = function(w) {
		
		
		
	}

