//global variables for nbcsports
var RandNumber = Math.round(Math.random()*1000000000000);
var AdPosition=0;//the position for an ad on the web page

// for each nbcsports page, the following function should be invoked
PartialLocalize();

/// <summary>
/// display an Ad by giving the url of the ad.
/// record a standard ad load, for those ad whose width and height cannot be determined, we set width=1, height=1
/// </summary>
///<para="dartADPara">json format parameter, as { cont: 'embslideshow', ucid: '32420001' } }</para>
function DisplayDartAdandRecordAdLoad(width, height, dartADPara, needExpandableAds) {

	var src = getDartAdandRecordAdLoadLink(width, height, dartADPara, needExpandableAds);
    document.write("<scr" + 'ipt type="text/javascript" src="' + src + '"></scr' + "ipt>");
}

function getDartAdandRecordAdLoadLink(width, height, dartADPara, needExpandableAds) {
    if (dartTags == null)
        return;
    var first = ''; if (AdPosition == 0) { first = 'ist'; }
    AdPosition++;
    var sz = '';
    if (width == null || width == '' || height == null || height == '') sz = "1x1";
    else sz = width + "x" + height;

    var nssect = '';
    var nss = '';
    var nssub = '';

    if (dartTags.ss == '' && pd_me.dt == 'Cover') {
        nss = "nbcsports_home";
        nssect = "home";
        nssub = "home";
    }
    else {
        if (dartTags.ss == '') {
            nss = "nbcsports";
        }
        else {
            nss = "nbcsports_" + dartTags.ss;
            nssect = dartTags.ss;
            nssub = dartTags.sss;
            if (dartTags.sss != '')
                nss = nss + "_" + dartTags.sss;
            if (dartTags.ssss != '')
                nss = nss + "_" + dartTags.ssss;
            if (dartTags.sssss != '')
                nss = nss + "_" + dartTags.sssss;
            if (dartTags.sss == '' && pd_me.isF) {
                nssub = "home";
                nss = nss + "_home";
            }
        }
    }

    var sub = (nssub != '') ? (";sub=" + nssub) : ";sub=null";
    var sub2 = (dartTags.ssss != '') ? (";sub2=" + dartTags.ssss) : ";sub2=null";
    var sub3 = (dartTags.sssss != '') ? (";sub3=" + dartTags.sssss) : ";sub3=null";
    var nssect = (nssect != '') ? nssect : "null";

    var cont = pd_me.dt;
    if (dartADPara && dartADPara.cont && dartADPara.cont != '')
        cont = dartADPara.cont;

    var ucid = pd_me.id;
    if (dartADPara && dartADPara.ucid && dartADPara.ucid != '')
        ucid = dartADPara.ucid;

    var category = 'category=nbcsports;';
    if (dartADPara && dartADPara.category && dartADPara.category == 'notile')
        category = '!category=notile;';

    var sectValue = nssect.replace(/ |(%20)/g, '_') + sub.replace(/ |(%20)/g, '_') + sub2.replace(/ |(%20)/g, '_') + sub3.replace(/ |(%20)/g, '_');

    var adsURL = "http://" + AdServers.ns
                + "/adj/" + AdServers.nsz
                + "/" + nss.replace(/ |(%20)/g, '_')
                + ";site=nbcsports;sect=" + sectValue
                + ";cont=" + cont
                + ";genre=sports;" + category
                + "ucid=" + ucid
                + ";loc=" + pd_me.nsl
                + ";aff=" + pd_me.nsa;

    //if the sieze is 728x90
    if (sz == '728x90') {
        var oldSize = sz;
        //if needExpandableAds
        if (needExpandableAds == true) {
            //add sz=970x66
            var comma = ',';
            var expandAdSize = '970x66';
            adsURL += ";sz=" + oldSize + comma + expandAdSize;
        }
        else {
            //keep original sz and add !category=noopapad
            var noOPA = 'noopapd';
            adsURL += ";!category=" + noOPA
	        	    + ";sz=" + oldSize;
        }
    }
    else {
        //other type of ads, keep the original url
        adsURL += ";sz=" + sz;
    }


    adsURL += ";dcopt=" + first
           + ";tile=" + AdPosition
           + ";pos=" + AdPosition
           + ';';

    //top is defined in global_header.js       
    if (top != null) {
        if (top.__nbcudigitaladops_dtparams) {
            adsURL += top.__nbcudigitaladops_dtparams;   //format: pm=1;
        }
    }

    //if top or top.__nbcudigitaladops_dtparams is null, ignore __nbcudigitaladops_dtparams 
    adsURL += "ord=" + RandNumber + "?";

    //***fix apostrophe or double apostrophe issues in the url*****
    if (adsURL.indexOf("'") > 0) {
    	var regExp = new RegExp("'", "g");
    	adsURL = adsURL.replace(regExp, escape("\\'"));
    }
    if (adsURL.indexOf("\"") > 0) {
    	var regExp2 = new RegExp("\"", "g");
    	adsURL = adsURL.replace(regExp2, escape("\\\""));
    }

    return adsURL;
}

/// <summary>
/// record a load for any thing
/// </summary>
function DisplayAndRecordAnyAd(AdJsUrl)
{	
	AdJsUrl=AdJsUrl.replace('"', '\\"');
	document.write('<scr' + 'ipt type="text/javascript" src="' + AdJsUrl +'"></s'+'cript>');
}

/// <summary>
/// If the end user come to NBCSports.com from an affiliate site, partial localize it.
/// </summary>
function PartialLocalize()
{
    var aff = GetUrlParameter("aff");    
    var currentCalls = readCookie("calls");
    if(aff != "" && currentCalls == "" )
    {
        SetCookie("calls", aff.toUpperCase());
    }
}
/// <summary>
/// Remove cookie entry
/// </summary>
function RemoveCookie(cName)
{
    SetCookie(cName, "", new Date(1900,1,1).toGMTString());    
}
function Delocalize()
{
    RemoveCookie("calls");
    RemoveCookie("stid");
    RemoveCookie("prvd");
    RemoveCookie("zip");
        
}

/// <summary>
/// Set Dart Tags, called by SectionDescription.xsl
/// </summary>
var dartTags;
function setDartTags(s, ss, sss, ssss, sssss) {
   dartTags = { s:s, ss:ss, sss:sss, ssss:ssss, sssss:sssss };
}

var $O = {}; //Omniture

//handler for flash objects, returns page level omniture data
$O.flashOmniParams = function() {
	try {
		//check for account value and omniture object existence,
		//these two must be valid for return object, otherwise
		//return null
		if (s_account && s_msn) {
			//create return object, copy required values
			var r = {};
			r.account = s_account;
			r.pageName = s_msn.pageName;
			r.pageURL = s_msn.pageURL;
			r.dc = s_msn.dc;
			r.visitorNamespace = s_msn.visitorNamespace;

			//copy valid prop and evar values
			for (var i = 1; i <= 50; i++) {
				var prop = 'prop' + i;
				var evar = 'eVar' + i;

				if (s_msn[prop]) {
					r[prop] = s_msn[prop];
				}
				if (s_msn[evar]) {
					r[evar] = s_msn[evar];
				}
			}

			return r;
		}
		return null;
	}
	catch (err) {
		return null;
	}
};