/*rcs13*/

var globalTitle;
var globalLineup;

var numberPlayers = 0;
var globalFlash;

var onPlay0;
var onPlay1;
var onPlay2;
var onPlay3;
var onPlay4;
var onPlay5;
var onPlay6;
var onPlay7;
var onPlay8;
var onPlay9;
var onPlay10;
var onPlay11;

var playersArr = new Array();

function onTemplateLoaded(message) 
{
    //alert("brightcove player loaded with message "+message);
    for(var i = 0; i <= playerIndex; i++)
    {
        var thisId;
        if (config["flashId"] == undefined) {
            thisId = "flashObj" + i;
        } else {
            thisId = escapeValue(config["flashId"]) + i;
        }
        numberPlayers++;
        playersArr.push(thisId);
        var func = new Function("evt", "globalFlash = '"+thisId+"'; callFlashInstance(globalFlash, 'getCurrentTitle');");
        eval("onPlay"+i+" = func;");
        callFlashInstance(thisId, "addEventListener", "streamStart", "onPlay"+i+"");
        //callFlash("addEventListener", "streamStart", "onPlay"+i+"");
        callFlash("addEventListener", "mediaLoad", "onMediaLoad");
    }
}

function onPlay(evt)
{
    callFlash("getCurrentTitle");
}

function getCurrentTitle_Result(obj)
{
//    alert("current title playing is "+obj.displayName);
    
    globalTitle = obj;
    if(typeof(obj.lineupId) == 'undefined' ||  obj.lineupId == null)
    {
        callFlashInstance(globalFlash, "getPlayerLineups");
    } else
    {
        callFlashInstance(globalFlash, "getLineupById", obj.lineupId);
    }
    
}

function getPlayerLineups_Result(obj)
{
    var thisThing = obj;
    var lineupFound = false;
    globalLineup = null;
    if(obj[0].id != -1)
    {
        for(var i = 0; i < obj.length && !lineupFound; i++)
        {
            lineupFound = searchArray(obj[i].videoIds, globalTitle.id);
            if(lineupFound)
                globalLineup = obj[i];
        }
    }
    getLineupById_Result(globalLineup)
    
}

function getLineupById_Result(obj)
{
    globalLineup = obj;
    if(typeof(s_siteName) != 'undefined')
    {
        callMetricsG(globalTitle, obj);
    } else if(typeof(s_coxnews) != 'undefined')
    {
        callMetricsH(globalTitle, obj);
    }

}

function callMetricsG(title, lineup)
{
   s_channel = "";
   s_pageType = "";
   var lineupName = "no linup";
   if(lineup != null)
       lineupName = lineup.displayName;
   s_prop13 = s_siteName + " | Media Player | "+lineupName+" | "+title.displayName;
//   s_hier3 = s_siteName.toUpperCase() +"|mediaplyer|brightcove|"+lineup.displayName+"|"+title.id+"|"+title.displayName;
   s_hier3 = "cxnt:"+s_siteName +":content:mediaplayer:brightcove:"+lineupName+":"+title.id+":"+title.displayName;
   clearEvarsG();
   if(lineup != null)
   	s_pageName = "http://"+document.location.host+document.location.pathname+"/bctid="+title.id+"/bclid="+lineup.id;
    else 
  	s_pageName = "http://"+document.location.host+document.location.pathname+"/bctid="+title.id;
   s_gs(s_account);
   
}

function clearEvarsG()
{
    if(typeof(s_prop10) != 'undefined')
        s_prop10 = "";
    if(typeof(s_prop11) != 'undefined')
        s_prop11 = "";
    if(typeof(s_prop12) != 'undefined')
        s_prop12 = "";
    if(typeof(s_prop14) != 'undefined')
        s_prop14 = "";
    if(typeof(s_prop15) != 'undefined')
        s_prop15 = "";
    if(typeof(s_prop16) != 'undefined')
        s_prop16 = "";
    if(typeof(s_prop17) != 'undefined')
        s_prop17 = "";
    if(typeof(s_prop18) != 'undefined')
        s_prop18 = "";
    if(typeof(s_prop19) != 'undefined')
        s_prop19 = "";
    if(typeof(s_prop20) != 'undefined')
        s_prop20 = "";
    if(typeof(s_prop21) != 'undefined')
        s_prop21 = "";
    if(typeof(s_prop22) != 'undefined')
        s_prop22 = "";
    if(typeof(s_prop23) != 'undefined')
        s_prop23 = "";
    if(typeof(s_prop24) != 'undefined')
        s_prop24 = "";
}


function callMetricsH(title, lineup)
{
   var lineupName = "no linup";
   if(lineup != null)
       lineupName = lineup.displayName;
   cx_channel = "";
   s_coxnews.pageType = "";
   s_coxnews.prop13 = cx_siteName + " | Media Player | "+lineupName+" | "+title.displayName;
//   s_hier3 = s_siteName.toUpperCase() +"|mediaplyer|brightcove|"+lineupName+"|"+title.id+"|"+title.displayName;
   s_coxnews.hier3 = cx_siteName +"|MEDIAPLAYER|brightcove|"+lineup.displayName+"|"+title.id+"|"+title.displayName;
   if(lineup != null)
        s_coxnews.pageName = "http://"+document.location.host+"/default/brightcove.html/bctid="+title.id+"/bclid="+lineup.id;
   else
        s_coxnews.pageName = "http://"+document.location.host+"/default/brightcove.html/bctid="+title.id;
   clearEvarsH();
   s_coxnews.t(s_account);

}

function clearEvarsH()
{
    if(typeof(s_coxnews.prop10) != 'undefined')
        s_coxnews.prop10 = "";
    if(typeof(s_coxnews.prop11) != 'undefined')
        s_coxnews.prop11 = "";
    if(typeof(s_coxnews.prop12) != 'undefined')
        s_coxnews.prop12 = "";
    if(typeof(s_coxnews.prop14) != 'undefined')
        s_coxnews.prop14 = "";
    if(typeof(s_coxnews.prop15) != 'undefined')
        s_coxnews.prop15 = "";
    if(typeof(s_coxnews.prop16) != 'undefined')
        s_coxnews.prop16 = "";
    if(typeof(s_coxnews.prop17) != 'undefined')
        s_coxnews.prop17 = "";
    if(typeof(s_coxnews.prop18) != 'undefined')
        s_coxnews.prop18 = "";
    if(typeof(s_coxnews.prop19) != 'undefined')
        s_coxnews.prop19 = "";
    if(typeof(s_coxnews.prop20) != 'undefined')
        s_coxnews.prop20 = "";
    if(typeof(s_coxnews.prop21) != 'undefined')
        s_coxnews.prop21 = "";
//    if(typeof(s_coxnews.prop22) != 'undefined')
//        s_coxnews.prop22 = "";
    if(typeof(s_coxnews.prop23) != 'undefined')
        s_coxnews.prop23 = "";
    if(typeof(s_coxnews.prop24) != 'undefined')
        s_coxnews.prop24 = "";
}

function searchArray(arr, value)
{
    var rval = false;
    for(var j = 0; j < arr.length && !rval; j++)
    {
        //this is the downside of loosely typed variables
        rval = (arr[j] - 0) == (value - 0);
    }

    return rval;
}

function getQueryString()
{
    var q = location.search.substring(1); 

    if (q != "")
    {
        if(!(q.indexOf("bcpid") >= 0) || !(q.indexOf("bctid") >= 0))
        {
	          s_coxnews.t(s_account);
        }
    }
    else
    {
        s_coxnews.t(s_account);
    }
}

