// 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;
			}			
		}	
	}
}

// ==============================================================
// 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() {	
	$("input.first").focus();
}

// LOOK FOR UNORDERED LISTS TO CREATED COLUMNAR LISTS
function createColumnarListsLR() {
	// 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 EMAIL 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);			
			}
		}	
	}
}

// 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");
	// Build selector for current host check
	var currentDomainSelector = "a[href$=" + window.location.host + "]";
	// Exclude certain links from off-site behavior
	$(currentDomainSelector).removeClass("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");
	// Create table alternate rows
	$("tr:odd").addClass("alt");	
}

// MAIN NAVIGATION - SECONDARY DROP-DOWN LISTS
function initDropDowns() {
	// Loop through all primary navigation
	$.each($("ul.nav-primary li"), function() {
		// Look for navigation that has children
		if ($(this).children().size() > 1) {
			// Initialize drop-downs
			$(this).addClass("has-drop-down");
			$("<span class='arrow'></span>").appendTo(this);
		}
		else {
			$(this).addClass("no-drop-down");
		}		
	});
	// Remove class from secondary navigation
	$("ul.nav-secondary li").removeClass("no-drop-down");
}

// STORIES ROLLOVERS
function initNews() {
	$("div.stories h2 a, div.stories h3 a").mouseover(function() {
		$(this).parent().parent().addClass("active");
	});
	$("div.stories h2 a, div.stories h3 a").mouseout(function() {
		$(this).parent().parent().removeClass("active");
	});
}

// DESIGN ADJUSTMENTS
function initAdjustments() {
	// Check to see if there is a duplicate primary content area and abort if detected
	if ($("div#primary div.duplicate").length != 0) {
		var objPrimary = $("div#primary");
		var modulesHeight = $("div#modules").height();
		var primaryHeight = objPrimary.height();
		if (modulesHeight > primaryHeight) {
			var modulesHeightPx = modulesHeight + "px";
			objPrimary.css("height",modulesHeightPx);
		}
	}
}

// SCROLLING TICKER
// Custom implementation of SIMPLYSCROLL
function initScroller() {	
	// Get width of scrolling items
	var defaultWidth = $("#spotlight").width();
	var currentWidth = 0;
	var newWidth = defaultWidth;
	$("#scroller li").css("white-space", "nowrap");
	$("#scroller li").css("position", "absolute");	
	$.each($("#scroller li"), function() {		
		currentWidth = $(this).width();
		if (currentWidth >= defaultWidth) {
			newWidth = currentWidth;
		}		
	});	
	$("#scroller li").css("position", "static");
	// Set width of scrolling items
	newWidthPx = newWidth + "px";	
	$("#scroller *").css("width", newWidthPx);
	// Start scroller via SIMPLYSCROLL MAC: 04.21.2010 changed speed to 1
	$("#scroller").simplyScroll({
		autoMode: 'loop',			
		frameRate: 90,
		speed: 1
	});	
}

// PAGE INDICATORS (NAVIGATION STATES)
function initIndicators() {
	// Get current URL
	var theURL = location.href;
	var theURLSplit = theURL.split("/");
	var thePath = "/" + theURLSplit[3];
	if (theURLSplit[4] != null) {
		thePath = thePath + "/" + theURLSplit[4];
	}
	if (theURLSplit[5] != null) {
		thePath = thePath + "/" + theURLSplit[5];
	}  
	var theHref;		
	$.each($("#navigation a, #subnavigation a"), function() {		
		theHref = $(this).attr("href");			
		if (theHref == thePath) {
			$(this).addClass("selected");
			theLinkParents = $(this).parents("li");
			$(theLinkParents).addClass("selected");	
		}
	});
}



// SERVICES LISTS 
function initServices() {
	var selector;
	// Loop through services list items
	$.each($("ul.services li"), function() {
		// Look for services that have children
		if ($(this).children().length > 1) {
			$(this).addClass("has-children");
			$(this).prepend("<span class='indicator'></span>");	
								
		}			
	});
	// Remove class from children
	$("ul.services li ul li").removeClass("has-children");	
	$("ul.services li ul li span.indicator").remove();	
	// Events
	$("ul.services span.indicator").click(function() {
		if ($(this).attr("class") == "indicator active") {
			$("ul.services span.indicator").removeClass("active");			
			$("ul.services li").removeClass("active");
			$("ul.services li ul").hide();			
		}
		else {
			$("ul.services span.indicator").removeClass("active");			
			$("ul.services li").removeClass("active");
			$("ul.services li ul").hide();		
			$(this).addClass("active");			
			$(this).parent().addClass("active");
			$(this).parent().children("ul").show();
		}					
	});		
}

/*
// SERVICES LISTS 
function initServices() {
	var selector;
	// Loop through services list items
	$.each($("ul.services li"), function() {
		// Look for services that have children
		if ($(this).children().length > 1) {
			$(this).addClass("has-children");			
		}			
	});
	// Remove class from children
	$("ul.services li ul li").removeClass("has-children");
	// Events
	$("ul.services li.has-children").click(function() {		
		if ($(this).attr("class") == "has-children active") {
			return true;
		}
		else {
			$("ul.services li").removeClass("active");
			$("ul.services li ul").hide();
			$(this).addClass("active");
			$(this).children("ul").show();
			return false;
		}
	});	
}
*/

// ADD PRINT CLASS FOR SPECIFIC PAGES 
function addPrintClass() {
	if ($("div#primary h1").text() == "People") {
		$("div#primary h1").addClass("screen");
	}
}

// CUFON
function initCufon() {
	if ($("h1").length > 0) {
		Cufon.replace('h1');
	}
	if ($("h2").length > 0) {
		Cufon.replace('h2');
	}
	if ($("h3").length > 0) {
		Cufon.replace('h3');
	}
	if ($("h4").length > 0) {
		Cufon.replace('h4');
	}
	if ($("h5").length > 0) {
		Cufon.replace('h5');
	}	
}

// SET EASING
$(function() {
	jQuery.easing.def = 'easeOutExpo';
});

// ON-LOAD EVENTS 
// Requires jQuery
$(document).ready(function() {
	addClasses();
	getNewWindowLinks();	
	goToFirstInput();		
	//initPseudoSelects();	
	initNews();
	initDropDowns(); // Turn on navigation arrows	
	// Drop-down navigation animation via Superfish
	$("ul.nav-primary").superfish({ 
    	delay:       1000, 
    	animation:   {opacity:"show",height:"show"},
    	speed:       "normal",
    	autoArrows:  false,
    	dropShadows: false
    });
	initAdjustments();
	// initIndicators(); ** NOT NEEDED. HANDLED BY .NET CONTROLS MAC: 04.21.2010 
	createMailtoLinks();
	// Scroller - custom simplyScroll
	if ($("#scroller").length != 0) {
		initScroller();
	}
	// Font transformation
	//initCufon(); RAM: 04.21.2010
	//createColumnarLists();
	initServices();
	addPrintClass();
});