function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}


function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}	
}

function sitemap(){
	if(!document.getElementById('tree')) return false;
	// grab all h3 elements
	var h = document.getElementById('tree').getElementsByTagName('h3');
	// grab all unordered lists
	var u = document.getElementById('tree').getElementsByTagName('ul');
	
	for(i=0;i<u.length;i++){
		// hide all unordered lists with 'sitemap' class
		
		if(u[i].className == 'tree'){
			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 = 'images/tree_folder.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 = 'none';
				
				// 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') ? 'tree_folder_min' : 'tree_folder';
					this.src = 'images/' + imgStatus + '.gif';
				}
			}
		}
		
	}
	
	for(x=0;x<h.length;x++){
		// assign unique IDS to each h2 element
		h[x].id = 'h3' + x;
		h[x].className = 'maximise';
		h[x].title = 'Click to Expand';
		
		
		// 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') ? 'minimise' : 'maximise';
			this.title = (ulStatus == 'block') ? 'Click to Collapse' : 'Click to Expand';
			this.className = hStatus;
			return false;
		}
	}
}
addLoadEvent(sitemap);


function prepareExpand(){
	if(document.getElementById("options")){
		var options = document.getElementById("options");	
		var separate = ' | ';
		var s = document.createTextNode(separate);
		var text = 'Expand All Sections';
		var t = document.createTextNode(text);
		var a = document.createElement("a");
		var class2 = 'expand';
		a.setAttribute('className', class2);
		a.setAttribute('class', class2);
		a.href = "#";
		a.onclick = function(){
			var u = document.getElementById('tree').getElementsByTagName('ul');	
			for(x=0;x<u.length;x++){
				// assign unique IDS to each h2 element
				u[x].id = 'ul' + x;
				u[x].title = 'Click to Collapse';
				u[x].style.display = 'block';
			}	
			var heads = document.getElementsByTagName('h3');
			for(i = 0; i < heads.length; i++){
				var minimise = 'minimise';
				heads[i].setAttribute('className', minimise);
				heads[i].setAttribute('class', minimise);
			}
			return false;		
		}
		options.appendChild(s);
		options.appendChild(a);
		a.appendChild(t);
	}
}
addLoadEvent(prepareExpand);

function prepareCollapse(){
	if(document.getElementById("options")){
		var options = document.getElementById("options");
		var separate = ' | ';
		var s = document.createTextNode(separate);
		var text = 'Collapse All Sections';
		var t = document.createTextNode(text);
		var a = document.createElement("a");
		var class2 = 'collapse';
		a.setAttribute('className', class2);
		a.setAttribute('class', class2);
		a.href = "cv";
		a.onclick = function(){
			var u = document.getElementById('tree').getElementsByTagName('ul');	
			for(x=0;x<u.length;x++){
				// assign unique IDS to each h2 element
				u[x].id = 'ul' + x;
				u[x].title = 'Click to Expand';
				u[x].style.display = 'none';
			}	
			var heads = document.getElementsByTagName('h3');
			for(i = 0; i < heads.length; i++){
				var maximise = 'maximise';
				heads[i].setAttribute('className', maximise);
				heads[i].setAttribute('class', maximise);
			}
		return false;		
		}		
		options.appendChild(s);
		options.appendChild(a);
		a.appendChild(t);
	}	
}
addLoadEvent(prepareCollapse);

function createBlockquote(){
	//takes text wrapped in <em> that's over 50 characters long and pastes as block code at the end of the content
	if(!document.getElementById || !document.getElementsByTagName 
	|| !document.createElement || !document.createTextNode || !document.getElementById("content")) return false;
	var content = document.getElementById("content");
	var ems = content.getElementsByTagName("em");
	for(var i=0; i<ems.length; i++){
		var current_em = ems[i];
		if(current_em.firstChild.nodeType != 3 || current_em.firstChild.length <50) continue;
		//create the markup		
		var close_quote = document.createElement("span");
		close_quote.setAttribute('className','close_quote');
		close_quote.setAttribute('class','close_quote');
		var quote = document.createElement("blockquote");	
		var text = current_em.firstChild.cloneNode(true);		
		var par = current_em.parentNode;
		close_quote.appendChild(text);
		quote.appendChild(close_quote);
		par.appendChild(quote);
	}	
}
addLoadEvent(createBlockquote);



function add_more_upload(){
	var new_total = Math.round(document.Uploader.TOTAL_FILE.value) + 1;
	document.getElementById('attach_'  + document.Uploader.TOTAL_FILE.value).innerHTML = 
	"<li><label for= 'theFile[" +  new_total  + "]'>File " +  new_total  + ":</label><input type='file' size='60' name='theFile[" +  new_total  + "]' /></li><li><label for= 'theCaption[" +  new_total  + "]'>Caption " +  new_total  + ":</label><input type='text' size='60' name='theCaption[" +  new_total  + "]' /></p><p id='attach_" + new_total  + "'><li id='attach_" + new_total  + "'>";
	document.Uploader.TOTAL_FILE.value++;
}

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnon\-html\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				objWarningText = document.createElement("em");
				objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

addEvent(window, 'load', getNewWindowLinks);

function displayAbbreviations(){
	if(!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;
	//get all the abbreviations
	var abbreviations = document.getElementsByTagName("abbr");
	if(abbreviations.length <1) return false;
	var defs = new Array();
	//loop throught the abbreviations
	for(var i = 0; i<abbreviations.length;i++){
		var current_abbr = abbreviations[i];
		if(current_abbr.childNodes.length <1) continue;
		var definition = current_abbr.getAttribute("title");
		var key = current_abbr.lastChild.nodeValue;
		defs[key] = definition;
	}
	//create the definitions list
	var dlist = document.createElement("dl");
	//loop throught the definitions
	for(key in defs){
		var definition = defs[key];
		//create the definition title
		var dtitle = document.createElement("dt");
		var dtitle_text = document.createTextNode(key);
		dtitle.appendChild(dtitle_text);
		//create the definition description
		var ddesc = document.createElement("dd");
		var ddesc_text = document.createTextNode(definition);
		ddesc.appendChild(ddesc_text);
		//add them to the definition list
		dlist.appendChild(dtitle);
		dlist.appendChild(ddesc);
	}
	if(dlist.childNodes.length < 1) return false;
	//create a headline
	var abbr_div = document.createElement("div");
	abbr_div.setAttribute('id', 'abbreviations');
	var header = document.createElement("h4");
	var header_text = document.createTextNode("Abbreviations and Definitions");
	header.appendChild(header_text);
	var con = document.getElementById("content");
	//add the headline to the body
	abbr_div.appendChild(header);
	//add the definition list to the body
	abbr_div.appendChild(dlist);
	var divs = document.getElementsByTagName('div');	
	for(var j = 0; j <divs.length;j++){
		if(divs[j].className == 'clear'){
			var clear = divs[j];
		}
	}
	con.insertBefore(abbr_div, clear);
}
addLoadEvent(displayAbbreviations);