// $Id: //placeware/main/build/placeware.ccc/lib/ccc/view-wmm.1/event.js#21 $
// Copyright 2001 PlaceWare, Inc.

if (isExplorer) {
	document.writeln(
	    '<SCRIPT LANGUAGE="JavaScript" FOR="Player7" EVENT="MarkerHit(marker)">\n'
	  + ' Player7_MarkerHit(marker);\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player7" EVENT="PlayStateChange(playState)">\n'
	  + ' Player7_PlayStateChange(playState);\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player7" EVENT="ScriptCommand(scType, scParam)">\n'
	  + ' Player7_ScriptCommand(scType, scParam);\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player7" EVENT="Error()">\n'
	  + ' Player7_Error();\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player6" EVENT="MarkerHit(marker)">\n'
	  + ' Player6_MarkerHit(marker);\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player6" EVENT="PlayStateChange()">\n'
	  + ' Player6_PlayStateChange();\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player6" EVENT="ScriptCommand(scType, scParam)">\n'
	  + ' Player6_ScriptCommand(scType, scParam);\n'
	  + '</SCRIPT>\n'
	  + '<SCRIPT LANGUAGE="JavaScript" FOR="Player6" EVENT="Error()">\n'
	  + ' Player6_Error();\n'
	  + '</SCRIPT>\n');
}

// Change text in the Q&A section
var prevText="";
function changeText(text)
{
    // Do not show QA section if we are in a 800x600 screen
    if (smScreen) return;

    // Only show the latest text and the previous one. (One Question and
    // One Answer). No history.
    var lyr = (isNetscape4)? document.QADiv.document.forms[0]: document.all;

    // if current text equals null, clear all text in this section.
    lyr.msgs.value = (text == null)? "" : prevText + text + "\n";

    prevText = (text == null)? "": text + "\n";
}

//===========================================================================
//  Functions for progress bar
//===========================================================================
var b_timer;
// Show buffering information while buffering the content
// This is for both player 6 and player 7
function showProgress(start)
{
    // reset the bar
    elements.DataDiv.setBody('');
    elements.BufferDiv.clipTo(0,0,8,0);
    clearTimeout(b_timer);
   
    // start showing the bar
    if (start && playerReady) {
        // Netscape does not recognize ccs style setting.
        var text = isNetscape4? '<font size="-1"><b>' + bufferingText + '</b></font>'
        		      : bufferingText;
       
	elements.DataDiv.setBody(text);
	startBuffering();
    }
}

// Show the buffering progress
function startBuffering()
{ 
    var width = (smScreen)? 88:130;
    var speed = (smScreen)?  1:  2;

    if (elements.BufferDiv.clipValue('r') < width) {
    	elements.BufferDiv.clipBy(0, speed, 0, 0);
    	b_timer=setTimeout(startBuffering, (bufferTime * 1000) / (width / speed));
    } else {
	// let the progress bar disappear after 5 secs.
	b_timer=setTimeout(showProgress, 5000, false);
    }

}

//===========================================================================
//  General Event Handlers for different browsers and media players
//===========================================================================

// When the player stop or play.
var firstPlay = true;
function playStateChange(playState)
{
    if (playState == STOPPED) {
    	player.resetInfo();
    	showProgress(false);
    }
    else if (playState == PAUSED) {
        showProgress(false);
    }
    else if (playState == PLAYING) {

    	if (firstPlay == true) {
	    player.resetInfo();
	    player.getMarkers();
	    firstPlay = false;
    	}

    	// Player 6 does not have buffering status, so use playing instead
    	if (player.name!="Player7")
    	    showProgress(true);
    }
    else if (playState == BUFFERING
    	  && player.name=="Player7") {  // to avoid confusion with Player 6
	showProgress(true);
    }

}

// When the Player raises a marker-hit event, select that marker in the slide list.
function markerHit(marker)
{
    var lyr;
    if (isNetscape4) {
        lyr = document.SlideDiv.document;
    } else
        lyr = document;

    // WMP marker is 1-based, change to 0-based for selectIndex
    lyr.TOCform.SlideList.selectedIndex = marker -1;
}

var RecTitle	  = "&nbsp;";  
var PresenterName = "&nbsp;";
// When the player plays a synchronized command or URL.
function scriptCommand(scType, scParam)
{
    if (scType == "presenter") {
        if (scParam=='') 
           scParam = "&nbsp;";
        if (scParam!=PresenterName) {
    	   PresenterName=scParam;
           elements.RecDiv.setBody(RecInfoTable(RecTitle, PresenterName));
        }
    } else if (scType == "presenterImage") {
     	if (!standalone) imgs.presenterImage.setSrc(scParam);

    } else if (scType == "URL=") {
    	showWebSite(scParam);

    } else if (scType == "msg" || scType == "chat") {
    	changeText(scParam);

    } else { // [DEBUG]
    	//changeText("[" + scType + ": " + scParam + "]");
    }
}

function error() {
    var err   = "";
    var count = player.errorCount();

    if (count > 0) {
    	for (var i=0; i<count; i++) {
    		err += player.errorCode(i) + ": " + player.errorDesp(i) + "\n";
    	}

    	alert("Windows Media Player Error\n\n" + err);
    	playerReady=false;
    	showProgress(false);
    }
}

//===========================================================================
//  Events for IE + WMP7
//===========================================================================
// When the player stop or play.
function Player7_PlayStateChange(playState)
{
    playStateChange(playState);
}

// When the player plays a synchronized command or URL.
function Player7_ScriptCommand(scType, scParam)
{
    scriptCommand(scType,scParam);
}

// When the Player raises a marker-hit event, select that marker
// in the slide list.
function Player7_MarkerHit(marker)
{
    markerHit(marker);
}

function Player7_Error()
{
    error();
}

//===========================================================================
//  Events for IE + WMP6.4
//===========================================================================

function Player6_PlayStateChange()
{
    playStateChange(player.state());
}

// When the player plays a synchronized command or URL.
function Player6_ScriptCommand(scType, scParam)
{
    scriptCommand(scType,scParam);
}

function Player6_MarkerHit(marker)
{
    markerHit(marker);
}

// Show error message to the user.
function Player6_Error()
{
    error();
}

//===========================================================================
//  Events for Netscape + WMP6.4
//===========================================================================
var r_timer;
function registerEventObservers ()
{
    // This helper function is required to register event observers with
    // the Java proxy. Registration MUST be done before desired events
    // will be sent.

    var plugin = player.player();
    
    // Wait until the applet is ready
    if (!document.appObs) {
        r_timer=setTimeout(registerEventObservers, 500);
    } else {
    	clearTimeout(r_timer);
    	navigator.plugins.refresh();	
       // Here are the event observers we want activated in the plug-in.
        document.appObs.setByProxyDSScriptCommandObserver(plugin, true);
        document.appObs.setByProxyDSPlayStateChangeObserver(plugin, true);
        document.appObs.setByProxyDSMarkerHitObserver(plugin, true);
        document.appObs.setByProxyDSErrorObserver(plugin,true);
    }
}

//*****************************************************************************
// setTimeout() is necessary because Netscape Navigator requires a new thread
// for DHTML called from the appObs proxy applet.
//*****************************************************************************
function OnDSPlayStateChangeEvt (OldPlayState, NewPlayState)
{
    var playState = parseInt(NewPlayState,10);
    setTimeout(playStateChange, 0, ++playState); // change to WMP7 standard
}

function OnDSScriptCommandEvt (scType, scParam)
{
    setTimeout(scriptCommand, 0, scType, scParam);
}

function OnDSMarkerHitEvt (marker)
{
    setTimeout(markerHit, 0, marker);
}

function OnDSErrorEvt()
{
    setTimeout(error,0);
}

