// Author: Brian Thopsey http://topcweb.com    - thanks to Rudi Shumpert
// Code for tracking of video player, Longtail - JWplayer on Omniture SC
// variables to help populate player
var currentBuffer = 0;
var currentPosition = 0;
var currentState = "NONE";
var defaultState = "NONE";       
var currentLoad = 0;
var clipDuration = 0;
var player;
var playerLocation = "http://media.cattlenetwork.com/designvideo/player-5-7lic.swf"; //--- Location of the JWplayer file
//unfortunately this is the piece that I cannot include and must be purchased here longtailvideo.com
//var skinLocation = "incs/stijl.swf";  //--- Location of the JWplayer buttons file
//The skin will also come with the player download


//var videoFileName = "test_video.flv"; //-- set this on in the html so that you can use this a centralized file

/* additional vars that are not neccessary */
  var autoPlay; 
  var viralShare;
  var captions;
  var captionFile;


    
// Function for instatiating player
function playerReady(obj) {
    //alert('the videoplayer '+obj['id']+' has been instantiated');
    player = document.getElementById(obj['id']);
     addListeners()
};

/*
//set the variables up for SWF object for loading the flash into the page 
var so = new SWFObject(playerLocation,'mpl',height,width,'9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('wmode','opaque');
  so.addVariable('author','Kaspersky Lab');
  so.addVariable('file',videoFileName);
  so.addVariable('image', placeHolderImage);
  so.addVariable('title',videoTitle);
  so.addVariable('skin',skinLocation);
  if (autoPlay == true){so.addVariable('autostart', 'true');}
  if (viralShare== true){so.addVariable('plugins', 'viral-2');}
  if (captions == true){
    so.addVariable('plugins', 'captions-1');
    so.addVariable('captions.file', captionFile);
  }

  so.write('mediaspace');   
  */
function addListeners() {
  //load the JWplayer event listners
  //alert('load the JWplayer event listners');
  if (player) {
      //alert('addlisteners');
      addAllModelListeners(); 
  }
}

function addAllModelListeners() {
//alert('addAllModelListeners');
   if (typeof player.addModelListener == "function") {    
     player.addModelListener("BUFFER", "doNothing"); //{percentage,id,client,version}.
     player.addModelListener("ERROR", "doNothing"); //{message,id,client,version}.
     player.addModelListener("LOADED", "doNothing"); //{loaded,total,offset,id,client,version}.
     player.addModelListener("META", "doNothing"); //{variable1,variable2,variable3,...,id,client,version}.
     player.addModelListener("STATE", "stateListener");//{newstate,oldstate,id,client,version}.
     player.addModelListener("TIME", "positionListener"); //{position,duration,id,client,version}.
  }
}

function doNothing(obj) { //nothing
}

function positionListener(obj) {  
  //let's us know where we are in the video
  currentPosition = obj.position ;  
  clipDuration = obj.duration ; 
}

function stateListener(obj) {    
//alert('stateListener');
  oldState = obj.oldstate;                      
  if (defaultState == "NONE") {
      defaultState = "started";
      getTimeValue();
  }   
  currentState = obj.newstate;  
  //currentTime = obj.position;   
  //alert('currentState =' + currentState);
  //pass the event to Omniture     
  if (currentState == "COMPLETED"){
      omniMediaTrackingDone(videoFileName,currentPosition);
    }
  if (currentState == "PLAYING"){             
       //alert('play: video filename:' + videoFileName );
       if (currentPosition  != "0"){    
           omniMediaTrackingResume(videoFileName,currentPosition);
       }
  }
  
  if (currentState == "PAUSED"){ 
//    alert('paused : current position:' + currentPosition);          
    omniMediaTrackingStop(videoFileName,currentPosition);
  }  
}

// according to Rudi you can not combine the listener events, so the functions below are a workaround to get the length/pos of the video file
function getTimeValue(){  
  if (currentPosition  == "0"){      
     //needed to allow the video to load in in order to capture the parameters
     setTimeout("getTimeValue()",100);
  }else{
     //alert('InitMediaTracking : clipDuration:' + clipDuration);          
     omniInitMediaTracking(videoFileName,clipDuration,'JWplayer');
  }
}

