// For using with Windows media video, include the playerwmv.js file
// for using with Brightcove video, include the playerbc.js file
// When instantiating this event handler, it must be named LRCustomHandler because auxillary
// functions depend on the object being so named.

var tutorialUseTimer = false;

function LRCustomEventHandler() {

	this.currentVideoName = "";
	this.currentVideoObject = null;
	this.clearCuePoints = false;
	
	var myself = this;
	var cuepointArray = new Array();
	var currentVideoCuesChecker;
	var currentPlayerElapsedTimeIncrementer;
	var theCurrentPlayerElapsedTime;
	var theCurrentPlayerMaxTime;
	var playerId;
	var theCurrentPlayer;
	
	// This function takes a different number of parameters depending on the type of
	// event that a listener is being added
	// Comments should be included in this function for each type of listener
	this.AddCustomEventListener = function() {
		var eventType = arguments[0];
		var params = [];
		for (var i = 1; i < arguments.length; i++) {
			params.push(arguments[i]);
		}
		switch(eventType){
			case "VideoCuePoint":
				// params for VideoCuePoint:
				// AddCustomEventListener("VideoCuePoint", player_type[BC|WMV], object_id, user_video_name, array of cuepoints with time(in seconds), name(cuepoint name), and handler(function to be called) example:[{time:1, name:"StartVideo", handler:"one('one')"},{time:15, name:"15 Seconds", handler:"two('two')"}])
				VideoCuePointListener(params);
				break;
		}
	}
	
	// This will remove event listeners. Event listeners will be removed the same way they are added.
	// So if an event requires the type, and a name, and the other parameters, the type and name will be
	// required for the removal. Comments should be included in this function for each type of listener
	this.RemoveCustomEventListener = function () {
		var eventType = arguments[0];
		var params = [];
		for (var i = 1; i < arguments.length; i++) {
			params.push(arguments[i]);
		}
		switch(eventType){
			case "VideoCuePoint":
				//RemoveCustomEventListener("VideoCuePoint", player_type[BC|WMV], object_id, user_video_name)
				RemoveVideoCuePoints(params);
				break;
		}
	}

	RemoveVideoCuePoints = function(inputs) {
		// This removes the cuepoints in the cuepoint array. The array is keyed on the user supplied video name
		var playerType = inputs[0];
		var playerId = inputs[1];
		var videoName = inputs[2];
		cuepointArray[videoName]=null;
		// is this a Brightcove video, and is it the current video? If so, we need to remove the cuepoints from
		// the player as well
		if((typeof BCRemoveCuePoints == 'function') && (videoName == this.currentVideoName)) {
			BCRemoveCuePoints();
		}
	}

	
	// This will remove event listeners for all custom events by type, or "ALL" can be used to clear all Custom Event Listeners
	this.ClearCustomEventListenerByType = function (eventType) {

		switch(eventType){
			case "All":
				cuepointArray = new Array();
				if((typeof BCRemoveCuePoints == 'function') && (videoName == this.currentVideoName)) {
					BCRemoveCuePoints();
				}
				break;

			case "VideoCuePoint":
				cuepointArray = new Array();
				if((typeof BCRemoveCuePoints == 'function') && (videoName == this.currentVideoName)) {
					BCRemoveCuePoints();
				}
				break;
		}
	}
	
	VideoCuePointListener = function(inputs) {
		// This sets the cuepoints into the cuepoint array. The array is keyed on the user supplied video name
		var playerType = inputs[0];
		playerId = inputs[1];
		var videoName = inputs[2];
		var cuepoints = inputs[3];
		cuepointArray[videoName]=cuepoints;
	}

	this.GetCuepointsForBCVideo = function() {
		// This formats the cuepoints passed in into a javascript array that is used
		// by the Brightcove player. There are supporting function in the playerbc include file that call
		// this function
		var cuepoints = cuepointArray[this.currentVideoName];
		var BCCuepoints = "[";
		var formattedCuepoints=[];
		for (i=0; i<cuepoints.length; i++) {
			BCCuepoints += "{time:" + cuepoints[i]['time'] +", name:'" + cuepoints[i]['name'] + "', type:1},";
		}
		BCCuepoints = BCCuepoints.substr(0,BCCuepoints.length - 1) + "]"
		formattedCuepoints = eval(BCCuepoints);
		return formattedCuepoints;
	}
	
	this.onVideoCuePoint = function(cuepointName) {
	// This is the main cuepoint handler for videos. When a cuepoint is reached, this function is called
	// it then looks in the cuepoint array with the current video name, and finds the function to call
	// this is done via the eval function
	
		var cuepoints = cuepointArray[this.currentVideoName];
		var found = false;
		var i=0;
		while(!found && i<cuepoints.length) {
			if (cuepoints[i]['name'] == cuepointName) {
				eval(cuepoints[i]['handler']);
				found = true;
			}
			i++;
		}
	}

	function incrementVideoTimer() {
	// increment the video timer used when the player cannot be reliably accessed
		theCurrentPlayerElapsedTime = theCurrentPlayerElapsedTime + .25;
	}
	
	this.WMVStartVideo = function(inVideoName, inVideoMaxTime){
	// This function is called from the load video function. Since events from the windows media player
	// cannot be reliably trapped, this function is called when starting a windows media video.
		theCurrentPlayer = document.getElementById(playerId);
		// set the video name to be used for the key for cuepoints
		this.currentVideoName = inVideoName;
		clearInterval(currentVideoCuesChecker);
		clearInterval(currentPlayerElapsedTimeIncrementer);
		theCurrentPlayerElapsedTime = 0;
		theCurrentPlayerMaxTime = inVideoMaxTime;
		// polls the video player for cuepoints
		currentVideoCuesChecker = setInterval(WMVCheckForVideoCue,750);
		// used when the media player cannot be accessed
		if (tutorialUseTimer) {
			currentPlayerElapsedTimeIncrementer = setInterval(incrementVideoTimer,250);
		}
	}

	function WMVCheckForVideoCue() {
		// check to see if the video is playing
		if (WMVVideoPlayerStatus('playState') == 3) {
			//Loop through the cuepoints to see if one has been hit
			var cuepoints = cuepointArray[myself.currentVideoName];
			if (cuepoints != null) {
				var i=0;
				var found = false;
				var currentPosition = WMVVideoPlayerStatus("theCurrentPosition");
				while(!found && i<cuepoints.length) {
					// we must check the current position +- 1/2 second to see if the cuepoint time falls between
					// this time segment, since we can't assume there will be an exact time match
					if ((cuepoints[i]['time'] <= currentPosition + .75 )&& (cuepoints[i]['time'] >= currentPosition - .5 )) {
						// cuepoint found, call handler
						myself.onVideoCuePoint(cuepoints[i]['name']);
						found = true;
					}
					i++;
				}
			}
		}
	}
	
	function WMVVideoPlayerStatus(inParamToReturn) {
	// This function is used to determine the status of the current windows media player
		var playerFailed = false;
		if (theCurrentPlayer == null) {
			playerFailed = true;
		}
		else {
			if (theCurrentPlayer.controls == null) {
				playerFailed = true;
			}
		}
		if (playerFailed) {
			// the player cannot be reliably accessed, so fake the video playing
			this.amIReal = "no";
			if (theCurrentPlayerMaxTime + 5 <=  theCurrentPlayerElapsedTime) {
				// "stopped"
				this.playState = 1;
			}
			else {
				// "playing"
				this.playState = 3;
			}
			this.theCurrentPosition = theCurrentPlayerElapsedTime;
		}
		else {
			this.amIReal = "yes";
			this.playState = theCurrentPlayer.playState;
			this.theCurrentPosition = theCurrentPlayer.controls.currentPosition;
		}
		switch(inParamToReturn) {
			case 'amIReal':
				return this.amIReal;
				break;
			case 'playState':
				return this.playState;
				break;
			case 'theCurrentPosition':
				return this.theCurrentPosition;
				break;
			default:
				return "";
				break;
		}
	}

}