var curCS;
var d = document;

function decode_str(string){
	return string.replace("&amp;","&");
}

function truncate_str(lib,zone){
	if( lib.length > 30 ) return decode_str(lib.substr(0,30)+"...");
	else return decode_str(lib);
}

/*
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 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(){
	var lstSelect = $A(d.getElementsByTagName('select'));
	lstSelect.each(function(select){
        if ((select.id == 'ptn_reprezentante') || (select.id == 'ptn_goplanet') || (select.id == 'dt_reprezentante')) {
    		select.style.display = 'none';
    
    		var myInput = d.createElement('input');
    		myInput.type = "hidden";
    		myInput.id = 'p_'+select.name;
    		myInput.name = 'p_'+select.name;
    		myInput.value = "";
    
    		var newCS = d.createElement('input');
    		newCS.id = 'new_'+select.id;
	    	newCS.name = 'new_'+select.name;
    		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){
    			var tmpOpt = d.createElement('div');
    			tmpOpt.setAttribute('value',opt.value);
    			tmpOpt.setAttribute('color',opt.color);
    			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'));
    				//$('p_'+this.getAttribute('CS')).value = this.getAttribute('value');
    				$('new_'+this.getAttribute('CS')).value = this.innerHTML;
    				$('new_'+this.getAttribute('CS')).style.backgroundPosition = 'top left';
    				$('new_'+this.getAttribute('CS')).style.color = '#83b4d8';
    				new Effect.Fade($('opts_new_'+this.getAttribute('CS')), {duration:0.1});
    			}
    			var optContent = d.createTextNode(opt.innerHTML);
    			tmpOpt.appendChild(optContent);
    			if(opt.selected){
    				newCS.value = truncate_str(opt.innerHTML,newCS.name);
    				if(opt.value != "") newCS.style.color = '#83b4d8';
    				else optTab.push(tmpOpt);
    				
    				myInput.value = opt.value;
    			}
    			else
    				optTab.push(tmpOpt);
    		});
    		optTab.each(function(opt){
    			opts.appendChild(opt);
    		});

    		var pN = select.parentNode;
    		pN.appendChild(myInput);
    		pN.appendChild(newCS);
    		pN.appendChild(opts);
        }
    });
	Event.observe(d, 'click', hideOpts, false);
}

function switchOptions(id){
	if($('opts_'+id).visible()==false){
		hideOpts();

		$(id).style.backgroundPosition='top left';
		var posSelect = getOffsets($(id));
		$('opts_'+id).style.left = posSelect[0]+'px';
		$('opts_'+id).style.top = posSelect[1]-3+$(id).getHeight()+'px';
		new Effect.BlindDown('opts_'+id, {duration:0.1});
	}
	else
		$('opts_'+id).style.display = 'none';
}

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.1});
		}
	});
}

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();
	}
}
