﻿function closePopup() {
    document.getElementById("videoBackground").style.visibility = "hidden";
    try {
        document.getElementById("videoBackground").style.width = 0;
        document.getElementById("videoBackground").style.height = 0;
    } catch (err) { }
    try {
        document.getElementById("videoBackground").style.width = '0px';
        document.getElementById("videoBackground").style.height = '0px';
    } catch (err) { }
    document.getElementById("videoPopup").style.visibility = "hidden";
    document.getElementById("silverlightControlHost").innerHTML = "";
}

function showBackground(backgroundElement, width, height, left, top, opacity) {
    try {
        backgroundElement.style.width = width;
        backgroundElement.style.height = height;
        backgroundElement.style.left = left;
        backgroundElement.style.top = top;
    } catch (err) { }
    try {
        backgroundElement.style.width = width + 'px';
        backgroundElement.style.height = height + 'px';
        backgroundElement.style.left = left + 'px';
        backgroundElement.style.top = top + 'px';
    } catch (err) { }
    try {
        backgroundElement.style.opacity = '.' + opacity;
    } catch (err) { }
    backgroundElement.style.filter = 'alpha(opacity=' + opacity + ')';
    backgroundElement.style.opacity = opacity / 100;
    backgroundElement.style.visibility = 'visible';
}

var tempHtml;
var displayDiv;

function closeVideoInline() {
    document.getElementById(displayDiv).innerHTML = tempHtml;
}

var videoDone;

function showVideoInline(videoUrl, thumbnailUrl, videoTitle, sourcePath, displayIn) {
    displayDiv = displayIn;
    tempHtml = document.getElementById(displayIn).innerHTML;
    document.getElementById(displayIn).innerHTML = "<object data='data:application/x-silverlight,' type='application/x-silverlight-2' width='100%' height='100%'>" +
        "<param name='source' value='" + sourcePath + "' />" +
        "<param name='onerror' value='onSilverlightError' />" +
        "<param name='background' value='black' />" +
        "<param name='Windowless' value='False' />" +
        "<param name='minRuntimeVersion' value='3.0' />" +		  
        "<param name='initParams' value='autostart=true,cc=false,markers=false,m=" + videoUrl + ",thumbnail=" + thumbnailUrl + ",title=" + videoTitle + "' />" +
        "<a href='http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0' target='_blank'><img src='http://go2.microsoft.com/fwlink/?LinkID=108181' alt='Get Silverlight' border='0' /></a>" +
        "</object>" +
        "<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>";

    videoDone = closeVideoInline;
}


function showPopup(videoUrl, thumbnailUrl, videoTitle, sourcePath) {
    document.getElementById('videoPopupBase').style.height = "362px";
    document.getElementById('videoPopupBase').style.width = "555px";

    document.getElementById('silverlightControlHost').innerHTML = "<object data='data:application/x-silverlight,' type='application/x-silverlight-2' width='514' height='288'>" +
        "<param name='source' value='" + sourcePath + "' />" +
        "<param name='onerror' value='onSilverlightError' />" +
        "<param name='background' value='black' />" +
        "<param name='Windowless' value='False' />" +
        "<param name='initParams' value='autostart=true,cc=false,markers=false,m=" + videoUrl + ",thumbnail=" + thumbnailUrl + ",title=" + videoTitle + "' />" +
        "<a href='http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0' target='_blank'><img src='http://go2.microsoft.com/fwlink/?LinkID=108181' alt='Get Silverlight' border='0' /></a>" +
        "</object>" +
        "<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>";

    showBackground(document.getElementById('videoBackground'), document.body.scrollWidth, document.body.scrollHeight, 0, document.body.scrollTop, 50);
    try {
        document.getElementById('videoPopup').style.left = (document.body.clientWidth - document.getElementById('videoPopup').offsetWidth) / 2;
        document.getElementById('videoPopup').style.top = ((document.documentElement.clientHeight - document.getElementById('videoPopup').offsetHeight) / 2) + document.body.parentNode.scrollTop;
    } catch (err) { }
    try {
        document.getElementById('videoPopup').style.left = (window.innerWidth - document.getElementById('videoPopup').offsetWidth) / 2 + 'px';
        document.getElementById('videoPopup').style.top = ((window.innerHeight - document.getElementById('videoPopup').offsetHeight) / 2) + window.scrollY + 'px';
    } catch (err) { }
    document.getElementById('videoPopup').style.visibility = 'visible';

    videoDone = closePopup;
}

// the ...Multi functions are used to allow multiple controls on a single page
var nowPlayingID = "";
function showPopupMulti(parentID, videoUrl, thumbnailUrl, backgroundUrl, videoTitle, sourcePath) {
    var parent = GetControlID(parentID);
    var objectID = "#" + parent + "_videoPopup";
    var popup = $("#" + parent + "_videoPopup");
    var popupbase = $(objectID + " > .videoPopupBase");
    popupbase.height(362);
    popupbase.width(555);

    var background = '';
    if (backgroundUrl != '') background = ",image=" + backgroundUrl;

    nowPlayingID = parent;
    videoDone = closePopupMultiWrapper;
    $(objectID + " .silverlightControlHost").html(
        "<object data='data:application/x-silverlight,' type='application/x-silverlight-2' width='514' height='288'>" +
        "<param name='source' value='" + sourcePath + "' />" +
        "<param name='onerror' value='onSilverlightError' />" +
        "<param name='background' value='black' />" +
        "<param name='Windowless' value='False' />" +
        "<param name='minRuntimeVersion' value='3.0' />" +
        "<param name='AutoUpgrade' value='false' />" + 
        "<param name='initParams' value='autostart=true,cc=false,markers=false,m=" + videoUrl + ",thumbnail=" + thumbnailUrl + ",title=" + videoTitle + background + "' />" +
        "<a href='http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0' target='_blank'><img src='http://go2.microsoft.com/fwlink/?LinkID=108181' alt='Get Silverlight' border='0' /></a>" +
        "</object>" +
        "<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>"
        );

    showBackgroundMulti($("#" + parent + "_videoBackground"), document.body.scrollWidth, document.body.scrollHeight, 0, document.body.scrollTop, 50);

    try {
        popup.css("left", (document.body.clientWidth - popup.width()) / 2);
        popup.css("top", ((document.documentElement.clientHeight - popup.height()) / 2) + document.body.parentNode.scrollTop);
    } catch (err) { }
    try {
        popup.css("left", (window.innerWidth - popup.width()) / 2 + 'px');
        popup.css("top", ((window.innerHeight - popup.height()) / 2) + window.scrollY + 'px');
    } catch (err) { }

    popup.css("visibility", "visible");
}


function GetControlID(pID) {
    var obj = $("#" + pID);
    if (obj.length < 1) obj = $("." + pID);
    return obj.attr("id");
}


function showBackgroundMulti(backgroundElement, width, height, left, top, opacity) {
    backgroundElement.width(width);
    backgroundElement.height(height);

    backgroundElement.css("left", left);
    backgroundElement.css("top", top);
    backgroundElement.css("filter", "alpha(opacity=" + opacity + ")");
    backgroundElement.css("opacity", opacity / 100);
    backgroundElement.css("visibility", "visible");
    backgroundElement.css("display", "block");
    backgroundElement.css("position", "absolute");
}


function closePopupMultiWrapper() { closePopupMulti(nowPlayingID); }
function closePopupMulti(parentID) {
    var back = $("#" + parentID + "_videoBackground");

    back.css("visibility", "hidden"); ; // .css("visibility", "hidden"); // .hide();
    back.width(0);
    back.height(0);

    $("#" + parentID + "_videoPopup").css("visibility", "hidden");
    $("#" + parentID + "_videoPopup .silverlightControlHost").html("");
}
