sfHover = function() {
	if (document.getElementById('globalNavPrimary')) {
		var sfEls = document.getElementById("globalNavPrimary").getElementsByTagName("li");
		for (var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover = function(){
				this.className += " hover";
			}
			sfEls[i].onmouseout = function(){
				this.className = this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


//ARE WE USING EXPLORER?
var g_isIE = navigator.appName.indexOf("Microsoft")!=-1;
var userAgent = navigator.userAgent.toLowerCase();
var version = parseFloat((userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1]);
if(g_isIE && version <='7' ){
	var oldIE = true;
}
//ABSTRACTED CONNECT EVENT FUNCTION FOR ANY BROWSER
function connectEvent(element, event, handler) {
	if (g_isIE) {
		element.detachEvent("on" + event, handler);
		element.attachEvent("on" + event, handler);
	} else {
		element.addEventListener(event, handler, false);
	}
}
function searchURL(section){
	if(window.location.href.search(section) >=0){
		return true;
	}
	else{
		return false;
	}
}

//Test for manual section highlight
var manualSection = false;
function activeSections() {
	if (document.getElementById('globalNavPrimary') && !manualSection) {
		var section = "navHome";
		var pathString = window.location.href.split("/");
		
		
		if (typeof pathString[3] != "undefined") {
			var section = pathString[3];
			var section = section.replace(".html", "");
			
			//EXCEPTION MAPPING		
			/* code IS added for sites we manage */
			if (searchURL("/industries/")) {
				var section = "industriesTab";
			}
			if (searchURL("/industries/apps")) {
				var section = "industriesTab";
			}
			if (searchURL("/industries/business/")) {
				var section = "productsTab";
			}
			if (searchURL("/university/")) {
				var section = "industriesTab";
			}
			if (searchURL("/publications/apps/")) {
				var section = "newsTab";
			}
			
			//CF7 Farm Specific
			if (searchURL("/bpc/")) {
				var section = "eventsTab";
			}
			if (searchURL("/businessgis/")) {
				var section = "eventsTab";
			}
			if (searchURL("/conference/")) {
				var section = "eventsTab";
			}
			if (searchURL("/dev/")) {
				var section = "eventsTab";
			}
			if (searchURL("/feduc/")) {
				var section = "eventsTab";
			}
			if (searchURL("/info/")) {
				var section = "eventsTab";
			}
			if (searchURL("/uc/")) {
				var section = "eventsTab";
			}
			
			
			//CF7 Farm Specific for ESRIApps folder
			if (searchURL("/apps/company/")) {
				var section = "companyTab";
			}
			if (searchURL("/apps/esriclips/")) {
				var section = "companyTab";
			}
			if (searchURL("/apps/Mapbook/")) {
				var section = "companyTab";
			}
			if (searchURL("/apps/grants/")) {
				var section = "industriesTab";
			}
			if (searchURL("/apps/products/")) {
				var section = "productsTab";
			}
			if (searchURL("/apps/Products/")) {
				var section = "productsTab";
			}
			if (searchURL("/apps/showcase/")) {
				var section = "productsTab";
			}
			if (searchURL("/apps/Products/maintenance/")) {
				var section = "productsTab";
			}
			if (searchURL("/apps/products/maintenance")) {
				var section = "productsTab";
			}
			if (searchURL("/apps/atp/")) {
				var section = "trainingTab";
			}
			if (searchURL("/partners/apps/")) {
				var section = "companyTab";
			}
			if (searchURL("apps/company/profservices")) {
				var section = "servicesTab";
			}
			
			//Special URLS that have tiers
			if (searchURL("/careers.*.esri\.com/")) {
				var section = "companyTab";
			}
			if (searchURL("/partners.*.esri\.com/")) {
				var section = "companyTab";
			}
			if (searchURL("/esripress.*.esri\.com/")) {
				var section = "trainingTab";
			}
			
		}
		
		if (typeof actSection != "undefined") {
			var section = actSection;
		}
		
		if (typeof section != "undefined") {
			if (document.getElementById(section)) {
				document.getElementById(section).setAttribute((oldIE ? 'className' : 'class'), "on");
			}
			else {
				document.getElementById("homeTab").setAttribute((oldIE ? 'className' : 'class'), "on");
			}
		}
		
		var myfilename = window.location.href.replace("http://", "");
		var myfilename = myfilename.replace(pathString[2], "");
		
		if (myfilename == "") {
			var myfilename = "index.html";
		}
		var links = document.getElementsByTagName("A");
		for (i = 0; i < links.length; i++) {
			if (links[i].getAttribute('href') == myfilename || links[i].getAttribute("href", 2) == myfilename) {
				links[i].setAttribute((oldIE ? 'className' : 'class'), "on");
			}
		}
	}
}

connectEvent(window, "load", activeSections);

//Removes the highlighted navigation tabs
function unHighlightSections(){
	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementById('globalNavPrimary').getElementsByTagName('A');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("class") && anchor.getAttribute("class")=="on"){
			anchor.className = "";
		}
		else{
			anchor.className = "";
		}
	}
}

//new way of highlighting tabs (replaces addTab)
function highlightSection(section){
	unHighlightSections();
	if(document.getElementById(section)){
		document.getElementById(section).setAttribute((oldIE ? 'className' : 'class'), "on");
	}
	manualSection = section;
}

//Redirect for older apps
function addTab(path, section){
	highlightSection(section);
}

/**
 * Fixes the xhtml strict lack of target attribute on links.
 * 
 * Usage:
 * Add the class "blankTarget" to your link and this script will open the link in a new window.
 * <a href="someLocation.html" class="blankTarget" title="Some Title">Popup</a>  
 * 
 */
function showPopup() {
  var popup = window.open(this.href, "popup", "status=1,toolbar=1,location=1,menubar=1,scrollbars=1,resizable=1");
  popup.focus();
  return false;
}

function blankTargetLinks() {

	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		if (anchor.getAttribute("href")) {
			var external = /blankTarget/;
			if (anchor.getAttribute("class")) {
				var classValue = anchor.getAttribute("class");
			}
			else{
				var classValue = anchor.getAttribute("className");
			}
			if (external.test(classValue)) {
				anchor.onclick = showPopup;
			}
		}
	}
	
}
window.onload = blankTargetLinks;