var curCS;
var d = document;
var w = window;
var t = true;
var f = false;
var n = null;

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(includecssclass, excludecssclass) {
	
	var incClass = includecssclass.evalJSON();
	var excClass = excludecssclass.evalJSON();
	
	// Récup de la liste de select à inclure
	var lstSelect = $$('select');
	if (Object.isArray(incClass) && incClass.length > 0) {		
		lstSelect = new Array();
		incClass.each(function(elem){
			lstSelect = lstSelect.concat($$('select.' + elem));
		});		
	}
		
	// Parcours les selects
	lstSelect.each(function(select){
		
		
		// Test si element à exclure
		var exclude = false;
		excClass.each(function(elem){	
			if (select.hasClassName(elem)) 
				exclude = true;				
		});
		
		if (!exclude) {
		
			// Div conteneur
			var newDiv = new Element('div', {
				'class': 'customselect'
			});
			
			// Si pas d'id en generer un !
			if (select.id == '') 
				select.id = Math.floor((new Date()).getTime() / 1000);
			
			select.style.display = 'none';
			var newCS = new Element('input', {
				'id': 'new_' + select.id,
				'class': 'newCS ' + select.className,
				'readonly': 'readonly'
			});
			newCS.onclick = function(){
				switchOptions(this.id);
			}
			newCS.onmouseover = function(){
				curCS = this.id;
			}
			newCS.onmouseout = function(){
				curCS = '';
			}
			
			var opts = new Element('div', {
				'id': 'opts_new_' + select.id,
				'class': '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;
				var tmpOpt = d.createElement('div');
				tmpOpt.setAttribute('value', opt.value);
				tmpOpt.setAttribute('CS', select.id);
				tmpOpt.className = opt.className;
				tmpOpt.onmouseover = function(){
					this.toggleClassName('optHover');
				}
				tmpOpt.onmouseout = function(){
					this.toggleClassName('optHover');
				}
				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 = d.createTextNode(opt.innerHTML);
				tmpOpt.appendChild(optContent);
				optTab.push(tmpOpt);
			});
			optTab.each(function(opt){
				opts.appendChild(opt);
			});
			
			var pN = select.parentNode;
			newDiv.appendChild(newCS);
			newDiv.appendChild(opts);
			pN.appendChild(newDiv);
		}
	});
	Event.observe(d, 'click', hideOpts, f);
}

function switchOptions(id){
	if($('opts_'+id).visible()==f){
		var browser = navigator.userAgent.toLowerCase()
		
		hideOpts();
		//$(id).style.backgroundPosition='bottom left';
		//var posSelect = getOffsets($(id));
		//$('opts_'+id).style.left = posSelect[0]+'px';
		//$('opts_'+id).style.top = posSelect[1]+$(id).getHeight()+'px';
		$('opts_'+id).style.left = '0px';
		if (/msie/.test(browser))
			$('opts_'+id).style.top = ($(id).getHeight()+1) +'px';
		else
			$('opts_'+id).style.top = $(id).getHeight() +'px';
		new Effect.BlindDown('opts_'+id, {duration:0.3});
	}
}

function hideOpts(){
	var lstopts = $A(d.getElementsByClassName('opts'));
	lstopts.each(function(opts){
		if(opts.visible() && 'new_'+opts.getAttribute('CS')!=curCS){
			//$('new_'+opts.getAttribute('CS')).style.backgroundPosition = 'top right';
			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;
			}
		});
		if($(targetSelect).onchange!=undefined){
			$(targetSelect).onchange();
		}
	}
	if($(targetSelect).onclick!=undefined){
			$(targetSelect).onclick();
	}
}
