if(window.addEventListener){
	window.addEventListener('load', sitemap, false); 
	window.addEventListener('load', readPrefs, false);
}else{
	window.attachEvent('onload', function(){
		sitemap();
		readPrefs();
		})
}

//			:::		START MAKE SITEMAP INTERACTIVE	:::

function sitemap(){

	//Get case select from the URL
	
	if ((document.URL.indexOf(".html")< 0) && (document.URL.indexOf("itmgforms")< 0)) {
		thisPage = 'index.html';

		//Loop through all the links
		for (i=5; i<document.links.length; i++ )
		{
			//Get only the filename from the href of navigation link
			pageLink = document.links[i].href.substring(document.links[i].href.lastIndexOf("/")+1 ,document.links[i].href.length);
			//If the current filename & the link-ed file name are the same, change the class
			if (thisPage == pageLink)
			{
				//Sets class to the link
				document.links[i].id = "selected";
			}
		}
	}

	else {
		thisPage = document.URL.substring(document.URL.lastIndexOf("/")+1 ,document.URL.length);

		//Remove anything from the hash onwards from the URL
		if (thisPage.indexOf("&")>= 0)
		{
			thisPage = thisPage.slice(0,thisPage.indexOf("&"));
		}

		if (thisPage.indexOf("#")>= 0)
		{
			thisPage = thisPage.slice(0,thisPage.indexOf("#"));
		}

		//Loop through all the links
		for (i=5; i<document.links.length; i++ )
		{
			//Get only the filename from the href of navigation link
			pageLink = document.links[i].href.substring(document.links[i].href.lastIndexOf("/")+1 ,document.links[i].href.length);
			//If the current filename & the link-ed file name are the same, change the class
			if (thisPage == pageLink)
			{
				//Sets class to the link
				document.links[i].id = "selected";
				document.getElementById("selected").parentNode.parentNode.className = 'selected';
			}
		}
	}

	if (document.URL.indexOf("/video_")> 0) {
		thisPage = 'video_01.html';

		//Loop through all the links
		for (i=5; i<document.links.length; i++ )
		{
			//Get only the filename from the href of navigation link
			pageLink = document.links[i].href.substring(document.links[i].href.lastIndexOf("/")+1 ,document.links[i].href.length);
			//If the current filename & the link-ed file name are the same, change the class
			if (thisPage == pageLink)
			{
				//Sets class to the link
				document.links[i].id = "selected";
			}
		}
	}

	if (document.URL.indexOf("/chips")== 30) {

		for (i=5; i<document.links.length; i++ )
		{
			//Sets class to the link
			document.links[i].id = "";
		}
	}

	// grab all h2 elements
	var h = document.getElementById('navigation').getElementsByTagName('h2');
	// grab all unordered lists
	var u = document.getElementById('navigation').getElementsByTagName('ul');

	for(i=0;i<u.length;i++){
		// hide all unordered lists with 'navigation' class
		if(u[i].className == 'navigation'){
			u[i].style.display = 'none';
		}

		// get all links
		var a = u[i].getElementsByTagName('a');
		for(z=0;z<a.length;z++){
			// checks if link has a class of 'parent'
			if(a[z].className == 'parent'){
				var li = a[z].parentNode;

				// creates maximise.gif element if it doesn't already exist
				var imgChk = li.getElementsByTagName('img');
				if(imgChk.length == 0){
					var img = document.createElement('img');
						img.className = 'icon';
						img.src = 'http://www.informatm.com/newt/dev/gsm/events/images/symbols/minimise.gif';
						img.style.verticalAlign = 'middle';
					li.insertBefore(img, a[z]);
				}

				// set style
				li.className = 'parent';

				//hide child unordered list
				ul = a[z].nextSibling;
				while (ul.nodeType != 1){
					ul = ul.nextSibling;
				}

				ul.style.display = 'block';

				// make clicking new image hide/show child unordered list
				img.onclick = function(){
					li = this.parentNode;
					ul = li.getElementsByTagName('ul')[0];
					var ulStatus = (ul.style.display == 'none') ? 'block' : 'none';
					ul.style.display = ulStatus;

					// toggle between maximise.gif and minimise.gif
					imgStatus = (ulStatus == 'block') ? 'minimise' : 'maximise';
					this.src = 'http://www.informatm.com/newt/dev/gsm/events/images/symbols/' + imgStatus + '.gif';
				}
			}
		}
		
	}

	for(x=0;x<h.length;x++){
		// assign unique IDS to each h2 element
		h[x].id = 'h2' + x;
		h[x].className = 'minimise';

		// make h2 element show/hide unordered list when clicked
		h[x].onclick = function(){
			var ul = this.nextSibling;

			while (ul.nodeType != 1){
				ul = ul.nextSibling;
			}

			var ulStatus = (ul.style.display == 'none') ? 'block' : 'none';

			ul.style.display = ulStatus;
			var hStatus = (ulStatus == 'block') ? 'maximise' : 'minimise';
			this.className = hStatus;

			// set cookie
			return writePrefs(this.id,ulStatus);
		}
	}
}

//			:::		END MAKE SITEMAP INTERACTIVE	:::

//			:::		START WRITE HIDE/SHOW COOKIE :::

function writePrefs(section,tf){
	var cookieName = section;
	var today = new Date();
	var expires = new Date(today.getTime() + 10*24*60*60*1000);
	var index = (document.cookie != document.cookie) ? document.cookie.indexOf(cookieName) : -1;

	if (document.cookie){
		var index = document.cookie.indexOf(cookieName);
		if (index != -1) {
			var namestart = (document.cookie.indexOf("=", index) + 1);

			if (document.cookie.substring(namestart) == tf){
				return false;
			}
		}
	}

//	document.cookie= section + " = " + tf + "; expires=" + expires.toGMTString();
}

//			::: END WRITE HIDE/SHOW COOKIE :::

//			::: START READ HIDE/SHOW COOKIE :::

function readPrefs() {
	// grab all h2 elements
	var h = document.getElementById('navigation').getElementsByTagName('h2');

	// check cookie for hide/show preferences
	for(i=0; i<h.length; i++){
		// gets the element after the h2 heading
		var ul = h[i].nextSibling;
		h[i].id = 'h2' + i;

		// makes sure ul is an element, not a blank space or carriage return
		while (ul.nodeType != 1){
			ul = ul.nextSibling;
		}

		var cookieName = 'h2' + i;

		if (document.cookie.length > 0) {
			var begin = document.cookie.indexOf(cookieName+"=");
			if (begin != -1) {
				begin += cookieName.length+1;
				var end = document.cookie.indexOf(";", begin);

				if (end == -1) end = document.cookie.length;

				// gets display status from cookie
				var secValue = unescape(document.cookie.substring(begin, end));

				// sets dispaly status to equal that which was in the cookie
				var secStatus = (secValue == 'none') ? 'none' : 'block';
				var h2Img = (secValue == 'none') ? 'maximise' : 'minimise';

				document.getElementById(cookieName).className = h2Img;
				ul.style.display = secStatus;
			}
		}
	}
	for(i=1; i<document.getElementById('navigation').getElementsByTagName('ul').length; i++){
		if (document.getElementById("navigation").getElementsByTagName('ul')[i].className == 'selected') {
			document.getElementById("selected").parentNode.parentNode.parentNode.getElementsByTagName('h2')[0].className = 'maximise';
		}
		else{
		}
	}
}

//			::: END READ HIDE/SHOW COOKIE :::