var curCS;
var d = document;
var w = window;
var t = true;
var f = false;
var n = null;

function getOffsets(obj) {
	
	//return new Array(0, 0);
	//alert(Position.positionedOffset(obj));
	return (Position.positionedOffset(obj));
	
	var offsetTop = obj.offsetTop;
	var offsetLeft = obj.offsetLeft;
	
	if ((obj = obj.offsetParent )!=null) {
		offsetTop += obj.offsetTop;
		offsetLeft += obj.offsetLeft;
	}
	
		
	return [offsetLeft, offsetTop];
}

function iniCustomSelect(){
	var lstSelect = $A(d.getElementsByTagName('select'));
	lstSelect.each(function(select){
		select.style.display = 'none';
		var newCS = d.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 = d.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;
			var tmpOpt = d.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 = d.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(d, 'click', hideOpts, f);
}

function switchOptions(id){
	if($('opts_'+id).visible()==f){
		hideOpts();
		$(id).style.backgroundPosition='bottom left';
		var posSelect = getOffsets($(id));
		$('opts_'+id).style.left = posSelect[0]+'px';
		if(document.all)
		{
			$('opts_'+id).style.top = posSelect[1]+'px';
		} else {
			$('opts_'+id).style.top = posSelect[1]+$(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 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;
			}
		});
		if($(targetSelect).onchange!=undefined){
			$(targetSelect).onchange();
		}
	}
	if($(targetSelect).onclick!=undefined){
			$(targetSelect).onclick();
	}
}
