/***
*	WindowsMediaPlayer 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
*/
WindowsMediaPlayer.prototype = new MediaPlayer();
WindowsMediaPlayer.prototype.constructor = WindowsMediaPlayer;
WindowsMediaPlayer.superclass = MediaPlayer.prototype;

/**
*	Constructor
*/
function WindowsMediaPlayer(src, width, height, version)
{
	this.init(src, width, height, version);
}

/**
*	Initializor
*/ 
WindowsMediaPlayer.prototype.init = function(src, width, height, version) {    
    WindowsMediaPlayer.superclass.init.call(this, src, width, height);
    this.setRequiredVersion( "7" );
    this.setPlayerClassId( "CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" );
    this.setApplicationType("application/x-mplayer2");
    this.setCodebase("http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112");
    this.setPluginName( "Windows Media Player" );
    this.player_states_map = Array("undefined","stopped","paused","playing","scanForward","scanReverse",
	         "buffering","waiting","mediaEnded","transitioning","ready","reconnecting");
}

/**
*	Player Controls
*/ 

WindowsMediaPlayer.prototype.start = function() {
	this.getPlayerElement().URL = this.getSrc();
}

WindowsMediaPlayer.prototype.play = function() {
	this.getPlayerElement().controls.play();
}

WindowsMediaPlayer.prototype.pause = function() {
	this.getPlayerElement().controls.pause();
}

WindowsMediaPlayer.prototype.stop = function() {
	this.getPlayerElement().controls.stop();
}

WindowsMediaPlayer.prototype.playPause = function() {
    if (this.getPlayerElement().playState == 3) { // playing
        this.pause();
    } else if (this.canPlay()) { // paused/stopped/ready/waiting
        this.play();
    } else {
        throw new Error("Player cannot play/pause at this time");
    }
}

WindowsMediaPlayer.prototype.canPlay = function() {
	return this.getPlayerElement().controls.isAvailable("play");
}

WindowsMediaPlayer.prototype.canPause = function() {
	return this.getPlayerElement().controls.isAvailable("pause");
}

WindowsMediaPlayer.prototype.canStop = function() {
	return this.getPlayerElement().controls.isAvailable("stop");
}

WindowsMediaPlayer.prototype.canPlayPause = function() {
	return (this.canPlay() || this.canPause());
}

/**
*	Stream State Controls
*/ 
WindowsMediaPlayer.prototype.canSeek = function() {
	return this.getPlayerElement().controls.isAvailable("currentPosition");
}

/* Returns the length of the clip in milliseconds */
WindowsMediaPlayer.prototype.getLength = function() {
	return parseInt(this.getPlayerElement().currentMedia.duration * 1000);
}

/* Returns the current position of the clip in milliseconds */
WindowsMediaPlayer.prototype.getPosition = function() {
	return parseInt(this.getPlayerElement().controls.currentPosition * 1000);
}

/* Sets the position of the clip.  Requires a a position in milliseconds */
WindowsMediaPlayer.prototype.setPosition = function( new_milli_position ) {
	if ( new_milli_position != null ) {
		parseInt(this.getPlayerElement().controls.currentPosition = (new_milli_position / 1000));
	}
}

/* Is the clip a live stream? */
WindowsMediaPlayer.prototype.isLiveStream = function() {
	return false; // no known WM attribute for this
}

/* Returns the title of the current stream */
WindowsMediaPlayer.prototype.getStreamTitle = function() {
	return this.getPlayerElement().currentPlaylist.name;
}

/* Returns the copyright of the current stream */
WindowsMediaPlayer.prototype.getClipCopyright = function() {
	return this.getPlayerElement().currentMedia.getItemInfo("Copyright");
}

/* Returns the current play state of the stream */
WindowsMediaPlayer.prototype.getPlayerState = function() {
	return this.getStateMap( this.getPlayerElement().playState );
}

/* Returns the number of entries in the playlist. */
WindowsMediaPlayer.prototype.getNumClips = function() {
	return this.getPlayerElement().currentPlaylist.count;
}

/* Returns the index of the current entry in the playlist */
WindowsMediaPlayer.prototype.getClipIndex = function() {
    for (index=0; index < this.getNumClips(); index++) {
        if (this.getPlayerElement().currentMedia.isIdentical(this.getPlayerElement().currentPlaylist.item(index))) {
            return index;
        }
    }
    return 0;
}

/* Returns the title of the current entry in the playlist */
WindowsMediaPlayer.prototype.getClipTitle = function( clip_index ) {
	var title = "";
	try {
		title = this.getPlayerElement().currentPlaylist.item(clip_index).name;
	} catch (e) {
		throw new Error("The player returned an error:" + e);
	}
	return title;
}

/* Returns the version information for the embedded player */
WindowsMediaPlayer.prototype.getPlayerVersion = function() {
	return this.getPlayerElement().versionInfo;
};

WindowsMediaPlayer.prototype.getHTML = function()
{
    var html = "";
    html += '<object classid="' + this.getPlayerClassId() + '" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getObjectId() + '"';
    if (this.getCodebase != "") { html += ' codebase="' + this.getCodebase() + '"'; }
    if (this.getApplicationType != "") { html += ' type="' + this.getApplicationType() + '"'; }
    html += '>';
    html += '<param name="src" value="' + this.getSrc() + '" />';;
    if (this.getParamTags() != null) {
        html += this.getParamTags();
    }
    if (this.getVariablePairs() != null) {
        html += '<param name="realVars" value="' + this.getVariablePairs() + '" />';
    }
    html += '</object>';
    return html;
};


