var currStoryLayerID = "moreStoriesList1";
var currSwimLayerID = "moreSwimList1";

function cnnGotoLayer(id) {
	if( id.indexOf( 'Swim' ) > 0 ) {
		if (currSwimLayerID) cnnDisplay(currSwimLayerID,"none");
		if (id) cnnDisplay(id,"block");
		currSwimLayerID = id;
	} else {
		if (currStoryLayerID) cnnDisplay(currStoryLayerID,"none");
		if (id) cnnDisplay(id,"block");
		currStoryLayerID = id;
	}
}

function cnnDisplay(id,value) {
	var elm = document.getElementById(id);
	elm.style.display = value;
}

function displayTodayDate() {
  var now = new Date();
  var today = now.getDate();
  var month = now.getMonth();
    var monthName = new Array(12)
      monthName[0]="January ";
      monthName[1]="February ";
      monthName[2]="March ";
      monthName[3]="April ";
      monthName[4]="May ";
      monthName[5]="June ";
      monthName[6]="July ";
      monthName[7]="August ";
      monthName[8]="September ";
      monthName[9]="October ";
      monthName[10]="November ";
      monthName[11]="December ";
  var year = now.getFullYear();

  document.write(monthName[month]+today+ ", "+year);
}

// this is for opening pop-up windows
function CNN_openPopup( url, name, widgets, openerUrl )
{
	var host = location.hostname;
	try {
		window.top.name = "opener";
	} catch (e) {}
	var popupWin = window.open( url, name, widgets );
	if(popupWin) {cnnHasOpenPopup = 1;}
	if ( popupWin && popupWin.opener ) {
		if ( openerUrl )
		{
			popupWin.opener.location = openerUrl;
		}
	}
	if ( popupWin) {
		popupWin.focus();
	}
}

// NAVBAR functions

function cnnStartList() {
	var sfEls = document.getElementById("siv_mainNv").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" cnnOver";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" cnnOver\\b"), "");
		}
	}
}

// Hide selects from Dropdown on IE rollover
function cnnToggleSelect(state) {
	var dom = (document.getElementById) ? true : false;
	var windows = (navigator.userAgent.toLowerCase().indexOf("windows")>-1) ? true : false;
	var ie5 = ((navigator.userAgent.toLowerCase().indexOf("msie")>-1) && dom) ? true : false;
	var cnn_selects = document.getElementsByTagName("select");
	if (windows && ie5) {
		for (i=0; i<cnn_selects.length; i++) {
			cnn_selects[i].style.visibility = state;
		}
	}
}

function goToLocation(loc){
	window.location= loc
}

// write the cover ecommerce link
// Model receives ecom url in escaped format
function writeEcomLink( url, boxid ){
	var ecomUrl = unescape(url);
	var theBox = document.getElementById(boxid);
	if(theBox)
	{
		theBox.innerHTML = '<a href="' + ecomUrl + '" target="_blank">BUY THIS COVER</a>';
	}
}

function writeEcomLinkHat( url, boxid ){
	var ecomUrl = unescape(url);
	var theBox = document.getElementById(boxid);
	if(theBox)
	{
		theBox.innerHTML = '<a href="' + ecomUrl + '" target="_blank"><img src="http://i.cdn.turner.com/sivault/.element/img/1.0/hat1.gif" alt="Buy Cover Reprint" border="0" width="435" height="23"/></a>';
	}
}

// write the cover ecommerce link for thumbnails

function writeEcomLinkThumb( url, boxid){
	var ecomUrl = unescape(url);
	var theBox = document.getElementById(boxid);
	if(theBox)
	{
		theBox.innerHTML = '<a href="' + ecomUrl + '" target="_blank"><img src="http://i.cdn.turner.com/sivault/.element/img/1.0/buy_cover_reprint.gif" alt="Buy Cover Reprint" border="0" width="96" height="12"/></a>';
		
	}
}

// display the search query on the page unescaped (in same format as user entered)
function writeSearchDisplay( str, boxid ){
	var unescStr = unescape(str);
	var theBox = document.getElementById(boxid);
	if(theBox)
	{
		theBox.innerHTML = unescStr;
	}
}

// cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length).split('&');
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* start text size */
var CNN_FONT_COOKIE_NAME = "cnnFont";
var CNN_FONT_COOKIE = readCookie( CNN_FONT_COOKIE_NAME ) || 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 setActiveStyleSheet2( CNN_CSS_TITLE ) {
	setActiveStyleSheet( CNN_CSS_TITLE )
	if( CNN_CSS_TITLE == 'LargeFont' ) {
		createCookie( CNN_FONT_COOKIE_NAME, 'LargeFont', 24*31 );
	} else {
		eraseCookie( CNN_FONT_COOKIE_NAME );
	}
}

/* end text size */
/* display the decade in query field and select the correct decade from decade drop down*/
function setDecadeQuery(date_header,year_diff){
		
		if(year_diff >= 9){
		    var year_begin = date_header;
			var selected_index = 0;	
			var header = "";
					
			switch(year_begin){
				case 1950:  selected_index = 1; break;
				case 1960:  selected_index = 2; break;
				case 1970:  selected_index = 3; break;
				case 1980:  selected_index = 4; break;
				case 1990:  selected_index = 5; break;
				case 2000:  selected_index = 6; break;
			}
			header = date_header +"s";
			document.refine.decade.selectedIndex = selected_index;	
		}else{
			header = date_header;
			document.getElementById('querytop').value = header;
		}	

}