// For scripts commonly used throughout the site only

// Preload images for Nav
if (document.images)
{
  preloadImage = new Image();
  // set image url
  imageUrl = new Array();
  imageUrl[0] = "../img/header/subnavBg.gif";
  imageUrl[1] = "../img/header/navBgHover.gif";
  imageUrl[2] = "../img/header/navBgSel.gif";
  imageUrl[3] = "../img/header/navBg2.gif";
  imageUrl[4] = "../img/header/navBg.gif";

   var i = 0;
   for(i=0; i<=4; i++) 
	 preloadImage.src = imageUrl[i];
} 
  

// This script toggles the visibility of divs
// Used for sign in drop down, referral affiliates info, classifieds advanced search
function toggle(elementName) {
	var ele = document.getElementById(elementName);
	if(ele.style.display == "block") {
			ele.style.display = "none";
	}
	else {
		ele.style.display = "block";
	}
} 

// BROWSER DETECT AND VARIABLES FOR DROPDOWN MENUS
// time before menu disappears when rolled out (1000 = 1 sec)
delayMenuTimeHover = 300;
delayMenuTime = 500;
// declare the timeout variable to prevent errors
var showDropDown;
var hideDropDown;
var topAdjust;
var currLeft;
var currTop;
var browserType;
// prevent positioning errors with browser rendering differences
if(window.opera){
	browserType="opera";
} else{
	if(navigator.userAgent.indexOf('Safari')!=-1){
			browserType="safari";
	}else{
		if(navigator.userAgent.indexOf('Gecko')!=-1){
		browserType="mozilla";
		}else{
			if(document.getElementById||document.all){
				browserType="iexplore"
			}
		}
	}
}

function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
   var rv = -1; // Return value assumes failure.
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   return rv;
}
var ver = getInternetExplorerVersion();


// SCRIPT TO CONTROL SUB /SUB SUB NAV MENU ITEMS
function navMenuDisplay(menuId,menuType){
	// hide all sub sub nav dropdowns from view, before showing the current one
	if(showDropDown>0){
		clearTimeout(showDropDown);
	}
	
	if(hideDropDown>0){
		clearTimeout(hideDropDown);
	}
	for(i=1;document.getElementById("navDropMenu"+i);i++){
		document.getElementById("navDropMenu"+i).style.display="none";
	}
	// go thru each menu button and make them normal state again
	for(j=1;j<=7;j++){
		parentTab=document.getElementById("navDropMenu"+j).getAttribute("rel");
		if(document.getElementById(parentTab).className=="navMenuHover"){
			document.getElementById(parentTab).className="navMenu";
		}
	}
	// now that previous hover states are cleared, make the rolled over link show hover state
	if(menuType=="main" && document.getElementById(menuId).className!="navMenuSelected"){
		document.getElementById(menuId).className="navMenuHover";
	}
	// check if it is a sub nav menu or sub sub nav menu (they have different offset widths/heights for diff browsers)
	if(browserType=="opera"){
		if(menuType=="main"){
			topAdjust=22;
			leftAdjust=0;
		}else{
			topAdjust=17;
			leftAdjust=0;
		}
	}
	if(browserType=="safari"){
		if(menuType=="main"){
			topAdjust=22;
			leftAdjust=0;
		}else{
			topAdjust=13;
			leftAdjust=0;
		}
	}
	if(browserType=="mozilla"){
		if(menuType=="main"){
			topAdjust=22;
			leftAdjust=0;
		}else{
			topAdjust=15;
			leftAdjust=1;
		}
	}
	if(browserType=="iexplore"){
		if(menuType=="main"){
			if (ver >= 8.0){
				topAdjust=22;
				leftAdjust=0;
			}
			else{
				topAdjust=35;
				leftAdjust=0;
			}
		}else{
			topAdjust=15;
			leftAdjust=-14;
		}
	}
	// check the links' rel attribute for the relevant dropdown to display
	menuRef=document.getElementById(menuId).getAttribute("rel");
	// run the findPos function to find the "top" position (relevant to the <li> top position) to place the dropdown div
	findPos(document.getElementById(menuId).parentNode);
	document.getElementById(menuRef).style.top=currTop+topAdjust+"px";
	document.getElementById(menuRef).style.left=currLeft+leftAdjust+"px";
	// now display it
	
	showDropDown = setTimeout(showDropDownMenu,delayMenuTimeHover);
	function showDropDownMenu(){
		document.getElementById(menuRef).style.display="block";
	}
	
	// clear the timeouts when user rolls over the dropdown (stop it from disappearing)
	document.getElementById(menuRef).onmouseover=function(){
		if (showDropDown >0){			
		clearTimeout(showDropDown);		
		}
		if(hideDropDown>0){
			clearTimeout(hideDropDown);
		}
	}
	// make sure that the menu fades off after the menu is rolled out
	document.getElementById(menuRef).onmouseout=function(){
		hideDropDown = setTimeout(hideDropDownMenu,delayMenuTime);
	}
	// make sure that the menu fades off after the parent menu link is rolled out also
	document.getElementById(menuId).onmouseout=function(){
		if (showDropDown >0){			
			clearTimeout(showDropDown);		
		}
		// clear timeout first! Important, or the previous setTimeout may still be in effect
		if(hideDropDown>0){
			clearTimeout(hideDropDown);
		}
		hideDropDown = setTimeout(hideDropDownMenu,delayMenuTime);
	}
	function hideDropDownMenu(){
		// hide the dropdown
		document.getElementById(menuRef).style.display="none";
		// go thru each menu button and make them normal state again (except the selected category link)
		for(k=1;k<=7;k++){
			parentTab=document.getElementById("navDropMenu"+k).getAttribute("rel");
			if(document.getElementById(parentTab).className=="navMenuHover"){
				document.getElementById(parentTab).className="navMenu";
			}
		}
	}
}

// used for finding position to place the dropdown menu (based on parent object (list item) position of <a> object)
function findPos(obj) {
		currLeft = currTop = 0;
	if (obj.offsetParent) {
		do {
		currLeft += obj.offsetLeft;
		currTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [currLeft,currTop];
}
}


// This script toggles the visibility of a div and switches text
// Currently used for the QuickStart Guide
function toggleDivTxt(divBody, divTxt) {
	var ele = document.getElementById(divBody);
	var text = document.getElementById(divTxt);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "<a href=\"#\">View</a>";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "<a href=\"#\">Hide</a>";
	}
}

// This script clears default text in form fields
function doClear(theText) 
{
	 if (theText.value == theText.defaultValue) {
		 theText.value = ""
	 }
}

