var d = document;
var currentPage = 'home';


// Writes out the global top navigation file.
if (d.location.href.indexOf('https:') != -1) {
	d.write('<script type="text/javascript" src="https://s.cnn.net/nascar/.element/js/2.0/global_nav.js"></script>');
} else /*if (d.location.href.indexOf('www.nascar.com') == -1)*/ {
	d.write('<script type="text/javascript" src="http://i.cdn.turner.com/nascar/.element/js/2.0/global_nav.js"></script>');
}

// this function is need to work around a bug in IE related to element attributes
function cnnHasClass(obj) {
	var result = false;
    if (obj.getAttributeNode('class') != null) {
    	result = obj.getAttributeNode('class').value;
    }
    return result;
}   

// This function creates the alternating bg color of data tables
function cnnSetTableBG(id) {
	var even = false;
	// default colors
	var evenClass = 'cnnDefault';
	var oddClass = 'cnnGeneric';
	// use series argument to specify a non-default color:
	var args = arguments[1];
	if (args) {
		oddClass = args;
	}
	var tBody = document.getElementById(id);
	if (!tBody) { return; }
	// gets all the rows in this tbody
	var trs = tBody.getElementsByTagName('tr');
	for (var i = 0; i < trs.length; i++) {
		if (!cnnHasClass(trs[i]) && ! trs[i].style.backgroundColor) {
			// gets all the cells in this row
			var tds = trs[i].getElementsByTagName('td');
			for (var j = 0; j < tds.length; j++) {
				var td = tds[j];
				if (j == 9) {
					td.className = even ? (evenClass + ' cnnLast') : (oddClass + ' cnnLast');
				} else {
					td.className = even ? evenClass : oddClass;
				}
			}
		}
	// flip from odd to even, or vice-versa
	even =  !even;
	}
}

function cnnCheckSponsorName(id) {
	if (d.getElementById(id)) {
	var tds = d.getElementById(id).getElementsByTagName('td');
		for (var i = 0; i < tds.length; i++) {
			var content = tds[i].innerHTML;
			if (content.indexOf('.com') != -1) {
				var pos = content.indexOf('.com');
				if (pos >= 13 ) {
					tds[i].innerHTML = content.substr(0,13) + "<br>" + content.substr(13,(content.length - 1));
				}
			} else if (content.lastIndexOf(' ') > 0) {
				if ((!(content.match(/href/g))) && (!(content.match(/img/g)))) {
					var strings = content.split(" ");
					tds[i].innerHTML = '';
					for (var j = 0; j < strings.length; j++) {
						if (strings[j].length > 13) {
							strings[j] = (strings[j].substr(0,13) + "-<br>" + strings[j].substr(13,strings[j].length));
						}
						tds[i].innerHTML += strings[j] + " ";
					}
				}
			}
		}
	}
}


// Gets all cookies //
function getCookies() {
	var hash = new Array;
	if (document.cookie != null) {
		var a = document.cookie.split('; ');
		for (var i=0; i < a.length; i++) {
			var nv = a[i].split('=');
			if (nv[1] != null) {
				hash[nv[0]] = nv[1];
			}
		}
	}
	return hash;
}


//  LaunchVideo -- Redesign '09 updates -- 12/8/08, Dave Young //
function cnnLaunchVideo(url, tab) {
    var videoUrl = url;
    var popupWidth = '800';
    var popupHeight = '600';
	var name = 'video';
    var suffixRE = /\.?$/;
    var suffix = suffixRE.exec(videoUrl);
	
	if (videoUrl.indexOf('/pr/') != -1) {
		videoUrl = videoUrl.replace('.html', "_frameset_exclude.html");
		popupWidth = 577;
    	popupHeight = 465;
		name = 'oldVideo';
	}
	
    if (suffix != '') {
        if ((videoUrl.indexOf('/2006/') != -1) && (videoUrl.indexOf('/pr/video') == -1)) {
			videoUrl = '/pr/video' + videoUrl.replace(suffixRE, "_frameset_exclude.html");
			popupWidth = 577;
	    	popupHeight = 465;
			name = 'oldVideo';
		} else {
            videoUrl = videoUrl.replace(suffixRE, "/frameset.exclude.html");
        }
    }
	
	if ((videoUrl.indexOf('/video/') == -1) && ((videoUrl.indexOf('/promos/') == -1) || (videoUrl.indexOf('/multimedia/') == -1))) {
        videoUrl = "/video" + videoUrl;
	}

	if (tab) {
		videoUrl += ('?'+tab);
	} else {
		videoUrl += ('?MostPopular');
	}

    if ((videoUrl.indexOf('/video/') != -1) && ((videoUrl.indexOf('/frameset.exclude.html') != -1) || (videoUrl.indexOf('_frameset_exclude.html') != -1))) {
        var popupWindow = window.open(videoUrl,name,'width=' + popupWidth + ',height=' + popupHeight + ',scrollbars=no,resizable=yes');
		popupWindow.focus();
    } else {
        top.location.href = videoUrl;
    }
}


// function to Launch old Videos
function LaunchVideo(url) {
	var videoUrl = url;
	var name = 'oldVideo';
   	var popupWidth = '577';
   	var popupHeight = '465';
    var suffixRE = /\.(((np\.|r1\.)?smil|ram)[^\/]*)?$/;
    var suffix = suffixRE.exec(videoUrl);
	if (suffix) {
   		if (videoUrl.indexOf('/2007/01/3') != -1) {
			name = 'video';
			popupWidth = '800';
			popupHeight = '600';  
			if (videoUrl.indexOf('/video/') == -1) {
				videoUrl = '/video' + videoUrl.replace(suffixRE, "/frameset.exclude.html");
			} else {
				videoUrl = videoUrl.replace(suffixRE, "/frameset.exclude.html");
			}
		} else if (videoUrl.indexOf('nascar247') != -1) { 
			name = '247';
			popupWidth = '730';
			popupHeight = '460';
			videoUrl = videoUrl.replace(suffixRE, "_frameset_exclude.html");
			if (videoUrl.indexOf('/video') == -1) {
				videoUrl = '/video' + videoUrl;
			}
		} else if (videoUrl.indexOf('/video') != -1) { 
			videoUrl = '/pr' + videoUrl.replace(suffixRE, "_frameset_exclude.html");
		} else {
			videoUrl = '/pr/video' + videoUrl.replace(suffixRE, "_frameset_exclude.html");
		}
		var popupWindow = window.open(videoUrl,name,'width=' + popupWidth + ',height=' + popupHeight + ',scrollbars=no,resizable=yes');
		popupWindow.focus();
	} else {
		top.location.href = videoUrl;
	}
}


// LaunchRacecast
function cnnLaunchApp(url, archive, raceID, sessionID, audioOffset, lapNum) {
	appUrl = htmlApp(url);

	if (url.indexOf('wc') > -1 || url.indexOf('bg') > -1) {
		width = '750';
		height = '600';
	} else if (url == 'tps') {
		width = '568';
		height = '547';
	} else if (url.indexOf('rad') > -1) {
		width = '577';
		height = '474';
	} else if (url.indexOf('pc') > -1) {
		width = '790';
		height = '554';
	} else if (url.indexOf('rv') > -1) {
		width = '1000';
		height = '700';
	}
    
	// OPEN Application
    if (appUrl) {	
		if (archive) {
			appUrl += '&archive=' + archive + '&raceID=' + raceID + '&sessionID=' + sessionID + '&audioOffset=' + audioOffset + '&lapNum=' + lapNum;
		}	window.open(appUrl,url,'width='+width+',height='+height+',toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes');
    }
}

// htmlRacecast
function htmlApp(param) {
    var htmlUrl = '';
    switch (param) {
        case 'rvr' :                    // RaceVision
            htmlUrl = '/pr/races/raceview/index.html?product=raceview';
            break;
			
        case 'pcr' :                    // PC 2.0 CUP RACE
            htmlUrl = '/pr/pitcommand/2.0/cup/race/frameset_race_exclude.html?product=pc';
            break;

        case 'pcq' :                    // PC 2.0 CUP QUAL
            htmlUrl = '/pr/pitcommand/2.0/cup/qual/frameset_qual_exclude.html?product=pc';
            break;

        case 'rad' :                    // Radio
            htmlUrl = '/pr/races/radio/index.html?product=radio';
            break;

        case 'tps' :                    // Scanner
            htmlUrl = '/pr/races/scanner/cup/race/index.html?product=scanner';
            break;

        case 'wcr' :                    // LL CUP RACE
            htmlUrl = 'http://www.nascar.com/races/leaderboard/cup/race/index.html';
            break;

        case 'wcq' :                    // LL CUP QUAL
            htmlUrl = 'http://www.nascar.com/races/leaderboard/cup/qual/index.html';
            break;

        case 'wcp1' :               // LL CUP PRACTICE1
            htmlUrl = 'http://www.nascar.com/races/leaderboard/cup/practice/index.html';
            break;

        case 'wcp2' :               // LL CUP PRACTICE2
            htmlUrl = 'http://www.nascar.com/races/leaderboard/cup/practice/index.html';
            break;

        case 'wcp3' :               // LL CUP PRACTICE3
            htmlUrl = 'http://www.nascar.com/races/leaderboard/cup/practice/index.html';
            break;

        case 'bgr' :                    // LL NBS RACE
            htmlUrl = 'http://www.nascar.com/races/leaderboard/bg/race/index.html';
            break;

        case 'bgq' :                    // LL NBS QUAL
            htmlUrl = 'http://www.nascar.com/races/leaderboard/bg/qual/index.html';
            break;
    }
    return htmlUrl;
}

// old launch function
function LaunchRacecast(url) {
	cnnLaunchApp(url);
}


///////////////////////////////  Functions below are carried over from '06  ///////////////////////////////

// this is for opening pop-up windows
function CNN_openPopup(url, name, widgets, openerUrl) {
    var host = location.hostname;
    var popupWin = window.open(url, name, widgets);

    window.top.name = "opener";

    if (popupWin && popupWin.opener) {
        if (openerUrl) {
            popupWin.opener.location = openerUrl;
            popupWin.focus();
        }
    }
}


// _____________________________________________________________________ NASCAR

// old window opening functions
function openPopUp( url, name, widgets ) { CNN_openPopup( url, name, widgets ); }
function pollSubPop( url, name, widgets ) { CNN_openPopup( url, name, widgets ); }
function openWindow( url, name, widgets ) { CNN_openPopup( url, name, widgets ); }
function jumpLink( url, name, widgets, openerUrl ) { CNN_openPopup( url, name, widgets, openerUrl ); }
function med_vod( vidlocation )
{
    CNN_openPopup( vidlocation, '620x460', 'width=620,height=460,scrollbars=no,resizable=no' );
}

// old window closing functions
function closeWindow() { top.close(); }


//CSI functions
var cnnCSIs = new Array();
var cnnUseDelayedCSI = 0;
var localUserAgent = navigator.userAgent.toLowerCase();
if((localUserAgent.indexOf('msie')>-1) && (localUserAgent.indexOf('mac')>-1)){cnnUseDelayedCSI = 1;}

//new - per NSCR-6672
function cnnAddContextualLinks(){
	if(document.getElementById('cnnContextualLinks')){
		var cLinks = document.getElementById('cnnContextualLinks');
		var cIframe = document.createElement('iframe');
		cIframe.id = 'cnnContextualLinksIframeDispatch';
		cIframe.setAttribute('marginWidth','0');
		cIframe.setAttribute('scrolling','no');
		cIframe.setAttribute('frameBorder','0');
		cIframe.setAttribute('align','left');
		cIframe.src = '/.element/ssi/global/2.0/links_sponsored_dispatch.html';
		cLinks.appendChild(cIframe);
	}
}
//end NSCR-6672


function cnnAddCSI(id,source,args,breakCache)
{
    //new - NSCR-6672
    if(id.toLowerCase().split(' ').join('')=='cnncontextuallinks'){
	cnnAddContextualLinks();
	return false;
    }
    //end NSCR-6672
    
    if(!args) { args='';}
    breakCache = ( !breakCache && ( source.charAt( 0 ) == '/' ) ) ? false : true;
    if(cnnUseDelayedCSI)
    {
        var newCSI = new Object();
        newCSI.src = source;
        newCSI.id  = id;
        newCSI.args = args;
        newCSI.breakCache = breakCache;
        cnnCSIs[cnnCSIs.length]=newCSI;
    }
    else
    {
        var today = new Date();
        var currTime = today.getTime();
        var iframeArgs = ( breakCache ) ? '&time='+currTime : '';
        if(args)
        {
            iframeArgs=iframeArgs+'&'+args;
        }
        var iframeHtmlSrc='<iframe src="'+source+'?domId='+id+iframeArgs+'" name="iframe'+id+'" id="iframe'+id+'" width="0" height="0" align="right" style="visibility:hidden"></iframe>';
        document.write(iframeHtmlSrc);
    }
}

function cnnUpdateCSI(html, id)
{
    var htmlContainerObj = document.getElementById( id ) || document.all[ id ];
    if(htmlContainerObj)
    {
        htmlContainerObj.innerHTML = html;
    }
    //force a refresh of the content area
    var htmlContentArea = document.body;
    if(htmlContentArea)
    {
        var previousTopVal = htmlContentArea.style.top || '0px';
        htmlContentArea.style.top = '1px';
        htmlContentArea.style.top = previousTopVal;
    }
}

function cnnHandleCSIs()
{
    if(document.body && document.body.innerHTML && cnnUseDelayedCSI)
    {
        var iframeOwner = document.getElementById( 'csiIframe' ) || document.all[ 'csiIframe' ];
        var iframeHtmlSrc = '';

        for(var incCounter=0;incCounter<cnnCSIs.length;incCounter++)
        {
            var src = cnnCSIs[incCounter].src;
            var id = cnnCSIs[incCounter].id;
            var today = new Date();
            var breakCache = cnnCSIs[incCounter].breakCache;
            var currTime = today.getTime();
            var args = ( breakCache ) ? '&time='+currTime : '';
            if(cnnCSIs[incCounter].args)
            {
                args=args+'&'+cnnCSIs[incCounter].args;
            }
            iframeHtmlSrc+='<iframe src="'+src+'?domId='+id+args+'" name="iframe'+id+'" id="iframe'+id+'" width="0" height="0" align="right"></iframe>';
        }
        if(iframeOwner)
        {
            iframeOwner.innerHTML=iframeHtmlSrc;
        }
    }
}

var cnnEnableCL = true;

//domain fix
var cnnDocDomain = '';
if(location.hostname.indexOf('.nascar.com')>0) {cnnDocDomain='nascar.com';}
if(location.hostname.indexOf('turner.com')>0) {if(document.layers){cnnDocDomain='turner.com:'+location.port;}else{cnnDocDomain='turner.com';}}
//if(cnnDocDomain) {document.domain = cnnDocDomain;}

// End CSI functions

// _____________________________________________________________ WebMonkey code
/*
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
*/

// This next little bit of code tests whether the user accepts 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