// 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 title;
var currentFile;
var currentSkin;
var playerLocation = "http://gale.cengage.com/press_room/media/player.swf"; //--- Location of the JWplayer file
var skinLocation = "http://gale.cengage.com/press_room/media/galeskin.swf";  //--- Location of the JWplayer buttons 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()
};

function deletePlayer(thePlayerWrapper, thePlaceholder, thePlayerId)
{
	swfobject.removeSWF(thePlayerId);
	var tmp=document.getElementById(thePlayerWrapper);
	if (tmp) { tmp.innerHTML = "<div id='" + thePlaceholder + "'></div>"; }

	var tmp2=document.getElementById("videoMain");
	if (tmp2) { tmp2.innerHTML = ""; }
}

function createPlayer(theFile, theSkin, theAutostart, width, height)
{
	var flashvars =
	{
	  author:'Gale',
		file:theFile,
		autostart:theAutostart,
		image:placeHolderImage,
		title:title,
		skin:theSkin,
    provider:'rtmp',
    bufferlength:1,
    streamer:'rtmp://wowza.cengage.com:443/gale'
	}

	var params =
	{
		allowfullscreen:"true",
		allowscriptaccess:"always",
		wmode:"opaque"
	}

	var attributes =
	{
		id:"player1",
		name:"player1"
	}

	swfobject.embedSWF(playerLocation, "placeholder1", width, height, "9.0.115", false, flashvars, params, attributes);
}


function initPlayer(theFile, videoTitle, theSkin, theAutostart, width, height, xml_head, xml_child) {
  title = videoTitle;
	currentFile = theFile;
	currentSkin = theSkin;
	deletePlayer('PlayerWrapper', 'placeholder1', 'player1');
	createPlayer(theFile, theSkin, theAutostart, width, height);
	get_main_video_details(xml_head, xml_child);
}

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;
  
  //pass the event to Omniture
  if (currentState == "COMPLETED")
  {
    omniMediaTrackingDone(title, currentPosition);
  }
  if (currentState == "PLAYING")
  {
    //alert('play');    //testing
    //alert(title); //testing
    if (currentPosition  != "0")
    {
      //omniMediaTrackingResume(title, currentPosition);
    }
  }
  if (currentState == "PAUSED")
  {
    //alert('paused');      //testing
    //alert(title);      //testing
    //alert(currentPosition);      //testing
    omniMediaTrackingStop(title, 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(title, clipDuration, 'JWplayer');
  }
}
