var IMAGEPATH = "images/";

// Add onload event to set up navigation menu
if (document.getElementById('menuheader') != null) {
	addLoadEvent(navigation);
}

// Add onload event to set target of external links
addLoadEvent(externalLinks);

/*
 * Event handler to execute after page has loaded
 *
 * @param func - Function to execute once the page has loaded 
 */
function addLoadEvent(func)
{
	// Store existing onload event
	var oldonload = window.onload;
	// Assign function parameter to onload event if none exists
	if (typeof window.onload != 'function') {
		window.onload = func;
	// Create new function to call existing and new function
	} else {
		window.onload = function()
		{
			if (oldonload) {
				oldonload();
			}
			func();
		} // function()
	} // if/else
} // addLoadEvent

/*
 * Change all anchor tags to the _blank target by searching for the
 * 'rel="external"' attribute
 */
function externalLinks()
{
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	} // for...
} // externalLinks()

/*
 * Hide element
 *
 * @param id - ID of element to hide
 */
function hide(id)
{
	document.getElementById(id).style.display = "none";
} // hide()

/*
 * Hide all arrows on navigation menu
 */
function hideAllArrows()
{
	hide('home-img');
	hide('services-img');
	hide('prices-img');
	hide('faq-img');
	hide('tips-img');
} // hideAllArrows

/*
 * Highlight proper navigation button and add mouse behaviors
 */
function navigation()
{
	var link;

	// Set mouse behaviors for "Home" menu item
	link = document.getElementById('nav_home');
	link.onmouseover = function() {swap('home-img', 'left_arrow.jpg')};
	link.onmouseout = function() {swap('home-img', 'blank.gif')};

	// Set mouse behaviors for "Services" menu item
	link = document.getElementById('nav_services');
	link.onmouseover = function() {swap('services-img', 'left_arrow.jpg')};
	link.onmouseout = function() {swap('services-img', 'blank.gif')};

	// Set mouse behaviors for "Prices" menu item
	link = document.getElementById('nav_prices');
	link.onmouseover = function() {swap('prices-img', 'left_arrow.jpg')};
	link.onmouseout = function() {swap('prices-img', 'blank.gif')};

	// Set mouse behaviors for "FAQ" menu item
	link = document.getElementById('nav_faq');
	link.onmouseover = function() {swap('faq-img', 'left_arrow.jpg')};
	link.onmouseout = function() {swap('faq-img', 'blank.gif')};

	// Set mouse behaviors for "Skin Care Tips" menu item
	link = document.getElementById('nav_tips');
	link.onmouseover = function() {swap('tips-img', 'left_arrow.jpg')};
	link.onmouseout = function() {swap('tips-img', 'blank.gif')};
} // navigation

/*
 * Display element
 *
 * @param id - ID of element to show
 */
 function show(id)
{
	document.getElementById(id).style.display = "";
} // show()

/*
 * Toggle element display
 *
 * @param id - ID of element to show/hide
 */
function showHide(id)
{
	var elem = document.getElementById(id);
	if (elem.style.display == "block") {
		hide(id);
	}
	else {
		show(id);
	}
} // showHide()

/*
 * Swap image source
 */
function swap()
{
	if (!document.getElementById) {
		return;
	}

	var swapPath = IMAGEPATH;
	var i = 0;
	var elem;

	// If # arguments is odd, then first parameter is image path
	if ((swap.arguments.length%2) != 0) {
		swapPath=swap.arguments[0];
		i = 1;
	}

	// Use arguments[] array to loop through images to swap
	for ( ; i<swap.arguments.length; i+=2) {
		elem = document.getElementById(swap.arguments[i]);
		// Swap image source
		elem.src = swapPath + swap.arguments[i+1];
	}
} // swap()