// Flash Detect & Renderer

var VBS_Result = false;

function CNN_FlashDetect() { }

CNN_FlashDetect.prototype.maxVersionToDetect = 8;
CNN_FlashDetect.prototype.minVersionToDetect = 3;

CNN_FlashDetect.prototype.hasPlugin = Boolean( Boolean(navigator.mimeTypes) &&
		navigator.mimeTypes.length &&
		Boolean(navigator.mimeTypes["application/x-shockwave-flash"]) &&
		navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin );

CNN_FlashDetect.prototype.hasActiveX = Boolean( Boolean(window.ActiveXObject) &&
		typeof ActiveXObject != "undefined" );

CNN_FlashDetect.prototype.hasWinIE = Boolean( Boolean(navigator.userAgent) &&
		( navigator.userAgent.indexOf( "MSIE" ) != -1 ) &&
		Boolean(navigator.appVersion) &&
		( navigator.appVersion.indexOf( "Win" ) != -1 ) );

CNN_FlashDetect.prototype.getVersion = function () {
	var versionNum = 0;
	var i = 0;

	if ( this.hasPlugin ) {
		if ( Boolean(navigator.plugins) && navigator.plugins.length && Boolean(navigator.plugins["Shockwave Flash"]) ) {
			var words = navigator.plugins["Shockwave Flash"].description.split( " " );
			for ( i = 0; i < words.length; ++i ) {
				if ( isNaN( parseInt( words[i] ) ) )
					continue;
				versionNum = words[i];
			}
		}
	} else if ( this.hasActiveX ) {
		var activeXObject = false;
		for ( i = this.maxVersionToDetect; i >= this.minVersionToDetect && !activeXObject; versionNum = ( activeXObject ? i : versionNum ), i-- ) {
			try {
				activeXObject = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
			} catch( e ) {
				// do nothing
			}
		}
	} else if ( this.hasWinIE ) {
		VBS_Result = false;
		for ( i = this.maxVersionToDetect; i >= this.minVersionToDetect && !VBS_Result; versionNum = ( VBS_Result ? i : versionNum ), i-- ) {
			execScript( 'on error resume next: VBS_Result = IsObject( CreateObject( "ShockwaveFlash.ShockwaveFlash.' + i + '" ) )', 'VBScript' );
		}
	}

	return ( versionNum );
}

CNN_FlashDetect.prototype.detectVersion = function ( num ) { 
	var isVersionSupported = false; 
	if ( ! isNaN( num ) ) { 
		//The following four lines fix how different browsers display the value for Flash 10+ 
		var strNum = this.getVersion(); 
		strNum = String(strNum)+'.'; 
		var arrNums = strNum.split('.'); 
		isVersionSupported = ( arrNums[0] >= parseInt( num ) ); 
	} 
	return ( isVersionSupported ); 
} 


function CNN_FlashObject( p_name, p_src, p_width, p_height, p_parameters, p_flashVars ) {
	this.m_name			= p_name;
	this.m_src			= p_src;
	this.m_width		= p_width;
	this.m_height		= p_height;
	this.m_flashVars	= p_flashVars;

// constructor
	if ( Boolean(p_parameters) )
	{
		this.setParams( p_parameters );
	}
}

// Declare member properties
CNN_FlashObject.prototype.m_name = '';
CNN_FlashObject.prototype.m_src = '';
CNN_FlashObject.prototype.m_width = '100%';
CNN_FlashObject.prototype.m_height = '100%';
CNN_FlashObject.prototype.m_flashVars = '';

CNN_FlashObject.prototype.m_params = {
	menu:		"false",
	quality:	"high",
	wmode:		"transparent"
};

CNN_FlashObject.prototype.setParam = function ( p_name, p_value ) {
	this.m_params[ p_name.toLowerCase() ] = p_value;
}

CNN_FlashObject.prototype.setParams = function ( p_paramHash ) {
	if ( typeof p_paramHash == "object" ) {
		for ( var param in p_paramHash ) {
			if ( p_paramHash[param] ) {
				this.setParam( param, p_paramHash[param] );
			}
		}
	}
}

CNN_FlashObject.prototype.getParam = function ( p_name ) {
	return ( this.m_params[ p_name.toLowerCase() ] );
}

CNN_FlashObject.prototype.getParams = function () {
	return ( this.m_params );
}

CNN_FlashObject.prototype.getFlashVarsString = function () {
	var flashVarsString = '';

	if ( typeof this.m_flashVars == "string" ) {
		flashVarsString = this.m_flashVars;
	} else if ( typeof this.m_flashVars == "object" ) {
		for ( var flashVar in this.m_flashVars ) {
			if ( flashVarsString != '' ) {
				flashVarsString += "&amp;";
			}
			flashVarsString += flashVar + "=" + escape( this.m_flashVars[flashVar] );
		}
	}

	return ( flashVarsString );
}

CNN_FlashObject.prototype.getAttributeString = function ( p_attr, p_value ) {
	return ( Boolean(p_value) ? ' ' + p_attr + '="' + p_value + '"' : '' );
}

CNN_FlashObject.prototype.getParamTag = function ( p_name, p_value ) {
	return ( Boolean(p_value) ? '<param name="' + p_name + '" value="' + p_value + '">' : '' );
}

CNN_FlashObject.prototype.getHtml = function () {
	var htmlString = '';
	var eachParam = '';
	var flashUrl = 'http://www.macromedia.com/go/getflashplayer';

// open object
	htmlString += '<object type="application/x-shockwave-flash" \
					classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	htmlString += this.getAttributeString( 'pluginspage', flashUrl );
	htmlString += this.getAttributeString( 'id', this.m_name );
	htmlString += this.getAttributeString( 'data', this.m_src );
	htmlString += this.getAttributeString( 'width', this.m_width );
	htmlString += this.getAttributeString( 'height', this.m_height );
	htmlString += this.getAttributeString( 'flashVars', this.getFlashVarsString() );
	htmlString += '>';
	htmlString += this.getParamTag( 'movie', this.m_src );
	for ( eachParam in this.getParams() ) {
		htmlString += this.getParamTag( eachParam, this.getParam( eachParam ) );
	}
	htmlString += this.getParamTag( 'flashVars', this.getFlashVarsString() );

// open embed
	htmlString += '<embed type="application/x-shockwave-flash"';
	htmlString += this.getAttributeString( 'pluginspage', flashUrl );
	htmlString += this.getAttributeString( 'name', this.m_name );
	htmlString += this.getAttributeString( 'src', this.m_src );
	htmlString += this.getAttributeString( 'width', this.m_width );
	htmlString += this.getAttributeString( 'height', this.m_height );
	for ( eachParam in this.getParams() ) {
		htmlString += this.getAttributeString( eachParam, this.getParam( eachParam ) );
	}
	htmlString += this.getAttributeString( 'flashVars', this.getFlashVarsString() );
	htmlString += '>';

// close embed
	htmlString += '<\/embed>';

// close object
	htmlString += '<\/object>';

	return ( htmlString );
}

CNN_FlashObject.prototype.writeHtml = function () {
	document.write( this.getHtml() );
}

// Page OnLoad
function cnnPageOnload() {
	cnnStartList();
}

// Drop-down
function cnnStartList() {
	var toggleCounter = 12;
	for( var x = 0; x <= toggleCounter; x++ ) {
		if (document.getElementById && document.getElementById("cnnDropNav"+x)) {
			navRoot = document.getElementById("cnnDropNav"+x).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 TryParseInt(str,defaultValue){
    var retValue = defaultValue;
    if(str!=null){
        if(str.length>0){
            if (!isNaN(str)){
                retValue = parseInt(str);
            }
        }
    }
    return retValue;
}

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(",");	
			flashVersion=versionArray[0];
		} else {
			flashVersion=TryParseInt(versionStr,0);
		}
		if (flashVersion>=version) {return true;}
		return false;
	}
}

function flashWrite(name,url,width,height,params,flashVars) {
	var flv='';
	for (var i in flashVars) {
		if (flv!='') flv+='&';
		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" />';
	for (var i in params) {out+=' <param name="'+i+'" value="'+params[i]+'" />';}
	out+='<param name="flashvars" value="'+flv+'" />';
	out+='<embed src="'+url+'" quality="high" wmode="transparent" width="'+width+'" height="'+height+'" name="'+name+'" ';
	for (var i in params) {out+=' '+i+'="'+params[i]+'" ';}
	out+='flashvars="'+flv+'" ';
	out+='type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	out+='</object>';
	document.write(out);
	return out;
}

// get element by id and class
function $e(e, f) { 
	if (f==null) return document.getElementById(e); 
	else return getElementByClass(f,'*',document.getElementById(e)); 
}
function cnnShow(e) { if (e.style) e.style.display = "block"; }
function cnnHide(e) { if (e.style) e.style.display = "none"; }
function cnnRand(lo,hi) { return Math.floor(Math.random()*(hi-lo+1))+lo; }

// getElementsByClass - little different than prototype's
function getElementsByClass(searchClass,tag,node) {
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tag == null ) tag = '*';
	if (!node.getElementsByTagName) return false;
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; }
	}
	return classElements;
}

// getElementByClass
function getElementByClass(searchClass,tag,node) {
	var x = getElementsByClass(searchClass,tag,node);
	if (x) {
		if (x.length > 0) { return x[0]; }
	}
	return false;
}

// get Param: returns URL query parameter of 'name'
function getParam( name ) {
	var regex = new RegExp( "[\\?&]"+name+"=*([^&#]*)" );
	var results = regex.exec( window.location.href );
	if( results == null ) { return false; } else { return results[1]; }
}

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="sites" value="si">' );
}

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?
	}
}
function cnnSubmitSearchSite( input ) {
	if( document[input].query.value != "" ) document[input].submit();
}

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

//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(cnnDocDomain && document.switchDocDomain) {document.domain = cnnDocDomain;}
//Nothing should go after this
