/* _____________________________________________________ Story font widget */
var CNN_FONT_COOKIE_NAME = "cnnFont";
var CNN_FONT_COOKIE_PATH = "/";
var CNN_FONT_COOKIE_DOMAIN = EditionDomain;
var CNN_FONT_COOKIE = WM_readCookie( 'cnnFont' );
var CNN_CSS_TITLE = CNN_FONT_COOKIE ? CNN_FONT_COOKIE : null;

function setActiveStyleSheet(CNN_CSS_TITLE) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == CNN_CSS_TITLE) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

window.onunload = function(e) {
  if (getActiveStyleSheet() != null) {
  	if (getActiveStyleSheet() != CNN_FONT_COOKIE) {
	  	WM_setCookie( CNN_FONT_COOKIE_NAME, getActiveStyleSheet(), 24*31, CNN_FONT_COOKIE_PATH, CNN_FONT_COOKIE_DOMAIN, '');
	}
  } else {
  	WM_killCookie( CNN_FONT_COOKIE_NAME, CNN_FONT_COOKIE_PATH, CNN_FONT_COOKIE_DOMAIN );
  }
}

window.onload = function() {
	if (typeof CNN_stripeAlertsTable == "function") {
		CNN_stripeAlertsTable();
	}
	if (typeof cnnHandleCSIs == "function") {
		cnnHandleCSIs();
	}
}

setActiveStyleSheet(CNN_CSS_TITLE);

/* _____________________________________________________ Activates navigation rollover */
function CNN_activateNav() {

	if (!document.getElementsByTagName || !document.getElementById('cnnGlobalNav') || cnnUseDelayedCSI) { return;}
	var cnnNavTDs = document.getElementById('cnnGlobalNav').getElementsByTagName('td');
	var cnnCurrentClassName;
	var cnnChildLinks;
	var cnnLink;
	for (var i = 0; i < cnnNavTDs.length; i++) {

		if (cnnNavTDs[i].hasChildNodes() && cnnNavTDs[i].className != "current" && cnnNavTDs[i].className != "cnnNavNoHilight") {
			cnnNavTDs[i].onclick = function () {
				var cnnChildrenLinks = this.childNodes;
					for (var x = 0; x < cnnChildrenLinks.length; x++)  {
						if (cnnChildrenLinks[x].nodeName.toLowerCase() == "a") {
							cnnLink = cnnChildrenLinks[x].getAttribute('href');
							CNN_goTo( cnnLink );
						}
					}
			}
		}
	
		if (cnnNavTDs[i].id != 'cnnLeftNav' && cnnNavTDs[i].id != 'cnnRightNav' && (i != 1) && (i != cnnNavTDs.length - 2) && cnnNavTDs[i].className != "current" && cnnNavTDs[i].className != "cnnNavNoHilight") {
			cnnNavTDs[i].onmouseover=function() {
				this.className+=" over";
			}
			cnnNavTDs[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp("over\\b"), "");
			}
		} else if ( i == 1  && cnnNavTDs[i].className != "current") {
			cnnNavTDs[i].onmouseover=function() {
				this.className+=" over";
					document.getElementById('cnnGlobalNav').className = "cnnHomeOver";

			}
			cnnNavTDs[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp("over\\b"), "");
					document.getElementById('cnnGlobalNav').className = "";				
				}
		
		} else if ( i == (cnnNavTDs.length - 2) && cnnNavTDs[i].className != "current" && cnnNavTDs[i].className != "cnnNavNoHilight") {
			cnnNavTDs[i].onmouseover=function() {
				this.className+=" over";
					document.getElementById('cnnGlobalNav').className = "cnnRightOver";

			}
			cnnNavTDs[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp("over\\b"), "");
					document.getElementById('cnnGlobalNav').className = "";		
		}
		}

	}
}

/* _____________________________________________________ Traveling Homepage */
function cnnChangeTabBox(strBoxId, intWhichTab) {
	var boxObj = document.getElementById(strBoxId);
	var nestedDivs = boxObj.getElementsByTagName('div');
	var nestedImgs = boxObj.getElementsByTagName('img');
	var containerObj = boxObj.parentNode;
	var containerDivs = containerObj.getElementsByTagName('div');
	var boolTriBox = false;
	var boolDualBox = false;
	
	for(i=0;i<containerDivs.length;i++) { // set containerDiv indexes
		if(containerDivs[i].id == "cnnTabContentOne") { var contentIndex1 = i; }
		if(containerDivs[i].id == "cnnTabContentTwo") { var contentIndex2 = i; }	
		if(containerDivs[i].id == "cnnTabContentThree") { var contentIndex3 = i; }
	}
	if(boxObj.parentNode.id== "cnnTriTabContainer") { boolTriBox = true; }
	if(boxObj.parentNode.id== "cnnDualTabContainer") { boolDualBox = true; }
	
	// swap background image sprite pos
	if(boolTriBox) { // it's a tri-tabbed box
		switch(intWhichTab) {
			case 2:
				boxObj.style.backgroundPosition = "0 -58px";
				break;
			case 1:
				boxObj.style.backgroundPosition = "0 -29px";
				break;
			case 0:
				boxObj.style.backgroundPosition = "0 0";
				break;
		}
	}
	if(boolDualBox) { // it's a dual-tabbed box
		switch(intWhichTab) {
			case 1:
				boxObj.style.backgroundPosition = "0 -118px";
				break;
			case 0:
				boxObj.style.backgroundPosition = "0 -87px";
				break;
		}
	}
	
	// change the active class & display/hide content
	switch(intWhichTab) {
		case 0: //user clicked on tab 1
			nestedDivs[0].className = "active";
			nestedDivs[1].className = "";
			if(boolTriBox) { nestedDivs[2].className = ""; } 
			
			if(boolTriBox) { containerDivs[contentIndex3].style.display = "none"; }
			containerDivs[contentIndex2].style.display = "none";
			containerDivs[contentIndex1].style.display = "block";			
			break;
		
		case 1: //user clicked on tab 2
			nestedDivs[0].className = "";
			nestedDivs[1].className = "active";
			if(boolTriBox) { nestedDivs[2].className = ""; }
			
			if(boolTriBox) { containerDivs[contentIndex3].style.display = "none"; }
			containerDivs[contentIndex2].style.display = "block";
			containerDivs[contentIndex1].style.display = "none";			
			break;		
		
		case 2: //user clicked on tab 3
			nestedDivs[0].className = "";
			nestedDivs[1].className = "";
			nestedDivs[2].className = "active";
			
			containerDivs[contentIndex3].style.display = "block";
			containerDivs[contentIndex2].style.display = "none";
			containerDivs[contentIndex1].style.display = "none";			
			break;
	}

	// swap the image label
	for (i=0;i<nestedImgs.length;i++) {
		if(nestedImgs[i].id) {
			if (i == intWhichTab) {
				cnnImgSwap(nestedImgs[i].id,1);
			} else {
				cnnImgSwap(nestedImgs[i].id);
			}
		}
	}
	
	// append the onmouseover, onmouseout
	for(i=0;i<nestedImgs.length;i++) {
		var parentObj = nestedImgs[i].parentNode;
		if(intWhichTab != i) {
			parentObj.onmouseover = function() { cnnTabBoxDivMouseOver(this); }
			parentObj.onmouseout = function() { cnnTabBoxDivMouseOut(this); }
		}
		else {
			parentObj.onmouseout = function() {}
			parentObj.onmouseover = function() {}
		}
	}	
}

function cnnTabBoxDivMouseOver(whichDiv) {
	if(whichDiv.childNodes[0]) {
		var strSrc = whichDiv.childNodes[0].src;
		if (strSrc.indexOf('_over.gif') == -1) {
			intChop = strSrc.length - 4;	
			strEnd = '_over.gif';
		}
		if (typeof(intChop) != "undefined") {
			newSrc = strSrc.substring( 0, intChop );
		}
		if (typeof(strEnd) != "undefined") {
			whichDiv.childNodes[0].src = newSrc + strEnd;
		}
	}
}

function cnnTabBoxDivMouseOut(whichDiv) {
	if(whichDiv.childNodes[0]) {
		var strSrc = whichDiv.childNodes[0].src;
		if (strSrc.indexOf('_over.gif') > -1) {	
			intChop = strSrc.length - 9;	
			strEnd = '.gif';
		}
		if (typeof(intChop) != "undefined") {
			newSrc = strSrc.substring( 0, intChop );
		}
		if (typeof(strEnd) != "undefined") {
			whichDiv.childNodes[0].src = newSrc + strEnd;
		}
	}
}