/* -------------------------------------------------------------------------
DATE:         2009
AUTHOR:       Thanh Vu (where otherwise not credited)
USAGE:        Intranet
------------------------------------------------------------------------- */

// =========================================================================
// ON LOAD EVENT LOADER
// =========================================================================
// enabling multiple onload functions via addLoadEvent method 
// courtesy of Simon Willison: 
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// ==================================================================================================
// EXTRANET DISCLAIMER
// ==================================================================================================
// creates a disclaimer alert when links (inside tables) are opened on the extranet
addLoadEvent( 
function() {
	// check first if this is the extranet
	if(location.href.search('extranet') > -1){
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++){
			var link_string = String(links[i]);
			// apply confirmation dialog only to document links 
			// (anything that isn't htm*, asp* or email link on the extranet domain)
			if(link_string.indexOf('.htm') < 0 && link_string.indexOf('.asp') < 0 && link_string.indexOf('mailto:') < 0 && link_string.indexOf('extranet') > 0){
				links[i].onclick = popUp;
			}
		}
	}
}
);

// creates a confirmation dialog
function popUp(){
	var result;
	var response = confirm('© Australian Rail Track Corporation Limited\n\nDisclaimer:\nThis document has been prepared by ARTC for internal use and may not be relied on by any other party without ARTC\'s prior written consent.  Use of this document shall be subject to the terms of the relevant contract with ARTC.\n\nARTC and its employees shall have no liability to unauthorised users of the information for any loss, damage, cost or expense incurred or arising by reason of an unauthorised user using or relying upon the information in this document, whether caused by error, negligence, omission or misrepresentation in this document.\n\nThis document is uncontrolled when printed.\n\nIn order to access this document you are required to acknowledge and accept the above terms and conditions by selecting "OK".  If you choose not to accept please select "Cancel"')
		if (response == true) {
			result = true;
		} else {
			result = false;
		}
	return result;
}

// =========================================================================
// MENU
// =========================================================================
// name: string. the menu label
// url: string. the url for this menu
// css_class: optional string. any class you would like applied to this menu
// extranet_copy: optional boolean. does this menu get copied to the extranet?
function menu(name, url, css_class, extranet_copy){
	this.name = name;
	this.url = url;
	this.css_class = css_class;	
	this.extranet_copy = extranet_copy;
	this.sub_menu_names = new Array();
	this.sub_menu_urls = new Array();
	this.sub_menu_classes = new Array();
	this.sub_menu_extranet_copy = new Array();
	this.addMenuItem = addMenuItem;
}

// -----------------------------------------------------------------------
function addMenuItem(name, url, css_class, extranet_copy){
	this.sub_menu_names[this.sub_menu_names.length] = name;
	this.sub_menu_urls[this.sub_menu_urls.length] = url;
	this.sub_menu_classes[this.sub_menu_classes.length] = css_class;
	this.sub_menu_extranet_copy[this.sub_menu_extranet_copy.length] = extranet_copy;
}

// -----------------------------------------------------------------------
// accepts root menu item to generate unordered list
// this method fills a div with an id="nav" with the menu code
// contents of this div tell the function:
// dir switches: applies any folder/dir switches you like to the beginning of non-absolute urls
// current page: case sensitive search that highlights the current page in the menu
function parseMenu(menu){
	// checks to see if the current domain is the extranet or search domain
	// if not, returns -1
	extranet = location.href.search('extranet');
	intranet_search = location.href.search('searchdev');
	site_domain = "http://intranet.artc.com.au/";
	content = "";
	var url;
	var css_class;
	
	// get the current page title and any dir switches
	var page_args = document.getElementById("nav").innerHTML.split(",");
	dir = page_args[0];
	var current_section = page_args[1].replace('\n', '', 'g');
	
	// strips out trailing spaces...
	if (current_section.charAt(current_section.length-1) == " "){
		current_section = current_section.substring(0,current_section.length-1);
	}
	
	// get the current page filename
	var current_page = document.URL.substr(document.URL.lastIndexOf('/')+1);
	
	// root menu has no name - do not display
	if ( menu.name != "" ){		
		// check if the url:
		// - is absolute
		// - being run on a different domain to the rest of the intranet documents
		// if so, do not append dir switch
		( menu.url.match("http://" ) || intranet_search > -1 ? url = menu.url : url = dir + menu.url );
		
		// if menu is being run on a different domain, make url absolute
		if( intranet_search > -1 ){
			url = site_domain + menu.url;
		}
		
		// has a css class been specified?
		( menu.css_class ? css_class =  'parent ' + menu.css_class : css_class = 'parent');
		
		// search and highlight current page title
		if( current_section == menu.name || current_page == menu.url.substr(menu.url.lastIndexOf('/')+1) ){
			css_class += ' current';
		}
		
		// if there are css classes, create the class attribute to be added to the link
		if( css_class.length != 0 ){
			css_class = '" class="' + css_class;
		}
		
		// check to see if this is the extranet, if so, only copy extranet items
		if(extranet > -1 && menu.extranet_copy == true){
			content += '<li><a href="' + url + css_class + '">' + menu.name + '</a><ul>';
		} else if(extranet == -1) {
			content += '<li><a href="' + url + css_class + '">' + menu.name + '</a><ul>';
		}
	}
	
	// hooray for recursion! print out submenus and their links
	for ( var n=0; n<menu.sub_menu_names.length; n++ ) {
		if ( typeof(menu.sub_menu_names[n] ) != 'object') {
			url = css_class = '';
			
			// check if the url:
			// - is absolute
			// - being run on a different domain to the rest of the intranet documents
			// if so, do not append dir switch
			( menu.sub_menu_urls[n].match("http://") || intranet_search > -1 ? url = menu.sub_menu_urls[n] : url = dir + menu.sub_menu_urls[n] );
			
			// if menu is being run on a different domain, make url absolute
			if( intranet_search > -1 ){
				url = site_domain + menu.sub_menu_urls[n];
			}
			
			// has a css class been specified?
			( menu.sub_menu_classes[n] ? css_class = menu.sub_menu_classes[n]: css_class = '');
			
			// search and highlight current page title
			if( current_section == menu.sub_menu_names[n] || current_page == menu.sub_menu_urls[n].substr(menu.sub_menu_urls[n].lastIndexOf('/')+1)){
				css_class += ' current'
			}
			
			// if there are css classes, create the class attribute to be added to the link
			if( css_class.length != 0 ){
				css_class = '" class="' + css_class;
			}
			
			// check to see if this is the extranet, if so, only copy extranet items
			// i pity the fool that has to modify this code - most likely probably me
			if(extranet > -1 && menu.sub_menu_extranet_copy[n] == true){
				content +=  '<li><a href="' + url + css_class + '">' + menu.sub_menu_names[n] + '</a></li>';
			} else if(extranet == -1) {
				content +=  '<li><a href="' + url + css_class + '">' + menu.sub_menu_names[n] + '</a></li>';
			}
		} else {
			// check to see if this is the extranet, if so, only copy extranet items
			if(extranet > -1 && menu.sub_menu_extranet_copy[n] == true){
				content += parseMenu(menu.sub_menu_names[n], dir);
			} else if(extranet == -1) {
				content += parseMenu(menu.sub_menu_names[n], dir);
			}
		}
	}
	
	content += '</ul></li>';
	return content;
}

// -----------------------------------------------------------------------
// construct the menus - last updated: 7/12/2007
// updated 7/12/2007 by AJ: I updated the links for web forms as they now point to "dynamic" folder where web forms will live.
//ohs = new menu('Occupational Health &amp; Safety','safety_ohs.html');
//ohs.addMenuItem('Forms, Policies &amp; Procedures','safety_ohs_fpp.html');

hr = new menu('People Culture &amp; Development','hr.html');
hr.addMenuItem('Delegations','hr_delegations.html');
hr.addMenuItem('Policies &amp; Forms','hr_p_p.html');
hr.addMenuItem('Enterprise Agreements','hr_enterprise_agreements.html');
hr.addMenuItem('Vacancies','PersonnelVacancies/hr_vacancies.aspx');
hr.addMenuItem('Performance Management System','hr_performance_management_system.html');
hr.addMenuItem('Employee Assistance Program','hr_eap.html');
hr.addMenuItem('Employee Bulletins','hr_bulletins.html');
hr.addMenuItem('Rail Safety Worker Competence','hr_safety.html');
hr.addMenuItem('Work Health &amp; Safety','pcd_whs.html');
//hr.addMenuItem(ohs);
hr.addMenuItem('Infrastructure Maintenance Competency Assessment', 'pcd_im_competency.aspx');

ccs_tsi = new menu('Technology','ccs_tsi.html');
ccs_tsi.addMenuItem('Policies, Procedures &amp; Forms','ccs_tsi_p_p.html');
ccs_tsi.addMenuItem('Systems Documentation','ccs_tsi_systems.html');
ccs_tsi.addMenuItem('Business Systems Procedures','ccs_tsi_bsp.html');
ccs_tsi.addMenuItem('iTIPS','ccs_tsi_itips.html');

ccs_rm = new menu('Records Management','ccs_records.html');
ccs_rm.addMenuItem('Policies &amp; Procedures','ccs_records_p_p.html');
ccs_rm.addMenuItem('Training &amp; Development','ccs_records_td.html');
ccs_rm.addMenuItem('eTrak Quick Reference Guides','ccs_records_tm.html');
ccs_rm.addMenuItem('eTricks','ccs_records_etricks.html');
ccs_rm.addMenuItem('FAQs','ccs_records_faqs.html');

ccs = new menu('C&amp;CS','ccs.html');
ccs.addMenuItem(ccs_tsi);
ccs.addMenuItem('ATMS','ccs_atms.html');
ccs.addMenuItem('Communications','ccs_communications.html');
ccs.addMenuItem('Geospatial Information Services','ccs_gis.html');
ccs.addMenuItem(ccs_rm);
ccs.addMenuItem('Signals','ccs_signals.html');

eng_all = new menu('All Disciplines','eng_all.html','all',true);
eng_all.addMenuItem('Procedures','eng_all_procedure.html','',true);
eng_all.addMenuItem('Work Instructions','eng_all_workinstruct.html','',true);
eng_all.addMenuItem('Forms','eng_all_form.html','',true);
eng_all.addMenuItem('Guidelines','eng_all_guideline.html','',true);

eng_sig = new menu('Signalling','eng_signal.html','sig',true);
eng_sig.addMenuItem('Procedures','eng_signal_procedure.html','',true);
eng_sig.addMenuItem('Work Instructions','eng_signal_workinstruct.html','',true);
eng_sig.addMenuItem('Forms','eng_signal_form.html','',true);
eng_sig.addMenuItem('Guidelines','eng_signal_guideline.html','',true);
eng_sig.addMenuItem('Manufacturer / Supplier Manuals','eng_signal_supply.html');

eng_ele = new menu('Electrical','eng_electrical.html','ele',true);
eng_ele.addMenuItem('Procedures','eng_electrical_procedure.html','',true);

eng_tnc = new menu('Track and Civil','eng_track-civil.html','tnc',true);
eng_tnc.addMenuItem('Procedures','eng_track-civil_procedure.html','',true);
eng_tnc.addMenuItem('Work Instructions','eng_track-civil_workinstruct.html','',true);
eng_tnc.addMenuItem('Forms','eng_track-civil_form.html','',true);
eng_tnc.addMenuItem('Guidelines','eng_track-civil_guideline.html','',true);
eng_tnc.addMenuItem('Manufacturer / Supplier Manuals','eng_track-civil_supply.html');

eng_rol = new menu('Rolling Stock','eng_rolling-stock.html','rol',true);
eng_rol.addMenuItem('Procedures','eng_rolling-stock_procedure.html','',true);
eng_rol.addMenuItem('Forms','eng_rolling-stock_form.html','',true);

eng_pne = new menu('Plant and Equipment','eng_plant-equip.html');
eng_pne.addMenuItem('Procedures','eng_plant-equip_procedure.html');
eng_pne.addMenuItem('Forms','eng_plant-equip_form.html');
eng_pne.addMenuItem('Guidelines','eng_plant-equip_guideline.html');

eng_com = new menu('Communications','eng_comms.html','com',true);
eng_com.addMenuItem('Procedures','eng_comms_procedure.html','',true);

eng_app = new menu('Type Approvals','eng_type-approvals.html','app',true);
eng_app.addMenuItem('Signalling','eng_type-approvals_signal.html','',true);
eng_app.addMenuItem('Track &amp; Civil','eng_type-approvals_track-civil.html','',true);

eng_net = new menu('Network Configuration','eng_network-config.html','net',true);
eng_net.addMenuItem('Drawing Management','eng_network-config_drawing.html','',true);
eng_net.addMenuItem('Network Alteration Notices','eng_network-config_nan.html');
eng_net.addMenuItem('Infrastructure System Maps','eng_network-config_infrastructure.html','',true);
eng_net.addMenuItem('Curve &amp; Gradient Details','eng_network-config_cd.html','',true);

eng_mon = new menu('Condition Monitoring','eng_condition-monitoring.html','mon');
eng_mon.addMenuItem('Wayside Equipment','eng_condition-monitoring_wayside.html');
eng_mon.addMenuItem('Track Recording Car','eng_condition-monitoring_ak.html','',true);


eng_sup = new menu('Support Resources','eng_support.html','sup');
eng_sup.addMenuItem('External Standards','eng_support_external.html');
eng_sup.addMenuItem('Document Library','eng_support_doc-library.html');

eng_hel = new menu('Help','eng_help.html','hel',true);
eng_hel.addMenuItem('Contact Us','Notifications/engineering_enquiry.aspx','',true);
eng_hel.addMenuItem('Recent Changes','eng_help_changes.html','',true);
eng_hel.addMenuItem('Document Definitions','eng_help_definitions.html','',true);

// on the extranet, don't display a base document
if(location.href.search('extranet') > -1){
	engineering = new menu('','', '',true);
} else {
	engineering = new menu('Engineering','eng.html', 'engineering',true);	
}

engineering.addMenuItem(eng_all,'','',true);
engineering.addMenuItem(eng_sig,'','',true);
engineering.addMenuItem(eng_ele,'','',true);
engineering.addMenuItem(eng_tnc,'','',true);
engineering.addMenuItem(eng_rol,'','',true);
engineering.addMenuItem('Operations','eng_operations.html','ops',true);

// this is a special page that is only visible on the extranet
if(location.href.search('extranet') > -1){
	engineering.addMenuItem('Plant and Equipment','eng_plant-equip-ex.html','pne', true);
} else {
	engineering.addMenuItem(eng_pne);	
}

engineering.addMenuItem(eng_com,'','',true);
engineering.addMenuItem('Hunter 200+','eng_hunter200.html');
engineering.addMenuItem('Waivers','eng_waivers.html','wai',true);
engineering.addMenuItem(eng_app,'','',true);
engineering.addMenuItem('Work Method Statements','eng_wms.html','wms');
engineering.addMenuItem(eng_net,'','',true);
engineering.addMenuItem(eng_mon);
engineering.addMenuItem(eng_sup);
engineering.addMenuItem(eng_hel,'','',true);

property = new menu('Property','property.html');
property.addMenuItem('Delegations','property_delegations.html');
property.addMenuItem('Leases, Licences, Access','property_leases.html');
property.addMenuItem('Maps &amp; Diagrams - NSW','property_maps_nsw.html');
property.addMenuItem('Maps &amp; Diagrams - SA/WA','property_maps_sa_wa.html');
property.addMenuItem('Maps &amp; Diagrams - VIC','property_maps_vic.html');
property.addMenuItem('Policies &amp; Procedures','property_p_p.html');

property_heritage = new menu('Property - Heritage','property_heritage.html');
property_heritage.addMenuItem('Heritage - NSW','property_heritage_nsw.html');
property_heritage.addMenuItem('Heritage - SA','property_heritage_svw.html');

property_environment = new menu('Property - Environment','property_environment.html');
property_environment.addMenuItem('Environment - NSW','property_environment_nsw.html');
property_environment.addMenuItem('Environment - SA','property_environment_svw.html');
property_environment.addMenuItem('Environment - QLD','property_environment_qld.html');

finance_procurement = new menu('Procurement','finance_procurement.html');
finance_procurement.addMenuItem('Purchasing - Policies &amp; Forms','finance_procurement_purchasing.html');

finance_contracts = new menu('Contracts Management','finance_contracts.html');
finance_contracts.addMenuItem('Policies &amp; Forms','finance_contracts_pp.html');

finance_payroll = new menu('Payroll','finance_payroll.html');
finance_payroll.addMenuItem('Policies &amp; Forms','finance_payroll_p_p.html');

finance = new menu('Finance','finance.html', 'finance');
finance.addMenuItem('Policies Procedures &amp; Forms','finance_p_p.html');
finance.addMenuItem(finance_procurement);
finance.addMenuItem('Delegations','finance_delegations.html');
finance.addMenuItem(finance_payroll);
finance.addMenuItem(finance_contracts);
finance.addMenuItem('Financial Systems','finance_systems.html');
finance.addMenuItem(property);
finance.addMenuItem(property_heritage);
finance.addMenuItem(property_environment);

operations_toc_waiver = new menu('Toc Waiver','operations_toc_archive.html');
operations_toc_waiver.addMenuItem('Archive','operations_toc_archive.html');
operations_toc_waiver.addMenuItem('Indemnity Form','operations_toc_indemnity_form.html');

operations = new menu('Operations','operations.html', 'operations');

operations.addMenuItem('Policies &amp; Procedures','operations_p_p.html');
operations.addMenuItem('NSW Policies &amp; Procedures','operations_nsw_p_p.html');
operations.addMenuItem(operations_toc_waiver);
operations.addMenuItem('Performance Reports','operations_performance_reports.html');
operations.addMenuItem('TAA Number Generator', 'TrainAlterationAdvices/Index.aspx');

legal = new menu('Legal','legal.html');
legal.addMenuItem('Policies &amp; Procedures','legal_p_p.html');

comp = new menu('Compliance Management', 'safety_compliance.html');
comp.addMenuItem('Policies &amp; Procedures', 'safety_compliance_p_p.html');

sr = new menu('Safeworking Rules','safety_sr.html');
sr.addMenuItem('VIC','safety_sr_vic.html');
sr.addMenuItem('NSW (Non RailCorp)','safety_sr_nsw.html');
sr.addMenuItem('SA - WA / VIC - NSW','safety_sr_swvn.html');
sr.addMenuItem('ARTC Network-Wide','safety_sr_consolidated.html');

risk_safety = new menu('Risk &amp; Compliance','safety.html', 'safety');
risk_safety.addMenuItem('Accreditation Register','safety_accreditation.html');
risk_safety.addMenuItem(comp);
risk_safety.addMenuItem('Incident Investigations','safety_incident.html');
risk_safety.addMenuItem('Interface Agreements','dynamic/InterfaceAgreements/safety_interface.aspx');
risk_safety.addMenuItem('Internal Audits','safety_internal.html');
risk_safety.addMenuItem(legal);
//risk_safety.addMenuItem('NCOP Code of Practice','safety_ncop.html');
risk_safety.addMenuItem('Risk Management','Notifications/risk_management.aspx');
risk_safety.addMenuItem('Safety Mngt Plan','safety_manag.html');
risk_safety.addMenuItem(sr);

indexes = new menu('Indexes','intranet_index.html', 'indexes');
indexes.addMenuItem('All documents','\documentindex');
indexes.addMenuItem('Reports','intranet_reports.html');
indexes.addMenuItem('Project Management','Project Management_forms.html');
indexes.addMenuItem('News Archive','intranet_news.html');

help = new menu('Help','help.html', 'help');
help.addMenuItem('FAQ\'s','help.html');
help.addMenuItem('Application Help','help_online_learning.html');

executive = new menu('Executive','executive.html', 'executive');
executive.addMenuItem('ARTC Logo &amp; Colours','executive_artc_logo.html');
executive.addMenuItem('Templates &amp; Letterheads','executive_templates.html');

intranet = new menu('','','',true);
intranet.addMenuItem(executive);
intranet.addMenuItem(hr);
intranet.addMenuItem(ccs);
intranet.addMenuItem(engineering,'','',true);
intranet.addMenuItem(finance);
intranet.addMenuItem(operations);
intranet.addMenuItem(risk_safety);
intranet.addMenuItem('Phone Directory','phone_directory.html', 'phone');
intranet.addMenuItem(indexes);
intranet.addMenuItem(help);

// updated: 03/04/2008, by DTV
// this is used to create the faux internet menu (running above the engineering sections) for the extranet
if(location.href.search('extranet') > -1){
	extranet_top = new menu('','','',true);
	extranet_top.addMenuItem('About ARTC','http://www.artc.com.au/Content.aspx?p=14','nav-about',true);
	extranet_top.addMenuItem('ARTC News','http://www.artc.com.au/Article/Overview.aspx?p=4','nav-news',true);
	extranet_top.addMenuItem('Route Standards','http://www.artc.com.au/Content.aspx?p=15','nav-route',true);
	extranet_top.addMenuItem('Safety Management','http://www.artc.com.au/Content.aspx?p=16','nav-safety',true);
	extranet_top.addMenuItem('Access Seeker','http://www.artc.com.au/Content.aspx?p=17','nav-access',true);
	extranet_top.addMenuItem('WebRams Access','http://webrams.artc.com.au/','nav-webrams',true);
	extranet_top.addMenuItem('Investment Strategies','http://www.artc.com.au/Content.aspx?p=154','nav-investment',true);
	extranet_top.addMenuItem('Infrastructure Standards','http://extranet.artc.com.au/','current nav-infrastructure',true);
	extranet_top.addMenuItem('Operations Standards','http://www.artc.com.au/Content.aspx?p=43','nav-operations',true);
	extranet_top.addMenuItem('Property','http://www.artc.com.au/Content.aspx?p=22','nav-property',true);
	extranet_top.addMenuItem('Members','http://www.artc.com.au/Content_Secure.aspx?p=23','nav-members',true);
	extranet_top.addMenuItem('Careers','http://www.artc.com.au/Career/Content.aspx?p=24','nav-careers',true);
	extranet_top.addMenuItem('Contact','http://www.artc.com.au/Content.aspx?p=7&cp=7','nav-contact',true);
	
	extranet_footer = new menu('','','',true);
	extranet_footer.addMenuItem('Home','http://www.artc.com.au','nav-ftr-home',true);
	extranet_footer.addMenuItem('Site Info','http://www.artc.com.au/Content.aspx?p=11','nav-ftr-info',true);
	extranet_footer.addMenuItem('Site Map','http://www.artc.com.au/SiteMap.aspx?p=2','nav-ftr-map',true);
	extranet_footer.addMenuItem('Industry Links','http://www.artc.com.au/Content.aspx?p=12','nav-ftr-links',true);
}

// -----------------------------------------------------------------------
// write the menus
// this has to be run inside an addLoadEvent because we need to wait for the 
// DOM to be populated before we can read the page arguments
addLoadEvent( 
function() { 
	var parsed_extranet_top = '';
	if(location.href.search('extranet') > -1){
		// create the extranet menu if the script is being run on the extranet
		parsed_extranet_top =  "<div id=\"nav-top\"><ul>" + parseMenu(extranet_top) + "</ul></div>";
		
		// replace the header link so that it goes back to the internet home page
		document.getElementById("header").innerHTML = "<a href=\"http://www.artc.com.au\" title=\"Australian Rail Track Corporation Ltd.\"><img src=\"http://www.artc.com.au/library/logo-artc.gif\" alt=\"Australian Rail Track Corporation\" /></a>";
		
		// generate the footer
		document.getElementById("footer").innerHTML =  "<ul>" + parseMenu(extranet_footer) + "</ul>";

		document.getElementById("nav").innerHTML =  parsed_extranet_top + "<div id=\"nav-eng\"><ul>" + parseMenu(intranet) + "</ul></div>"; 
	} else {
		document.getElementById("nav").innerHTML =  "<ul>" + parseMenu(intranet) + "</ul>"; 
	}
	
	// this calls the SUPERFISH menus that create menus with a delay
	$('#nav ul').superfish();
});



