var curCS;

function iniCustomSelect(){
	$$('select.custom_select').each(function(select) {
		select.style.display = 'none';
		var newCS = document.createElement('input');
		newCS.id = 'new_'+select.id;
		newCS.className = 'newCS';
		newCS.readOnly = "readOnly";
		newCS.onclick = function(){
			switchOptions(this.id);
		}
		newCS.onmouseover = function(){curCS = this.id;}
		newCS.onmouseout = function(){curCS = '';}
		
		var opts = document.createElement('div');
		opts.id = 'opts_new_'+select.id;
		opts.className = 'opts';
		opts.style.display = 'none';
		opts.setAttribute('CS',select.id);
		opts.onmouseover = function(){curCS = 'new_'+this.getAttribute('CS');}
		opts.onmouseout = function(){curCS = '';}
		
		var optTab = new Array();
		
		var lstOpt = $A(select.getElementsByTagName('option'));
		lstOpt.each(function(opt){
			if(opt.selected) newCS.value = opt.innerHTML;
			if(opt.value != '') {
				var tmpOpt = document.createElement('div');
				tmpOpt.setAttribute('value',opt.value);
				tmpOpt.setAttribute('CS',select.id);
				tmpOpt.onmouseover=function(){this.className='optHover';}
				tmpOpt.onmouseout=function(){this.className='opt';}
				tmpOpt.onclick=function(){
					setSelectValue(this.getAttribute('CS'), this.getAttribute('value'));
					$('new_'+this.getAttribute('CS')).value = this.innerHTML;
					$('new_'+this.getAttribute('CS')).style.backgroundPosition = 'top left';
					new Effect.Fade($('opts_new_'+this.getAttribute('CS')), {duration:0.3});
				}
				var optContent = document.createTextNode(opt.innerHTML);
				tmpOpt.appendChild(optContent);
				optTab.push(tmpOpt);
			}
		});
		optTab.each(function(opt){
			opts.appendChild(opt);
		});
		
		var pN = select.parentNode;
		pN.appendChild(newCS);
		pN.appendChild(opts);
	});
	Event.observe(document, 'click', hideOpts, false);
}

function switchOptions(id){
	if($('opts_'+id).visible()==false){
		hideOpts();
		$(id).style.backgroundPosition='bottom left';
		var posSelect = $(id).cumulativeOffset();;
		$('opts_'+id).style.left = posSelect[0]+'px';
		$('opts_'+id).style.top = posSelect[1]+$(id).getHeight()+'px';
		new Effect.BlindDown('opts_'+id, {duration:0.3});
	}
}

function hideOpts(){
	var lstopts = $A(document.getElementsByClassName('opts'));
	lstopts.each(function(opts){
		if(opts.visible() && 'new_'+opts.getAttribute('CS')!=curCS){
			$('new_'+opts.getAttribute('CS')).style.backgroundPosition = 'top left';
			new Effect.Fade(opts, {duration:0.3});
		}
	});
}

function setSelectValue(targetSelect, valueSelect){
	if(targetSelect == "service") {
		if($(targetSelect).value!=valueSelect){
			var ts = $(targetSelect);
			var lstOpts = $A(ts.getElementsByTagName('option'));
			lstOpts.each(function(opt){
				if(opt.value==valueSelect){
					opt.selected = 'selected';
					throw $break;
				}
			});
			if($(targetSelect).onchange!=undefined){
				$(targetSelect).onchange();
			}
		}
		if($(targetSelect).onclick!=undefined){
				$(targetSelect).onclick();
		}
	} else {
		window.location.replace(valueSelect);
	}
}

Event.observe(window, 'load', iniCustomSelect, false);

