/*
 *  (c) Copyright 2000-2005
 *      everstream, Inc.
 *      All rights reserved.
 */

//=============================================================================================
//                  Basic Media Player Library
//=============================================================================================

var wmpLoading = null;
var curWMP = null;
function WindowsMediaPlayer() {
    this.Volume = 60;
    this.Mute = false;
    this.Width = 0;
    this.Height = 0;
    this.Name = "MPlayer";
    this.ID = "MPlayer";
    this.Position = null;
    this.obj = null;
    this.hasWMPlugin = this.wmPluginCheck();
    this.useActiveX = this.activexCheck();
}

WindowsMediaPlayer.prototype.wmPluginCheck = function() {
    var os = navigator.platform;
    if (navigator.platform == "Win32") {
	if (navigator.plugins.length) {
	    for (var i = 0; i < navigator.plugins.length; i ++ ) {
		if (navigator.plugins[i].name.toLowerCase().indexOf("windows media player plug-in") != -1)
		    return true;
	    }
	}
    }
    return false;
}

WindowsMediaPlayer.prototype.activexCheck = function() {
    if (this.hasWMPlugin) {
        return true;
    }

    var os = navigator.platform;
    if (navigator.platform == "Win32") {
	if (navigator.plugins.length) {
	    for (var i = 0; i < navigator.plugins.length; i ++ ) {
		if (navigator.plugins[i].name.toLowerCase().indexOf("activex") != -1)
		    return true;
	    }
	} else {  // assume it's there
	    return true;
	}
    }
    return false;
}

WindowsMediaPlayer.prototype.create = function(name) {
    if (this.exists()) return this.parentWin;
    this.parentWin = addWindow(this.render(), name);
    this.obj = document.getElementById(this.ID);
    if ((this.obj != null) && this.useActiveX && this.Position != null) {
	wmpLoading = this;
	this.curTimer = window.setTimeout(this.Timer, 500);
	addEvent(this.obj,"CurrentPlaylistChange", this.playlistChange, false, false);
    } else {
	initialized = true;
    }
    if ((this.obj != null) && this.useActiveX) {
	curWMP = this;
	addEvent(this.obj,"PlayStateChange", this.playStateChange, false, false);
    }
    return this.parentWin;
}

WindowsMediaPlayer.prototype.exists = function() {
    return this.parentWin != null;
}

WindowsMediaPlayer.prototype.destroy = function() {
    try {
	if (this.useActiveX)
	    this.obj.controls.stop();
    } catch(e) {
    }
    this.clearLoading();
    removeEvent(this.obj,"PlayStateChange", this.playStateChange, false, false);
    if (this.parentWin != null) {
	var body = document.getElementById("body");
	body.removeChild(this.parentWin);
	this.parentWin = null;
    }
    this.obj = null;
    if (this == curWMP) curWMP = null;
}

WindowsMediaPlayer.prototype.render = function() {
    var a;
    if (this.useActiveX) {
	a = "<object ID='" + this.ID + "' Name='" + this.Name + "'";
        if (this.hasWMPlugin) {
            a += " type='application/x-ms-wmp'";
        } else {
	    a += " CLASSID='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6'";
        }
	a += " height='" + this.Height + "' width='" + this.Width + "'";
        if (!this.hasWMPlugin)
            a += " VIEWASTEXT";
        a += ">";
	a += "<param NAME='URL' VALUE='" + this.URL + "'>";

if (!this.hasWMPlugin) {
	a += "<param NAME='settings.AutoStart' VALUE='";
	if (this.Position != null)
	    a += "0'>";
	else
	    a += "1'>";

	a += "<param NAME='uiMode' VALUE='none'>";
}
	a += "<param NAME='Enabled' VALUE='1'>";
	a += "<param NAME='enableContextmenu' VALUE='0'>";
if (!this.hasWMPlugin) {
	a += "<param NAME='settings.volume' VALUE='" + this.Volume + "'>";
}
	a += "</object>";
    } else {
	var vol = -10000;
	if (this.Volume != 0)
	    vol = Math.floor((Math.log(this.Volume / 100) / 2.303) * 2000);
	if (vol > 0) vol = 0;
	if (vol < -10000) vol = -10000;
	if (this.Mute) vol = -10000;
	    
        if (this.Height == 0) this.Height = 60;
	if (this.Width == 0) this.Width = 300;
	a = "<embed type='application/x-mplayer2' ID='" + this.ID + "' Name='" + this.Name + "' HEIGHT='" + this.Height + "' WIDTH='" + this.Width + "'";
	a += " SRC='" + this.URL + "'";
	a += " SHOWCONTROLS='1'";
	a += " SHOWPOSITIONCONTROLS='0'";
	a += " SHOWTRACKER='0'";
	a += " SHOWDISPLAY='0'";
	a += " VOLUME='" + vol + "'";
	a += " AUTOSTART='1'>";
	a += "</embed>";
    }
    return a;
}

WindowsMediaPlayer.prototype.GetVolume = function() {
    return this.Volume;
}

WindowsMediaPlayer.prototype.SetVolume = function(v) {
    this.Volume = v;
    if (this.obj != null) {
	if (this.useActiveX)
	    this.obj.settings.volume = v;
	else if (this.exists()) {
	    this.destroy();
	    this.create();
	}
    }
}

WindowsMediaPlayer.prototype.GetURL = function()
{
    return this.URL;
}

WindowsMediaPlayer.prototype.SetURL = function(url)
{
    this.URL = url;
    this.playLoaded = false;
    if (this.exists()) {
	this.destroy();
	this.create();
    }
}

WindowsMediaPlayer.prototype.IsPaused = function()
{
    if ((this.obj == null) || !this.useActiveX) return true;

    var curState = this.obj.playState;
    return (curState == playStateUndefined ||
            curState == playStateStopped ||
            curState == playStatePaused);
}

WindowsMediaPlayer.prototype.CanSkip = function()
{
    if ((this.obj == null) || !this.useActiveX) return false;
    return (this.obj.network.bufferingProgress > 95) ||
           (this.obj.playState == playStatePlaying);
}

WindowsMediaPlayer.prototype.Play = function()
{
    if (this.obj != null) {
	if (this.useActiveX) {
	    this.obj.controls.play();
	    return;
	}
    }
    this.create();
}

WindowsMediaPlayer.prototype.Pause = function()
{
    if ((this.obj != null) && this.useActiveX) {
	this.obj.controls.pause();
    } else {
	this.destroy();
    }
}

WindowsMediaPlayer.prototype.Stop = function()
{
    if (this.obj != null) {
	if (this.useActiveX) {
	    this.obj.controls.stop();
	} else {
	    this.destroy();
	}
    }
}

WindowsMediaPlayer.prototype.GetMute = function()
{
    return this.Mute;
}

WindowsMediaPlayer.prototype.SetMute = function(v)
{
    this.Mute = v;
    if (this.obj != null) {
	if (this.useActiveX) {
	    this.obj.settings.mute = v;
	} else if (this.exists()) {
	    this.destroy();
	    this.create();
	}
    }
}

WindowsMediaPlayer.prototype.MoveNext = function()
{
    if ((this.obj != null) && this.useActiveX)
	this.obj.controls.next();
}

WindowsMediaPlayer.prototype.IsLiveStream = function()
{
    if ((this.obj == null) || !this.useActiveX) return true;
    var elem = this.obj.currentPlaylist.getItemInfo("LiveStream");
    if ((elem) && (elem == "true"))
        return true;
    else
        return false;
}

WindowsMediaPlayer.prototype.GetMediaIndex = function()
{
    if ((this.obj == null) || !this.useActiveX) return 0;
    return getMediaIndex(this.obj, this.obj.currentMedia);
}

WindowsMediaPlayer.prototype.doPosition = function()
{
    var thisSelection = 0;

    if (this.IsLiveStream())
        this.Position = null;

    if (this.Position != null) {
        thisSelection = this.Position;

        if (thisSelection >= this.obj.currentPlaylist.count)
            thisSelection = 0;

        if (isNaN(thisSelection))
            thisSelection = 0;
    }

    this.obj.controls.playItem(this.obj.currentPlayList.item(thisSelection));
    initialized = true;
}

WindowsMediaPlayer.prototype.clearLoading = function() {
    window.clearTimeout(this.curTimer);
    this.curTimer = -1;
    removeEvent(this.obj,"CurrentPlaylistChange", this.playlistChange, false, false);
    if (wmpLoading == this)
	wmpLoading = null;
}

WindowsMediaPlayer.prototype.Timer = function() {
    if (wmpLoading == null) return;
    wmpLoading.curTimer = window.setTimeout(wmpLoading.Timer, 500);
    var curState = wmpLoading.obj.playState;

    if (curState == playStateReady) {
        if (wmpLoading.lastState == playStateReady) {
	    wmpLoading.doPosition();
	    wmpLoading.clearLoading();
	}
    }

    if (wmpLoading != null)
	wmpLoading.lastState = curState;
}

WindowsMediaPlayer.prototype.playlistChange = function(change)
{
    if (wmpLoading == null) return;
    var processChange = false;

    if ((change == playlistChangeInfoChange) || (change == playlistChangeNameChange))
        processChange = true;

    if (processChange && !wmpLoading.playLoaded) {
        wmpLoading.playLoaded = true;
	wmpLoading.obj.controls.stop();
    }
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=||=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\\
// Live Stream Code
WindowsMediaPlayer.prototype.playStateChange = function(newState)
{
    if (curWMP == null) return;
    if (curWMP.obj != null) 
	if (curWMP.obj.settings != null)
	    curWMP.obj.settings.mute = curWMP.Mute;
}

//******************************************************************************
// Media Player openState constants
//******************************************************************************
var openStateUndefined = 0;
var openStatePlaylistChanging = 1;
var openStatePlaylistLocating = 2;
var openStatePlaylistConnecting = 3;
var openStatePlaylistLoading =  4;
var openStatePlaylistOpening =  5;
var openStatePlaylistOpenNoMedia = 6;
var openStatePlaylistChanged =  7;
var openStateMediaChanging = 8;
var openStateMediaLocating = 9;
var openStateMediaConnecting = 10;
var openStateMediaLoading = 11;
var openStateMediaOpening = 12;
var openStateMediaOpen = 13;
var openStateBeginCodecAcquisition = 14;
var openStateEndCodecAcquisistion = 15;
var openStateBeginLicenseAcquisition = 16;
var openStateEndLicenseAcquisition = 17;
var openStateBeginIndividualization = 18;
var openStateEndIndividualization = 19;
var openStateMediaWaiting = 20;
var openStateOpeningUnknownURL = 21;

//******************************************************************************
// Media Player playState constants
//******************************************************************************
var playStateUndefined = 0;
var playStateStopped = 1;
var playStatePaused = 2;
var playStatePlaying = 3;
var playStateScanForward = 4;
var playStateScanReverse = 5;
var playStateBuffering = 6;
var playStateWaiting = 7;
var playStateMediaEnded = 8;
var playStateTransitioning = 9;
var playStateReady = 10;
var playStateReconnecting = 11;

//******************************************************************************
// Media Player playlistChange constants
//******************************************************************************
var playlistChangeUnknown = 0;
var playlistChangeClear = 1;
var playlistChangeInfoChange = 2;
var playlistChangeMove = 3;
var playlistChangeDelete = 4;
var playlistChangeInsert = 5;
var playlistChangeAppend = 6;
var playlistChangeNotSupported = 7;
var playlistChangeNameChange = 8;
var playlistChangeNotSupported2 = 9;
var playlistChangeSort = 10;

function openStateName(s)
{
    var ret = "";

    switch(s) {
        case openStateUndefined:              ret = "Undefined";                break;
        case openStatePlaylistChanging:       ret = "PlaylistChanging";         break;
        case openStatePlaylistLocating:       ret = "PlaylistLocating";         break;
        case openStatePlaylistConnecting:     ret = "PlaylistConnecting";       break;
        case openStatePlaylistLoading:        ret = "PlaylistLoading";          break;
        case openStatePlaylistOpening:        ret = "PlaylistOpening";          break;
        case openStatePlaylistOpenNoMedia:    ret = "PlaylistOpenNoMedia";      break;
        case openStatePlaylistChanged:        ret = "PlaylistChanged";          break;
        case openStateMediaChanging:          ret = "MediaChanging";            break;
        case openStateMediaLocating:          ret = "MediaLocating";            break;
        case openStateMediaConnecting:        ret = "MediaConnecting";          break;
        case openStateMediaLoading:           ret = "MediaLoading";             break;
        case openStateMediaOpening:           ret = "MediaOpening";             break;
        case openStateMediaOpen:              ret = "MediaOpen";                break;
        case openStateBeginCodecAcquisition:  ret = "BeginCodecAcquisition";    break;
        case openStateEndCodecAcquisistion:   ret = "EndCodecAcquisistion";     break;
        case openStateBeginLicenseAcquisition:ret = "BeginLicenseAcquisition";  break;
        case openStateEndLicenseAcquisition:  ret = "EndLicenseAcquisition";    break;
        case openStateBeginIndividualization: ret = "BeginIndividualization";   break;
        case openStateEndIndividualization:   ret = "EndIndividualization";     break;
        case openStateMediaWaiting:           ret = "MediaWaiting";             break;
        case openStateOpeningUnknownURL:      ret = "OpeningUnknownURL";        break;
    }

    return ret;
}

function playStateName(s)
{
    var ret = "";

    switch (s) {
        case playStateUndefined:     ret = "playStateUndefined";     break;
        case playStateStopped:       ret = "playStateStopped";       break;
        case playStatePaused:        ret = "playStatePaused";        break;
        case playStatePlaying:       ret = "playStatePlaying";       break;
        case playStateScanForward:   ret = "playStateScanForward";   break;
        case playStateScanReverse:   ret = "playStateScanReverse";   break;
        case playStateBuffering:     ret = "playStateBuffering";     break;
        case playStateWaiting:       ret = "playStateWaiting";       break;
        case playStateMediaEnded:    ret = "playStateMediaEnded";    break;
        case playStateTransitioning: ret = "playStateTransitioning"; break;
        case playStateReady:         ret = "playStateReady";         break;
        case playStateReconnecting:  ret = "playStateReconnecting";  break;
    }
    return ret;
}

function getMediaIndex(player, m)
{
    var i = 0;
    var pl = player.currentPlaylist;
    for (i = 0; i < pl.count; i++) {
        if (pl.item(i).isIdentical(m))
            return i + 1;
    }
    return 0;
}

