// 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;
	}
}

// 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" 
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}			
		}	
	}
}

// LOOK FOR TABLES AND CREATE ALTERNATE ROWS
// This auto adjusts alternate rows as new rows are added
function createAlternateRows() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all table rows		
		var row;
		var tablerows = document.getElementsByTagName('tr');
		var counter = 0;
		for (var i = 0; i < tablerows.length; i++) {
			row = tablerows[i];
			if (counter == 1) {
				row.className = 'alt';
				counter = 0;
			}
			else {
				row.className = '';
				counter ++;
			}							
		}	
	}
}

// LOOK FOR UNORDERED LISTS TO CREATED COLUMNAR LISTS
function createColumnarLists() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all unordered lists	
		var ul;
		var ullists = document.getElementsByTagName('ul');		
		for (var i = 0; i < ullists.length; i++) {
			ul = ullists[i];
			// Find all lists with a class name of "columnar"
			if (/\bcolumnar\b/.test(ul.className)) {
				var counter = 0;
				// Get all children
				for (var j = 0; j < ul.childNodes.length; j++) {								
				 	var li = ul.childNodes[j];					
					// Check to make sure this is an element node rather than a white space (text) node
					if (li.nodeType == '1') {
						// Add classes to list items
						if (counter == 1) {
							li.className = 'column2';							
							counter = 0;						
						}
						else {
							li.className = 'column1';							
							counter ++;						
						}
						// Append additional class if no content exists
						if (li.innerHTML == '') {
							li.className = li.className + ' none';
						}
					}								
				}				
			}					
		}
	}
}

// ==============================================================
// 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;
	}
}
// ==============================================================
// ==============================================================

// ADD CLASSES
// Requires jQuery
function addClasses() {
	// Add classes to different types of links to control behavior
	jQuery("a[href^=http://],a[href^=https://]").addClass("off-site");
	jQuery("a[href$=pdf],a[href$=jpg],a[href$=gif],a[href$=png],a[href$=sflb]").addClass("non-html");
	jQuery("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
	jQuery(currentDomainSelector).removeClass("off-site");
	jQuery("a[rel~=lightbox]").removeClass("non-html");
}

// ADJUST HEADER
// Script specific to this design - adjusts layout of header based on size of uploaded image
function adjustHeader() {
	var divHeight = jQuery("div#wb-header").height();
	var imgHeight = jQuery("div#wb-logo-client").height();
	var adjHeight = ((divHeight - imgHeight) / 2);
	adjHeight = Math.round(adjHeight);
	adjHeight = adjHeight + "px";	
	jQuery("div#wb-logo-client").css("margin-top",adjHeight);
}

// Set jQuery to "no conflict" mode
jQuery.noConflict();

// ON-LOAD EVENTS 
// Requires jQuery
jQuery(document).ready(function() {
	addClasses();
	getNewWindowLinks();
	adjustHeader();
	createAlternateRows();
	createColumnarLists();
	initPseudoSelects();
});