// This function is used for the Suckerfish (flyout) menus
sfHover = function() {
	var sf = getElementsByClassName('ul', 'ClearFix nav');
	if (sf == null) { return; }
	var sfEls = sf.getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// Use this to access elements by class instead of id
function getElementsByClassName(elTagName, elClassName) {
	if (document.getElementsByTagName) {
		var elementList = document.getElementsByTagName(elTagName);
		for (var i = 0; i < elementList.length; i++) {
			if (document.all) {
				theClassName = elementList[i].getAttribute('className');
			} else {
				theClassName = elementList[i].getAttribute('class');
			}
			if (theClassName == elClassName) {
				return elementList[i];
			}
		}
	}
}
// Tools and Resources dropdown
function toolsJump() {
	box = document.getElementById("search-select");
	destination = box.options[box.selectedIndex].value;
	var optionClassName=box.options[box.selectedIndex].className;
	if (destination) {
		if (optionClassName == 'NewWin') { // if className is NewWin open in new window 
			fnOpenWindow(destination, "newWindow", 750, 820); 
		} else {
			location.href = destination;
		}
	}
}
// Health Info dropdown
function healthInfoJump() {
	box = document.getElementById("HealthInfoSelect");
	destination = box.options[box.selectedIndex].value;
	var optionClassName=box.options[box.selectedIndex].className;
	if (destination) {
		if (optionClassName == 'NewWin') { // if className is NewWin open in new window 
			fnOpenWindow(destination, "newWindow", 750, 820); 
		} else {
			location.href = destination;
		}
	}
}

// Quick Tools
$(document).ready(function() {
	$("#QuickTools .Handle").click(function() {
	});
	$("#QuickTools .Handle").click(function(e) {
		var selected = $(this)[0].parentNode.id;
		var intStr = selected.substr(5);
		var clickedToolNumber = parseInt(intStr);
		
		var onLiId = $(".On",$("#QuickTools"))[0].id;
		var intStr = onLiId.substr(5);
		var OnToolNumber = parseInt(intStr);
		
		
		$("#QuickTools li").removeClass("On");
		$(this).parent().addClass("On");
		
		if(OnToolNumber < clickedToolNumber)
		{
			for(i = OnToolNumber;i<clickedToolNumber;i++)
			{
				var totalWidth = (i*32) - 32;
				$("#Tools"+i).animate({top: (-182 + totalWidth)}, 500);
			}
		}
		else
		{
			for(i =clickedToolNumber ;i<=OnToolNumber;i++)
			{
				var totalWidth = (i*32) - 32;
				$("#Tools"+i).animate({top: (totalWidth)}, 500);
			}
		}
		return false;
	});
});


// Quick Links select dropdown
$(document).ready(function() {
initSelect();
function initSelect () {
var sel = document.getElementById('search-select');
if (!sel) return;
sel.style.display = "none";

var div = document.createElement('div');

div.className = 'select-bg';

var ul = document.createElement('ul');
  ul.className = 'select-replacement';
  // collect our object’s options

  var obj = document.getElementById('search-select');
  var opts = obj.options;
  // iterate through them, creating <li>s
  for (var i=0; i<opts.length; i++) {
    var li = document.createElement('li');
	var a = document.createElement('a');
    var txt = document.createTextNode(opts[i].text);
	a.appendChild(txt);
    li.appendChild(a);
    li.selIndex = opts[i].index;
    li.selectID = obj.id;
	li.onclick = function() {
			selectMe(this);	
		}
	
	if(opts[i].selected){
		li.className = "top";
		li.onclick = function() {  
			this.parentNode.className += ' selectOpen';
			var lis = this.parentNode.getElementsByTagName('li');
			lis[0].className = 'top';
			if(this != lis[0])
				this.className = "selected";
			this.onclick = function () {
			selectMe(this);
			}
		}
		
	}
	    
	if (window.attachEvent) {
      li.onmouseover = function() {
        this.className += ' hover';
      }
      li.onmouseout = function() {
        this.className = 
          this.className.replace(new RegExp(" hover\\b"), '');
      }
    }
    
    ul.appendChild(li);
    
    if(opts[i].selected){
        var lis = ul.getElementsByTagName('li');
        var first_item = ul.firstChild;
        var copy_item = first_item.cloneNode(true);
        copy_item.className='';
        ul.insertBefore(copy_item, first_item);
    
        lis[0].innerHTML = li.innerHTML;
        lis[0].selectID = li.selectID;
        lis[0].selIndex = li.selIndex;
        
        lis[0].onclick = function() {
	        selectMe(this);
	    }
	}
    
  }
  // add the ul to the form
  div.appendChild(ul)
  obj.parentNode.appendChild(div);
}
});

function selectMe(obj) {
    var lis = obj.parentNode.getElementsByTagName('li');
    for (var i=0; i<lis.length; i++) {
    if (lis[i] != obj) {
        lis[i].className='';
        lis[i].onclick = function() {
        selectMe(this);
        }
    } else {
		lis[0].innerHTML = obj.innerHTML;
		lis[0].selectID = obj.selectID;
		lis[0].selIndex = obj.selIndex;
		
        setVal(obj.selectID, obj.selIndex);
        obj.className='top';
        obj.parentNode.className = 
        obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
        obj.onclick = function() {
        obj.parentNode.className += ' selectOpen';
		lis[0].className = 'top';
		if(lis[0] == obj) {
			for(var j = 1; j<lis.length; j++) {
				if(lis[0].innerHTML == lis[j].innerHTML)
					lis[j].className = 'selected';
			}
		}
		else
			obj.className = 'selected';
        this.onclick = function() {
            selectMe(this);
        }
        }
    }
    }
}
function setVal(objID,val) {
  var obj = document.getElementById(objID);
  obj.selectedIndex = val;
}
