 /**
 * VideoPlayer Object Class
 *  
 */

// An object of player references, used for callbacks (see below)
window.videoPlayers = {};

function VideoPlayer(config, id, flashVer, swfFile) {

	// Perform basic checks that parameters are correct
	if(!(config instanceof Array || config instanceof Object)) {
		throw new Error("Config is not array or object");
	}
	
	if(!(typeof id == 'string')) {
		throw new Error("id is not a valid string");
	}	
		
	// Set the id in the object
	VideoPlayer.prototype.id = id;
	
	// Create a reference to the video player, and set the event callback function
	window.videoPlayers[id] = this;
	config.eventCallback = "videoPlayers." + id + ".eventCallback";
	
	// Keep track of the player status
	VideoPlayer.prototype.status =  {
	 	playerCreated: true,
	 	templateLoaded: false,
	 	contentLoaded: false
	};
	
	// Setting and invoking user-defined callback functions
	VideoPlayer.prototype.eventCallbacks = {};
	
	// Check that swfobject library is present
	 if(typeof(swfobject) == 'undefined') {
		throw new Error("swfobject library not loaded");
	}

	// var flashvars = config;
	var params = {
		scale: "showall",
		wmode: "opaque",
		allowfullscreen: "true",
		allowScriptAccess: "always" // TODO set to "sameDomain" for production
	};

	swfobject.embedSWF(swfFile, id, config.width, config.height, flashVer, false, config, params);
	
	// Keep a reference to the DOM element
	VideoPlayer.prototype.flashObj = document.getElementById(this.id);
}

// Add callbacks trigged on events received by eventCallback()
VideoPlayer.prototype.addEventCallback = function(event, callback) {	
 	this.eventCallbacks[event] = callback;
};


// Load a title by id
// VideoPlayer.prototype.loadTitleById = function(id) {
// 	
// 	// Perform basic checks that parameters are correct
// 	if(isNaN(id)) {
// 		throw new Error("id is not a number, its value is " + id);
// 	}	
// 	
// 	if(this.status.playerCreated) {
// 		callFlash("loadTitleById", id);
// 	}
// 	else {
// 		throw new Error("Player not created");
// 	}
// 	
// };

// Load a title by reference id
VideoPlayer.prototype.loadTitleByReferenceId  = function(refId) {
	
	if(!this.status.playerCreated) {
	 	throw new Error("Player not created");
	}
	
	this.flashObj.loadTitleByReferenceId(refId);
};


// Generic callback for events sent from the player / SWFObject
VideoPlayer.prototype.eventCallback = function(eventObj) {
	// alert("Player " + this.id + " recieved an event object with type=" + eventObj.type)
	
	// Any internal handling of events
	switch (eventObj.type) {
		case "templateLoaded":
			this.status.templateLoaded = true;
			break;
		case "contentLoad":
			this.status.contentLoaded = true;
			break;
		default:
	}
	
	// A callback function for this event has been set then call it, passing the event and player
	if( this.eventCallbacks[eventObj.type] ) {
		this.eventCallbacks[eventObj.type](eventObj, this);
	}
	
	return true;
};

// Get the current title DTO object
VideoPlayer.prototype.getCurrentTitle  = function() {
	
	if(!this.status.playerCreated) {
	 	throw new Error("Player not created");
	}
	
	var titleDTO = this.flashObj.getCurrentTitle();
	return titleDTO;
};