﻿// JavaScript Document

menuObj = function() {};

// general variables
menuObj.prototype.textPrintViewLink = '<!--| <a href="javascript:menuJS.biggerFont(); void(0);" id="mainMenuBiggerFont">Bigger Font Size</a> | <a href="javascript:menuJS.smallerFont(); void(0);" id="mainMenuSmallerFont">Smaller Font Size</a> -->| <a href="javascript: menuJS.closeAllSections(); void(0);" id="mainMenuOpenCloseAllSections">Close all</a> | <a href="javascript: menuJS.printView(); void(0);" id="mainMenuViewPrintFriendly">Printer Friendly</a> |';
menuObj.prototype.textScreenViewLink = '| <a href="javascript: menuJS.closeAllSections(); void(0);" id="mainMenuOpenCloseAllSections">Close all</a> | <a href="javascript: menuJS.screenView(); void(0);" id="mainMenuViewPrintFriendly">Screen View</a> |';
menuObj.prototype.textSectionMenuLink = '<span onMouseOut = "" onClick="return menuJS.minimizeSection(this.parentNode.parentNode.id);" style="cursor: hand"><img src="_graphics/common/button_min.png" alt="minimize" width="23" height="23" /></span><span onMouseOut = "" onClick="menuJS.closeSection(this.parentNode.parentNode.id);" style="cursor: hand"><img src="_graphics/common/button_close.png" alt="close" width="23" height="23" /></span>';

menuObj.prototype.CSS_Screen = "_css/site.css";
menuObj.prototype.CSS_Print  = "_css/site_print.css";
menuObj.prototype.CSS_Default = "_css/site";

menuObj.prototype.Graphic_Max_Button = "_graphics/common/button_max.png";
menuObj.prototype.Graphic_Min_Button = "_graphics/common/button_min.png";

menuObj.prototype.currentFontSize = 0;



// Preload the graphics
// Taken from MM DreamWeaver MX snippets
menuObj.prototype.simplePreload = function() { 
	var args = this.simplePreload.arguments;
	document.imageArray = new Array(args.length);
	
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}

// create all icons that will only be accessible on js-enabled browsers
menuObj.prototype.addObjectsOnPage = function() {
	var d = document.getElementsByTagName("div");

	for (var i = 0; i < d.length; i++) {
		if ("sectionMenu" == d[i].className) {
			d[i].innerHTML = this.textSectionMenuLink;
		}
	}

	// Update the linkToSections
	var d = document.getElementsByTagName("a");

	for (i = 0; i < d.length; i++) {
		if ("linksToSectionsHREF" == d[i].className) {
			var s = d[i].href;
			s = s.substring(s.indexOf("#") + 1);
			d[i].href = "javascript:menuJS.displaySection('" + s + "'); void(0);";
		}
	}
	
	// create the general menu
	var d = document.getElementById("generalMenuAtTop");
	d.innerHTML = this.textPrintViewLink; 
}

// minimize a section
menuObj.prototype.minimizeSection = function(obj) {
	try {
		var d = document.getElementById(obj);

		d.className = d.className.substring(0, d.className.indexOf("sectionMenuBottom"));		// hide the bottom border
	
		// check the children collections
		for (var i = 0; i < d.childNodes.length; i++) {
			var dc = d.childNodes[i];
			if ("sectionContent" == dc.className) {																						// shows the content
				dc.style.display = "none";
			}
		
			var dcs = dc.className + "";
			if (dcs.indexOf("sectionMenu") != -1) {
				dc.childNodes[0].onclick = function() {menuJS.maximizeSection(obj)};						// change the onclick function
				dc.childNodes[0].firstChild.src = this.Graphic_Max_Button												// change the button
				dc.className += " sectionMenuBottom";																						// hide the bottom line
			}
		}
	} catch(e) {
		// prevent a JS error message from popping up when the user tries to minimize a nonexistent section
	}
}

// maximize a section
menuObj.prototype.maximizeSection = function(obj) {
	try {
		var d = document.getElementById(obj);

		d.className = d.className.substring(0, d.className.indexOf("sectionMenuBottom"));		// hide the bottom border
	
		// check the children collections
		for (var i = 0; i < d.childNodes.length; i++) {
			var dc = d.childNodes[i];
			if ("sectionContent" == dc.className) {																						// shows the content
				dc.style.display = "";
			}
		
			var dcs = dc.className + "";
			if (dcs.indexOf("sectionMenu") != -1) {
				dc.childNodes[0].onclick = function() {menuJS.minimizeSection(obj)};						// change the onclick function
				dc.childNodes[0].firstChild.src = this.Graphic_Min_Button												// change the button
				dc.className = "sectionMenu";																										// display the bottom line
			}
		}
	} catch(e) {
		// prevent a JS error message from popping up when the user tries to maximise a nonexistent section
	}
}

// !!! NEED TO CHECK IF ALL SECTIONS HAVE BEEN CLOSED, UPDATE THE LINKS ABOVE !!!

// close a section
menuObj.prototype.closeSection = function(obj) {
	try {
		var d = document.getElementById(obj);
		d.style.display = "none";
	} catch(e) {
		// prevent a JS error message from popping up when the user tries to close a nonexistent section
	}
}

// show the hidden section
menuObj.prototype.showSection = function(obj) {
	try {
		var d = document.getElementById(obj);
		d.style.display = "";
	} catch(e) {
		// prevent a JS error message from popping up when the user tries to display a nonexistent section
	}
}

// display a section that otherwise minimized or closed
menuObj.prototype.displaySection = function(sect) {
	// show hidden section
	this.showSection("d_" + sect);
	
	// maximize the section 
	this.maximizeSection("d_" + sect);
	
	document.location = "#" + sect;
}

// close all sections
menuObj.prototype.closeAllSections = function() {
	for (var i = 0; i < this.sectionOrder.length; i++) {
		this.closeSection(this.sectionOrder[i]);
	}

	// change the words and link on the main menu	
	var d = document.getElementById("mainMenuOpenCloseAllSections");
	d.innerHTML = "Open All";		
	d.href = "javascript: menuJS.openAllSections()";
}

// open all sections
menuObj.prototype.openAllSections = function() {
	for (var i = 0; i < this.sectionOrder.length; i++) {
		this.showSection(this.sectionOrder[i]);
	}
	
	// change the words and link on the main menu
	var d = document.getElementById("mainMenuOpenCloseAllSections");
	d.innerHTML = "Close All";		
	d.href = "javascript: menuJS.closeAllSections()";
}

// load the printer css
menuObj.prototype.printView = function() {
	var lnk = document.getElementsByTagName("LINK")[0];
	lnk.href = this.CSS_Print;
		
	try {
	 	// create the general menu
		var d = document.getElementById("generalMenuAtTop");
		d.innerHTML = this.textScreenViewLink; 
	} catch (e) {
		lnk.href = this.CSS_View;
	}
}

menuObj.prototype.screenView = function() {
	// revert the view back to the screen view
	var lnk = document.getElementsByTagName("LINK")[0];
	lnk.href = this.CSS_Screen;
	
 	// create the general menu
	var d = document.getElementById("generalMenuAtTop");
	d.innerHTML = this.textPrintViewLink; 
}

menuObj.prototype.changeFontSize = function(size) {
	var s = "";
	
	if (this.currentFontSize < 0) {
		s = "-1";
	} else if (this.currentFontSize > 0) {
		s = "_1";
	}
	
	var lnk = document.getElementsByTagName("LINK")[0];
	lnk.href = this.CSS_Default + s + ".css";
}

// make a bigger font (20% for every step)
menuObj.prototype.biggerFont = function() {
	if (this.currentFontSize < 6) {
		this.currentFontSize++;
		this.changeFontSize();
	}
}

// make a smaller font (20% for every step);
menuObj.prototype.smallerFont = function() {
	if (this.currentFontSize > -6) {
		this.currentFontSize--;
		this.changeFontSize();
	}
}

// initialize the object
menuObj.prototype.init = function() {
	// get the name of sections and put them in an array
	this.sectionOrder = new Array();
	
	var d = document.getElementsByTagName("div");
	for (var i = 0; i < d.length; i++) {
		if ("sectionBox" == d[i].className) {
			this.sectionOrder[this.sectionOrder.length] = d[i].id;
		}
	}

	this.simplePreload('/_graphics/common/button_max.png', '/_graphics/common/button_min.png');
	
	this.addObjectsOnPage();
}

