document.cnnAdNetwork = "3475.golf";
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 + ';';
	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 );
	alert(WM_readCookie( 'cnnad_solbright' ));
	// if you have the cookie, write out the script file source
	if( WM_readCookie( 'cnnad_solbright' ) ) {
		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 );
	document.cnnBranding = document.cnnBranding.replace( /\./g, "_" );
	if( cnnad_isCMSStory() ) {
		retValue += cnnad_getSlug( ( document.cnnAdPathArray )[ ( document.cnnAdPathArray ).length - 2 ]  ); // if the url is a story, pass up the slug
	}
	return retValue;
}

function cnnad_getPtyp( dart_value, input ) { // sometimes input has an incorrect ptyp, so return the proper one
	var retValue = input;
	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_getSlug( input ) { // returns the article id number
	var retValue = "";
	retValue += 'slug=' + ( document.cnnAdPath ).replace( /\/gdc\/news\/article.asp\?id\=/g, "" ) + ';';
	return retValue;
}

function cnnad_isCMSStory() { // determines if the story could have been created through the CMS
	var retValue = false;
	if( ( ( document.cnnAdPath ).indexOf( '/gdc/news/article.asp?id=' ) ) != -1 ) { retValue = true; } // if the path starts with '/gdc/news/article.asp?id=', 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.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 ) {
	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);
	}
	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
