/* 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 removeFullTextTranscript() {
	jQuery("#ez-fullTranscript-area").hide();
	jQuery("#ez-fullTranscript-area").hide(); // twice ?
}
EVERYZING.metaPlayerRequest.removeTranscript = removeFullTextTranscript;
EVERYZING.metaPlayerRequest.setEpisodeId = function(value) {
	// write your own code here
}
// 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
jQuery(document).ready(function() { 
	// Sample for Omniture
	//jQuery(document).bind('EVERYZING.metaplayerEvent', EVERYZING.handleOmniture);
	jQuery(document).bind('EVERYZING.metaplayerEvent', EVERYZING.handleTracking);
});
EVERYZING.handleTracking = function(e, eventData) {
	var trackIt = false;
	switch (eventData.action) {
		case "action.player.play":
		case "action.player.jumpto":
		case "action.player.pause":
		case "action.player.stop":
			trackIt = true;
			break;
		default:;
	}
	if (trackIt) {
		// build XML and execute AJAX call to post data
		// or store up XML and post the whole thing on page unload
	}
}
/* 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);
	} 
}
*/

// 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
// These function are registered in the player and probably should
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");
}