document.cnnAdNetwork = "3475.sik";
document.cnnBranding = "";
document.cnnAdPath = ( ( document.URL ).replace( /http:\/\/([^\/]+)/, "" ) ).replace( /\/\//, "/" );
if( ( document.cnnAdPath ).charAt( ( document.cnnAdPath ).length - 1 ) == "/" ) document.cnnAdPath += "index.html"; // make sure there's always a filename
document.cnnAdPathArray = ( ( document.cnnAdPath ).substr( 1 ) ).split( "/" );


function cnnad_createAd( dart_value, parameters, width, height ) {
	var adcode = 'http://ad.doubleclick.net/adj/' + document.cnnAdNetwork + '/';
	var adparameters = cnnad_getKeywords( dart_value, parameters ) + cnnad_getPath() + cnnad_getFile();
	var adsize = 'dcove=d;sz=' + width + 'x' + height + ';';
	if( ( ( ( dart_value == 'fantasy' ) && ( cnnad_getPath() == "path=fantasy;" ) && ( cnnad_getFile() == "file=index_html;" ) ) || ( dart_value == 'games' ) || ( dart_value == 'freeze_frame' ) ) && ( width == 300 ) ) adsize = adsize.replace( /250/, "250,300x600" );
	var addcopt = ''; if( document.cnnAdInterstitial != "" ) addcopt = 'dcopt=' + document.cnnAdInterstitial + ';';
	var adtile = 'tile=' + document.cnnAdTile + ';'; if( dart_value != "test" ) { adtile = 'p' + adtile; }
	var adord = 'ord=' + document.cnnAdRandom;

	// put it all together
	adcode = '<scr'+'ipt language="JavaScript1.1" src="' + adcode + cnnad_getDartValue( dart_value ) + adparameters + adsize + addcopt + adtile + adord + '?"></scr'+'ipt>';

	// write it
	document.write( adcode );

	// if you have the cookie, write out the script file source
	if( WM_readCookie( 'cnnad_solbright' ) == "set" ) {
		document.write( '<div style="background-color:#f00;color:#fff;font-family:verdana;font-size:9px;text-align:center;">' + ( ( ( adcode.substring( ( adcode ).indexOf( 'http' ), ( adcode ).lastIndexOf( '"' ) ) ).replace( /;/g, "; " ) ).replace( /\//g, " / " ) ).replace( /\./g, ". " ) + '</div>' );
	}
}

function cnnad_getDartValue( input ) {  // sometimes input isn't a proper zone value, so return a proper one
	if( input != "" ) { input += '/'; } // all non-blank dart values should end with a slash
	return input + ';'; // all dart values should end with a semicolon
}

function cnnad_getKeywords( dart_value, input ) { // return a complete list of keywords
	var retValue = cnnad_getPtyp( dart_value, input );
	return retValue;
}

function cnnad_getPtyp( dart_value, input ) { // sometimes input has an incorrect pty, so return the proper one
	var retValue = input;
	if( cnnad_isTeamPage( dart_value ) ) {
		retValue = cnnad_getPtypHelper( input, "team" );
	} else if( cnnad_isSchedulePage( dart_value ) ) {
		retValue = cnnad_getPtypHelper( input, "schedule" );
	} else if( cnnad_isScoreboardPage( dart_value ) ) {
		retValue = cnnad_getPtypHelper( input, "scoreboard" );
	} else if( ( document.cnnAdPath ).indexOf( '/games/index.html' ) == 0 ) {
		retValue = 'ptyp=main;';
	}
	return retValue;
}

function cnnad_getPtypHelper( input, ptyp ) { // if ptyp exists in input then overwrite ptyp; else add ptyp
	var retValue = input;
	if( input.indexOf( 'ptyp' ) > -1 ) {
		var ptypArray = input.split( ";" );
		for( var x = 0; x < ptypArray.length; x++ ) {
			if( ptypArray[x].indexOf( 'ptyp' ) > -1 ) {
				ptypArray[x] = 'ptyp=' + ptyp + '';
			}
		}
		retValue = ptypArray.join( ";" );
	} else {
		retValue += 'ptyp=' + ptyp + ';';
	}
	return retValue;
}

function cnnad_isTeamPage( zone ) { // determines if the page is a team page
	var retValue = false;
	var teamsIndex = arrayIndexOf( "teams", document.cnnAdPathArray );
	if( teamsIndex > 0 ) {
		if( ( zone == "football_ncaa" ) || ( zone == "football_nfl" ) || ( zone == "baseball_mlb" ) || ( zone == "basketball_nba" ) || ( zone == "basketball_ncaa" ) || ( zone == "hockey_nhl" ) ) { retValue = cnnad_getFile() == "file=index.html;"; }
	}
	return retValue;
}

function cnnad_isSchedulePage( zone ) { // determines if the page is a schedule page
	var retValue = false;
	var teamsIndex = arrayIndexOf( "schedules", document.cnnAdPathArray );
	if( teamsIndex > 0 ) {
		if( ( zone == "football_ncaa" ) || ( zone == "football_nfl" ) || ( zone == "baseball_mlb" ) || ( zone == "basketball_nba" ) || ( zone == "basketball_ncaa" ) || ( zone == "golf" ) || ( zone == "hockey_nhl" ) ) { retValue = cnnad_getFile() == "file=index.html;"; }
	}
	return retValue;
}

function cnnad_isScoreboardPage( zone ) { // determines if the page is a scoreboard page
	var retValue = false;
	var teamsIndex = arrayIndexOf( "scoreboards", document.cnnAdPathArray );
	if( teamsIndex > 0 ) {
		if( ( zone == "football_ncaa" ) || ( zone == "football_nfl" ) || ( zone == "baseball_mlb" ) || ( zone == "basketball_nba" ) || ( zone == "basketball_ncaa" ) || ( zone == "hockey_nhl" ) ) { retValue = cnnad_getFile() == "file=index.html;"; }
	}
	if( ( zone == "golf" ) && ( arrayIndexOf( "leaderboards", document.cnnAdPathArray ) > 0 ) ) { retValue = cnnad_getFile() == "file=index.html;"; } // golf uses leaderboards in the url of its scoreboard page
	return retValue;
}

function cnnad_getSlug( input ) { // given a slug, split on the period and pass each element as a value of "slug"
	var retValue = "";
	var adslug_array = input.split( "." );
	for( var x = 0; x < adslug_array.length; x++ ) { retValue += 'slug=' + adslug_array[x] + ';'; }
	return retValue;
}

function cnnad_isCMSStory() { // determines if the story could have been created through the CMS
	var retValue = false;
	if( ( document.cnnAdPathArray )[0].match( /^\d{4}/ ) ) { retValue = true; } // if the first element of the path array is 4 digits, it's a story.
	else if( ( document.cnnAdPathArray )[0] == "pr" ) {
		if( ( ( document.cnnAdPathArray )[1] == "subs" ) || ( ( document.cnnAdPathArray )[1] == "subs2" ) ) {
			if( ( document.cnnAdPathArray )[2] == "siexclusive" ) {
				retValue = true; // if the first three elements of the path array are "pr", "subs" or "subs2", and "siexclusive", it's a story
			}
		}
	}
	return retValue;
}

function cnnad_getPath() { // given a path, split on the slash and pass each element as a value of "path" ( url minus file minus hostname )
	var retValue = "";
	var temp_path = "";
	if( ( document.location.host ).indexOf( 'secondthought' ) > -1 ) { temp_path += document.location.host + "/"; }
	else if( document.location.host == "games.si.cnn.com" ) { temp_path += document.location.host + "/"; }
	if( document.cnnAdPathArray.length > 1 ) {
		if( document.cnnAdPathArray[0] != "si_adspaces" ) {
			temp_path += document.cnnAdPathArray.slice( 0, document.cnnAdPathArray.length - 1 ).join( "/" );
		}
	}
	var path_array = temp_path.split( "/" );
	for( var x = 0; x < path_array.length; x++ ) { retValue += 'path=' + path_array[x].replace( /\./g, "_" ) + ';'; }
	return retValue;
}

function cnnad_getFile() { // return the file of the url
	var retValue = "";
	var fileArray = ( document.cnnAdPathArray[ document.cnnAdPathArray.length - 1] ).split( "#" );
	if( document.cnnAdPathArray[0] != "si_adspaces" ) {
		retValue = 'file=' + fileArray[0] + ';'
	}
	return retValue.replace( /\./g, "_" );
}

function arrayIndexOf( searchValue, in_array ) { // if searchValue exists in in_array, return the index of searchValue; otherwise return -1
	var ret_value = -1;
	for( var x = 0; x < in_array.length; x++ ) { if( in_array[x] == searchValue ) { ret_value = x; } }
	return ret_value;
}


// Cookies
function WM_browserAcceptsCookies() {
	var WM_acceptsCookies = false;
	if ( document.cookie == '' ) {
		document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
		if ( document.cookie.indexOf( 'WM_acceptsCookies=yes' ) != -1 ) {
			WM_acceptsCookies = true;
		} // If it succeeds, set variable
	} else { // there was already a cookie
		WM_acceptsCookies = true;
	}

	return ( WM_acceptsCookies );
}

function WM_setCookie( name, value, hours, path, domain, secure ) {
	if ( WM_browserAcceptsCookies() ) { // Don't waste your time if the browser doesn't accept cookies.
		var numHours = 0;
		var not_NN2 = ( navigator && navigator.appName
					&& (navigator.appName == 'Netscape')
					&& navigator.appVersion
					&& (parseInt(navigator.appVersion) == 2) ) ? false : true;

		if ( hours && not_NN2 ) { // NN2 cannot handle Dates, so skip this part
			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.
	}
} // WM_setCookie

function WM_readCookie( name ) {
	if ( document.cookie == '' ) { // there's no cookie, so go no further
		return false;
	} else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if ( (firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=') ) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if (lastChar == -1) lastChar = theBigCookie.length;
			return unescape( theBigCookie.substring(firstChar, lastChar) );
		} else { // If there was no cookie of that name, return false.
			return false;
		}
	}
} // WM_readCookie

function WM_killCookie( name, path, domain ) {
	var theValue = WM_readCookie( name ); // 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
	}
} // WM_killCookie

// Rotating Ad and adding a new parameter for games pages
function cnnad_createRefreshAd ( dart_value, parameters, width, height, refresh ) {
		
		// path value for games pages - default blank
		var pathValue = '';
		// for games pages, populate pathValue
		if (document.location.href.indexOf("/games/") != -1) {
			var gameUrl = document.location.href;
			var endSlug = gameUrl.lastIndexOf("/");
			var beginSlug = gameUrl.indexOf("games/") + 6;
			var slugLength = endSlug*1 - beginSlug*1;
			var gameSlug = gameUrl.substr(beginSlug, slugLength);
			pathValue = "path="+gameSlug+";";
		}
		
		document.cnnAdInterstitial = ( document.cnnAdInterstitial == undefined ? "ist" : "" );
		document.cnnAdTile = (document.cnnAdTile||0) + 1;
		document.cnnAdRandom = document.cnnAdRandom || Math.ceil(1+1E12*Math.random());
		// dart_value, parameters, width, height
		var src = '/.element/ssi/section/1.1/adshow.html?dart_value='+(dart_value)
			+'&parameters='+(parameters) + pathValue
			+'&width='+width
			+'&height='+height
			+'&Interstitial='+(document.cnnAdInterstitial)
			+'&Tile='+(document.cnnAdTile)
			+'&Random='+(document.cnnAdRandom)
			+'&refresh='+refresh;
		var frameHTML = '<iframe width="'+width+'" scrolling="no" height="'+height+'" frameborder="0" marginheight="0" marginwidth="0" id="browsePane" name="browsePane" src="'+src+'"></iframe>';
		document.write (frameHTML);
}
