/***
*	QuicktimePlayer 0.0.1
*
*	An implementation class for the MediaPlayer Interface.  
*	Should be called by the MediaPlayerFactory.
*
*	(c) 2006 Tachometry Corporation.  
*	contact: support@tachometry.com
*
*	Licensed under the Apache License (v. 2.0)
*	http://www.apache.org/licenses/LICENSE-2.0
*/

/**
*	Inherit from MediaPlayer
*/
QuicktimePlayer.prototype = new MediaPlayer();
QuicktimePlayer.prototype.constructor = QuicktimePlayer;
QuicktimePlayer.superclass = MediaPlayer.prototype;

/**
*	Constructor
*/
function QuicktimePlayer(src, width, height, version)
{
	this.init(src, width, height, version);
}

/**
*	Initializor
*/ 
QuicktimePlayer.prototype.init = function(src, width, height, version) {    
    QuicktimePlayer.superclass.init.call(this, src, width, height+20);
    this.setRequiredVersion( version );
	this.setPlayerClassId( "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" );
	this.setApplicationType("video/quicktime");
	this.setCodebase("http://www.apple.com/qtactivex/qtplugin.cab");
	this.setPluginName( "Quicktime Player" );
	this.setParam("autostart",true);
}

/**
*	Player Controls
*/ 

QuicktimePlayer.prototype.play = function() {
	this.getPlayerElement().Play();
}

QuicktimePlayer.prototype.pause = function() {
	this.getPlayerElement().Stop(); // no native pause method
}

QuicktimePlayer.prototype.stop = function() {
	this.getPlayerElement().Stop(); // stops playing
	this.getPlayerElement().Rewind(); // rewind to the beginning
}

QuicktimePlayer.prototype.playPause = function() {
	if (this.getPlayerElement().GetRate() == 0 ) {
		this.getPlayerElement().Play(); 
	} else {
		this.getPlayerElement().Stop();
	}
}

QuicktimePlayer.prototype.canPlay = function() {
	if (this.getPlayerElement().GetRate() == 0) {
		return true;
	}
	return false;
}

QuicktimePlayer.prototype.canPause = function() {
	if (this.getPlayerElement().GetRate() == 1) {
		return true;
	}
	return false;
}

QuicktimePlayer.prototype.canStop = function() {
	if (this.canPlayPause() && this.getPosition() > 1 ) {
		return true;
	}
	return false;
}

QuicktimePlayer.prototype.canPlayPause = function() {
	if (this.canPlay() || this.canPause()) {
		return true;
	}
	return false;
}

/**
*	Stream State Controls
*/ 
QuicktimePlayer.prototype.canSeek = function() {
	return this.canPlayPause(); // no known live stream control
}

/* Returns the length of the clip in milliseconds */
QuicktimePlayer.prototype.getLength = function() {
	return this.getMilliValue( this.getPlayerElement().GetDuration() );
}

/* Returns the current position of the clip in milliseconds */
QuicktimePlayer.prototype.getPosition = function() {
	return this.getMilliValue( this.getPlayerElement().GetTime() );
}

/* Sets the position of the clip.  Requires a position in milliseconds */
QuicktimePlayer.prototype.setPosition = function( new_milli_position ) {
	if ( new_milli_position != null ) {
		this.pause();
		this.getPlayerElement().SetTime( this.getFrameValue(new_milli_position) );
		this.play();
	}
}

/* Is the clip a live stream? */
QuicktimePlayer.prototype.isLiveStream = function() {
	return false; // no known live stream
}

/* Returns the title of the current stream */
QuicktimePlayer.prototype.getStreamTitle = function() {
	return this.getPlayerElement().GetMovieName();
}

/* Returns the copyright of the current stream */
QuicktimePlayer.prototype.getClipCopyright = function() {
	return this.getPlayerElement().GetUserData("@cpy");
}

/* Returns the current play state of the stream */
QuicktimePlayer.prototype.getPlayerState = function() {
	var state = this.getPlayerElement().GetPluginStatus();
	switch ( state.toLowerCase() )
	{
		case "waiting" :
			return this.getStateMap(1); break;
		case "loading" :
			return this.getStateMap(2); break;
		case "playable" :
			return this.getStateMap(0); break;
		case "complete" :
			if (this.getPlayerElement().GetRate() == 0 && this.canStop()) {
				return this.getStateMap(4); // paused
			} else if (this.getPlayerElement().GetRate() == 0) {
				return this.getStateMap(0); // stopped
			} else {
				return this.getStateMap(3); // playing
			}			
			break;
		default :
			return state; break;				
	}
}

/* Returns the number of entiries in the playlist. */
QuicktimePlayer.prototype.getNumClips = function() {
	return this.getPlayerElement().GetTrackCount();
}

/* Returns the index of the current entry in the playlist */
QuicktimePlayer.prototype.getClipIndex = function() {
	// Quicktime doesn't seem to support a way to get the current clip
	// If the number of clips is equal to 1, we can assume we are in clip number zero
	// otherwise we cannot determine the current clip index
	if (this.getNumClips() == 1)	{
		return "0";
	}
	else {
		throw new Error("Cannot determine clip index");
	}
}

/* Returns the title of the current entry in the playlist */
QuicktimePlayer.prototype.getClipTitle = function( clip_index ) {
	if (clip_index != "") {
		return this.getPlayerElement().GetTrackName( clip_index - 0 );
	}
	return "";
}

/* Returns the version information for the embedded player */
QuicktimePlayer.prototype.getPlayerVersion = function() {
	return this.getPlayerElement().GetPluginVersion();
};

/* Returns the millisecond value for the given frame position based on current player frame rate */
QuicktimePlayer.prototype.getMilliValue = function( frame_position ) {
	var timescale = this.getPlayerElement().GetTimeScale(); // frame rate per second
	var seconds = frame_position / timescale;
	return Math.round(seconds * 1000); // returns milliseconds
};

/* Returns the frame value for the given milliseconds based on current player frame rate */
QuicktimePlayer.prototype.getFrameValue = function( milli_value ) {
	var timescale = this.getPlayerElement().GetTimeScale(); // frame rate per second
	var seconds = milli_value / 1000;
	return Math.round( seconds * timescale ); // returns frames
};

document.write('<OBJECT ID="'+ this.getObjectId +'" WIDTH=0 HEIGHT=0 CLASSID="CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"></OBJECT>');

