// ********************************
// swap image & switchclass       *
// ********************************
function switchclass(id, newclass){
	if(document.getElementById(id)){    //check the element exists and can be accessed
		document.getElementById(id).className = newclass
	}
}
// ********************************
// nav app-specific functions     *
// ********************************
var numMenus = 3;
function showMenu(menuNumber, eventObj)	{
    var menuId = 'ul-' + menuNumber;
	return true;
}
function hideAllMenus() {
	return true; // killing legacy code!
}

// ********************************
// timout code                    *
// ********************************
var timerID = null;
var timecount = 1;

function stopTime() {
	clearTimeout(timerID);
}
function startTime() {
	timerID = setTimeout("hideAllMenus()", timecount);
} 

window.onload = setup;

function setup()	{
	detect = navigator.userAgent.toLowerCase();
	browser = 0;
	
	if (detect.indexOf('msie') + 1) { browser = -1; }
	if (detect.indexOf('mac') + 1) { browser = browser - 1; }
	
	if (browser > -2) { 
		ieHover('nav-main','LI','DIV');
	}
	else {
		/* alert(browser); */
	}
	ieFirstLast('nav-main','UL');
	ieFirstLast('nav-section','UL');
	ieFirstLast('siteinfo-legal','UL');
	ieFirstLast('nav-global','UL');

	OtherSite.setup();
	var FBRotherSiteReturn = document.getElementById("FBRotherSiteReturn");
	var FBRotherSiteCounter = document.getElementById("FBRotherSiteCounter");
	if (FBRotherSiteReturn)
	{
		FBRotherSiteReturn.setAttribute("href",document.referrer);
	}
	if (FBRotherSiteCounter)
	{
		window.setInterval(function(){
			OtherSite.setCount(FBRotherSiteCounter);
		},1300);
	}
}

// ********************************
// IE Hover states                *
// ********************************

function ieFirstLast(container,tag) {
	if(document.all) {
		if(document.getElementById(container))	{
			var allElements = document.getElementById(container).getElementsByTagName(tag);
			var i = 0;
			while (i < allElements.length)	{
				try {
					allElements[i].firstChild.className+=" first-child";
					allElements[i].lastChild.className+=" last-child";
					i++;
				} catch (e){return true;}
			}
		}
	}
}

function ieHover(id,element,parent) {
	if(document.all) {
		if(document.getElementById(id))
		{
			var ieEls = document.getElementById(id).getElementsByTagName(element);
			for (var i=0; i<ieEls.length; i++) {
				if (ieEls[i].parentNode.parentNode.tagName == parent) {
					ieEls[i].onmouseover=function() {
						this.className+=" iehover";
					}
					ieEls[i].onmouseout=function() {
						this.className=this.className.replace(new RegExp(" iehover\\b"), "");
					}
				}
			}
		}
	}
}

var devSiteLinks = "";
var thisSiteLink = "/";

var OtherSite = {
	allAnchors : document.getElementsByTagName("a"),
	
	cleanUrl : function(arr){
		for (var x = 0; x < arr.length; x++)
		{
			arr[x] = arr[x].replace("http://","");
			arr[x] = arr[x].replace("https://","");
			arr[x] = arr[x].replace("www.","");
			if (arr[x].indexOf("/") != -1)
				arr[x] = arr[x].substring(0,arr[x].indexOf("/"))
		}
		return arr;
	},
	
	getOthers : function(){
		var x = document.getElementById("nav-global").getElementsByTagName("a")[0].href;
		var z = devSiteLinks.split(",");
		z.push(x);
		OtherSite.cleanUrl(z);
		return z;
	},
	
	setup : function(){
		var x = OtherSite.getOthers();
		var z = OtherSite.allAnchors;
		
		for (var c = 0; c < z.length; c++)
		{
			for (var n = 0; n < x.length; n++)
			{
				if (x[n] != "")
				{
					if (z[c].href.indexOf(x[n]) != -1 && z[c].href.indexOf("/go/?") == -1)
					{
						//z[c].href = thisSiteLink + "go/?url=" + encodeURI(z[c].href);
					}
				}
			}
		}
	},
	
	otherCount : 5,
	
	setCount : function(obj){
		if (this.otherCount > 0){
			this.otherCount--;
			obj.innerHTML = this.otherCount;
		}
		else this.goThere();
	},
	
	goThere : function(){
		var going = (document.location.search).replace("?url=","");
		if (going.indexOf("?") != -1)
			going += "&fromCM=true";
		else
			going += "?fromCM=true";
		document.location = going;
	}
};














