//======================  Select Replacement

//Thanx Alex Vincent (http://www.codingforums.com/showthread.php?t=7028)
var notWhitespace = /\S/;

function cleanWhitespace(node) {
  for (var x = 0; x < node.childNodes.length; x++) {
    var childNode = node.childNodes[x]
    if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
// that is, if it's a whitespace text node
      node.removeChild(node.childNodes[x])
      x--
    }
    if (childNode.nodeType == 1) {
// elements can have text child nodes of their own
      cleanWhitespace(childNode)
    }
  }
}


 

var current_open;

// show specific dropdown menu:
showSelected = function(elt){
	var y = 0;
	var x = 0;
	var thisRef = elt;
	while( thisRef.offsetParent != null ) {
		y +=thisRef.offsetTop;
		x += thisRef.offsetLeft;
		thisRef = thisRef.offsetParent;
    }
	if(IE5 || IE55){
		    y += (elt.offsetHeight-6);
    } else if(IE7){
        y += (elt.offsetHeight+6);
        x += 15;	    
    } else if(IE){
        y += (elt.offsetHeight-4);		    
    } else if(Safari){
        y += (elt.offsetHeight+6);
		    x += 15;
    } else {
		    y += (elt.offsetHeight-8);
  	}
 
 	x += 0;
	document.getElementById(elt.parent_id).style.top = y + "px";
	document.getElementById(elt.parent_id).style.left= x + "px";

	document.getElementById(elt.parent_id).style.overflow= "auto";
	document.getElementById(elt.parent_id).style.height= "130px";


	setTimeout('current_open="' + elt.parent_id + '";', 10);
}

// Create dropdown menus in custom select:
selectReplacement = function(elt,selectedValue){
	cleanWhitespace(elt);
	
	var hidden_submits = elt.parentNode.getElementsByTagName("div");
	for(var n=0; n<hidden_submits.length; n++){
		if(hidden_submits[n].className.indexOf("hidden-function")>-1){
			hidden_submits[n].style.display = "none";
		}
	}
	
	
	
	var dd_options = elt.getElementsByTagName("option");
	var dd_div = document.createElement("div");
	
	
	for(var i=0; i<dd_options.length; i++){
		
		if(!(dd_options[i].firstChild.nodeValue==selectedValue)){
			var this_value = dd_options[i].firstChild.nodeValue;
			var this_a = document.createElement("A");
			//default color text in select
			//this_a.style.color = "#525152";
			this_a.url_value = dd_options[i].value;
			this_a.parent_id = "dd_"+elt.id;
			//this_a.selection_id = "selected_"+elt.id; /* removed 20 oct 2006 */
			this_a.open_in = "intern";
			if(dd_options[i].className == "open-ext") { this_a.open_in = "extern"; }
			
      
      this_a.onmouseover = function(){ 
				if(IE) { this.style.cursor = "hand"; }
				else { this.style.cursor = "pointer"; }
		  
      this.className = "customDropDownHover";
		
			}
			
			
			this_a.onmouseout = function(){
				/* this is handeled in css now
        this.style.color = "#525152";
				this.style.background = "none";
				this.style.borderBottom = "1px solid #eee";
				*/
				this.className = "";
			}
			this_a.onclick = function(){ 
				if(this.open_in == "intern"){
					location.href = this.url_value;
				} else {
					window.open(this.url_value);
				}
				/*
				removed 20 oct 2006 so that selected option doesn't stay selected (see also above)
				var orig_value = document.getElementById(this.selection_id).firstChild.nodeValue;
				var orig_url = document.getElementById(this.selection_id).url_value;
				var orig_opener = document.getElementById(this.selection_id).open_in;
				
				document.getElementById(this.selection_id).innerHTML = this.firstChild.nodeValue;
				document.getElementById(this.selection_id).url_value = this.url_value;
				document.getElementById(this.selection_id).open_in = this.open_in;
				
				this.url_value = orig_url;
				this.innerHTML = orig_value;		
				this.open_in = orig_opener;*/
						
				document.getElementById(this.parent_id).style.left="-999em";
				current_open = "";
				return false;
			}
			dd_div.appendChild(this_a);
			this_a.appendChild(document.createTextNode(this_value));
			if(dd_options[i].className == "cat")
			{				
				this_a.style.background = "#0071B5";
				this_a.style.color = "#fff";
			}
		} 
		else
		{
			var this_value = dd_options[i].firstChild.nodeValue;
			var this_selection = document.createElement("a");
	
      
      //this_selection.style.color = "#000";
			this_selection.onmouseover = function(){ 
				if(IE) { this.style.cursor = "hand"; }
				else { this.style.cursor = "pointer"; }
				
			}
			this_selection.onmouseout = function(){
			
			};
			
			
      this_selection.onclick = function(){
				if(this.parent_id==current_open){
					document.getElementById(this.parent_id).style.left = "-999em";
					current_open = "";
				} else {
					if(!(current_open=="" || current_open==null || current_open=="undefined")){
						document.getElementById(current_open).style.left = "-999em";
					}
					showSelected(this);
				}
				return false;
			};
			this_selection.appendChild(document.createTextNode(this_value));
			elt.parentNode.appendChild(this_selection);		
			this_selection.className = "selected-item";
			this_selection.parent_id = "dd_"+elt.id;
			this_selection.id = "selected_"+elt.id;
			this_selection.url_value = dd_options[i].value;
			if(dd_options[i].className == "open-ext")  this_selection.open_in = "extern" ;
			elt.style.display = "none"; 
		}
	}
	
	document.getElementsByTagName("body")[0].appendChild(dd_div);
	//document.getElementById("selected_customSelect").appendChild(dd_div);
	dd_div.id = "dd_"+elt.id;
	dd_div.className = "alt-dropdown";

}

    function setForm() {
      var s = document.getElementsByTagName('select');
      for (var i=0; i<s.length; i++) {
		if(s[i].className.indexOf("custom-select")>-1){
			alert(s[i].options[0].firstChild.nodeValue);
			selectReplacement(s[i],s[i].options[0].firstChild.nodeValue);
		}
      }
    }
    SelectChangeStyle = function() {
      (document.all && !window.print) ? null : setForm();
    }
	
checkDropDowns = function (e) {
	if(!(current_open=="" || current_open==null || current_open=="undefined")){
		if(document.getElementById(current_open) && document.getElementById(current_open).offsetLeft > 0){
			document.getElementById(current_open).style.left = "-999em";
			current_open = "";
		}
	}	
}

//======================  end select replacements
function initSelects() { 
var theSelect = document.getElementById('customSelect');
if(theSelect == null) return; //in some cases, customSelect is not present
var firstElement = theSelect.options[0].firstChild.nodeValue;
//alert(theSelect + firstElement);
selectReplacement(theSelect,firstElement);
}

addEvent(window,'load',initSelects);
addEvent(window,'load',captureClick);

