var selects;
var selectText = "";

//this function runs when the page is loaded so put all your other onload stuff in here too.
function init() {
if(findPosX(document.getElementById('stylesheetTest')) == -999) {replaceSelects();}	
}


function replaceSelects() {
	//get all the select fields on the page
    selects = document.getElementsByTagName('select');
	//cycle trough the select fields
    for(var i=0; i < 1; i++) {
		//create and build div structure
		var selectArea = document.createElement('div');
		/*ajouté */
		selectArea.className = "selectArea";
		var left = document.createElement('div');
		var right = document.createElement('div');
		var center = document.createElement('div');
		var button = document.createElement('a');
		var text = document.createTextNode(selectText);
		center.id = "mySelectText"+i;
		button.href="javascript:showOptions("+i+")";
		selectArea.className = "selectArea";
		left.className = "left";
		right.className = "right";
		center.className = "center";
		right.appendChild(button);
		center.appendChild(text);
		selectArea.appendChild(right);
		selectArea.appendChild(center);
		
		//hide the select field
        selects[i].style.display='none'; 
		
		//insert select div
		selects[i].parentNode.insertBefore(selectArea, selects[i]);
		
		//build & place options div
		var optionsDiv = document.createElement('div');
		optionsDiv.className = "optionsDivInvisible";
		optionsDiv.id = "optionsDiv"+i;
		optionsDiv.style.left = findPosX(selectArea) + 'px';
		optionsDiv.style.top = findPosY(selectArea) + 19 + 'px';
		optionsDiv.style.position ='absolute';
		var ppane = document.createElement('div');
		ppane.id = "pane4";
		ppane.className = "scroll-pane";
		var hholder = document.createElement('div');
		hholder.className = "holder";

		for(var j=0; j < selects[i].options.length; j++) {
			var optionHolder = document.createElement('p');
			var optionLink = document.createElement('a');
			var optionTxt = document.createTextNode(selects[i].options[j].text);
			a = optionLink;
			optionLink.href = "javascript:showOptions("+i+");a;selectMe('"+selects[i].id+"',"+j+","+i+");";
			optionLink.appendChild(optionTxt);
			optionHolder.appendChild(optionLink);
			ppane.appendChild(optionHolder);
			hholder.appendChild(ppane);
			optionsDiv.appendChild(hholder);
			selectArea.onmouseout=function () {timer=setTimeout("hideOptions(0)",2000);selectArea.onmouseover=function (){clearTimeout(timer);};};
			
			optionsDiv.onmouseover=function (){clearTimeout(timer);};
			optionsDiv.onmouseout=function () {timer=setTimeout("hideOptions(0)",2000)};

		}
		
		//insert options div
		document.getElementsByTagName("body")[0].appendChild(optionsDiv);
		
	}}

function showOptions(g) {elem = document.getElementById("optionsDiv"+g);
		if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";$('#pane4').jScrollPane();}
		else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}}
		

function selectMe(selectFieldId,linkNo,selectNo) {
	//feed selected option to the actual select field
	selectField = document.getElementById(selectFieldId);
	for(var k = 0; k < selectField.options.length; k++) {
		if(k==linkNo) {
			selectField.options[k].selected = "selected";
		}
		else {
			selectField.options[k].selected = "";
		}
	}
	textVar = document.getElementById("mySelectText"+selectNo);
	var newText = document.createTextNode(selectField.options[linkNo].getAttribute('name'));
	var newText2 = selectField.options[linkNo].value;
	parent.location.href=newText2;
	textVar.replaceChild(newText, textVar.childNodes[0]);
}

function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {
		posTop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return posTop;}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {
		posLeft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return posLeft;}
	/****************************/
	function hideOptions(g) {elem = document.getElementById("optionsDiv"+g);
	if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}}
	function resteOptions(g) {elem = document.getElementById("optionsDiv"+g);
	if(elem.className=="optionsDivVisible") 
	{clearTimeout(timer);}
	}
window.onload = init;

