// 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) {
		    // For IE 8
			try {
		        if (newWindow.focus()) {
			        newWindow.focus();
			    }
		    }
		    catch(err) {		        
		        return false;		        
		    }	
		return false;
		}
	return true;
	}
}

// VIDEO WINDOW
// Create a new window
function openInVideoWindow(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'), 'popup', 'width=355, height=270, status=no, scrollbars=no, resizable=yes');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		// Google Analytics event tracking
		pageTracker._trackEvent('Video', 'View video', this.innerHTML);	    
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
// Add the openInNewWindow function to the onclick event of links with a class name of "non-html"
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var objWarningText;
		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;
			}
			// Find all links with a class name of "video" (Added by GML)
			else if (/\bvideo\b/.test(link.className)) {				
				link.onclick = openInVideoWindow;
			}
			// Remove hyperlinks from sub-navigation headers (Added by GML)
			else if (/\bsubnav\-header\b/.test(link.className)) {
				link.href = '#fix';
			}
		}
	objWarningText = null;
	}
}

// ==============================================================
// PSEUDO-SELECT MENU version 3 - Greg Laycock, Fahlgren
// Emulate SELECT element so that search engines can follow links
// ==============================================================
// Initialize PSEUDO-SELECT elements (use on-load)
function initPseudoSelects() {
    // Check that the browser is DOM compliant
	if (document.getElementsByTagName) {
	    var theClass;
		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)) {
			   	theClass = objDiv.className;
				objDiv.className = theClass + '-active';
				objDiv.onclick = emulateSelectElement;
			}
	    }
    }
}
// Open and close PSEUDO-SELECT elements
function emulateSelectElement() {
	var on = 'pseudo-select-active on'
	var off = 'pseudo-select-active off'
	switch (this.className) {
		case on:	
			this.className = off;
			break;
		case off:
		default:			
			this.className = on;
			break;
	}
}
// ==============================================================
// ==============================================================

// SOUND EFFECTS
function playSound(snd) {
	thisMovie("sounds").playSound((snd));
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}

// SHOW AND HIDE FORM ELEMENTS
function formShowHide(theElement,theInput,theSwitch) {
	var theObj = document.getElementById(theElement);
	if (theInput.checked == true) {
		if (theSwitch == 'off') {
			theObj.style.display = 'none';
		}
		else {
			theObj.style.display = 'block';
		}					
	}
	else {
		theObj.style.display = 'none';
	}
}

// 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);			
			}
		}	
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		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();							
			}				
		}	
	}
}

// SHOW "EMAIL THIS PAGE" BUTTON
// Button requires JavaScript to function so is only shown if JavaScript is available
function showEmailButton() {
	document.getElementById('button-email').style.display = 'block';	
}

// SHOW/HIDE "EMAIL THIS PAGE" BOX 
function sendPage(showhide) {
	if (document.getElementById) {
		var theStyle;
		switch (showhide) {
			case 'show':
				theStyle = 'block';
				break;
			default:
				theStyle = 'none';
		}
		document.getElementById("email-this-page").style.display = theStyle;		
	}
}

// ADD CLASSES
// Requires jQuery
function addClasses() {
	// Add classes to different types of links to control behavior
	$("a[href^=http://],a[href^=https://],a[target=_blank]").addClass("off-site");
	$("a[href$=.pdf],a[href$=.doc],a[href$=.jpg],a[href$=.gif],a[href$=.png],a[href$=.sflb]").addClass("track-event non-html");
	$("a[href$=.jpg],a[href$=.gif],a[href$=.png]").addClass("lightbox");
	$("a[href$=pdf],a[href*=PDFs]").addClass("pdf");	
	// Build selector for current host check
	var currentDomainSelector = "a[href*=" + window.location.host + "]";
	// Exclude certain links from updated behavior
	$(currentDomainSelector).removeClass("off-site");
	$("a[rel~=lightbox],a[class~=lightbox]").removeClass("non-html");
	$("a[rel~=lightbox],a[class~=lightbox]").removeClass("off-site");
}

// GOOGLE ANALYTICS GENERIC EVENT TRACKING
// Requires jQuery
function initEventTracking() {	
	$(".track-event").click(function() {	
		// Determine HTML element type
		var strTag = this.nodeName.toLowerCase();
		// Get data from HTML element		
		if (strTag == "a") {
			var strHref = $(this).attr("href");		
			var strText;
			if ($(this).text() != "") {
				strText = $(this).text();
			}
			else {
				if ($(this).find("img").length > 0) {
					strText = $(this).find("img").attr("alt");
				}
				else {
					strText = "No text";
				}
			}
		}
		// Set Google Analytics variables
		var gaCategory = strTag;
		var gaAction = strText.replace(/["']{1}/gi,"");
		var gaLabel = strHref.replace(/["']{1}/gi,"");
		// Test function or send data to Google
		if (location.hash == "#test") {
			alert(gaCategory + ", " + gaAction + ", " + gaLabel);
		}
		else {
			// INSERT GOOGLE TRACKING CODE HERE
			// Differs based on which version of GA is being used on the given site
			pageTracker._trackEvent(gaCategory, gaAction, gaLabel);		 
		}		
	});	
}

// JAVASCRIPT CHECK
// Requires jQuery
// Show elements that require JavaScript
function scriptCheck() {
	$(".req-js").removeClass();
}

// LIGHTBOX
// Requires jQuery
function initLightbox() {
	$('a.lightbox').lightBox();
}

// JQUERY READY
$(document).ready(function() {	
	//scriptCheck();
	addClasses();
	initEventTracking();
	getNewWindowLinks();
	createMailtoLinks();
	goToFirstInput();
	initPseudoSelects();	
	initLightbox();
});