/////////////////////////////////////////////////////////
////-------------------------------------------------////
////         (c) Copyright 2000, BYOBroadcast.com.   ////
////              All Rights Reserved.               ////
////-------------------------------------------------////
/////////////////////////////////////////////////////////
//		BYOBAudioAPI.js v1.0   10/02/00

var serverName = "archive.byobroadcast.com"
var codeBase = "http://" + serverName + "/applets/";
var mediaURL = "http://" + serverName + "/core/" + "playlist.jsp"
var callURL = "http://" + serverName + "/core/" + "callin.jsp"

var BYOB_POPUP_HEIGHT = 40;
var BYOB_POPUP_WIDTH = 225;

var DEFAULT_HEIGHT = 60;
var DEFAULT_WIDTH = 78;
var DEFAULT_SKIN = "generic";
var DEFAULT_BGCOLOR = "white";
var DEFAULT_AUTOPLAY = true;

var height = DEFAULT_HEIGHT;
var width = DEFAULT_WIDTH;
var skin = DEFAULT_SKIN;
var bgcolor = DEFAULT_BGCOLOR;
var autoplay = DEFAULT_AUTOPLAY;

var playerInitialised = false
var win = self;

  ///////////////////////////////////////
 // byobWritePlayer
// public method that does various checks and writes the applet tag to win.document
/// customer_id is nonnullable
/// pass in null for default values on other arguments
function byobWritePlayer( customer_id, media_id, heightIn, widthIn, skinIn, bgcolorIn, autoplayIn )
{
    setParameters( heightIn, widthIn, skinIn, bgcolorIn, autoplayIn );

    if( (media_id == null) && noJavascriptToJavaCalls() )
    {
        // there is nothing to do
        //window.alert("noJavascriptToJavaCalls allowed");
    }
    else
    {
        writePlayer( customer_id, media_id );
        win.document.close();
    }
}

  ////////////////////
 // byobPopupPlayer
// public method that does various checks, opens a popup to win and writes the player applet.
/// pass in null for default values on other arguments
function byobPopupPlayer( customer_id, media_id, heightIn, widthIn, skinIn, bgcolorIn, autoplayIn)
{
    setParameters( heightIn, widthIn, skinIn, bgcolorIn, autoplayIn );

    if( (media_id == null) && noJavascriptToJavaCalls() )
    {
        // there is nothing to do
        //window.alert("noJavascriptToJavaCalls allowed");
    }
    else
    {
        popupPlayer( customer_id, media_id );

    }
}


  /////////////////////////////
 // byobPlay
// uses whatever player applet is present and plays media_id that is passed in.
// if no player is present a popup opens and plays
function byobPlay( customer_id, media_id )
{
    var file = mediaURL + "?byob_media_id=" + media_id;

    //if user has closed window or is using a noJavascriptToJavaCalls() set-up
    //popup and play message
    if( noJavascriptToJavaCalls() || win.closed )
    {
        //window.alert("IE MAC uses a popup to play");
        popupPlayer( customer_id, media_id );
    }
    else
    {
        win.document.player.playPost( file );
        win.focus();
    }
}

  ///////////////////////////////////////
 // byobPhoneCallIn
// public method that does various checks and writes the applet tag to win.document
/// customer_id and key are nonnullable
/// pass in null for default values on other arguments
function byobPhoneCallIn( customer_id, key, heightIn, widthIn )
{
    setParameters( heightIn, widthIn );
    win = window.open( callURL + "?cid=" + customer_id + "&key=" + key, null, "height=" + BYOB_POPUP_HEIGHT + ",width=" + BYOB_POPUP_WIDTH + ",directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no" );
    win.focus();
}

  ///////////////////////////////////////
 // byobWriteRecorder
// public method that does various checks and writes the recorder applet tag to win.document
/// customer_id and key are nonnullable
/// pass in null for default values on other arguments
function byobWriteRecorder(customer_id, key, heightIn, widthIn, skinIn, bgcolorIn )
{
    setParameters( heightIn, widthIn, skinIn, bgcolorIn );

    if (navigator.userAgent.indexOf("Win")>=1)
    {
        writeRecorder( customer_id, key );
        win.document.close();
    }
    else
    {
        if( key != null )
            byobPhoneCallIn( customer_id, key );
    }
}

  ///////////////////////////////////////
 // byobPopupRecorder
// public method that does various checks, opens a popup to win and writes the player applet.
/// customer_id and key are nonnullable
/// pass in null for default values on other arguments
function byobPopupRecorder( customer_id, key, heightIn, widthIn, skinIn, bgcolorIn )
{
    setParameters( heightIn, widthIn, skinIn, bgcolorIn );
    //popupRecorder( customer_id, key );

    if (navigator.userAgent.indexOf("Win")>=1)
        popupRecorder( customer_id, key );

    else
        if( key != null )
            byobPhoneCallIn( customer_id, key );

}

  ///////////////////////////////////////
 // byobPrepareRecord
// public method calls prepare record on the present recorder applet.
// if there is no recoder present a new one opens prepared.
/// customer_id and key are nonnullable
function byobPrepareRecord( customer_id, key )
{
    if (navigator.userAgent.indexOf("Win")>=1)
    {
        // if user has closed recorder window open a new one
        if( win.closed )
            popupRecorder( customer_id, key );

        else
            if ( isPlayerInitialised() )
            {
                win.document.recorder.prepareRecordWithKey( customer_id, key );
                win.focus();
            }
            else
                alert( "Applet is initialising, please wait a few seconds and try again" );
    }
    //popup phone number for the MAC
    else
        byobPhoneCallIn( customer_id, key );

}

  ///////////////////////////////////////
 // byobUserChoose
// public method that opens a popup to win that allows the
// user to choose between microphone and telephone recording
/// customer_id and key are nonnullable
/// pass in null for default values on other arguments
function byobUserChoose( customer_id, key, heightIn, widthIn, skinIn, bgcolorIn )
{
    if( key == null )
    {
        alert("a key is required for this functionality");
        return;
    }

    setParameters( heightIn, widthIn, skinIn, bgcolorIn );

    if( navigator.userAgent.indexOf("Mac")>=1 )
        byobPhoneCallIn( customer_id, key );
    else
    {
        win = window.open( "", null, "height=" + BYOB_POPUP_HEIGHT + ",width=" + BYOB_POPUP_WIDTH + ",directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no" );
    	win.document.writeln("<html><head><title>BYOBroadcast choose recorder</title></head>");
	    win.document.writeln("<body bgcolor='" + bgcolor + "' >");
	    win.document.writeln("<center>");
	    win.document.writeln("Choose a recording device please<BR>");
 	    win.document.writeln("<FORM>");
// 	    win.document.writeln("<INPUT type=button value=Microphone onClick=javascript:self.resizeTo("+height+","+width+");opener.popupRecorder(" + customer_id + ",'" + key + "');>");
// 	    win.document.writeln("<INPUT type=button value=Telephone onClick=javascript:self.resizeTo("+height+","+width+");opener.byobPhoneCallIn(" + customer_id + ",'" + key + "');>");
 	    win.document.writeln("<INPUT type=button value=Microphone onClick=javascript:opener.popupRecorder(" + customer_id + ",'" + key + "');>");
 	    win.document.writeln("<INPUT type=button value=Telephone onClick=javascript:opener.byobPhoneCallIn(" + customer_id + ",'" + key + "');>");
	    win.document.writeln("</FORM>");
	    win.document.writeln("</center>");
	    win.document.writeln("</body></html>");
	    win.document.close();
        win.focus();
    }
}

function setParameters( heightIn, widthIn, skinIn, bgcolorIn, autoplayIn )
{
    if( heightIn != null )
        height = heightIn;

    if( widthIn != null )
        width = widthIn;

    if( skinIn != null )
        skin = skinIn;

    if( bgcolorIn != null )
        bgcolor = bgcolorIn;

    if( autoplayIn != null )
        autoplay = autoplayIn;

//window.alert("Parameters set: height=" + height + ", width=" + width + ", skin=" + skin + ", bgcolor=" + bgcolor + ", autoplay=" + autoplay);
}

function noJavascriptToJavaCalls()
{
    var calls = false;

    // IE MAC can not make javascript to java calls
    if( (navigator.userAgent.indexOf("Mac")>=1) && (navigator.userAgent.indexOf("MSIE")>=1) )
        calls = true;

    return calls;
}

function done()
{
}

function isPlayerInitialised()
{
    return playerInitialised;
}

function setPlayerInitialised( bIsInitialised )
{
    playerInitialised = bIsInitialised;
}

function writeMediaId( media_id )
{
    if( media_id != null )
    {
 	    win.document.writeln("<param name='autoplay' value='" + autoplay + "' > ");
	    win.document.writeln("<param name='media' value='" + mediaURL + "?byob_media_id=" + media_id + "' >");
    }
}

function writePlayer( customer_id, media_id )
{
//    window.alert("writePlayer(..):: height=" + height + ", width=" + width + ", skin=" + skin);

	    win.document.writeln("<APPLET code=com.byobroadcast.applets.player.Player.class height=" + height + " width="+ width +" name=player");
 	    win.document.writeln("archive='" + codeBase + "PlayerApplet.jar'");
	    win.document.writeln("codebase='" + codeBase + "' MAYSCRIPT>");
	    win.document.writeln("<param name=bgColor value='" + bgcolor + "' >");
 	    win.document.writeln("<param name='configname' value='" + skin + "' >");
 	    win.document.writeln("<param name='revertplay' value='1' >");

        writeMediaId( media_id );

        win.document.writeln("You must enable Java in your web browser to use the BYOBroadcast.com Media Player</APPLET>");
}

function popupPlayer( customer_id, media_id )
{
    win = window.open( "", null, "height=" + height + ",width=" + width + ",directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no" );

    win.document.writeln("<script language='javascript'>");
	win.document.writeln("function done() { window.close(); }");
	win.document.writeln("function setPlayerInitialised( bIsInitialised ) { playerInitialised = bIsInitialised; }");
	win.document.writeln("</script>");
	win.document.writeln("<html><head><title>BYOBroadcast.com media player</title></head>");
	win.document.writeln("<body>");
	win.document.writeln("<center>");

    writePlayer( customer_id, media_id );

    win.document.writeln("</center>");
	win.document.writeln("</body></html>");
	win.document.close();
    win.focus();
}


function writeKey( key )
{
//alert("writeKey called: key=" + key);
    if( key != null )
        win.document.writeln("<param name='key' value='" + key + "'>");
}

function writeRecorder(customer_id, key )
{
    win.document.writeln("<APPLET code=com.byobroadcast.applets.postapplet.ControllerApplet.class");
    win.document.writeln("height=" + height + " width=" + width);
    win.document.writeln("name=recorder archive='" + codeBase + "PostingAppletClasses.jar'");
    win.document.writeln("codebase='" + codeBase + "' MAYSCRIPT>\n");
    win.document.writeln("<param name=bgColor value='" + bgcolor + "' >");
    win.document.writeln("<param name='configname' value='" + skin + "'>");
    win.document.writeln("<param name='customerid' value='" + customer_id + "'>");

    writeKey(key);
    //win.document.writeln("<param name='key' value='" + key + "'>");

    win.document.writeln("<param name='acceptor' value='http://" + serverName + "/servlets/AspMediaPostAcceptor'>");
    win.document.writeln("<PARAM NAME='cabbase' VALUE='" + codeBase + "PostingAppletClasses.cab'>");
    win.document.writeln("You must enable Java in your web browser to use the BYOBroadcast.com Media Player</APPLET>");
}

function popupRecorder( customer_id, key )
{
    win = window.open( "", null, "height=" + height + ",width=" + width + ",directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no" );

    win.document.writeln("<html><head><title>BYOBroadcast.com media recorder</title></head>");
    win.document.writeln("<script language='javascript'>");
	win.document.writeln("function done() { window.close(); }");
	win.document.writeln("function complete( post_media_id, duration ) { opener.complete( post_media_id, duration ); }");
	win.document.writeln("function setPlayerInitialised( bIsInitialised ) { opener.setPlayerInitialised( bIsInitialised ); }");
	win.document.writeln("</script>");
	win.document.writeln("<body>");
	win.document.writeln("<center>");

    writeRecorder( customer_id, key );

	win.document.writeln("</center>");
	win.document.writeln("</body></html>");
	win.document.close();
    win.focus();

}



