var bc_baseUrl="http://stats.findlaw.com/";
var pixelUrl="http://files.findlaw.com/video.findlaw.com/player/tracking/1x1.gif";
var bc_progressInterval=20;
var bc_channel="video.findlaw.com";

var bc_playerId="";
var bc_titleId="";
var bc_stringTitle="";
var bc_playerId="";
var bc_lineupId="";
var bc_titleRefId="";
var bc_lastProgressReport=0;
var bc_titleLengthMs=0;
var bc_currentPosition=0;
var bc_paused = false;

function onTemplateLoaded(message){
  bc_playerId = config.playerId;  //config is defined in the brightcove invocation script which 
                               //always runs before this function is invoked. 
  resetStats();

  callFlash("addEventListener", "contentLoad",   "onContentLoad");
  callFlash("addEventListener", "streamStart",   "onStreamStart");
  callFlash("addEventListener", "progress",      "onProgress");
  callFlash("addEventListener", "mediaComplete", "onComplete");
  callFlash("addEventListener", "pause",         "onPause");
  callFlash("addEventListener", "play",          "onPlay");
}

function resetStats(){
  bc_paused=false;
  bc_titleId="";
  bc_stringTitle="";
  bc_lineupId="";
  bc_titleRefId="";
  bc_lastProgressReport=0; 
  bc_progressCount=0; 
  bc_currentPosition=0;
  bc_paused=false;
}

function onStreamStart(data){
  resetStats();
  callFlash("getCurrentTitle");
  report("play", 0);
}

function onPause(data){
  bc_paused = true;
  report("pause", bc_currentPosition );
}

function onPlay(data){
  if(bc_paused){
    bc_paused = false;
    report("resume", bc_currentPosition );
  }
}

function onComplete(data){
  report("complete", bc_titleLengthMs/1000 );
}

function onContentLoad(){
  //attempt to report "player ready" every second until the metadata has loaded
  //and can be reported.
  doReady.intervalId = setInterval(doReady, 1000);
}

function doReady(){
  //if the metadata is available then report it and stop the interval.
  if(!bc_titleId || bc_titleId == ""){
    callFlash("getCurrentTitle");
    if(bc_titleId){
      report("ready", 0);
      clearInterval(doReady.intervalId);
    }
  }
}
doReady.intervalId = 0;

function onProgress(data){
  bc_currentPosition = data.parameters.position;
  if(bc_currentPosition - bc_lastProgressReport >= bc_progressInterval){
    bc_lastProgressReport=bc_currentPosition;
    report("playback", bc_currentPosition);
  }
}

function report(type, position){
  report.sequence = report.sequence + 1;
  position = Math.round(position);
  args = "prod=bcp/type=" + type + "/pid=" + bc_playerId + "/tid=" + bc_titleId + "/lid=" + bc_lineupId + "/refid=" + escape(bc_titleRefId) + "/seq=" + report.sequence + "/pos=" + position + "/channel=" + escape(bc_channel);

  if(type == "play"){
    args = args + "/titleName=" + escape(bc_stringTitle);
  }

  args = args + "/*" + pixelUrl;

  image = new Image();
  image.src = bc_baseUrl + args;
}
report.sequence = 0;

function getCurrentTitle_Result(title){
  bc_stringTitle = title.displayName;
  bc_titleId = title.id;
  bc_lineupId = title.lineupId;
  bc_titleRefId = title.referenceId;
  bc_titleLengthMs = title.length;
}

function abort(){
  if(!abort.alreadyReported){
    report("abort", bc_currentPosition);
    abort.alreadyReported=true;
  }
}
abort.alreadyReported=false;

window.onunload=abort;
window.onbeforeunload=abort;
