/* probably obsolete*/
if (typeof ezplayer_config == "undefined") {
	ezplayer_config = {};
}	
ezplayer_config.ezMaxTitleLength = 60;
ezplayer_config.ezMaxDescLength  = 80;

if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }
EVERYZING.metaPlayerRequest = new Object();

var Player = new Object(); // some legacy thing apparently
var playerInst0 = null;    // this too
var ezMetaPlayerName = "ezplayer"; // the name of the Flash object

// TODO wrap all this in an EZMetaPlayer object
// Called from Flash to get query terms on hash or search portion of url
function getEzQuery() {
	var query = window.location.hash;
	if (!query  || query.length == 0) {
		query = window.location.search;
	}
	if (!query || query.length == 0) {
		return "";
	}
	var parts = query.substr(1).split("&");
	for (var i = 0; i < parts.length; i++) {
		if (parts[i].indexOf("q=") == 0) {
			return unescape(parts[i].substr(2).replace(/\+/g, " "));
		}
	}
	return "";
}
EVERYZING.metaPlayerRequest.getQuery = getEzQuery;
function getEzSeek() {
	var query = window.location.hash;
	if (!query  || query.length == 0) {
		query = window.location.search;
	}
	if (!query || query.length == 0) {
		return 0;
	}
	var parts = query.substr(1).split("&");
	for (var i = 0; i < parts.length; i++) {
		if (parts[i].indexOf("seek=") == 0) {
			return parts[i].substr(5);
		}
	}
	return 0;
}
EVERYZING.metaPlayerRequest.getSeek = getEzSeek;
// Called from Flash when new media loaded (e.g., continuous play, highlights reel)
function setEzMediaTitle(value){
	jQuery(".ez-playerMod-title *").attr("title", value);
	jQuery(".ez-playerMod-title *").text(value);
    //EVERYZING.playerMod_title_trunc(ezplayer_config.ezMaxTitleLength);
}
EVERYZING.metaPlayerRequest.setTitle = setEzMediaTitle;
function setEzMediaPubDate(value){
	jQuery(".ez-playerMod-pubdate *").text(value);
}
EVERYZING.metaPlayerRequest.setPubDate = setEzMediaPubDate;
function setEzMediaDescription(value){
	jQuery(".ez-playerMod-description *").text(value);
	//EVERYZING.playerMod_episode_trunc(ezplayer_config.ezMaxDescLength);
}
EVERYZING.metaPlayerRequest.setDescription = setEzMediaDescription;
// When the "lander" video is finished, remove the full transcript because it's
// no longer relevant. (We may, in future, refresh the text via Ajax or whatever.)
function removeEzMediaTranscript() {
	jQuery("#ez-fullTranscript-area").hide();
	jQuery("#ez-fullTranscript-area").hide(); // twice ?
}
EVERYZING.metaPlayerRequest.removeFullTextTranscript = removeEzMediaTranscript;
EVERYZING.metaPlayerRequest.setThumbnail = function(url) {

}
EVERYZING.metaPlayerRequest.setEpisodeId = function(value) {
	// write your own code here
}

// arr is an array of cue.List.Arg
EVERYZING.metaPlayerRequest.handleContentCue = function(timestamp, term, code, arr) {
    // TEST
    //alert("Content cue at " + timestamp + " for " + term);
    eval(code);
}

// PLAYER EVENTS
// This is called by the player on player functions play, pause, jumpto, etc. 
EVERYZING.handlePlayerEvent = function(eventData) {
  jQuery(document).trigger('EVERYZING.metaplayerEvent', [eventData]);
}
// Bind all appropriate handlers for player events: ads, metrics, whatever

EVERYZING.handleTracking = function(e, eventData) {
	var trackIt = false;
	switch (eventData.action) {
	    // "dotted" name are deprecated. 
	    // All actions are defined in AS3 MetricsEvent and AdEvent
		case "action.player.play":
		case "action.player.jumpto":
		case "action.player.pause":
		case "action.player.stop":
		case "action_player_play":
		case "action_player_jumpto":
		case "action_player_pause":
		case "action_player_stop":
		case "metrics_video_begin":
		case "metrics_video_complete":
		//ad_preroll_start
		//ad_preroll_complete
		//ad_postroll_start
		//ad_postroll_complete
			trackIt = true;
			break;
		default:;
	}
	if (trackIt) {
		//If this is an event we want to track pass it to the
		//tracker.  But only if the tracker is enabled.

		try {
			var tracker = new EZTracker();   
		 	tracker.sendTrackingData(eventData);
		} catch(err)  {
			//tracking not enabled
		}
		 		
	}
}
jQuery(document).bind('EVERYZING.metaplayerEvent', EVERYZING.handleTracking);

/* SAMPLE event handler for Omniture	 
EVERYZING.handleOmniture = function (e, eventData) {
	// map appropriate player actions to more generic Omniture actions 
	var omnitureAction = "";
	switch (eventData.action) {
		case "action.player.play":
		case "action.player.jumpto":
			omnitureAction = "play";
			break;
		case "action.player.pause":
			omnitureAction = "pause";
			break;
		case "action.player.stop":
			omnitureAction = "close";
			break;
		// TODO need "open" when video starts, which is not currently
		// differentiated from "play"	
		default:;
	}				
	//alert("Omniture action: " + omnitureAction + ", " + eventData.time);
	// This is the function provided by the Omniture API. Maybe.
	if (omnitureAction != "") {
		trackOmniture(omnitureAction, eventData.time);
	} 
}
jQuery(document).bind('EVERYZING.metaplayerEvent', EVERYZING.handleOmniture);

*/


// For testing and RAMP console embedded player dynamic updates
function updateEZPlayerConfig(array) {
	// An array of arrays. Each component array is config key name and config value.
	getEZMovie(ezMetaPlayerName).ezPlayerConfig(array);
}
function updateEZPlayerStyles(foundationSkin, themeSkin) {
	getEZMovie(ezMetaPlayerName).ezUpdateSkin([foundationSkin || "", themeSkin || ""]);
}
function getEZMovie(movieName) {
    return document.getElementById(movieName);
}

// For player control
// The functions after getEZMovie are registered in the player as callbacks
function EZPlayer_play() {
	getEZMovie(ezMetaPlayerName).ezPlay();
}
function EZPlayer_pause() {
	getEZMovie(ezMetaPlayerName).ezPause();	
}
function EZPlayer_isPlaying() {
	return getEZMovie(ezMetaPlayerName).ezIsPlaying();
}
function EZPlayer_jumpTo(time) {
	getEZMovie(ezMetaPlayerName).ezJumpTo(time, "js");
}
