/* -------------------------------------------------------------------------
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.
hr = new menu('Human Resources','hr.html');
hr.addMenuItem('HR Delegations','hr_delegations.html');
hr.addMenuItem('HR Policies &amp; Forms','hr_p_p.html');
hr.addMenuItem('ARTC Enterprise Agreements','hr_enterprise_agreements.html');
hr.addMenuItem('Vacancies','Dynamic/hrvacancies/hr_vacancies.aspx');
hr.addMenuItem('Performance Management System','hr_performance_management_system.html');
hr.addMenuItem('Employee Assistance Programme','hr_eap.html');
hr.addMenuItem('HR Bulletins','hr_bulletins.html');

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('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(ccs_rm);
ccs.addMenuItem('Signals','ccs_signals.html');

eng_pp = new menu('Policies &amp; Procedures','engineering_p_p.html','pp',true);
eng_pp.addMenuItem('Forms','engin_pp_std_forms.html','',true);

eng_signalling = new menu('Signalling','engineering_signalling.html','sign',true);
eng_signalling.addMenuItem('Standards &amp; Procedures','engineering_signalling_sp.html','',true);
eng_signalling.addMenuItem('Service Schedules','engineering_signalling_ss.html','',true);
eng_signalling.addMenuItem('Forms','engineering_signalling_f.html','',true);
eng_signalling.addMenuItem('Support Documents','engineering_signalling_sd.html','',true);

eng_com_std = new menu('Common Standards','engineering_common_standards.html','cstd',true);
eng_com_std.addMenuItem('Track &amp; Civil','engin_comm_std_track.html','',true);
eng_com_std.addMenuItem('Signalling','engin_comm_std_signal.html','',true);

eng_nsw_std = new menu('NSW Standards','engineering_nsw_standards.html','nstd',true);
eng_nsw_std.addMenuItem('Track Geometry Std.','engin_nsw_std_trackgeo.html','',true);
eng_nsw_std.addMenuItem('Bridges &amp; Structure','engin_nsw_std_bridges.html','',true);
eng_nsw_std.addMenuItem('Track','engin_nsw_std_track.html','',true);
eng_nsw_std.addMenuItem('Right of Way','engin_nsw_std_right.html','',true);
eng_nsw_std.addMenuItem('Turnouts','engin_nsw_std_turnouts.html','',true);
eng_nsw_std.addMenuItem('Level Crossings','engin_nsw_std_level.html','',true);
eng_nsw_std.addMenuItem('Signalling','engin_nsw_std_signalling.html','',true);
eng_nsw_std.addMenuItem('Electrical','engin_nsw_std_electrical.html','',true);
eng_nsw_std.addMenuItem('Communications','engin_nsw_std_comm.html','',true);
eng_nsw_std.addMenuItem('Mobile Plant','engin_nsw_std_mobile.html','',true);
eng_nsw_std.addMenuItem('Services','engin_nsw_std_services.html','',true);
eng_nsw_std.addMenuItem('Rolling Stock','engin_nsw_std_rollingstock.html','',true);
eng_nsw_std.addMenuItem('Forms','engin_nsw_std_exam.html','',true);

eng_tec = new menu('Technical Bulletins','engineering_tech.html','tech',true);
eng_tec.addMenuItem('Engineering Notes/Manuals','engineering_tech_manuals.html','',true);
eng_tec.addMenuItem('Engineering Instructions','engineering_eng_instruc.html','',true);
eng_tec.addMenuItem('Engineering Bulletins','engineering_eng_bulletins.html','',true);
eng_tec.addMenuItem('Commentaries','engineering_tech_commentaries.html');
eng_tec.addMenuItem('New Equip &amp; Sys Approvals','engineering_tech_type.html','',true);
eng_tec.addMenuItem('Suppliers','engineering_suppliers.html');
eng_tec.addMenuItem('Waivers','engineering_tech_waivers.html','',true);

eng_inf_con = new menu('Configuration Management','engineering_infra_config.html','infr',true);
eng_inf_con.addMenuItem('Infrastructure System Maps','engineering_infra_maps.html','',true);
eng_inf_con.addMenuItem('Curve &amp; Gradient Details','engineering_cd_details.html','',true);
eng_inf_con.addMenuItem('Network Alteration Notices','engineering_nan.html');
eng_inf_con.addMenuItem('Configuration Management Manual','engineering_cmm.html');

engineering = new menu('Engineering','engineering.html', 'engineering',true);
engineering.addMenuItem(eng_pp,'','',true);
engineering.addMenuItem(eng_signalling,'','',true);
engineering.addMenuItem('ARTC CoP','engineering_artc_cop.html','acop',true);
engineering.addMenuItem(eng_com_std,'','',true);
engineering.addMenuItem(eng_nsw_std,'','',true);
engineering.addMenuItem('Western Jurisdiction','engineering_western.html','west',true);
engineering.addMenuItem('Victoria','engineering_victoria.html','vic',true);
engineering.addMenuItem('North-South Corridor','engin_nsw_std_nscor.html','nscr',true);
engineering.addMenuItem(eng_tec,'','',true);
engineering.addMenuItem('Work Method Statements','engineering_nsw_work.html');
engineering.addMenuItem(eng_inf_con,'','',true);
engineering.addMenuItem('Drawing Management','engineering_drawing.html','draw',true);

// this is a special page that is only visible on the extranet
if(location.href.search('extranet') > -1){
	engineering.addMenuItem('Plant &amp; Equipment','engineering_plant_equip_ex.html','pleq',true);
}

engineering.addMenuItem('Aust. Standards Online','engineering_online.html');
engineering.addMenuItem('Communications','engineering_communications.html');
engineering.addMenuItem('Wayside Equipment','engineering_wayside.html');
engineering.addMenuItem('Plant &amp; Equipment (Internal)','engineering_plant_equip.html');
engineering.addMenuItem('Asset Systems User Group','engineering_asset_sys_usergroup.html');
engineering.addMenuItem('Track Recording Car','engineering_ak_car.html');
engineering.addMenuItem('Document Library','engineering_doc_lib.html');
engineering.addMenuItem('Consultant Capabilities','engineering_con_cap.html');

property = new menu('Property','property.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','dynamic/OnCallLists/operations.aspx', '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('Occupational Health &amp; Safety (OHS)','safety_ohs.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('Policies &amp; Procedures','intranet_p_p.html');
indexes.addMenuItem('Configurations','intranet_configurations.html');
indexes.addMenuItem('Standards & Manuals','intranet_standards_manuals.html');
indexes.addMenuItem('Forms','intranet_forms.html');
indexes.addMenuItem('Reports','intranet_reports.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://www.artc.com.au/Content.aspx?p=144','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=\"top-nav\"><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 + "<ul>" + parseMenu(intranet) + "</ul>"; 
	
	// this calls the SUPERFISH menus that create menus with a delay
	if(location.href.search('extranet') > -1){
		// on the extranet, the superfish script needs to be called on the inner #nav ul 
		// otherwise the root engineering menu item will become the first level of the menu with the subsequent levels as drop downs (which we don't want)
		$('#nav ul ul').superfish();
	} else {
	   	$('#nav ul').superfish();
	}
});


