/* Copyright 2007 - 2008 Cédric Michaux */
/* This code isn't redistributable      */

	function setState(mode){
		switch (mode){
			case "closed":
				currentOptionsList.style.display = "none";
				currentContainer.style.position = "static";
				break;
				
			case "opened":
				currentContainer.style.position = "relative";
				currentOptionsList.style.display = "block";
				clearTimeout(openTimer);
				openedState = true;
				break;
		}
		
		
	}
			
	function buildPseudoSelector(){
		openedState = false;
		
		document.getElementsByTagName("body")[0].onclick = function(){
			if(openedState == true)
				setState("closed");
		}
		
		for(i=0;selectorArray[i];i++){
			if(originalSelector = document.getElementById("selector-"+selectorArray[i])){
			
				pseudoSelector = document.createElement("div");
				pseudoSelector.id = "pseudoSelector-"+selectorArray[i];
				pseudoSelector.className = "pseudoSelector";
				
				selectorDisplay = document.createElement("input");
				selectorDisplay.type = "text";
				selectorDisplay.setAttribute("readonly","readonly");
				selectorDisplay.id = "selectorDisplay-"+selectorArray[i];
				selectorDisplay.className = "selectorDisplay";
				selectorDisplay.relative = selectorArray[i];
				selectorDisplay.onclick = function(){
					if(openedState == true)							
						setState("closed");
						
					currentOptionsList = document.getElementById("selectorOptions-"+this.relative);
					currentContainer = document.getElementById("pseudoSelector-"+this.relative);
					
					

					if(currentOptionsList.style.display == "none"){
						openTimer = setTimeout("setState('opened')", 10);
					}
					else
						setState("closed");
					
				}
				
				pseudoSelector.appendChild(selectorDisplay);
				
				selectorOptions = document.createElement("ul");
				selectorOptions.id = "selectorOptions-"+selectorArray[i];
				selectorOptions.className = "selectorOptions";
				selectorOptions.style.display = "none";
				
			
				for(j=0; originalSelector.childNodes[j];j++){
					if(originalSelector.childNodes[j].nodeName.toLowerCase() == "option"){
						currentLi = document.createElement("li");
						currentLi.className = "option";
						
						currentSelectorLink = document.createElement("a");
						currentSelectorLink.href = "#";
						currentSelectorLink.id = "selectorLink-"+selectorArray[i]+"-"+originalSelector.childNodes[j].value;
						currentSelectorLink.value = originalSelector.childNodes[j].value;
						currentSelectorLink.relative = selectorArray[i];
						
						currentSelectorLink.onclick = function(){
							document.getElementById("selectorDisplay-"+this.relative).value = this.firstChild.nodeValue;
							document.getElementById("selector-"+this.relative).value = this.value;
							currentOptions = document.getElementById("selector-"+this.relative).childNodes;
							this.parentNode.parentNode.style.display = "none"
							return false;
						}
						
						currentLinkContent = document.createTextNode(originalSelector.childNodes[j].firstChild.nodeValue);
						
						currentSelectorLink.appendChild(currentLinkContent);
						currentSelectorLink.appendChild(currentLinkContent);
						currentLi.appendChild(currentSelectorLink);
						selectorOptions.appendChild(currentLi);
						
						
						if(originalSelector.childNodes[j].selected)
							selectorDisplay.value = originalSelector.childNodes[j].firstChild.nodeValue;
					}
				}

				pseudoSelector.appendChild(selectorOptions);
				
				originalSelector.style.display = "none";
				originalSelector.parentNode.appendChild(pseudoSelector);
			}
		
		}
	}	
	
	window.onload = function(){
		buildPseudoSelector();
	}