// 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() {
	$("tr:even").addClass("alt");	
}

// 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';
						}
					}								
				}				
			}					
		}
	}
}

// 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 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;
	}
}
// ==============================================================
// ==============================================================

// 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();							
		}				
	}	
}

// IE HYPERLINK BACKGROUND BUG FIX
// Moves background image from hyperlink to nested span element
function fixArrow() {
	if (document.createElement && document.appendChild) {	
		var theArrow;
		var link;
		var links = document.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) {
			link = links[i];		
			if (/\bspecial\b/.test(link.className)) {
				theArrow = document.createElement("span");
				theArrow.className = link.className;
				theArrow.style.backgroundImage = link.currentStyle.backgroundImage;					
				link.className = "";
				link.appendChild(theArrow);			
			}
		}
	}
}

// ADD CLASSES
// Requires jQuery
function addClasses() {
	// Add classes to different types of links to control behavior
	$("a[href^=http://],a[href^=https://]").addClass("off-site");
	$("a[href$=pdf],a[href$=jpg],a[href$=gif],a[href$=png],a[href$=sflb]").addClass("non-html");
	$("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]").removeClass("non-html");
}

// JAVASCRIPT CHECK
// Show elements that require JavaScript
function scriptCheck() {
	$(".req-js").removeClass();
}

// MAIN NAVIGATION
// Main navigation events
function initNav() {
	$("li.level-1").mouseover(function() {
		$("li.level-1").removeClass("active");
		$(this).addClass("active");
	});
	$("li.level-2").mouseover(function() {
	    $("li.level-2").removeClass("active");
	    $(this).addClass("active");
	});	
	$("div.contactSalesRep").mouseover(function() {
		$("div.contactSalesRep").removeClass("active");
		$(this).addClass("active");		
	});	
    $("div.contactSalesRep").mouseout(function() {
		$("div.contactSalesRep").removeClass("active");		
	});
}
// Return navigation to previous state
function returnNav() {
	$("body").click(function() {
		$("li.level-1").removeClass("active");
        $("li.level-2").removeClass("active");
	});
	$("div#content").mouseover(function() {
		$("li.level-1").removeClass("active");
        $("li.level-2").removeClass("active");
	});	
}

// ENTER KEY SUBMIT
// Makes the enter button submit the form using the button on the page.  Make sure the submit button has a class of "default".
function initEnter() {
	$("form .input input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('input[type=submit].default').click();
			return false;
		} else {
			return true;
		}
    });
}

// ENABLE ACTIVE "CONTACT US" CONTENT
function initContactUs() {
	// Remove alternative row classes from headers
	$("table.contacts tr.header").removeClass("alt");
	// Make email address clickable
	$("table.contacts td.email").wrapInner("<span class='cloak'></span>");
	// Hide table rows until requested
	$("table.contacts tr:not()").css("display", "none");
	// Show header table rows
	$("table.contacts tr.header").css("display", "table-row");
	// Show selected table rows; hide other table rows	
	$("table.contacts tr.header").click(function() {
		var theClasses = $(this).attr("class");
		var theClass = theClasses.replace("header ",".");
		$("table.contacts tr").css("display", "none");
		$("table.contacts tr.header").css("display", "table-row");
		$(this).parent().find("tr.definitions").css("display", "table-row");		
		$(theClass).css("display","table-row");
	});
	$("table.contacts caption").click(function() {
		$("table.contacts tr").css("display", "none");
		$("table.contacts tr.header").css("display", "table-row");
		$(this).parent().find("tr").css("display", "table-row");
	});
	// Hard-coded link...
	$("a[href*='#sci']").click(function() {
		$("table.contacts tr").css("display", "none");
		$("table.contacts tr.header").css("display", "table-row");
		$("table.contacts tr.sci").css("display", "table-row");
	});
	// Pre-select table rows
	if (location.hash) {
		var theHash = location.hash;
		var theHashHref = theHash.replace("#","");
		var theHashSelector = "a[name=" + theHashHref + "]";
		var theHashClass = $(theHashSelector).parent().parent().attr("class");		
		var theMainClass = theHashClass.replace("header ",".");
		$(theMainClass).parent().find("tr.definitions").css("display", "table-row");		
		$(theMainClass).css("display","table-row");
	}
}

// SCI and Contact tables adjustments
function fixTables() {
	// Get SCI and Contact table objects
	var sciTables = $("table.sci, table.contacts");	
	// Variables
	var containerWidth = $("div#primary").width();
	var tableWidth;
	var overflowWidth;
	// Check for IE 6 (or lower) by looking for attribute from IE 6 CSS
	if ($(sciTables).css("z-index") == "6") {
		// IE 6 or older
		// Add necessary wrap
		$(sciTables).wrap("<div class='sci-ie6'></div>");
		// Set fixed height on table container
		var containerHeight;
		var containerHeightPx;
		$.each($("div.sci-ie6"), function() {
			containerHeight = $(this).height();
			containerHeightPx = containerHeight + "px";
			$(this).css("height", containerHeightPx);
		});
		// Collapse table after height determined
		$.each(sciTables, function() {
			$(this).css("position", "absolute");
		});	
		// Assume tables overflow - cannot get accurate measurement due to incorrect handling of overflow		
		$(sciTables).addClass("sci-adjusted");
	}
	else {
		// Not IE 6 or older
		// See if any SCI tables overflow parent
		$.each(sciTables, function() {						
			tableWidth = $(this).width();
			overflowWidth = tableWidth - containerWidth;
			if (overflowWidth > 0) {
				adjustmentNeeded = true;
			}
		});	
		// If any tables overflow, table adjustment(s) needed
		if (adjustmentNeeded) {		
			$(sciTables).addClass("sci-adjusted");
		}
	}	
	// Check to make sure tables do not overlap left navigation
	var navHeight = $("div#nav-content").height();
	var tableOffset = $(sciTables).offset().top;
	var tableOverlap = navHeight - tableOffset;
	// If overlap, move table away from navigation
	if (tableOverlap > 0) {
		var tableOverlapPx = tableOverlap + "px";
		$(sciTables).css("marginTop",tableOverlapPx);
	}	
}

// ON-LOAD EVENTS 
// Requires jQuery
$(document).ready(function() {
	addClasses();
	getNewWindowLinks();	
	goToFirstInput();
	initPseudoSelects();
	createAlternateRows();
	fixTables();
	initContactUs();
	scriptCheck();
	initNav();
	returnNav();
	initEnter();	
	createMailtoLinks();
});