// $Id: //placeware/main/build/placeware.ccc/lib/ccc/view-wmm.1/player.js#22 $
// Copyright 1999-2001 PlaceWare, Inc.

// Play States for Windows Media Player
var UNDEFINED = 0;
var STOPPED   = 1;  // Player 6 = 0
var PAUSED    = 2;  // Player 6 = 1
var PLAYING   = 3;  // Player 6 = 2
var FORWARD   = 4;  // Player 6 = 4
var REVERSE   = 5;  // Player 6 = 5
var BUFFERING = 6;
var WAITING   = 7;  // Player 6 = 3
var MEDIAENDED= 8;  // Player 6 = 8
var TRANSITION= 9;
var READY     =10;

var player;
var playerReady=false;

//======================================================================
//===  Player Object
//======================================================================
// Object to encapsulate different versions of the Windows Media Players
function Player(name, height,width, filename) { // constructor
    this.name        = name;
    this.height      = height;
    this.width       = width;
    this.file        = filename;

    this.show        = ShowPlayer;
    this.player      = GetPlayer;
    this.play        = Play;
    this.title       = GetTitle;
    this.marker      = GetCurrentMarker;
    this.markerCount = GetMarkerCount;
    this.markerName  = GetMarkerName;
    this.state       = GetPlayerState;
    this.progress    = GetBufferingProgress;
    this.setMarker   = SetCurrntMarker;
    this.setBuffer   = SetBufferingTime;
    this.getBuffer   = GetBufferingTime;
    this.setFileName = SetFileName;

    this.errorCount  = GetErrorCount;
    this.errorCode   = GetErrorCode;
    this.errorDesp   = GetErrorDesp;

    this.resetInfo   = ResetInfo;
    this.getMarkers  = GetMarkers;
}

function ShowPlayer() {
    if (this.name=="Player7")
    	document.write('<OBJECT ID="' + this.name + '"'
    			+ ' height="' + this.height + '"'
			+ ' width="'  + this.width + '"'
			+ ' CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"\n>'
			+ '</OBJECT>');
    else
    	document.write('<OBJECT ID="' + this.name + '" height="' + this.height + '"'
		 	+ ' width="'  + this.width + '"'
			+ ' CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"\n'
			+ ' STANDBY="Loading Windows Media Player..."'
			+ ' TYPE="application/x-oleobject"\n>'
			+ '<EMBED TYPE="application/x-mplayer2"'
			+ ' NAME="'   + this.name + '"'
			+ ' HEIGHT="' + this.height + '"'
			+ ' WIDTH="'  + this.width + '"\n>'
			+ '</EMBED>'
			+ '</OBJECT>');
    playerReady = true;
}

function GetPlayer() {

    if (this.name=="Player6ns")
     	return document.Player6ns;
    else if (this.name=="Player7")
    	return Player7;
    else
    	return Player6;
}

function Play() {

    if (this.name=="Player7")
        this.player().controls.play();
    else // Player6
	this.player().Play();
}

function GetTitle() {

    if (this.name == "Player7")
        return this.player().currentMedia.getItemInfo("title");
    else // Player6
        return this.player().GetMediaInfoString(8);
}

function GetMarkerCount() {
    var count=0;
    if (this.name=="Player6ns")
        // KB056 WMB2: Need to convert the return value to Integer
        count= parseInt(this.player().GetMarkerCount());
    else if (this.name=="Player7")
    	count= this.player().currentMedia.markerCount;
    else // Player6
    	count= this.player().MarkerCount;

    return count;

}

function GetMarkerName(i) {
    if (this.name == "Player7")
	return this.player().currentMedia.getMarkerName(i + 1);
    else // Player6
	return this.player().GetMarkerName(i + 1);
}

// WMP markers are 1-based, change to 0-based for the selectIndex
function GetCurrentMarker() {

    if (this.name=="Player6ns")
        return this.player().GetCurrentMarker()-1;
    else if (this.name == "Player7")
        return this.player().controls.currentMarker-1;
    else // Player6
    	return this.player().CurrentMarker-1;
}

// n is the selectIndex and is 0-based
function SetCurrntMarker(n) {

    if (this.name == "Player6ns")
    	this.player().SetCurrentMarker(n+1);
    else if (this.name == "Player7")
    	this.player().controls.currentMarker = n+1;
    else // Player6
    	this.player().CurrentMarker = n+1;
}

// s is number of seconds
// the bufferingTime is a read-only value in Player6
// this function is currently not used.
function SetBufferingTime(s) {

    if (this.name == "Player7")
    	Player7.network.bufferingTime = s * 10000;

}

// the unit is 'second'
// this function is currently not used.
function GetBufferingTime() {

    if (this.name == "Player6ns")
    	return this.player().GetBufferingTime();
    else if (this.name == "Player7")
    	return (this.player().network.bufferingTime/10000);
    else // Player6
    	return this.player().BufferingTime;
}

// set filename
function SetFileName() {

    if (this.name == "Player6ns")
        this.player().SetFileName(this.file);
    else if (this.name == "Player7")
        this.player().URL = this.file;
    else // Player6
    	this.player().FileName = this.file;
}

function GetPlayerState() {

    if (this.name=="Player6ns")
	return (parseInt(this.player().GetPlayState(),10) + 1);
    else if (this.name == "Player7")
    	return this.player().playState;
    else
    	return (this.player().PlayState + 1);
}

function GetBufferingProgress() {
var p;
    if (this.name=="Player6ns")
        p= this.player().GetBufferingProgress();
    else if (this.name == "Player7")
        p= this.player().network.bufferingProgress;
    else
    	p= this.player().BufferingProgress;
    // if ( p>0 && p<100) alert("progress=" + p); // Debug
    return p;
}


function GetErrorCount(){

    // errorCount only supports WMP7
    if (this.name == "Player7")
    	return this.player().error.errorCount;
    // check hasError for WMP64
    else if (this.name == "Player6ns")
        return (this.player().GetHasError()) ? 1:0;
    else
    	return (this.player().hasError) ? 1:0;
}

function GetErrorCode(i){

    if (this.name == "Player6ns")
    	return this.player().GetErrorCode();
    else if (this.name == "Player7")
    	return this.player().error.item(i).errorCode;
    else
    	return this.player().ErrorCode;

}

function GetErrorDesp(i){

    if (this.name == "Player6ns")
    	return this.player().GetErrorDescription();
    else if (this.name == "Player7")
    	return this.player().error.item(i).errorDescription;
    else
    	return this.player().ErrorDescription;

}

function ResetInfo() {

    // set presenter name to empty string
    if (PresenterName!="&nbsp;") {
      PresenterName="&nbsp;";
      elements.RecDiv.setBody(RecInfoTable(RecTitle, PresenterName));
    }
    
    // set presenter image to blank image
    imgs.presenterImage.setSrc("");
    
    changeText(null); // clear the texts in the QA section

}


// Get the Player's markers, now that playback started.
// ** Modified this function so that it works with Media Player 6.4
var isFakeOption = true;
function GetMarkers() {

    // Get Marker Count
    var list= (isNetscape4)? document.SlideDiv.document.TOCform.SlideList:
    			    document.TOCform.SlideList;
    
    // Remove the fake options before inserting real ones.
    if (isFakeOption)  {
	// Remove the fake options
	for (i = list.options.length - 1; i >= 0; i--) {
		if (isNetscape4)
		   list.options[i] = null;
		else
	    	   list.options.remove(i);
        }
	isFakeOption = false;
    }

    var markerName;
    var markerOption; // short name that can be displayed within the table
    var count = this.markerCount();

    // set the recording title after receiving the count.
    RecTitle=this.title();
    elements.RecDiv.setBody(RecInfoTable(RecTitle, PresenterName));

    for (i = 0; i < count; ++i) {
	   markerName = this.markerName(i);
	   // Limit the length of the markerName to avoid the scroll
	   // bar to be cut off in IE
	   markerOption = (isNetscape4)? markerName: markerName.substr(0, 35);
	   list.options[i]       = new Option(markerName);
	   list.options[i].text  = markerOption;
	   list.options[i].value = markerName;
    }

}


