var agt = navigator.userAgent.toLowerCase();
var bIE = (navigator.appName.indexOf("Internet", 0) != -1) ? true : false;
var bNN4 = document.layers;
var bNN = (navigator.appName.indexOf("Netscape", 0) != -1) ? true : false;
var is_win = ((agt.indexOf("win") != -1) || (agt.indexOf("16bit") != -1));

function SilverLightPlayer(doc, format, playerURL, width, imagewindow,
                           imagewindowheight, controlpanel, controlpanelheight,
                           status, statusheight, writePlayer) {
    this.doc = doc;
    this.format = format;
    this.playerURL = playerURL;
    this.width = width;
    this.imagewindow = imagewindow;
    this.imagewindowheight = imagewindowheight;
    this.controlpanel = controlpanel;
    this.controlpanelheight = controlpanelheight;
    this.status = status;
    this.statusheight = statusheight;
    this.writePlayer = writePlayer;
    this.strPlayer = "";
    this.intLiveTimestamp = null;
    this.silverlightPlayer = null;
    this.intPlayerCurrentPositionMs = 0;
    
    var d = this.doc;
    
    var playerwidth = this.width;
    var playerhheight =
        ((this.imagewindow == 1) ? this.imagewindowheight : 0) +
        ((this.controlpanel == 1) ? this.controlpanelheight : 0) +
        ((this.status == 1) ? this.statusheight : 0);
    
    this.strPlayer =
        '<object id="MEDIAPLAYER"' +
        '        data="data:application/x-silverlight,"' +
        '        type="application/x-silverlight-2"' +
        '        width="' + playerwidth + '"' +
        '        height="' + playerhheight + '">' +
        '    <param name="source" value="/clients/default/mainpages/on24/WMPlayer.xap"/>' +
        '    <param name="onerror" value="onSilverlightError" />' +
        '    <param name="background" value="Transparent" />' +
        '    <param name="windowless" value="true" />' +
        '    <param name="minRuntimeVersion" value="2.0.31005.0" />' +
        '    <param name="autoUpgrade" value="true" />' +
        '    <param name="initParams" value="filename=' + playerURL +
                                            ',width=' + width +
                                            ',imagewindow=' + imagewindow +
                                            ',imagewindowheight=' + imagewindowheight +
                                            ',controlpanel=' + controlpanel +
                                            ',controlpanelheight=' + controlpanelheight +
                                            ',status=' + status +
                                            ',statusheight=' + statusheight + '"/>' +
        '        <a href="http://go.microsoft.com/fwlink/?LinkID=124807"' +
        '           style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181"' +
        '           alt="Get Microsoft Silverlight"' +
        '           style="border-style: none"/></a>' +
        '</object>'

      if (this.writePlayer) {
        d.writeln(this.strPlayer);
    }
    
}
    
// Returns cached player
SilverLightPlayer.prototype.getSilverlightPlayer=function() {
    if (!this["silverlightPlayer"]) {
        this.silverlightPlayer = this.doc.getElementById("MEDIAPLAYER");
    }
    return this.silverlightPlayer;
}

// This value is set to true if the EVENT_INFO element for audio clips
// is active and there are multiple audio clips in the MEDIA_URL array
SilverLightPlayer.prototype.boolHasPlaylist = false; 

// This value is set when the setPosition function is called
SilverLightPlayer.prototype.intGoToPosition = null; 

// Values are clip durations, in milliseconds, ordered by MEDIA_SEQUENCE
SilverLightPlayer.prototype.arrPlaylist = null;

// Values are MEDIA_URL objects taken from ep1.mediaurl array
SilverLightPlayer.prototype.arrAudioClips = null; 

// This method is independent of the actual player type
SilverLightPlayer.prototype.getPlaylistEntryByTotalOffset = function(intTotalOffset) {
    var intClipOffsets = 0;

    if (!this.boolHasPlaylist || !this.arrPlaylist) {
        return 0;
    }

    for (var i = 0; i < this.arrPlaylist.length; i++) {
        intClipOffsets += this.arrPlaylist[i];
        if (intClipOffsets > intTotalOffset) {
            return i;
        }
    }

    return 0;
}

// This method is independent of the actual player type
SilverLightPlayer.prototype.getOffsetModulus = function(intTotalOffset) {
    var intEntryIndex = this.getPlaylistEntryByTotalOffset(intTotalOffset);
    var intOffsetTotal = this.getLengthOfPreviousEntries(intEntryIndex);
    return intTotalOffset - intOffsetTotal;
}

// This method is independent of the actual player type
SilverLightPlayer.prototype.getLengthOfPreviousEntries = function(intEntryIndex) {
    var intClipOffsets = 0;

    if (!this.boolHasPlaylist || !this.arrPlaylist) {
        return 0;
    }

    for (var i = 0; i < intEntryIndex; i++) {
        if (this.arrPlaylist[i] && this.arrPlaylist[i] > 0) {
            intClipOffsets += this.arrPlaylist[i];
        }
    }

    return intClipOffsets;
}

//this method is independent of the actual player type
SilverLightPlayer.prototype.getPosition = function() {
    var position = this.getPositionAbsolute();

    //if (window["console"]) console.log(position)
     //subtracts the streamoffset value from the player position
    if (this.streamoffset && this.streamoffset <= position) {
        position = position - this.streamoffset;
    }
    
     //this code adds the total durations for previous entries in the playlist
    if (this.boolHasPlaylist) {
        var tempposition = position + this.getLengthOfPreviousEntries(this.getCurrentEntry());

        //if the position changes dramatically, RealPlayer may be in transition between clips, so wait
        //call this only if the delta is large, if it is Real, and if it is not changing chapters
        if ((tempposition - this.intPlayerLastPositionMs) > 1500
                && this.format.indexOf("rm") != -1
                && this.intGoToPosition == null) { 
            tempposition = position + this.getLengthOfPreviousEntries(this.getCurrentEntry() - 1);
        } else if ((tempposition - this.intPlayerLastPositionMs) < 1500) {
            this.intGoToPosition = position;
        }
        position = tempposition;
    }
    
    this.intPlayerLastPositionMs = this.intPlayerCurrentPositionMs;
    this.intPlayerCurrentPositionMs = position;
    
    //checks whether the position should be in a different playlist entry and acts accordingly
    //if (this.boolHasPlaylist && this.intGoToPosition!=null) this.setPlaylistPosition();
    
    return position;
}

SilverLightPlayer.prototype.getPositionAbsolute = function() {
    try {
        return this.getSilverlightPlayer().Content.Page.getPosition() * 1000;
    } catch(e) {
        // ignore
    }

    return 0;
}

//this method is independent of the actual player type
SilverLightPlayer.prototype.setPlaylistPosition = function() {
    if (this.intGoToPosition == null || !this.boolHasPlaylist) {
        return;
    }

    var intGoToEntryIndex = this.getPlaylistEntryByTotalOffset(this.intGoToPosition);
    
    //if we are in the correct clip, set the position
    if (this.getCurrentEntry() == intGoToEntryIndex) {
        this.setPosition(this.intGoToPosition/1000);
        return;
    } 
    
     // we need to change the playlist entry index
     this.setCurrentEntry(intGoToEntryIndex);
}

//this method is independent of the actual player type
SilverLightPlayer.prototype.setPosition = function(pos, idx, direction) {
    //add streamoffset to position
    if (this.streamoffset) {
        pos = (pos / 1) + (this.streamoffset / 1000);
    }
    
    if (this.boolHasPlaylist) {
        var intOffset = pos * 1000;
        var intGoToEntryIndex = this.getPlaylistEntryByTotalOffset(intOffset);
        
        if (this.getCurrentEntry() != intGoToEntryIndex) {
            this.intGoToPosition = intOffset;
            this.setCurrentEntry(intGoToEntryIndex);
            return;
        } else {
            intOffsetModulus = this.getOffsetModulus(intOffset);
            pos = intOffsetModulus / 1000;
            this.intGoToPosition = null;
            if (pos < 1) {
                return; //do nothing if we are near the beginning of the media file
            }
        }
    }
    this.setPositionAbsolute(pos);
}

SilverLightPlayer.prototype.setPositionAbsolute = function(pos) {
    if (window["console"]) {
        console.log(pos);
    }
    try {
    	this.getSilverlightPlayer().Content.Page.setPosition(pos);
    } catch(e) {
        // ignore
    }
}

SilverLightPlayer.prototype.play = function() {
    try {
	this.getSilverlightPlayer().Content.Page.play();
    } catch(e) {
        // ignore
    }
}

SilverLightPlayer.prototype.pause = function() {
    try {
	this.getSilverlightPlayer().Content.Page.pause();
    } catch(e) {
        // ignore
    }
}

SilverLightPlayer.prototype.stop = function() {
    try {
	this.getSilverlightPlayer().Content.Page.stop();
    } catch(e) {
        // ignore
    }
}

SilverLightPlayer.prototype.getPlayState = function() {
    try {
	return this.getSilverlightPlayer().Content.Page.getPlayState();
    } catch(e) {
	return "status";
    }
}

SilverLightPlayer.prototype.getPlayerStatus = function() {
    return this.getPlayState().toLowerCase();
}

SilverLightPlayer.prototype.canPlay=function() {
    return this.getPlayerStatus() != "playing";
}

SilverLightPlayer.prototype.setVolume = function(val) {
    try {
        this.getSilverlightPlayer().Content.Page.setVolume(val);
    } catch (e) {
        // ignore
    }
}

SilverLightPlayer.prototype.setMute = function(param) {
    // param can be true or false (1 or 0)
    try {
        this.getSilverlightPlayer().Content.Page.setMute(param == "0" ? "false" : "true");
    } catch (e) {
        // ignore
    }
}

SilverLightPlayer.prototype.getCopyright = function() {
    // TODO
    return "";
}

SilverLightPlayer.prototype.getTitle = function() {
    // TODO
    return "";
}

var player;
SilverLightPlayer.prototype.setSource = function(playlistURI) {
    try {
        this.getSilverlightPlayer().Content.Page.setSource(playlistURI);
    } catch (e) {
        // this is to workaround simulive race condition
        player = this;
        setTimeout("player.setSource('" + playlistURI + "');", 1000);
    }
}

SilverLightPlayer.prototype.getTotalEntries = function() {
    // TODO
    return -1;
}

SilverLightPlayer.prototype.getCurrentEntry = function() {
    // TODO
    return -1;
}

SilverLightPlayer.prototype.gotoNextEntry = function() {
    // TODO
}

SilverLightPlayer.prototype.gotoPrevEntry = function() {
    // TODO
}

SilverLightPlayer.prototype.getLength = function() {
    // TODO
    return -1;
}

SilverLightPlayer.prototype.setCurrentEntry = function(intEntryIndex) {
    // TODO
}