﻿
var oldWrapper = null;
var oldCode = null;
var player = null;
var theSkin = null;
var strMediaShare = null;
var divMediaShare = null;


function removeOldPlayer(theOldWrapper, theOldCode) {
    document.getElementById(theOldWrapper).innerHTML = theOldCode;
}

function playFile(theWrapper, thePlaceholder, thePlayer, theType, theWidth, theHeight, theFile, theImage, theTitle) {

    // update media share links and title
    var theID = theWrapper.replace("wrapper", "")
    if (document.getElementById("MediaShare" + theID)) {
        updateMediaShare(theID, theFile, theType, theWidth, theHeight, theTitle, theImage); 
    }
    
    //player
    if ((oldWrapper == null) || (!document.getElementById(oldWrapper)) || (document.getElementById(oldWrapper).innerHTML.indexOf("placeholder") > -1)) { 
        // no player exists -- note, all those checks are necessary to catch AJAX changes
        createPlayer("NewPlayer", theWrapper, thePlaceholder, thePlayer, theType, theWidth, theHeight, theFile, theImage, theTitle); 
    }
    else { // player does exist
        if (oldWrapper != theWrapper) { // remove existing player and create new one
            document.getElementById(oldWrapper).innerHTML = oldCode; // Remove Old Player
            createPlayer("NewPlayer", theWrapper, thePlaceholder, thePlayer, theType, theWidth, theHeight, theFile, theImage, theTitle);
        } else { // use existing player
            createPlayer("UseExisting", theWrapper, thePlaceholder, thePlayer, theType, theWidth, theHeight, theFile, theImage, theTitle);
        }
    }    
}

function createPlayer(NewOrExisting, theWrapper, thePlaceholder, thePlayer, theType, theWidth, theHeight, theFile, theImage, theTitle) {
    if (parseInt(theWidth) < 200) {
        theSkin = "Media/MediaPlayer/style_Sm.xml";
    } else {
        theSkin = "Media/MediaPlayer/style_Lg.xml"; 
        if (theType != "sound") { theImage = "Media/MediaImages/_blank.gif"; } //do not change this
    }
    
    var flashvars = {
        id: thePlayer,
        file: theFile,
        type: theType,
        image: theImage,
        skin: theSkin,
        autostart: "false", // do not use autostart=true (it messes up video play) instad use player.sendEvent('PLAY', true); in addListeners
        mute: "false",
        quality: "true",
        repeat: "none",
        shuffle: "false",
        stretching: "fill",
        volume: "75" //,
        //backcolor: "FFFFFF",
        //frontcolor: "000000",
        //lightcolor: "000000",
        //screencolor: "000000"
    }
    if (NewOrExisting=="NewPlayer") { 
        oldWrapper = theWrapper;
        oldCode = document.getElementById(oldWrapper).innerHTML;
        var params = { allowfullscreen: "true", allowscriptaccess: "always", wmode: "transparent" }
        var attributes = { id: thePlayer, name: thePlayer }
        
        swfobject.embedSWF("Media/MediaPlayer/player.swf", thePlaceholder, theWidth, theHeight, "9.0.115", false, flashvars, params, attributes);
        //alert( document.getElementById(theWrapper).innerHTML);
        
   } else { var player = document.getElementById(thePlayer);
        if (player) {
            player.sendEvent('STOP');
            player.sendEvent('LOAD', flashvars);
        }
    }
}

function playerReady(thePlayer) {
    player = window.document[thePlayer.id];
    addListeners();
}

function addListeners() { //autostart workaround because autostart=true causes video problems
    if (player) {
        setTimeout("player.sendEvent('PLAY', true)", 5);
    } else {
        setTimeout("addListeners()", 100);
    }
}

function updateMediaShare(strID, strFile, strType, strWidth, strHeight, theTitle, theImage) {
    //each time a new file is loaded, update: ms_EmailLink##.href  -  ms_DownloadLink##.href  -  ms_EmbedTxtbx##.value
   
    var ms_EmailLink = document.getElementById("ms_EmailLink" + strID); 
    var ms_DownloadLink = document.getElementById("ms_DownloadLink" + strID);
    var ms_EmbedTxtbx = document.getElementById("ms_EmbedTxtbx" + strID);
    var ms_TitleDiv = document.getElementById("ms_TitleDiv" + strID);
    
    var strEmailSubject, strEmailBody, strEmbedCode, strNewString
    
    // Clear Prev values -------------
    if (ms_DownloadLink) { ms_DownloadLink.href = "";}
    if (ms_EmailLink) { ms_EmailLink.href = ""; }
    if (ms_EmbedTxtbx) { ms_EmbedTxtbx.value = ""; }

    // Get new email subject and body
    if (strType == "sound") {
        strEmailSubject = escape("Listen to this... ");
        strEmailBody = encodeURI("audio file from Phillips Theological Seminary website:") + "%0A" + encodeURI(strFile);
    } else {
        strEmailSubject = escape("Watch this video... ");
        strEmailBody = encodeURI("Video file from Phillips Theological Seminary website:") + "%0A" + encodeURI(strFile);
    }
    // Get new embed code
    if (strType == "sound") { var strPlayerHeight = "20"; } else { strPlayerHeight = strHeight; }
    var strServer = location.protocol + '//' + location.hostname + location.pathname.substring(0, location.pathname.lastIndexOf('/'))
    if (strFile.indexOf("http") == -1) { strFile = strServer + "/Media/" + strFile }
    if (theImage.indexOf("/") == -1) { theImage = strServer + "/Media/MediaImages/" + theImage }
    else if (theImage.indexOf("http") == -1) { theImage = strServer + "/" + theImage }
    
    var strEmbedCode = " " + 
        "<embed src=\"" + strServer + "/Media/MediaPlayer/player.swf\" " +
        "flashvars=\"file=" + strFile + "&image=" + theImage + "\" width=\"" + strWidth + "\" height=\"" + strPlayerHeight + "\" " +
        "allowfullscreen=\"true\" allowscriptaccess=\"always\" ></embed>"

    // Set New values , using a zero timeout for now -------------
    if (ms_DownloadLink) {
        setTimeout("updateText('ms_DownloadLink" + strID + "', 'href', '" + strFile + "')", 0);
    }
    if (ms_EmailLink) {
        strNewString = "MailTo:?subject=" + strEmailSubject + "&body=" + strEmailBody;
        setTimeout("updateText('ms_EmailLink" + strID + "', 'href', '" + strNewString + "')", 0);
    }
    if (ms_EmbedTxtbx) {
        setTimeout("updateText('ms_EmbedTxtbx" + strID + "', 'value', '" + strEmbedCode + "')", 0);
    }

    if (ms_TitleDiv && theTitle) {
        setTimeout("updateText('ms_TitleDiv" + strID + "', 'innerHTML', '" + theTitle + "')", 0);
    }
}
function updateText(OjbID, ObjElement, NewText) {
    document.getElementById(OjbID)[ObjElement] = NewText; 
}

function MediaWindow(MediaID, MediaSz) {
    day = new Date();
    id = day.getTime();
    URL = "MediaPlayer.aspx?ID=" + MediaID 
    if (MediaSz == "lg") {
        strWidth = "650"; strHeight = "515"; 
        URL = URL + "&sz=lg"
    }
    else {
        strWidth = "440"; strHeight = "350"; 
    }
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + strWidth + ",height=" + strHeight + ",left=440,top=225');");
}

var TickerInterval
function startticker(divMarque, content) {
    if (document.getElementById(divMarque + "_outer") && document.getElementById(divMarque)) {
        var scrollspeed = "1";  // speed is scrollspeed (1 = slow, 5 = fast)
        var divWidth = document.getElementById(divMarque + "_outer").style.width;
        document.getElementById(divMarque).style.left = (parseInt(divWidth) + 10) + "px";
        document.getElementById(divMarque).innerHTML = '<span id="' + divMarque + '_inner">Now Playing: ' + content + '<\/span>';
        var offsetWidth = document.getElementById(divMarque + '_inner').offsetWidth;
        TickerInterval = setInterval("scrollticker('" + divMarque + "', '" + divWidth + "', '" + offsetWidth + "', '" + scrollspeed + "')", 40);
    } 
}
function stopticker() {
    if (TickerInterval) { clearInterval(TickerInterval); }
}
function scrollticker(divMarque, divWidth, offsetWidth, scrollspeed) {
    mq = document.getElementById(divMarque);
    mq.style.left = (parseInt(mq.style.left) > (-10 - offsetWidth)) ? parseInt(mq.style.left) - scrollspeed + "px" : parseInt(divWidth) + 10 + "px";
}
