/************************************************************************
*	
*	JavaScript: 	Common.js
*
************************************************************************
*	
*	DESCRIPTION: 	Common functions that can be used on any page
*
*************************************************************************/

	// Globals ********************************

	var activeMenu 	= "";
	var cursorX 	= "";
	var cursorY 	= "";
	var oldIE 		= true;
	var submitSearch = false;
	
	var thisBrowser = navigator.userAgent;
	
	if ((thisBrowser.indexOf("MSIE 8") != -1) || (thisBrowser.indexOf("Firefox") != -1) || (thisBrowser.indexOf("Safari") != -1))
	{
		oldIE = false;
	}
	
	var currentTab  = "";
	
	var navigationMenu = getElement("dropDownMenu"); // Navigation Menu Element

/*  On Load Actions * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Fires after the page is fully loaded, with the acception of any
 *				asynchronous elements (ex: Flash, Ajax).
 *
 ****************************************************************************************/
 
	window.onload = function()
  	{
  		document.onmouseup = hideMenu;
	    //document.onkeyup = function() {return false;}
		setNavigationTabs();
		
		if (getElement("mainImage1"))
		{
			setRandomImages();	// From "homepageImageRandomizer.js
		}
		
	}

    function submitenter(thisField, e)
    {
        var keycode;
        
        if (window.event)
        {
            keycode = window.event.keyCode;
        }
        else if (e)
        {
            keycode = e.which;
        }
        else
        {
            return true;
        }
        
        if (keycode == 13)
        {
            location.href = '/searchResults.aspx?cx=000918917656184378803:pyb7wgjhoei&cof=FORID:11&q=' + thisField.value;
            return false;
        }
        else
        {
            return true;
        }
    }

/*  Check Keystroke * *************************************************************
 *
 *	TAKES:		Keystroke Event
 * 	RETURNS:	NOTHING
 *	NOTES:		
 *
 ****************************************************************************************/
 
    function checkKeystroke(e)
    {
        if ((e.charCode == 13) & (submitSearch))
        {
            submitSearch();
        }
    }

/*  Submit Search * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Submit Custom Search
 *
 ****************************************************************************************/
 
    function submitSearch()
    {
        location.href = "/searchResults.aspx?cx=000918917656184378803:pyb7wgjhoei&cof=FORID:11&q=" + document.getElementById("q").value;
    }

/*  Get Mouse X * *************************************************************
 *
 *	TAKES:		Mouse Event
 * 	RETURNS:	The X position of the mouse on the screen, from the left side.
 *	NOTES:		Handles support of the IE DOM, and W3C DOM
 *
 ****************************************************************************************/
 
	function getMouseX(evt)
	{
		if (!oldIE)
		{
			return evt.pageX
		}
		else
		{
		   return event.clientX
		}
		
	}

/*  Get Mouse Y * *************************************************************
 *
 *	TAKES:		Mouse Event
 * 	RETURNS:	The Y position of the mouse on the screen, from the left side.
 *	NOTES:		Handles support of the IE DOM, and W3C DOM
 *
 ****************************************************************************************/
 
	function getMouseY(evt)
	{
		if (!oldIE)
		{
			return evt.pageY
		}
		else
		{
		   return event.clientY
		}
	}

/*  Update Cursor Position* *************************************************************
 *
 *	TAKES:		Mouse Event
 * 	RETURNS:	NOTHING
 *	NOTES:		This function is usually attached to a global "onmousemove" mouse event,
 *				in order to keep the cursor position updated.
 *
 ****************************************************************************************/
 
	function updateCursorPosition(e)
	{
		cursorX = getMouseX(e);
		cursorY = getMouseY(e);
	}

/*  Get Element * *************************************************************
 *
 *	TAKES:		Element Id
 * 	RETURNS:	Object that bears the element id provided.
 *	NOTES:		
 *
 ****************************************************************************************/
 
	function getElement(elementId)
	{
		 return document.getElementById(elementId);
	}

/*  Get Element Position X * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The X position of the element object
 *
 *************************************************************************/
	
	function getElementX(contentElement)
	{
		var iReturnValue = 0;
		while(contentElement != null)
		{
			iReturnValue += contentElement.offsetLeft;
			contentElement = contentElement.offsetParent;
		}
		
		return iReturnValue;
	}
	

/*  Get Element Position Y * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Y position of the element object
 *
 *************************************************************************/
	
	function getElementY(contentElement)
	{
		var iReturnValue = 0;
		while(contentElement != null)
		{
			iReturnValue += contentElement.offsetTop;
			contentElement = contentElement.offsetParent;
		}
		
		return iReturnValue;

	}

/*  Set Element X * *******************************************************
 *
 *	TAKES:		Element Object, X position value
 * 	RETURNS:	NOTHING, changes the X position of the element object.
 *
 *************************************************************************/
 
	function setElementX(element, posXValue)
	{
			element.style.left = posXValue + "px";
	}
	
	
/*  Set Element Y * *******************************************************
 *
 *	TAKES:		Element Object, Y position value
 * 	RETURNS:	NOTHING, changes the Y position of the element object.
 *
 *************************************************************************/
	
	function setElementY(element, posYValue)
	{
			element.style.top	= posYValue + "px";	
	}
	
/*  Get Width ********************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Width of the element object.
 *
 *************************************************************************/	

	function getWidth(element)
	{
		return element.offsetWidth;
	}
	
	
/*  Get Height * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Height of the element object.
 *
 *************************************************************************/
 
	function getHeight(element)
	{
		return element.offsetHeight;
	}
	
/*  Get Window Width * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	The width in pixels of the current window.
 *	NOTES:		
 *
 ****************************************************************************************/
 
	function getWindowWidth()
	{
		return document.all?document.body.clientWidth:window.innerWidth; 	
	}

/*  Get Window Height * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	The height in pixels of the current window.
 *	NOTES:		
 *
 ****************************************************************************************/
 
	function getWindowHeight()
	{
		return document.all?document.body.clientHeight:window.innerHeight; 	
	}

/*  Set Drop Down Menu Position * *************************************************************
 *
 *	TAKES:		Tab Element
 * 	RETURNS:	NOTHING
 *	NOTES:		Sets relative to the Navigation Tab Element.
 *
 ****************************************************************************************/
 
	function setDropDowmMenuPosition(tabElement)
	{
		if (oldIE)
		{
			navigationMenu.style.left = getElementX(tabElement) - 3;
			navigationMenu.style.top = getElementY(tabElement) + getHeight(tabElement) - 4;
		}
		else
		{
			// Firefox, Netscape
			navigationMenu.style.left = (getElementX(tabElement) - 2) + "px";
			navigationMenu.style.top = (getElementY(tabElement) + getHeight(tabElement) - 3) + "px";
		}
		
	}

/*  Set Drop Down Menu Content * *************************************************************
 *
 *	TAKES:		Link Name Collections Array, Link Url Collection Array
 * 	RETURNS:	NOTHING
 *	NOTES:		Builds the menu based on the arrays provided.
 *
 ****************************************************************************************/
 
	function setDropDownMenuContent(linkNameCollection, linkUrlCollection)
	{
		if ((linkNameCollection.length > 0) & (linkUrlCollection.length > 0))
		{
			var blankTarget = "";
			var divContainer = document.createElement("div");
			
			var menuContainer = document.createElement("div");
			menuContainer.className = "linkMenuContent";
			menuContainer.setAttribute("onmouseover", "toggleTabElementActions(currentTab, true); showMenu();");
			menuContainer.setAttribute("onmouseout", "navigationMouseOutActions();");
			
			for(var ctr=0; ctr<linkNameCollection.length; ctr++)
			{
				
				var menuItemElement = document.createElement("div");
				menuItemElement.className = "linkMenuItem";
				menuItemElement.setAttribute("onmouseover", "showMenu(); this.className='linkMenuItemHover';");
				menuItemElement.setAttribute("onmouseout", "this.className='linkMenuItem';");
				
				if ((linkUrlCollection[ctr].match("http://")  == null) & (linkUrlCollection[ctr].match("https://")  == null))
				{
					blankTarget = "location.href='" + linkUrlCollection[ctr] + "'";
				}
				else
				{
					blankTarget = "window.open('" + linkUrlCollection[ctr] + "', '_blank')";
				}
				
				menuItemElement.setAttribute("onclick", blankTarget);
				menuItemElement.innerHTML = linkNameCollection[ctr];
				
				menuContainer.appendChild(menuItemElement);
			}
			
			divContainer.appendChild(menuContainer);
			navigationMenu.innerHTML = divContainer.innerHTML;
		}
	}

/*  Show Menu * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Displays the menu
 *
 ****************************************************************************************/
	
	function hideMenu()
	{
		navigationMenu.style.display = "none";
	}

/*  Hide Menu * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Hides the menu
 *
 ****************************************************************************************/
	
	function showMenu()
	{
		navigationMenu.style.display = "block";
	}

/*  Clear Menu * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Clesrs the menu
 *
 ****************************************************************************************/
 
	function clearMenu()
	{
		navigationMenu.innerHTML = "";
	}

/*  Get Tag Group * *******************************************************
 *
 *	TAKES:		Element tag, regular expression that describes the id pattern
 *				of the  group of tag elements you're looking for.
 * 	RETURNS:	Element Array of matched element tag ids with the provided regular
 *				expression.
 *	NOTE:		Uses concat to load the element array
 *
 *************************************************************************/
	
	function getTagGroup(elementTag, regExp)
	{
		var tagArray = document.getElementsByTagName(elementTag);
		var elementArray = new Array();
		
		for(ctr=0; ctr < tagArray.length; ctr++)
		{
			if ((tagArray[ctr].id).search(regExp) != -1)
			{
				if (elementArray.length > 0)
				{
					elementArray = elementArray.concat(new Array(tagArray[ctr]));
				}
				else
				{
					elementArray = new Array(tagArray[ctr]);
				}
			}
		}
		
		return elementArray;
	}
	
/*  Set Navigation Tabs * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTES:		Takes the elements within the "navMenuTab[int]" div tags and builds the
 *				navigation tabs.
 *
 ****************************************************************************************/

	function setNavigationTabs()
	{
		var tabElements 	= getTagGroup("div", "navLink");
		var tabElement 		= "";
		
		var tagElements		= "";
		var tagElement		= "";
		var tabTitle		= "";
		var tabChild		= "";
		var onClickEnabled	= false;
		
		if (tabElements.length > 0)
		{
			for(var ctr=0; ctr<tabElements.length; ctr++)
			{
				tabElement = tabElements[ctr];
				
				if (tabElement)
				{	
					// Get the Contents of the DIV Container ******************
					tagElements = tabElement.getElementsByTagName("input");
					
					if (tagElements.length > 0)
					{
						for(var tagCtr=0; tagCtr<tagElements.length; tagCtr++)
						{
							tagElement = tagElements[tagCtr];
							
							if (tagElement)
							{
								// Set the Tab Title ***********************
								if (tagElement.id == "tabTitle")
								{
									tabChild = getNavigationTabElement(tagElement.value);
									var divContainer = document.createElement("div");
									divContainer.appendChild(tabChild);
									tabElement.innerHTML += divContainer.innerHTML;
									
								}
								
								// Set the Tab Url **************************
								if (tagElement.id == "tabUrl")
								{
									onClickEnabled = true;
								}
							}
							
						}
					}
					
					// Set the Click Actions *********************************
					if (!onClickEnabled)
					{
						tabElement.onclick = function()
						{
							// Keeps the menu visible
							showMenu();
						}
					}
					else
					{
						// For Tabs that are clickable, get the link Url value *********
						tabElement.onclick = function()
						{
							var linkElements = this.getElementsByTagName("input");
							var linkElement = "";
							
							for(var linkCtr=0; linkCtr<linkElements.length; linkCtr++)
							{
								linkElement = linkElements[linkCtr];
								
								if (linkElement.id == "tabUrl")
								{
									document.location.href = linkElement.value;
									
								}
							}
						}
	
					}
					
					// Set the Tab Actions ***********************************
					tabElement.onmouseover = function()
					{
						// Get tabMain Element ******************************
						currentTab = getElement(this.id);
						toggleTabElementActions(currentTab, true);
						
						clearMenu();
						getTabMenu(this);
					}
					
					tabElement.onmouseout = function()
					{
						navigationMouseOutActions();
					}
					
					
				}// End If
				
			} // End For: ctr
			
		} 
	}

/*  Get Navigation Tab Element * *************************************************************
 *
 *	TAKES:		Tab Title
 * 	RETURNS:	Tab Table Element
 *	NOTES:		
 *
 ****************************************************************************************/
 
	function getNavigationTabElement(tabTitle)
	{
		var tabTable 		= document.createElement("table");
		tabTable.cellPadding = 0;
		tabTable.cellSpacing = 0;
		
		var tableBody 		= document.createElement("tbody");
		
		// Main Row ************************************
		var tableRowMain	= document.createElement("tr");
		
			// Row Columns ************************************
			var rowMainCol1 = document.createElement("td");
			
			// Set the Tab Title ****************
			rowMainCol1.className 	= "navTabMain";
			rowMainCol1.id			= "tabMain";
			rowMainCol1.innerHTML 	= tabTitle;
			
			// Append to the row *******************
			tableRowMain.appendChild(rowMainCol1);
			
		tableBody.appendChild(tableRowMain);
		tabTable.appendChild(tableBody);
		
		return tabTable;
	}

/*  Get Tab Main Element * *************************************************************
 *
 *	TAKES:		Tab Element, Is Mouse Over (true | false)
 * 	RETURNS:	NOTHING
 *	NOTES:		Sets the styles and actions for the navigation tab.
 *
 ****************************************************************************************/
 
	function toggleTabElementActions(tabElement, isMouseOver)
	{
		var tabColElements = tabElement.getElementsByTagName("td");
		var tabColElement  = "";
		
		if (tabColElements.length > 0)
		{
			for(var ctr=0; ctr<tabColElements.length; ctr++)
			{
				tabColElement = tabColElements[ctr];
				
				if (tabColElement != undefined)
				{
					if (tabColElement.id == "tabMain")
					{
						if (isMouseOver)
						{
							tabColElement.className = "navTabMainHover";
						}
						else
						{
							tabColElement.className = "navTabMain";
						}
					}
					
				}
			}
		}
	}
	
	function navigationMouseOutActions()
	{
		if (navigationMenu.innerHTML == "")
		{
			// Tab, no menu
			toggleTabElementActions(currentTab, false);	
		}
		else
		{
			// Tab and menu
			toggleTabElementActions(currentTab, false);	
			hideMenu();
		}
	}

/*  Get Tab Menu * *************************************************************
 *
 *	TAKES:		Tab Element
 * 	RETURNS:	NOTHING
 *	NOTES:		Builds the tab menu and sets it to the Drop Down Element
 *
 ****************************************************************************************/
 
	function getTabMenu(tabElement)
	{
		var menuItems 	= tabElement.getElementsByTagName("div");
		var menuItem  	= "";
		var menuDetails = "";
		var menuDetail 	= "";
		
		var menuItemText 	= new Array();
		var menuItemUrl 	= new Array();
		
		if (menuItems.length > 0)
		{
			for(var ctr=0; ctr<menuItems.length; ctr++)
			{
				menuItem = menuItems[ctr];
				
				if (menuItem)
				{
					menuItemDetails = menuItem.getElementsByTagName("input");
					
					if (menuItemDetails.length > 0)
					{
						for(var detailCtr=0; detailCtr<menuItemDetails.length; detailCtr++)
						{
							menuDetail = menuItemDetails[detailCtr];
							
							if (menuDetail)
							{
								switch(menuDetail.id)
								{
									case "menuText":
									
										menuItemText.push(menuDetail.value);
										break;
									
									case "menuUrl":
										
										menuItemUrl.push(menuDetail.value);
										break;
									
									default:
										
										break;
								}
							}
						}
					}
				}
				
			} // End For
			
		} // End Menu Items
		
		setDropDownMenuContent(menuItemText, menuItemUrl);
		setDropDowmMenuPosition(tabElement);
		showMenu();	
	}
	

	
/*  Fade Out Element * *******************************************************
 *
 *	TAKES:		Element Object, Starting Fade Opacity Value, Fade Rate value between 1 and 100
 * 	RETURNS:	NOTHING
 *	NOTE:		Fades the element object at the rade rate specified.
 *
 *************************************************************************/
	
	function fadeOutElement(fadeElementId, fadeOpacity, fadeRate)
	{
		fadeOpacity = fadeOpacity - fadeRate;
		
		if (fadeOpacity < 0) { fadeOpacity = 0; }
		
		setOpacity(fadeElementId, fadeOpacity)
		
		if (fadeOpacity > 0)
		{
			setTimeout("fadeOutElement('" + fadeElementId + "', " + fadeOpacity + ", " + fadeRate + ")", 10);
		}
		
	}
	
/*  Fade Out Element Callback * *******************************************************
 *
 *	TAKES:		Element Object, Starting Fade Opacity Value, Fade Rate value between 1 and 100
 * 	RETURNS:	NOTHING
 *	NOTE:		Fades the element object at the rade rate specified.
 *
 *************************************************************************/
	
	function fadeOutElementCallback(fadeElementId, fadeOpacity, fadeRate, callback)
	{
		fadeOpacity = fadeOpacity - fadeRate;
		
		if (fadeOpacity < 0) { fadeOpacity = 0; }
		
		setOpacity(fadeElementId, fadeOpacity)
		
		if (fadeOpacity > 0)
		{
			setTimeout("fadeOutElementCallback('" + fadeElementId + "', " + fadeOpacity + ", " + fadeRate + ", '" + callback + "')", 10);
		}
		else
		{
			eval(callback);
		}
		
	}
	
/*  Fade Element * *******************************************************
 *
 *	TAKES:		Element Object, Starting Fade Opacity Value, Fade Rate value between 1 and 100
 * 	RETURNS:	NOTHING
 *	NOTE:		Fades the element object at the rade rate specified.
 *
 *************************************************************************/
	
	function fadeInElement(fadeElementId, fadeOpacity, fadeRate)
	{
		fadeOpacity = fadeOpacity + fadeRate;
		
		if (fadeOpacity > 100) { fadeOpacity = 100; }
		
		setOpacity(fadeElementId, fadeOpacity)
		
		if (fadeOpacity < 100)
		{
			setTimeout("fadeInElement('" + fadeElementId + "', " + fadeOpacity + ", " + fadeRate + ")", 50);
		}
		
	}
	
	
/*  Set Opacity * *******************************************************
 *
 *	TAKES:		Element Object Id
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets the opacity of the element
 *
 *************************************************************************/
 
	function setOpacity(elementId, opacityValue)
	{
		if (elementId != "")
		{
			var fadeElement = getElement(elementId);
			
			fadeElement.style.filter = "alpha(opacity=" + opacityValue + ");"; // IE
			fadeElement.style.zoom = "1";
			fadeElement.style.MozOpacity = opacityValue/100; // FireFox, Safari
		}
	}
	
	
/*  Clear Search Field* *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		
 *
 *************************************************************************/
 
	function clearSearchField()
	{
		var searchFieldElement = getElement("searchFieldValue");
		
		if (searchFieldElement.value == "Search")
		{
			searchFieldElement.value = "";
		}
	}
	
	
	
	
	
	
	
	
	
	
	

