/* 
@description	There are functions necessary for highlighting the current page on the Alan Wong's Restaurant site.
@author		Benjamin Scheib
@date	November 12, 2008
*/

/* Finds the active page or page section (in the event a subnav exists) */

// @param link_class   Optional parameter to specify link to highlight
//                     if not specified, searches for active menu item to highlight
// @param container_id Optional parameter to specify container (ie sub or main nav)

function highlightPage(link_class,container_id)
{						
	/* search for a specific div containing a menu link (div w/class of the link) */
	if(link_class)
	{
		var link_div = getElementsByStyleClass(link_class,container_id)[0];	// the div with the link
		
		var link = link_div.getElementsByTagName("a")[0];					// the actual link
		if(link)
			rollover(link);													// pass the link to rollover
	}
	/* check all links to see if they are active */
	else
	{
		var links = document.getElementsByTagName("a");			
		for(var i=0; i<links.length; i++)
		{
			/* if is active link break and change to rollover image */
			var link = links[i];
			if(link.id.indexOf('active_menu')!=-1)
			{
				rollover(link);	
				break;
			}
		}
	}
} // end of highlight page function
//---------------------------------------------------------------------------------------

/* Changes the image source of a menu image to reflect a highlighted page */

// @param link  An anchor (link) object that contains an image to be altered.

function rollover(link)
{	
	// extract the image from the link object
	var link_image = link.getElementsByTagName("img")[0];
	// change the src to reflect the hightlighted image convention
	if((link_image)&&(link_image.src.indexOf("logo.gif")==-1) )
	{		
		link_image.src = link_image.src.replace(".jpg","_G.jpg");
		link_image.src = link_image.src.replace(".gif","_G.gif");
		
	}
} // end of rollover function
//---------------------------------------------------------------------------------------

/* Gets all the elements of a given class within a container with a given id */

// @param  className    The String name of the class.
// @param  containerId 	The String id of the container.
// @return  An Array containing all elements of a given class within the object
//          with the given containerId or the document if the container doesn't exist

function getElementsByStyleClass(className,containerId) {
  var all;				// hold all elements within the container
  var container = null; // object container
  
  // check to see if the container was specified
  if(containerId)
  		container = document.getElementById(containerId);
 
  // check to see if container exists and if not use the document as a container
  if(container)
  		all = container.getElementsByTagName('*');
  else
  		all = document.getElementsByTagName('*');
 
  // search all elements within the container for the given class name
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];
  
  return elements;
} // end of getElementByStyleClass function
