// 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 mediaPlayerName = "Unknown";
var player;    
var playerLocation = "/includes/demo/player.swf"; //--- Location of the JWplayer file
//var skinLocation = "incs/stijl.swf";  //--- Location of the JWplayer buttons file
//The skin will also come with the player download
//var videoFileName = "name_of_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;
 

// instantiate player object
function playerReady(thePlayer) {
	//alert('the videoplayer has been instantiated');
    player = window.document[thePlayer.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
  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}.
	 mediaPlayerName = player.getConfig().client;
  }
}
 
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,mediaPlayerName);
  }
}