function MediaPlayer(properties)
{
	this.playlistIndex = 0;
	this.properties = properties;
	this.playlistLength = this.properties['titles'].length;
}

MediaPlayer.prototype.setStatus = function(status)
{
	var playerId = this.properties['id'];
	var ctl = $(playerId + '_status');
	if (ctl) ctl.firstChild.nodeValue = status;
}

MediaPlayer.prototype.isPlaying = function()
{
	var player = this.getPlayerObject();
	if (!player) return false;
	if (!player.GetURL()) return false;
	return (player.GetRate() != 0);
}

MediaPlayer.prototype.getPlayerObject = function()
{
	var playerId = this.properties['id'];
	var player = $(playerId + '_player');
	// check if the player html has loaded yet
	if (!player) return false;
	// check IE separately; it halts on checking Play's existence
	if ((document.all && (navigator.userAgent.indexOf("Opera") == -1)) || (player && player.Play)) {
		return player;
	}
	var player = $(playerId + '_player_embed');
	if (player && player.Play) {
		return player;
	}
	return null;
}

MediaPlayer.prototype.playPause = function()
{
	if (this.isPlaying()) {
		this.pause();
	} else {
		this.play();
	}
}

MediaPlayer.prototype.go = function(index)
{
	this.playlistIndex = index;
	var player = this.getPlayerObject();
	if (!player) return false;
	player.SetURL(this.properties['urls'][this.playlistIndex]);
	this.play();
}

MediaPlayer.prototype.play = function()
{
	var player = this.getPlayerObject();
	if (!player) return false;
	//player.SetControllerVisible(false);
	player.Play();

	var playerId = this.properties['id'];
	$(playerId + '_index').firstChild.nodeValue = (this.playlistIndex + 1) + '/' + this.playlistLength;
	$(playerId + '_selector').selectedIndex = this.playlistIndex;
	$(playerId + '_playpause').title = 'Pause';
	$(playerId + '_playpause').className = 'mediaplayer_pause';

	SetCookie('mediaplayer_status', 'playing');
}
MediaPlayer.prototype.pause = function()
{
	var playerId = this.properties['id'];
	$(playerId + '_playpause').title = 'Play';
	$(playerId + '_playpause').className = 'mediaplayer_play';

	var player = this.getPlayerObject();
	if (!player) return false;
	player.Stop();
	SetCookie('mediaplayer_status', 'paused');
}
MediaPlayer.prototype.next = function()
{
	if (this.playlistIndex < this.playlistLength - 1) {
		this.playlistIndex++;
	} else {
		this.playlistIndex = 0;
	}
	var player = this.getPlayerObject();
	if (!player) return false;
	player.SetURL(this.properties['urls'][this.playlistIndex]);
	this.play();
}
MediaPlayer.prototype.previous = function()
{
	if (this.playlistIndex > 0) {
		this.playlistIndex--;
	} else {
		this.playlistIndex = this.playlistLength - 1;
	}
	var player = this.getPlayerObject();
	if (!player) return false;
	player.SetURL(this.properties['urls'][this.playlistIndex]);
	this.play();
}

/* mpTrack is a separate 'thread' that monitors a mediaplayer */

function mpTrack(controller)
{
	var player = controller.getPlayerObject();
	if (player) {
		if (player.GetURL()) {
			var time = player.GetTime();
			var endTime = player.GetEndTime();
			var timeScale = player.GetTimeScale();
			var playerId = controller.properties['id'];
			if (controller.isPlaying()) {
				$(playerId + '_time').firstChild.nodeValue = mpFormatTime(time / timeScale) + ' / ' +
					mpFormatTime(endTime / timeScale);
				controller.setStatus('Playing');
			} else {
				if (time >= endTime) {
					controller.setStatus('Finished');
					controller.next();
				} else if ($(playerId + '_playpause').title == 'Play') {
					controller.setStatus('Paused');
				} else {
					var status = player.GetPluginStatus();
					controller.setStatus('QT not playing: ' + (status == null ? '' : status));
				}
			}
		} else {
			var status = player.GetPluginStatus()
			controller.setStatus('QT has no URL: ' + (status == null ? '' : status));
		}
	} else {
		//controller.setStatus('QuickTime not installed');
	}
	setTimeout(function(){ mpTrack(controller); }, 200);
}

function mpFormatTime(secs)
{
	var minutes = parseInt((secs / 60) + " ");
	var seconds = parseInt((secs - minutes * 60) + " ");
	if (seconds < 10) seconds = "0" + seconds;
	if (minutes < 10) minutes = "0" + minutes;
	return minutes + ':' + seconds;
}
function mpIsQTInstalled() {
	var qtInstalled = false;
	qtObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
			var plugin = navigator.plugins[i];
			if (plugin.name.indexOf("QuickTime") > -1) {
				qtInstalled = true;
			}
		}
	} else {
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
		qtInstalled = qtObj;
	}
	return qtInstalled;
}
function mpLoadPlayer(id, code)
{
	// do not use $()
	document.getElementById(id).innerHTML = code;
}
