// JavaScript Document
var sb_currentSelectBox;
function sb_buildSelectBox(myElement){
	sb_currentSelectBox=myElement;
	myElement.onclick=null;
	sb_createSelectBox();
}
// parse the selectBox HTML into an object for easier modification,
// removes the need for using unique id's for each copy of the select box.
// Also sets the width of the drop list.
function sb_createSelectBox(){
	var myElement=sb_currentSelectBox;
	var mySelectBoxObject = new Object();
	mySelectBoxObject.element = myElement;
	mySelectBoxObject.optionElements = new Array();
	mySelectBoxObject.optionOver = new Array();
	for(var i=0; i<myElement.childNodes.length; i++){
		// set up input if input present, determines whether select box is handled as drop down 
		// menu with anchor links or handled as a form element.
		if(myElement.childNodes[i].nodeName=="INPUT"){
			mySelectBoxObject.input=myElement.childNodes[i];
			mySelectBoxObject.value=mySelectBoxObject.input;
		}
		// parse display box
		if(myElement.childNodes[i].className=="selectBoxDisplay"){
			// display object, the everything minus the options list
			mySelectBoxObject.displayElement=myElement.childNodes[i];
			mySelectBoxObject.displayElement.id=sb_selectBoxArray.length+1-1;
			mySelectBoxObject.displayElement.onmouseover=function(){sb_overSelectBox(this)};
			mySelectBoxObject.displayElement.onmouseout=function(){sb_outSelectBox(this)};
			mySelectBoxObject.displayElement.onclick=function(){sb_openSelectBox(this)};
			for(var j=0; j<myElement.childNodes[i].childNodes.length; j++){
				if(myElement.childNodes[i].childNodes[j].className=="selectBoxDisplayLeft"){
					// display left element
					mySelectBoxObject.displayLeftElement=myElement.childNodes[i].childNodes[j];
					for(var k=0; k<myElement.childNodes[i].childNodes[j].childNodes.length; k++){
						if(myElement.childNodes[i].childNodes[j].childNodes[k].className=="selectBoxDisplayArrow"){
							// arrow element
							mySelectBoxObject.displayArrowElement=myElement.childNodes[i].childNodes[j].childNodes[k];
							for(var l=0; l<myElement.childNodes[i].childNodes[j].childNodes[k].childNodes.length; l++){
								if(myElement.childNodes[i].childNodes[j].childNodes[k].childNodes[l].className=="selectBoxDisplayMiddle"){
									//content element, also used to set default value of select box based on innerHTMl
									mySelectBoxObject.displayContentElement=myElement.childNodes[i].childNodes[j].childNodes[k].childNodes[l];
									mySelectBoxObject.displayValue=myElement.childNodes[i].childNodes[j].childNodes[k].childNodes[l].innerHTML;
								}
							}
							break;
						}
					}
					break;
				}
			}
		}
		// parse list object
		if(myElement.childNodes[i].className=="selectBoxListContainer"){
			mySelectBoxObject.listElementContainer = myElement.childNodes[i];
			mySelectBoxObject.listElementContainer.id=sb_selectBoxArray.length+1-1;
			mySelectBoxObject.listElementContainer.onmouseover=function(){sb_overSelectBoxContainer(this)};
			mySelectBoxObject.listElementContainer.onmouseout=function(){sb_outSelectBoxContainer(this)};
			mySelectBoxObject.listElementContainer.style.width=(myElement.offsetWidth-6)+"px";
			
			for(var x=0; x<myElement.childNodes[i].childNodes.length; x++){
				if(myElement.childNodes[i].childNodes[x].className=="selectBoxList"){
					mySelectBoxObject.listElement=myElement.childNodes[i].childNodes[x];
					var listHeight=mySelectBoxObject.listElement.offsetHeight;
					if(listHeight>150){
						mySelectBoxObject.listElement.style.height="250px";
						mySelectBoxObject.listElement.style.overflow="auto";
					}
					mySelectBoxObject.listElementContainer.style.display="none";
					mySelectBoxObject.listElementContainer.style.visibility="visible";
					
					for(var j=0; j<myElement.childNodes[i].childNodes[x].childNodes.length; j++){
						if(myElement.childNodes[i].childNodes[x].childNodes[j].className=="selectBoxListElement"){
							myElement.childNodes[i].childNodes[x].childNodes[j].id=mySelectBoxObject.optionOver.length+1-1;
							myElement.childNodes[i].childNodes[x].childNodes[j].onmouseover=function(){sb_listElementOver(mySelectBoxObject.listElementContainer,this); sb_checkSelectBoxOver();};
							myElement.childNodes[i].childNodes[x].childNodes[j].onmouseout=function(){sb_listElementOut(mySelectBoxObject.listElementContainer,this); sb_checkSelectBoxOver();};
							if(mySelectBoxObject.input){
								myElement.childNodes[i].childNodes[x].childNodes[j].onclick=function(){sb_listElementSelect(mySelectBoxObject,this);};
								for(var k=0; k<myElement.childNodes[i].childNodes[x].childNodes[j].childNodes.length; k++){
									if(myElement.childNodes[i].childNodes[x].childNodes[j].childNodes[k].nodeName=="A"){
										myElement.childNodes[i].childNodes[x].childNodes[j].displayValue=myElement.childNodes[i].childNodes[x].childNodes[j].childNodes[k].innerHTML;
									}
									else if(myElement.childNodes[i].childNodes[x].childNodes[j].childNodes[k].id=="value"){
										myElement.childNodes[i].childNodes[x].childNodes[j].value=myElement.childNodes[i].childNodes[x].childNodes[j].childNodes[k].innerHTML;
									}
								}
							}
							mySelectBoxObject.optionElements.push(myElement.childNodes[i].childNodes[x].childNodes[j]);
							mySelectBoxObject.optionOver.push(false);
						}
					}
				}
			}
		}
	}
	
	mySelectBoxObject.over=false;
	mySelectBoxObject.closing = false;
	mySelectBoxObject.displayOver = false;
	
	sb_selectBoxArray.push(mySelectBoxObject);
	
	sb_openSelectBox(mySelectBoxObject.displayElement);
}
