// Page OnLoad
function cnnPageOnload() {
	cnnPopulateWriters( cnnGetObject( 'cnnSIWriterSelect' ) );
	cnnStartList();
}

// Rotating video box
var currLayerId = "cnnSiVid1";
var currentLayer="1";
var flashID=null;

function cnnGotoLayer(id) {
	if (currLayerId) cnnDisplay(currLayerId,"none");
	if (id) cnnDisplay(id,"block");
	currLayerId = id;
}

function cnnDisplay(id,value) {
	var elm = document.getElementById(id);
	elm.style.display = value;
}

function cnnRotate(id) {
	if (currentLayer=="5") currentLayer="1"; 
	else currentLayer++;
	cnnGotoLayer("cnnSiVid"+currentLayer);
	flashID=setTimeout("cnnRotate()", 7000);
}

function manualLayer(id) {
	clearTimeout(flashID);
	cnnGotoLayer(id);
}

function cnnInitRotate() {
	flashID=setTimeout("cnnRotate()", 7000);
}

// Top stories tabs
function showTab(tabId, tabNo) {
	var tabCollection=document.getElementById(tabId);
	tabCollection.className='cnnTab'+tabNo+'Visible';
}

// Drop-down 
function cnnStartList() {
	if (document.getElementById && document.getElementById("cnnDropNav")) {
		navRoot = document.getElementById("cnnDropNav").getElementsByTagName("LI");
		for (i=0; i<navRoot.length; i++) {
			node = navRoot[i];
			if (node.className == "cnnMenu") {
				node.onmouseover=function() {this.className = 'cnnMenuOver';}
				node.onmouseout=function() {this.className = 'cnnMenu';}
			}
		}
	}
}

// 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;
		}
	}
}

// Flash Player Version Detection
// Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
var isIE=(navigator.appVersion.indexOf("MSIE")!=-1) ? true : false;
var isWin=(navigator.appVersion.toLowerCase().indexOf("win")!=-1) ? true : false;
var isOpera=(navigator.userAgent.indexOf("Opera")!=-1) ? true : false;
var flashVersion=0;
flashDetect(); // auto-detect flash version

function flashGetVerIE() {
	var version=0;
	var axo;
	var e;
	try {
		axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version=axo.GetVariable("$version");
	} 
	catch (e) {}
	if (!version) {
		try {
			axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			version="WIN 6,0,21,0";
			axo.AllowScriptAccess="always";
			version=axo.GetVariable("$version");
		} 
		catch (e) {}
	}
	if (!version) {
		try {
			axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version=axo.GetVariable("$version");
		} 
		catch (e) {}
	}
	return version;
}

function flashGetVer() {
	var flashVer=-1;
	if (navigator.plugins!=null && navigator.plugins.length>0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2=navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription=navigator.plugins["Shockwave Flash"+swVer2].description;			
			var descArray=flashDescription.split(" ");
			var tempArrayMajor=descArray[2].split(".");
			var versionMajor=tempArrayMajor[0];
			var flashVer=versionMajor;
		}
	}
	else if (isIE && isWin && !isOpera) {flashVer=flashGetVerIE();}	
	return flashVer;
}

function flashDetect(version) {
	var versionStr=flashGetVer();
	if (versionStr==-1) {return false;} 
	else if (versionStr!=0) {
		if(isIE && isWin && !isOpera) {
			var tempArray=versionStr.split(" "); 	
			var tempString=tempArray[1];			
			var versionArray=tempString.split(",");	
		} 
		else {var versionArray=versionStr;}
		flashVersion=versionArray[0];
		if (flashVersion>=version) {return true;}
		return false;
	}
}

function flashWrite(name,url,width,height,params,flashVars) {
	var flv='';
	for (var i in flashVars) {flv+=i+'='+flashVars[i]+'&';}
	var out='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+width+'" height="'+height+'" id="'+name+'">';
	out+='<param name="movie" value="'+url+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" />';
	out+='<param name="flashvars" value="'+flv+'" />';
	for (var i in params) {out+='<param name="'+i+'" value="'+params[i]+'" />';}
	out+='<embed src="'+url+'" quality="high" wmode="transparent" width="'+width+'" height="'+height+'" name="'+name+'" ';
	out+='flashvars="'+flv+'" ';
	out+='type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	out+='</object>';
	document.write(out);
	return out;
}

function cnnGetObject( id ) {
	var object = null;
	if (document.getElementById) object = document.getElementById( id );
	else if (document.all) object = document.all[ id ];
	return object;
}

// 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);
}

// new cookie functions

function CNN_getCookies() {
	var hash = new Array;
	if ( document.cookie ) {
		var cookies = document.cookie.split( '; ' );
		for ( var i = 0; i < cookies.length; i++ ) {
			var namevaluePairs = cookies[i].split( '=' );
			hash[namevaluePairs[0]] = unescape( namevaluePairs[1] ) || null;
		}
	}
	return hash;
}

function CNN_parseCookieData( cookieDataString ) {
	var cookieValues = new Object();
	var separatePairs = cookieDataString.split( '&' );
	for ( var i = 0; i < separatePairs.length; i++  ) {
		var separateValues = separatePairs[i].split( ':' );
		cookieValues[separateValues[0]] = separateValues[1] || null;
	}
	return cookieValues;
}

function CNN_setCookie( name, value, hours, path, domain, secure ) {
		var numHours = 0;

		if ( hours) {
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
				numHours = hours;
			} else if ( typeof(hours) == 'number' ) { // calculate Date from number of hours
				numHours = ( new Date((new Date()).getTime() + hours*3600000) ).toGMTString();
			}
		}

		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.

}


function CNN_killCookie( name, path, domain ) {
	var allCookies = CNN_getCookies();

	var theValue = allCookies[ name ] || null; // We need the value to kill the cookie
	if ( theValue ) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
}

var allCookies = CNN_getCookies();


// Search
function CNNSI_writeSearchFields() {
	document.write('<input type="hidden" name=Coll value="si_xml">');
	document.write('<input type="hidden" name="QuerySubmit"  value="true" />');
	document.write('<input type="hidden" name="Page"  value="1" />');
	document.write('<input type="hidden" name="QueryText" value="">');
}

function CNNSI_validateSearchForm( theForm )
{
	var site = 'si';
	var queryString = theForm.query.value;

	if ( theForm.sites )
	{
		if ( theForm.sites.options ) {		//	"sites" should be a select
			site = theForm.sites.options[theForm.sites.selectedIndex].value;
		} else {
			if ( theForm.sites.length )
			{
				for ( i = 0; i < theForm.sites.length; i++ )
				{
					if ( theForm.sites[i].checked ) {
						site = theForm.sites[i].value;
					}
				}
			}
			else
			{
				site = theForm.sites.value;
			}
		}
	}

	if ( !queryString ) {
		return false;
	}

	switch ( site.toLowerCase() ) {
		case "google":
			theForm.action = "http://websearch.si.cnn.com/search/search";
			theForm.query.value = queryString;
			return true;
		case "web":
			theForm.action = "http://websearch.si.cnn.com/search/search";
			theForm.query.value = queryString;
			return true;
		case "si":
			theForm.action = "http://search.sportsillustrated.cnn.com/pages/search.jsp";
			theForm.action = "http://search.sportsillustrated.cnn.com/pages/search.jsp";
			theForm.Coll.value = 'si_xml';
			theForm.QuerySubmit.value = 'true';
			theForm.Page.value = '1';
			theForm.QueryText.value = queryString;
			return true;
		default:
			return true;						//	unsupported site?
	}
}

// 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();
	}
}

// video player
function cnnVideo( mode, arg, expiration )
{
	var openURL='/video/player/quickdetect.exclude.html';
	var cnnVideoArgs='mode='+mode+'&arg='+arg;
	if(openURL.indexOf('http://')==-1) {openURL='http://sportsillustrated.cnn.com/'+openURL;}	
	CNN_openPopup( openURL+'?'+cnnVideoArgs, 'CNNVideoPlayer', 'scrollbars=no,resizable=no,width=770,height=570' );
}

document.write("<scr"+"ipt type=\"text/javascript\" language=\"JavaScript1.2\" src=\"http://i.cdn.turner.com/si/.element/ssi/js/2.0/adcode.js\"><\/scr"+"ipt>");
document.write("<scr"+"ipt type=\"text/javascript\" language=\"JavaScript1.2\" src=\"http://i.cdn.turner.com/si/.element/js/3.0/cnnSLads.js\"><\/scr"+"ipt>");

//This must always be at the very bottom
var cnnDocDomain = '';
if(location.hostname.indexOf('cnn.com')>0) {cnnDocDomain='cnn.com';}
if(location.hostname.indexOf('turner.com')>0) {if(document.layers){cnnDocDomain='turner.com:'+location.port;}else{cnnDocDomain='turner.com';}}
if( ( window.location.href != window.location.protocol + '//' + window.location.host + '/' ) && ( window.location.href != window.location.protocol + '//' + window.location.host + '//' ) && ( ( document.URL ).replace( /http:\/\/([^\/]+)/, "" ) != '/.element/ssi/sect/3.0/MAIN/728x90.exclude.html' ) && ( ( window.location.pathname ).indexOf( '/dell/' ) != 0 ) && ( document.URL.replace( /http:\/\/([^\/]+)/, "" ) != '/.element/ssi/sect/3.0/MAIN/728x90_dell.exclude.html' ) )
if(cnnDocDomain) {document.domain = cnnDocDomain;}
//Nothing should go after this