
/**
 * MediaPlayer Class.
 * 
 * @param 	flashView The Flash view to call methods on.
 */
function MediaPlayer(flashView) 
{
    this.flashView = flashView;
    this.playList = [];
}

MediaPlayer.prototype.flashView;
MediaPlayer.prototype.selected;
MediaPlayer.prototype.playList;
MediaPlayer.prototype.photoList;

/** @private */
MediaPlayer.prototype.ready;

/**
 * @return 	Whether the player components are all ready and ready.
 */
MediaPlayer.prototype.isReady = function()
{
	return this.ready;
}



/**
 * @return 	The Flash View that this JS controls.
 */
MediaPlayer.prototype.getFlashView = function() 
{
    return this.flashView;
}

/**
 * @return 	The HTML View (JS/HTML/AJAX composite) that this JS controls.
 */
MediaPlayer.prototype.getHtmlView = function()
{
	return this.htmlView;
}

/**
 * Play a video.
 */
//MediaPlayer.prototype.playVideo = function(url, artist, title, attribution, watermark)
MediaPlayer.prototype.playVideo = function(media_vid)
{
	if (!this.ready) return;
	this.selected = media_vid;
	this.flashView.playVideo(media_vid.url, media_vid.artist, media_vid.title, stripCopyright(media_vid.attribution), media_vid.watermark);

}

/**
 * Toggle pause on the currently playing video. If the video is playing, this 
 * will pause it at the current point. If the video is paused, this will 
 * resume playback from the current point.
 */
MediaPlayer.prototype.pauseVideo = function()
{
	if (!this.ready) return;
	this.flashView.pauseVideo();
}

/**
 * Stop video playback of the current video. The video's playhead will go back 
 * to 0 seconds, and if the video is playing it will stop.
 */
MediaPlayer.prototype.stopVideo = function()
{
	if (!this.ready) return;
	this.flashView.stopVideo();
}

/**
 * Show an image.
 */
MediaPlayer.prototype.showImage = function(media_item)
{
	if (!this.ready) return;
	this.selected = media_item;
	this.flashView.showImage(media_item.url, media_item.artist, emptyString(media_item.title), stripCopyright(media_item.attribution), media_item.watermark);
}

MediaPlayer.prototype.playVideoIndex = function(index)
{
	if(index >= 0 && index < this.playList.length){
		this.playVideo(this.playList[index]);
	}
}
	
MediaPlayer.prototype.playPhotoIndex = function(index)
{
	this.showImage(this.photoList[index]);
}	
	
/**
* Put the media player in a sleep state (freeze video). Used when the client
 * needs to balance performance with intense JS. Specifically aimed at solving
 * problem of JS animation during video playback on Windows platform.
 */
MediaPlayer.prototype.sleep = function()
{
 this.flashView.sleep();
}

/**
 * Wake the media player from a sleep state (frozen video).
 */
MediaPlayer.prototype.wake = function()
{
 this.flashView.wake();
}


/**
 * Object to hold media items
 */
function MediaItem(url, artist, title, attribution, watermark, type, call_context) 
{
    this.url = url;
    this.artist = artist;
    this.type = type;
    this.title = title;
    this.attribution = attribution;
    this.watermark = watermark;
    this.call_context = call_context;
}

MediaItem.prototype.url;
MediaItem.prototype.artist;
MediaItem.prototype.title;
MediaItem.prototype.attribution;
MediaItem.prototype.watermark;
MediaItem.prototype.type;
MediaItem.prototype.call_context;

/* Function to replace html copyright tag so flash displays correctly */
function stripCopyright(attribution) {
	if (attribution != null && attribution != '') {
		attribution = attribution.replace('&copy;', '(c)');
	} 
	return attribution;
}
/* If string is empty return null */
function emptyString(str) {
	if (str == "" || str == '') {
		return null;
	}
	return str;
}

//// CALLBACKS FROM FLASH (GLOBAL TO DOCUMENT)
