/************************************************
*
* Menu Rendering / Image Swap Code
*
* Purpose:
*   This code is used to populate the sub-nav
*   menu on the page and to handle the menu
*   on-mouse event image swaps.
*
* Author(s): AJ Hill, Nehal Deliwala
* Date: 2004-06-05
*
************************************************/

/**
 * subMenuClassChange()
 *
 * This function accepts a submenu item and the
 * class which the current page link should be
 * changed to. It returns the altered sub-menu.
 *
 * Example:
 *
 *     alert(subMenuClassChange(menulist,'menu','menu_highlight'));
 *
 * @param submenu - string submenu
 * @param current_class - current class to replace for submenu item
 * @param new_class - new class to use for submenu item
 * @return altered sub-menu
 */
 
function subMenuClassChange(submenu,current_class,new_class) {
	

	// Define regex object
	var myregex = /[\/]{0,1}[^\/]*$/gi;
	
	// Take the current file's name from the URL
	var file = myregex.exec(document.URL);

	//myregex = /[\x3c]a href="[\w\W]*?"[\w\W]*?class="menu"[\x3e][\w\W]+?[\x3c]\/a[\x3e]/gi;
	// Build regex object to strip out the appropriate menu item
	myreg1 = "[\\w]*?[\\x3c]a href=\"[^\\\"]*?";
	myreg2 = "[\\w\\W]*?class=\"" + current_class + "\"[\\x3e]";
	myreg3 = "[\\w\\W]+?[\\x3c]\\/a[\\x3e]";
	myregex = new RegExp(myreg1 + file + myreg2 + myreg3, "gi");

	// Pull the item out of the sub-menu
	var menuitem = myregex.exec(submenu);
	if (menuitem != null) {
		menuitem = menuitem[0]; // Get rid of array complication

		// Replace class w/ new class
		var new_menuitem = menuitem.replace(current_class,new_class);

		// Replace old item in sub-menu with new
		submenu = submenu.replace(menuitem,new_menuitem);
	}
	
	// Return altered sub-menu
	return submenu;
}

/*
#
# Configure the menulist and the path at the same time.
#
# Paths replace the {PATH} string in the menulist, use '' for no link
# but you MUST include it if there is a valid menulist item.
#
*/

// Choose the menu default
var menudefault = 0;

// Add menu lists to the array
var menulist = new Array();
var pathlist = new Array();


// SERVER ROOT
var sRoot = 'http://www.mwkl.co.uk/';

//web root
menulist[0] = sRoot;
pathlist[0] = '';

/*
*
* Example (Note: Paths may be relative):
*
* menulist[1] = '<a href="{PATH}index.php" class="menu">My Main Page</a> | <a href="{PATH}other.php" class="menu">My Other Page</a>';
* pathlist[1] = 'http://www.mwkl.co.uk/';
*
*/

//about us
menulist[1] = '<a href="{PATH}overview.php" class="menu">Overview</a> | <a href="{PATH}history.php" class="menu">History</a> | <a href="{PATH}policies_standards.php" class="menu">Policies & Standards</a> | <a href="{PATH}location.php" class="menu">Location</a> | <a href="{PATH}facilities.php" class="menu">Facilities</a> | <a href="{PATH}resources.php" class="menu">Resources</a> | <a href="{PATH}sustainability.php" class="menu">Sustainability</a>';
pathlist[1] = sRoot + '1.AboutUs/';

//Product Lines
menulist[2] = '<a href="{PATH}lng.php" class="menu">LNG</a> | <a href="{PATH}gas_processing.php" class="menu">Gas Processing</a> | <a href="{PATH}refining.php" class="menu">Refining</a> | <a href="{PATH}fertilisers.php" class="menu">Fertilisers</a> | <a href="{PATH}polymers.php" class="menu">Polymers</a> | <a href="{PATH}petrochemicals.php" class="menu">Petrochemicals</a> | <a href="{PATH}chemicals.php" class="menu">Chemicals</a>';
pathlist[2] = sRoot + '2.ProductLines/';

//Services
menulist[3] = '<a href="{PATH}capabilities.php" class="menu">Capabilities</a> | <a href="{PATH}studies.php" class="menu">Studies</a> | <a href="{PATH}basic_engineering.php" class="menu">Basic Engineering</a>  | <a href="{PATH}epc.php" class="menu">EPC</a> | <a href="{PATH}project_management.php" class="menu">Project Management</a> | <a href="{PATH}technical_consultancy.php" class="menu">Technical Consultancy</a>';
pathlist[3] = sRoot + '3.Services/';

//Procurement
menulist[4] = '<a href="{PATH}introduction.php" class="menu">Introduction</a> | <a href="{PATH}suppliers.php" class="menu">Suppliers</a> | <a href="http://www.mwklsupplier.com" target="_blank" class="menu">MWKL Supplier Portal</a>';
pathlist[4] = sRoot + '4.Procurement/';

//Portfolio
menulist[5] = '<a href="{PATH}clients_list.php" class="menu">Clients</a> | <a href="{PATH}projects.php" class="menu">Projects</a>';
pathlist[5] = sRoot + '5.Portfolio/';

//Careers
menulist[6] = '<a href="{PATH}opportunities.php" class="menu">Opportunities</a> | <a href="{PATH}graduates_home.php" class="menu">Graduates</a> | <a href="{PATH}community.php" class="menu">Community</a> | <a href="{PATH}home.html" class="menu">MWKL PodCast</a>';
pathlist[6] = sRoot + '6.Careers/';

//News Center
menulist[7] = '<a href="{PATH}events.php" class="menu">Events</a> | <a href="{PATH}press_releases.php" class="menu">Press Releases</a> | <a href="{PATH}brochures_publications.php" class="menu">Brochures & Publications</a>';
pathlist[7] = sRoot + '7.NewsCentre/';

//Contacts
menulist[8] = '<a href="{PATH}Sales_and_Media_Contacts.php" class="menu">Sales & Media</a> | <a href="{PATH}personnel_Contacts.php" class="menu">Personnel</a> | <a href="{PATH}Procurement_Contacts.php" class="menu">Procurement</a> | <a href="{PATH}Technology_Contacts.php" class="menu">Technologies</a> | <a href="{PATH}world_office_locations.php" class="menu">World Office Locations</a>';
pathlist[8] = sRoot + '8.Contacts/';

//login
menulist[9] = '(No sub-menu available)';
pathlist[9] = sRoot + '9.Login/';

//Subcontracts
menulist[10] = '<a href="{PATH}introduction.php" class="menu">Introduction</a> | <a href="{PATH}sub_contractors.php" class="menu">Subcontractors</a>';
pathlist[10] = sRoot + '10.Subcontracts/';

//Construction
menulist[11] = '(No sub-menu available)';
pathlist[11] = sRoot + '11.Construction/';

// Do a quick replace of the path for all
// the menulist items
for (i=0;i<menulist.length;i++) {
	// Do path replace
	menulist[i] = menulist[i].replace(/\{PATH\}/g,pathlist[i]);
}

// Declare timeout holder
var menu_rollback;
// Declare rollback timeout (in milliseconds)
var menu_rollback_time = 5000;
// Declare default menu
var default_menu = -1;

function submenu(index) {
	// Do default menu handling
	if (default_menu == -1) {
		default_menu = index;
		menulist[index] = subMenuClassChange(menulist[index],'menu','menu_highlight');
		// Assign realm underline image
		setHeading(index);
	}
	// Load current submenu
	var obj;
	obj = document.getElementById('SubMenu');
	obj.innerHTML = menulist[index];
}

// Declare timeout holder
var menu_rollback_img;
// Declare rollback timeout (milliseconds)
// --- should be the same as menu_rollback_time!!!
var menu_rollback_img_time = menu_rollback_time;
// Declare default "On" image + src
var default_menu_img = '';

function yaImgChange(imgid) {
	// Reset headings
	for (i=1;i<mnuId.length;i++) {
		document.getElementById(mnuId[i]).className = 'mnuMain';
	}
	// Make chosen image "live"
	//document.getElementById(imgid).src = imgSrc;
	//alert(imgid + " | " + imgSrc + " | " + pathToImages);
	document.getElementById('mnu_' + imgid).className = 'mnuMainOn';
}

function rollbackMenu() {
	// Clear timeouts if needed
	if (menu_rollback) {
		clearTimeout(menu_rollback);
	}
	if (menu_rollback_img) {
		clearTimeout(menu_rollback_img);
	}
	// Set timers to rollback blocks
	menu_rollback = setTimeout('submenu(' + default_menu + ');',menu_rollback_time);
	if (default_menu != 0) {
		// Business as usual
		menu_rollback_img = setTimeout('yaImgChange("' + default_menu_img + '");',menu_rollback_img_time);
	}
}


/**
 * getParentFolder()
 *
 * Returns the parent folder name
 * of the current document. Note:
 * If the URL does not have a
 * trailing '/' then it will give
 * the parent of the parent.
 *
 * Author:	AJ Hill
 * Date:	2004-09-27
 *
 * @return String parent folder name
 */
 
function getParentFolder() {

	// Get URL
	var file = document.URL;

	// Clear local path backslashes
	file = file.replace(/\\/g,'/');

	// Define regex object
	var myregex = /[\/]{0,1}[^\/]*$/gi;
	
	// Take the current file's name from the URL
	var filename = myregex.exec(file);

	// Define regex object 2
	var myregex = /([\/])([^\/]*?)([\/])$/gi;

	// Grab only the immediate parent folder name
	var file2 = file.replace(filename,'/');
	file = myregex.exec(file2);
	file = file[2];

	// return result
	return file;
}


var mnuId = new Array();
mnuId[1] = "mnu_about";;
mnuId[2] = "mnu_product_lines";
mnuId[3] = "mnu_services";
mnuId[4] = "mnu_procurement";
mnuId[10] = "mnu_subcontracts";
mnuId[11] = "mnu_construction";
mnuId[5] = "mnu_portfolio";
mnuId[6] = "mnu_careers";
mnuId[7] = "mnu_news";
mnuId[8] = "mnu_contacts";
mnuId[9] = "mnu_clients";


function setHeading(index){
	// Underline the current main menu area by default
	//alert('setHeading() just called with index: ' + index);
	switch (index) {
		case 0:
			// Home page
			subMenuClassChange();
		break;
		case 1:
			default_menu_img = 'about';
		break;
		case 2:
			default_menu_img = 'product_lines';
		break;
		case 3:
			default_menu_img = 'services';
		break;
		case 4:
			default_menu_img = 'procurement';
		break;
		case 5:
			default_menu_img = 'portfolio';
		break;
		case 6:
			default_menu_img = 'careers';
		break;
		case 7:
			default_menu_img = 'news';
		break;
		case 8:
			default_menu_img = 'contacts';
		break;
		case 9:
			default_menu_img = 'clients';
		break;
		case 10:
			default_menu_img = 'subcontracts';
		break;
		case 11:
			default_menu_img = 'construction';
		break;
		default:
			alert('setHeading() does not recognize index: ' + index);
		break;
	}
	// for anything other than the homepage...
	if (index != 0) {
		// Run the highlight
		yaImgChange(default_menu_img);
	}
}

function timeoutClearer() {
	// Clear timeouts if needed
	if (menu_rollback) {
		clearTimeout(menu_rollback);
	}
	if (menu_rollback_img) {
		clearTimeout(menu_rollback_img);
	}
}

/*
#
# goDefault (Main Menu Click Link)
#
# You need to map the path indexes to the buttons
# below as specified above.
#
*/

function goDefault(imgid) {
	switch (imgid) {
		case 'about':
			window.location.replace(pathlist[1]);
		break;
		case 'product_lines':
			window.location.replace(pathlist[2]);
		break;
		case 'services':
			window.location.replace(pathlist[3]);
		break;
		case 'procurement':
			window.location.replace(pathlist[4]);
		break;
		case 'subcontracts':
			window.location.replace(pathlist[10]);
		break;
		case 'construction':
			window.location.replace(pathlist[11]);
		break;
		case 'portfolio':
			window.location.replace(pathlist[5]);
		break;
		case 'careers':
			window.location.replace(pathlist[6]);
		break;
		case 'news':
			window.location.replace(pathlist[7]);
		break;
		case 'contacts':
			window.location.replace(pathlist[8]);
		break;
		case 'clients': /* AKA clients login */
			window.location.replace(pathlist[9]);
		break;
		default:
			alert('menu.js has encountered an unknown id (' + imgid + ')');
		break;
	}
}

// MWKL Index Search
function mwklIndexSearch() {
    if (searchform.search_field.value == "" || searchform.search_field.value == "Type Text Here") {
        alert('You cannot search the MWKL search facility\nusing a blank field - please type in a text!');
    } else {
		document.searchform.action="../search/search_results.php";
        searchform.submit();
    }
}
