// Author: Brian Thopsey <a href="http://topcweb.com" title="http://topcweb.com">http://topcweb.com</a>    - 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://procapslabs.com/Video/player.swf"; //--- Location of the JWplayer file
//unfortunately this is the piece that I cannot include and must be purchased here longtailvideo.com
var skinLocation = "NONE";  //--- Location of the JWplayer buttons file
//The skin will also come with the player download
//var videoFileName = "NONE"; //-- 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

if (height != "" && width != "") {
    var so = new SWFObject(playerLocation, 'mpl', height, width, '10');
    if (so != null) {
        so.addParam('allowfullscreen', 'true');
        so.addParam('allowscriptaccess', 'always');
        so.addParam('wmode', 'opaque');
        so.addVariable('author', 'Procaps Laboratories');
        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
  if (player) {
      addAllModelListeners(); 
  }
}
 
function 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) {    
  oldState = obj.oldstate;                      
  if (defaultState == "NONE") {
      defaultState = "started";
      getTimeValue();
  }   
  currentState = obj.newstate;  
  //currentTime = obj.position;   
 
  //pass the event to Omniture     
  if (currentState == "COMPLETED"){
      omniMediaTrackingDone(videoFileName,currentPosition);
    }
  if (currentState == "PLAYING"){             
       //alert('play');    //testing
       if (currentPosition  != "0"){    
        omniMediaTrackingResume(videoFileName,currentPosition);
       }
  }
  if (currentState == "PAUSED"){ 
    //alert('paused');      //testing
    //alert(videoFileName);      //testing
    //alert(currentPosition);      //testing
    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{
     omniInitMediaTracking(videoFileName,clipDuration,'JWplayer');
  }
}
