/**
 * Author: Gary Chin
 * Last Update: Aug 18, 2002
 * Copyright (c) 2002 Sympel Inc.
 */

var NS = (document.layers ? true:false);  // Test if the end user is using NetScape or not
var hideContent = (NS ? 'hide':'hidden');
var showContent = (NS ? 'show':'visible');

var imgMenuOn = new Array();
var imgMenuOff = new Array();

var imgSubMenuOn = new Array();
var imgSubMenuOff = new Array();
var imgSubMenuClick = new Array();
var menuClicked = false;

var obj;
var menus;
var subMenus;
var subMenuFrameObj = parent.frames[2];
var contentFrameObj = parent.frames[3];

var preMenuID;  // ID of previous menu image displayed
var preMenuIndex;  // Index of previous menu image displayed
var clickedMenuID;
var clickedMenuIndex;

var preSubMenuID;
var preSubMenuIndex;
var clickedSubMenuID;
var clickedSubMenuIndex;

var preSubSubMenuID;

var preLayer;  // For NS only to keep track of the previous sub menu layer name

var subDisplay = false;  // Use to determine if sub sub menu should show or hide

//--------------------------------------- Menus Initialization ----------------------------------------//
/**
 * Initialization function for preloading images etc
 ***************************************************/
function initialize()
{
	menus = new Array(new menu('about'), new menu('products'), new menu('signup'), new menu('contact'));
	subMenus = new Array(new subMenu('busisign'), new subMenu('residentsign'), new subMenu('info'),
			new subMenu('corp'), new subMenu('network'), new subMenu('partner'), new subMenu('press'),
			new subMenu('investor'), new subMenu('inetserv'), new subMenu('dataserv'), new subMenu('voiceserv'),
			new subMenu('proserv'), new subMenu('coloc'));
}

//----------------------------------- Menu Initialization Routines -------------------------------------//
/**
 * Contains the main menu(menu bar) information
 **********************************************/
function menu(id)
{
	this.name = id;
	this.layername = "ns_" + id;
	preLoadMenuImg(id);
}

/**
 * Preloads the main menu images
 *******************************/
function preLoadMenuImg(id)
{
	var imgMenuPath = 'images/menu/';

	imgMenuOn[imgMenuOn.length] = new Image();  // Once decleared a new, imgMenuOn.length becomes 1
	imgMenuOn[imgMenuOn.length - 1].src = imgMenuPath + "menu_" + id + "_on.jpg";  // -1 to fix the index after it's increased from new Image()
	imgMenuOff[imgMenuOff.length] = new Image();
	imgMenuOff[imgMenuOff.length - 1].src = imgMenuPath + "menu_" + id + "_off.jpg";
}

//--------------------------------- Sub Menu Initialization Routines ------------------------------------//
/**
 * Contains the sub menu information
 ***********************************/
function subMenu(id)
{
	this.name = id;
	preLoadSubMenuImg(id);
}

/**
 * Preloads the sub menu images
 ******************************/
function preLoadSubMenuImg(id)
{
	var imgSubMenuPath = 'images/submenu/';

	imgSubMenuOn[imgSubMenuOn.length] = new Image();
	imgSubMenuOn[imgSubMenuOn.length - 1].src = imgSubMenuPath + "sub_" + id + "_on.jpg";
	imgSubMenuOff[imgSubMenuOff.length] = new Image();
	imgSubMenuOff[imgSubMenuOff.length - 1].src = imgSubMenuPath + "sub_" + id + "_off.jpg";
	imgSubMenuClick[imgSubMenuClick.length] = new Image();
	imgSubMenuClick[imgSubMenuClick.length - 1].src = imgSubMenuPath + "sub_" + id + "_click.jpg";
}

//------------------------------------------------- Misc ------------------------------------------------//
/**
 * Main menu image swaping function
 **********************************/
function swapMenuImg(id, index, isOn, onClick, layer_name)  // layer_name is for ns only
{
	if (NS)  // Netscape browser
	{
		if (onClick)
		{
			if (clickedMenuID) document.layers[layer_name].document.images[clickedMenuID].src = imgMenuOff[clickedMenuIndex].src;
			document.layers[layer_name].document.images[id].src = imgMenuOn[index].src;
			clickedMenuID = id;
			clickedMenuIndex = index;
			clickedSubMenuID = "";

			// This is hard coded to display the contact info page whenever the user clicks on the
			// "contact us" option  (implemented due to user friendly reason)
			if (id == "contact") top.frames[2].location = "sub_contactmenu.htm";
		}
		else if (isOn)
		{
alert("a " + clickedMenuID);
			if (id != clickedMenuID) document.layers[layer_name].document.images[id].src = imgMenuOn[index].src;
			if (!clickedMenuID) displaySubMenu(id);
			contentFrameObj.hideSubSubMenu();
			swapSubMenuImg(preSubMenuID, preSubMenuIndex, false, false, menus[index].layername);
			preMenuID = id;
			preMenuIndex = index;
			preSubMenuID = "";
		}
		else // (!isOn)
		{
			if (id != clickedMenuID) document.layers[layer_name].document.images[id].src = imgMenuOff[index].src;
		}
	}
	else  // IE browser
	{
		if (onClick)
		{
			// turn off clickedMenuID image first
			if (clickedMenuID) document.images[clickedMenuID].src = imgMenuOff[clickedMenuIndex].src;

			// turn on the new id imgage
			document.images[id].src = imgMenuOn[index].src;

			clickedMenuID = id;
			clickedMenuIndex = index;
			clickedSubMenuID = "";  // this is to avoid not loading click img when different menu is choosen

			// This is hard coded to display the contact info page whenever the user clicks on the
			// "contact us" option  (implemented due to user friendly reason)
			if (id == "contact") top.frames[2].location = "sub_contactmenu.htm";
		}
		else if (isOn)
		{
//alert("a " + clickedMenuID);
			// Prevent redundancy
			if (id != clickedMenuID) document.images[id].src = imgMenuOn[index].src;

			// Display the sub menu of current id if not clicked
			if (!clickedMenuID) displaySubMenu(id);

			// Hide the previous sub sub menu & sawp the sub menu img to off
			contentFrameObj.hideSubSubMenu();
			swapSubMenuImg(preSubMenuID, preSubMenuIndex, false);

			preMenuID = id;
			preMenuIndex = index;
			preSubMenuID = "";  // this is to avoid loading an img that's on another page which causes an error
		}
		else // (!isON)
		{
			if (id != clickedMenuID) document.images[id].src = imgMenuOff[index].src;
		}
	}
}

/**
 * Displays the sub menu of current selected main menu
 *****************************************************/
function displaySubMenu(id)
{
	top.frames[2].location = "sub_" + id + "menu.htm";
}

/**
 * Sub menu image swaping function
 *********************************/
function swapSubMenuImg(id, index, isOn, hasChild, layer_name)
{
	if (NS)
	{
		if (isOn)
		{
			if (preSubMenuID && preSubMenuID != "")
			{
				if (clickedSubMenuID && preSubMenuID != clickedSubMenuID) subMenuFrameObj.document.layers[layer_name].document.images[preSubMenuID].src = imgSubMenuOff[preSubMenuIndex].src;
				contentFrameObj.hideSubSubMenu();
			}
			if (id != clickedSubMenuID) subMenuFrameObj.document.layers[layer_name].document.images[id].src = imgSubMenuOn[index].src;
			if (hasChild) displaySubSubMenu(id, true);
			preSubMenuID = id;
			preSubMenuIndex = index;
			preLayer = layer_name;
		}
		else // (!isOn)
		{
			if (id != clickedSubMenuID && id != "") subMenuFrameObj.document.layers[layer_name].document.images[id].src = imgSubMenuOff[index].src;
			displaySubSubMenu(id, false);
		}
	}
	else
	{
		if (isOn)
		{
			// Turn the previous sub menu id to off and hide the sub sub menu if applicable
			if (preSubMenuID && preSubMenuID != "")
			{
				if (clickedSubMenuID && preSubMenuID != clickedSubMenuID) subMenuFrameObj.document.images[preSubMenuID].src = imgSubMenuOff[preSubMenuIndex].src;
				contentFrameObj.hideSubSubMenu();
			}

			if (id != clickedSubMenuID) subMenuFrameObj.document.images[id].src = imgSubMenuOn[index].src;

			if (hasChild) displaySubSubMenu(id, true);

			preSubMenuID = id;
			preSubMenuIndex = index;
		}
		else // (!isOn)
		{
			if (id != clickedSubMenuID && id != "") subMenuFrameObj.document.images[id].src = imgSubMenuOff[index].src;
			displaySubSubMenu(id, false);
		}
	}
}

/**
 * Displays the sub sub menu of the selected sub menu
 ****************************************************/
function displaySubSubMenu(id, display)
{
	if (display)
	{
		contentFrameObj.showSubSubMenu(id);
	}
}

/**
 * Loads the requested page into the content frame
 *************************************************/
function loadPage(page, id, index)
{
	if (NS)
	{
		var menuLayer = "ns_menubar";  // The is the layer name for main menu bar

		// preLayer is basically this layer
		if (clickedSubMenuID) subMenuFrameObj.document.layers[preLayer].document.images[clickedSubMenuID].src = imgSubMenuOff[clickedSubMenuIndex].src;
		subMenuFrameObj.document.layers[preLayer].document.images[id].src = imgSubMenuClick[index].src;
		contentFrameObj.location = page;
		if (!clickedMenuID) swapMenuImg(preMenuID, preMenuIndex, false, true, menuLayer);
	}
	else
	{
		if (clickedSubMenuID) subMenuFrameObj.document.images[clickedSubMenuID].src = imgSubMenuOff[clickedSubMenuIndex].src;
		subMenuFrameObj.document.images[id].src = imgSubMenuClick[index].src;
		contentFrameObj.location = page;
		if (!clickedMenuID)  // used for the first time when not clicked
		{
			swapMenuImg(preMenuID, preMenuIndex, false, true);
		}
	}
	clickedSubMenuID = id;
	clickedSubMenuIndex = index;
}

/**
 * This is called by subsubmenu.js to swap the sub menu img to off (a wraper)
 ****************************************************************************/
function swapOff()
{
	swapSubMenuImg(preSubMenuID, preSubMenuIndex, false, false, preLayer);
}
