// ======================
// TRIGGER EVENTS ON-LOAD
// ======================
// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
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] );
	}
}
// removeEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
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;
	}
}
// ======================

// OPEN A PAGE IN A NEW WINDOW
// 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 {		
		var newWindow = window.open(this.getAttribute('href'), '', 'width=780, height=500, scrollbars=yes, resizable=yes, toolbar=yes, location=yes, directories=no, menubar=yes, copyhistory=no');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		// Find all links
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html" - Use for PDF documents and the like
			if (/\bnon\-html\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}
			// Find all links with a class name of "off-site" (Added by GML)
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}			
		}	
	}
}

// LOOK FOR CLOAKED LINKS AND MAKE THEM CLICKABLE
// Hopefully this will help hide e-mail addresses from spam spiders
function createMailtoLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email; // E-mail address
		var mailto; // The mailto hyperlink
		var cloak; // The mailto span element
		var spans = document.getElementsByTagName('span'); // Array of span elements
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			// Find all span elements with a class name of "cloak"
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";									
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

// ================================
// PSEUDO-SELECT MENU version 2 GML
// ================================
var psMenuClose = '1.7em'; // Set to default line height
var psMenuOpen = 'auto';
// Emulate SELECT element so that search engines can follow links
function emulateSelectElement(theElement) {	
    switch (theElement.style.height) {
		case psMenuOpen:			
			theElement.style.height = psMenuClose;
			break;
		case psMenuClose:			
			theElement.style.height = psMenuOpen;
			break;
		default:			
			theElement.style.height = psMenuOpen;
			break;
	}
}
// Collapse all PSEUDO-SELECT elements (use on-load)
function collapsePseudoSelects() {
    // Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
	    var objDiv;
	    var aryDivs = document.getElementsByTagName('div');
	    for (var i = 0; i < aryDivs.length; i++) {
		    objDiv = aryDivs[i];
		    // Find all divs with a class name of "pseudo-select"
		    if (/\bpseudo\-select\b/.test(objDiv.className)) {
			   	objDiv.style.height = psMenuClose;
			   	objDiv.style.position = "absolute"; // CUSTOM
				objDiv.style.right = "30px"; // CUSTOM
		    }					
	    }
    }
}
// ================================

// HIGHLIGHT TAB NAVIGATION (PAGE INDICATOR)
function highlightTab() {
	var strTabId = document.getElementById("content").className;
	if (strTabId != "" && strTabId != null) {
		// Change folder top
		var objFolder = document.getElementById("folder-top");
		objFolder.className = strTabId;
		// Change tab	
		if (document.getElementById("nav-" + strTabId)) {	
			var objTab = document.getElementById("nav-" + strTabId);
			objTab.className = "tab selected";
			objTab.childNodes[0].className = "selected";
		}
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {		
	var objInput;		
	var aryInput = document.getElementsByTagName('input');
	for (var i = 0; i < aryInput.length; i++) {
		objInput = aryInput[i];
		// Find all inputs with a class name of "first"
		if (/\bfirst\b/.test(objInput.className)) {				
			objInput.focus();							
		}				
	}	
}

// ON-LOAD EVENTS
addEvent(window, 'load', highlightTab);
addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', createMailtoLinks);
addEvent(window, 'load', collapsePseudoSelects);
addEvent(window, 'load', goToFirstInput);