document.write('<script type="text/javascript" src="http://i.cdn.turner.com/money/fn_adspaces/cnn_adspaces_metrics.js">');
document.write('</scr' + 'ipt>');

var cnnad_tileID = cnnad_getID();
var cnnad_enabled = true;
var cnnad_adIframes = new Array();
var cnnad_adIframesSrc = {}
function cnnad_getID() {
	return (new Date().getTime());
}

// Start Quantcast Inline Delivery Tag -->
// DO NOT REMOVE _quantgc function
function _quantgc(n) {
	var c=document.cookie;if(!c)return '';
	var i=c.indexOf(n+"=");if(-1==i)return '';
	var len=i+n.length+1;
	var end=c.indexOf(";", len);
	return c.substring(len,end<0?c.length:end);
}
// Code below is customizable per publisher
quantSegs = "";
var _qsegs = _quantgc('__qseg').split('|');
for (var i=0;i < _qsegs.length && i < 10;i++) {
	var qArr=_qsegs[i].split("_")
	var separator = "&";
	if (i==9 || i==_qsegs.length-1)
		separator = "";
	if (qArr.length>1) { quantSegs += ("qcseg=" + qArr[1] + separator); }
}
// End Quantcast Inline Delivery Tag -->

function cnnad_preview(cnnad_adstring) {
	if ( location.host.indexOf("turner.com") > -1) {
		// we are on preview
		return cnnad_adstring.replace("ads\.cnn\.com","ads.turner.com");
	}
	return cnnad_adstring;
}

function cnnad_renderAd(cnnad_url) {
// this function is deprecated in failsafe ads solution. -sg
	if(cnnad_enabled == true) {
		document.write("<script type=\"text/javascript\"");
		document.write(" src=\""+cnnad_url+"&tile="+cnnad_getDynamicTileID(cnnad_url)+"&page.allowcompete=yes\"></scr");
		document.write("ipt>");
	}
}

function cnnad_createAdNoTileId(adId,cnnad_url,cnnad_height,cnnad_width,target) {
	cnnad_url = cnnad_preview(cnnad_url);
	cnnad_url += '&domId=' + adId;
	cnnad_adIframesSrc[adId] = cnnad_url + '&domId=' + adId;
	document.write('<iframe ALLOWTRANSPARENCY="true" hspace="0" vspace="0" marginHeight="0" marginWidth="0" src="' + cnnad_url + '&page.allowcompete=yes&domId=' + adId + '" border="0" frameBorder="0" height="0" width="0" scrolling="no"  id="'+adId+'" style="position: absolute; visibility: hidden;" ></iframe>');
}

function cnnad_createAd(adId,cnnad_url,cnnad_height,cnnad_width) {
	cnnad_url = cnnad_metricUrl(cnnad_url,cnnad_height,cnnad_width);
	cnnad_url = cnnad_preview(cnnad_url);
	cnnad_adIframesSrc[adId] = cnnad_url + '&tile=' + cnnad_getDynamicTileID(cnnad_url) + '&domId=' + adId;
	document.write('<iframe ALLOWTRANSPARENCY="true" hspace="0" vspace="0" marginHeight="0" marginWidth="0" src="' + cnnad_url + '&tile=' + cnnad_getDynamicTileID(cnnad_url) + '&page.allowcompete=yes&domId=' + adId + '" border="0" frameBorder="0" height="0" width="0" scrolling="no"  id="'+adId+'" style="position: absolute; visibility: hidden;" ></iframe>');
}

function cnnad_writeAd(cnnad_callid,cnnad_url) {
        if(cnnad_enabled == true) {
                document.write("<script id=\"" + cnnad_callid + "\" type=\"text/javascript\" onload=\"cnnSendData();\"");
                document.write(" src=\""+cnnad_url+"&tile="+cnnad_getDynamicTileID(cnnad_url)+"\"></scr");
                document.write("ipt>");
        }
}

function cnnad_showAd(cnnad_id) {
	if (document.getElementById(cnnad_id))
	{
		document.getElementById(cnnad_id).style.position = 'relative';
		document.getElementById(cnnad_id).style.visibility = 'visible';
	} else 
	{
		//alert("Error displaying ad component: " + cnnad_id);	
		// just do not display the ad. -sg
	}
}


function cnnad_setAdSize(docId,height,width) {
        if (document.getElementById(docId)) {
                document.getElementById(docId).height = height;
                document.getElementById(docId).width = width;
        }
		if ( (width==475 && height==900) || (width==336 && (height==280 || height==600)) || (width==300 && (height==250 || height==600)) ){
			//story design calls to have vertical padding only on certain ad sizes. this passes the necessary info to the story page.
			verticalAdWidth=width;
			verticalAdHeight=height;
			}
		if(height==90){
		adHeight = height;
	}
}

function cnnad_refreshAds ()
{
    if (! cnnad_adIframes)
    {
        return;
    }
    for (var i = 0; i < cnnad_adIframes.length; i++)
    {
        var cnnad_id = cnnad_adIframes[i];
        if (document.getElementById(cnnad_id))
        {
            var elem = document.getElementById(cnnad_id);
			
            if (elem.width > 0 && elem.height > 0)
            {
				elem.contentWindow.location.href = cnnad_adIframesSrc[cnnad_id];
				
            }
        }
    }
	setTimeout("cnnad_refreshAds();",60*1000*8);
}

// ********* BEGIN INTERSTITIAL FUNCTIONS ************ //
var cnnad_interstitPID = null;
var cnnad_playingInterstitial = 0;
function cnnad_endInterstitial(adId)
{
	// do the work to put things back
	var styleNode = document.getElementById('interstitialcss'+adId);
	var adNode = document.getElementById('interstitial'+adId);

	if(adNode && adNode.parentNode)
	{
		adNode.parentNode.removeChild(adNode);
	}
	if(styleNode && styleNode.parentNode)
	{
		styleNode.parentNode.removeChild(styleNode);
	}
	
	// for IE, we need to add another style to make sure tables show up
	if (typeof(document.createStyleSheet) != 'undefined')
	{
		var cssNode = document.createStyleSheet();
		cssNode.addRule('table','{display:block}');
	}

	cnnad_interstitPID = null;
	cnnad_playingInterstitial = 0;
}

function cnnad_startInterstitial(adId,cnnad_url,timeout) 
{
	cnnad_playingInterstitial = 1;
	var adUrl =  cnnad_url + '&tile=' + cnnad_getDynamicTileID(cnnad_url) + '&page.allowcompete=yes&domId=' + adId;
	// do the work to hide the contents here -- we hide by moving to to the left
	document.write('<div id="interstitial'+adId+'" class="interstitial" align="center"><iframe ALLOWTRANSPARENCY="true" hspace="0" vspace="0" marginHeight="0" marginWidth="0" src="'+adUrl+'" border="0" frameBorder="0" height="0" width="0" scrolling="no" id="'+adId+'"></iframe></div>');
	if(!timeout) { timeout = 1500;}
	cnnad_interstitPID = window.setTimeout('cnnad_endInterstitial("'+adId+'");',timeout);
}

function cnnad_resetInterstitial(adId,timeout)
{
	cnnad_playingInterstitial = 1;
	var elem = document.getElementById(adId)
	if (null != elem && elem.height > 20 && elem.width > 20)
	{
		if(cnnad_interstitPID)
		{
			window.clearTimeout(cnnad_interstitPID);
		}
		if(!timeout) { timeout = 15000;}
		cnnad_interstitPID = window.setTimeout('cnnad_endInterstitial("'+adId+'");',timeout);
	}
}
function interstitialCSS(adId)
{
	var cssString = '<style type="text/css" id="interstitialcss'+adId+'">';
	cssString += 'body div.interstitial, body div.interstitial iframe';
    cssString += '{display:block !important\;}';
    cssString += 'body *';
    cssString += '{display:none\;}';
	cssString += 'body div.interstitial, body div.interstitial iframe';
    cssString += '{display:block !important\;}';
	cssString += '</style>';
	document.write(cssString)
}
function selectInterstitial(){
	if(typeof intAdTagPresent != 'undefined')	// only go thru this is ad include not present
	{
		if(location.pathname.match('/autos/'))
		{ // ADSPACE: auto/interstitial.1x1
			interstitialCSS(635823);
			cnnad_startInterstitial(635823,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=auto&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/lifestyle/'))
		{ // ADSPACE: lifestyle/interstitial.1x1
			interstitialCSS(819570);
			cnnad_startInterstitial(819570,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=lifestyle&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/data/')||location.pathname.match('/markets/'))
		{ // markets_and_stocks/interstitial.1x1
			interstitialCSS(274122);
			cnnad_startInterstitial(274122,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=markets_and_stocks&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/funds/')||location.pathname.match('/mutualfund/'))
		{ // ADSPACE: mutual_funds/interstitial.1x1
			interstitialCSS(635767);
			cnnad_startInterstitial(635767,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=mutual_funds&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/quote/'))
		{ // ADSPACE: quotes_n_portfolio/interstitial.1x1
			interstitialCSS(697009);
			cnnad_startInterstitial(697009,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=quotes_n_portfolio&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/real_estate/')||location.pathname.match('/realestate/'))
		{ // ADSPACE: real_estate/interstitial.1x1
			interstitialCSS(288681);
			cnnad_startInterstitial(288681,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=real_estate&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/pf/')||location.pathname.match('/moneymag/'))
		{ // ADSPACE: personal_finance/interstitial.1x1
			interstitialCSS(482959);
			cnnad_startInterstitial(482959,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=personal_finance&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/services/'))
		{ // ADSPACE: services/interstitial.1x1
			interstitialCSS(998314);
			cnnad_startInterstitial(998314,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=services&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/smbusiness/')||location.pathname.match('/fsb/'))
		{ // ADSPACE: small_business/interstitial.1x1
			interstitialCSS(884302);
			cnnad_startInterstitial(884302,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=small_business&params.styles=fs_interstitial");
		}
		else if(location.pathname.match('/technology/')||location.pathname.match('/fortune/'))
		{ // ADSPACE: technology/interstitial.1x1
			interstitialCSS(608277);
			cnnad_startInterstitial(608277,"http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_interstitial&cnn_money_rollup=technology&params.styles=fs_interstitial");
		}
		else{ // ADSPACE: business_news/interstitial.1x1 
			interstitialCSS(12697);
			cnnad_startInterstitial(12697,"http://ads.cnn.com/html.ng/site=cnn&cnn_money_position=1x1_interstitial&cnn_money_rollup=business_news&params.styles=fs_interstitial");
		}
	}
}
// ********* END INTERSTITIAL FUNCTIONS ************ //

function selectSurvey(){
	if(location.pathname!='/' && location.pathname!='/index.html')
	{
		if(location.pathname.match('/autos/'))
		{ // ADSPACE: auto/survey/survey.1x1
			cnnad_createAd("542200","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=auto&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/lifestyle/'))
		{ // ADSPACE: lifestyle/survey/survey.1x1
			cnnad_createAd("266088","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=lifestyle&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/data/')||location.pathname.match('/markets/'))
		{ // ADSPACE: markets_and_stocks/survey/survey.1x1
			cnnad_createAd("506627","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=markets_and_stocks&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/funds/')||location.pathname.match('/mutualfund/'))
		{ // ADSPACE: mutual_funds/survey/survey.1x1
			cnnad_createAd("796445","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=mutual_funds&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/quote/'))
		{ // ADSPACE: quotes_n_portfolio/survey/survey.1x1
			cnnad_createAd("626396","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=quotes_n_portfolio&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/real_estate/')||location.pathname.match('/realestate/'))
		{ // ADSPACE: real_estate/survey/survey.1x1
			cnnad_createAd("897512","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=real_estate&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/pf/')||location.pathname.match('/moneymag/'))
		{ // ADSPACE: personal_finance/survey/survey.1x1
			cnnad_createAd("186611","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=personal_finance&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/smallbusiness/')||location.pathname.match('/fsb/'))
		{ // ADSPACE: small_business/survey/survey.1x1
			cnnad_createAd("199679","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=small_business&params.styles=fs","1","1");
		}
		else if(location.pathname.match('/technology/')||location.pathname.match('/fortune/'))
		{ // ADSPACE: technology/survey/survey.1x1
			cnnad_createAd("411857","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=technology&params.styles=fs","1","1");
		}
		else{ // ADSPACE: business_news/survey/survey.1x1
				cnnad_createAd("84066","http://ads.cnn.com/html.ng/site=cnn_money&cnn_money_position=1x1_survey&cnn_money_rollup=business_news&params.styles=fs","1","1");
		}
	}
}
// ********* END SURVEY FUNCTIONS ************ //

// ********* DYNAMIC TYLEID ****************** //
var cnnad_adTileIDGroup = new Array();
var cnnad_newTileIDIteration = 0;

function cnnad_newTileIDGroup(newGroupArray) {
	var newTileID = (cnnad_tileID * 1 + (++cnnad_newTileIDIteration));
	for (var i = 0; i < newGroupArray.length; i++) {
		cnnad_adTileIDGroup[cnnad_adTileIDGroup.length] = {tileID:newTileID, adName:newGroupArray[i]}; 
	}
}

function cnnad_getDynamicTileID(adURL) {
	var returnId = cnnad_tileID;
	
	// look at cnnad_adTileIDGroup array and see if any dynamic tile IDs have been requested
	if(cnnad_adTileIDGroup.length > 0){
		
		// grab the ad position from the adURL string
		// cnn specifically uses "_position", other sites may vary
		var ad_position;
		if(adURL.match("_position=") ){
			ad_position = cnnad_getParamValue(adURL, "_position=", "&");
		}
		else if(adURL.match("_pos=")){
			ad_position = cnnad_getParamValue(adURL, "_pos=", "&");
		}
		else {
			ad_position = "";
		}

		// iterate through the array and find one that matches
		for (var i = 0; i < cnnad_adTileIDGroup.length; i++) {
			var adName = cnnad_adTileIDGroup[i].adName;
			if(ad_position == adName){
				returnId = cnnad_adTileIDGroup[i].tileID;
				break;
			}			
		}
	}
	
	// return either the original static ID or the new tile ID from the array
	return returnId;
}

function cnnad_getParamValue(paramString, parameter, endCharacter){
	if(paramString.match(parameter)){
		var startOfString = paramString.indexOf(parameter)+parameter.length;
		var endOfString = paramString.indexOf(endCharacter,startOfString);
	 	var parameterValue = paramString.substring(startOfString,endOfString);
	 	return parameterValue;
	 }
	 else{
	 	return "";
	 }
}

// ********* END DYNAMIC TYLEID ************** //


