var curCS, derCS;
var d = document;
var t = true;
var f = false;
var n = null;
var nb_option = 8;
var w_asc = 13;
var padding_input = 5;
var w_fleche = 35;
var initCS=0;
var track_en_cours=0;
function getOffsets(obj) {
	var offsetTop = obj.offsetTop;
	var offsetLeft = obj.offsetLeft;
	while ((obj = obj.offsetParent )!=null) {
		offsetTop += obj.offsetTop;
		offsetLeft += obj.offsetLeft;
	}
	return [offsetLeft, offsetTop];
}

function iniCustomSelect(){
	if (initCS==0) { 
		initCS=1;
		var lstSelect = $A(d.getElementsByTagName('select'));
		lstSelect.each(function(select){
				var pN = select.parentNode;
				
				select.style.display = 'none';
				var newCS = document.createElement('input');
				newCS.id = 'new_'+select.id;
				newCS.name = 'new_'+select.id;
				newCS.className = 'newCS';
				newCS.readOnly = "readOnly";
				var w = pN.offsetWidth - padding_input;
				var wpn = w-w_fleche;
				if (wpn>0)
				newCS.style.width = wpn+'px';
				newCS.onclick = function(){
					switchOptions(this.id);
				}
				//newCS.onmouseover = function(){curCS = this.id;}
				//newCS.onmouseout = function(){curCS = '';}
				
				var flCS = document.createElement('div');
				flCS.className = 'fleche';
				flCS.onclick = function(){
					switchOptions(newCS.id);
				}
				//flCS.onmouseover = function(){curCS = newCS.id;}
				//flCS.onmouseout = function(){curCS = '';}
				
				var compo = document.createElement('div');
				compo.className = 'div_compo';
				compo.appendChild(newCS);
				compo.appendChild(flCS);
				
				var opts = document.createElement('div');
				opts.id = 'opts_new_'+select.id;
				opts.className = 'opts';
				opts.style.display = 'none';
				opts.setAttribute('CS',select.id);
				//var w = select.getAttribute('width');
				if (parseInt(w)>0) opts.style.width = parseInt(w)+'px';
				//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.text;
					var tmpOpt = document.createElement('div');
					tmpOpt.className = 'opt';
					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 = opt.text;
						$('new_'+this.getAttribute('CS')).style.backgroundPosition = 'top left';
						new Effect.Fade($('opts_new_'+this.getAttribute('CS')), {duration:0.3});
					}
					var optContent = document.createTextNode(opt.text);
					tmpOpt.appendChild(optContent);
					optTab.push(tmpOpt);
				});

				var cont = document.createElement('div');
				cont.id = 'cont_new_'+select.id;
				cont.className = 'content_select';
				if (optTab.length > nb_option) {
					cont.style.height = "120px";
					var delta_w = w_asc;
				}
				else var delta_w = 0;
				
				if (parseInt(w)>0) {
					w_m = parseInt(w)-delta_w;
					cont.style.width = w_m+'px';
				}
				
				optTab.each(function(opt){
					cont.appendChild(opt);
				});
				
				opts.appendChild(cont);
				
				if (optTab.length > nb_option) {
					var asc = document.createElement('div');
					asc.id = 'asc_new_'+select.id;
					asc.className = 'ascenceur';
					
					var track = document.createElement('div');
					track.className = 'track';
					track.id = 'track_'+select.id;
					track.onmouseup = function(){
						Event.observe(document, 'mouseup', hideOpts, f);
						setTimeout("annule_track()", 100);
					}
					track.onmousedown = function(){
						track_en_cours=1;
						Event.stopObserving(document, 'mouseup', hideOpts, f);
					}
					
					asc.appendChild(track);
					opts.appendChild(asc);
				}
				pN.appendChild(compo);
				//pN.appendChild(flCS);
				pN.appendChild(opts);
		});
		Event.observe(document, 'mouseup', hideOpts, f);
	}
}
function annule_track() {
	track_en_cours=0;
}
function switchOptions(id){
	//if($('opts_'+id).visible()==f){
		if (curCS == id) curCS = "";
		else curCS = id;
		hideOpts();
		if (curCS == id) {
			$(id).style.backgroundPosition='bottom left';
			var posSelect = getOffsets($(id));
			var l = posSelect[0] - 5;
			var t = posSelect[1]+$(id).getHeight() + 1;
			$('opts_'+id).style.left = l+'px';
			$('opts_'+id).style.top = t+'px';
			//new Effect.BlindDown('opts_'+id, {duration:0.3});
			$('opts_'+id).style.display="block";
			$('opts_'+id).style.opacity=1;
			if ($('asc_'+id)) {
				var scrollbar = new Control.ScrollBar('cont_'+id,'asc_'+id);
				scrollbar.scrollTo('top');
			}
			curCS="";
		}
	//}
}

function hideOpts(){
	if (track_en_cours==0) {
		var lstopts = $A(document.getElementsByClassName('opts'));
		lstopts.each(function(opts){
			//alert(opts.id+"-"+opts.visible());
			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).value!=valueSelect){
		var ts = $(targetSelect);
		var lstOpts = $A(ts.getElementsByTagName('option'));
		lstOpts.each(function(opt){
			if(opt.value==valueSelect){
				opt.selected = 'selected';
				throw $break;
			}
		});
		try {
			if($(targetSelect).onchange!=undefined){
				$(targetSelect).onchange();
			}
		}
		catch(ex) {}
	}
	if($(targetSelect).onclick!=undefined){
			$(targetSelect).onclick();
	}
}

function init_custom() {
	iniCustomRadio();
	iniCustomSelect();
}

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