﻿var ShowPopup = false; var PlayerClientID = "playerhost";
function ajaxFunction(data, url) {
    var xmlHttp;
    try { xmlHttp = new XMLHttpRequest(); } catch (e) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; } } }
    xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader('Content-Type', 'text/xml'); xmlHttp.setRequestHeader('Content-Length', data.length + '');
    xmlHttp.send(data);
}

/////// Silver Light //////////////////////////////////////
if (!window.Silverlight) { window.Silverlight = {}; }

Silverlight._silverlightCount = 0;
Silverlight.fwlinkRoot = 'http://go2.microsoft.com/fwlink/?LinkID=';
Silverlight.onGetSilverlight = null;
Silverlight.onSilverlightInstalled = function() { window.location.reload(false); };

// isInstalled, checks to see if the correct version is installed
//////////////////////////////////////////////////////////////////
Silverlight.isInstalled = function(version) {
    var isVersionSupported = false; var container = null;
    try {
        var control = null;
        try { control = new ActiveXObject('AgControl.AgControl'); if (version == null) { isVersionSupported = true; } else if (control.IsVersionSupported(version)) { isVersionSupported = true; } control = null; }
        catch (e) {
            var plugin = navigator.plugins["Silverlight Plug-In"];
            if (plugin) {
                if (version === null) { isVersionSupported = true; }
                else {
                    var actualVer = plugin.description; if (actualVer === "1.0.30226.2") { actualVer = "2.0.30226.2"; }
                    var actualVerArray = actualVer.split("."); while (actualVerArray.length > 3) { actualVerArray.pop(); } while (actualVerArray.length < 4) { actualVerArray.push(0); }
                    var reqVerArray = version.split("."); while (reqVerArray.length > 4) { reqVerArray.pop(); }
                    var requiredVersionPart; var actualVersionPart; var index = 0; do { requiredVersionPart = parseInt(reqVerArray[index]); actualVersionPart = parseInt(actualVerArray[index]); index++; } while (index < reqVerArray.length && requiredVersionPart === actualVersionPart);
                    if (requiredVersionPart <= actualVersionPart && !isNaN(requiredVersionPart)) { isVersionSupported = true; }
                }
            }
        }
    }
    catch (e) { isVersionSupported = false; }
    if (container) { document.body.removeChild(container); }
    return isVersionSupported;
}
Silverlight.WaitForInstallCompletion = function() { if (!Silverlight.isBrowserRestartRequired && Silverlight.onSilverlightInstalled) { try { navigator.plugins.refresh(); } catch (e) { } if (Silverlight.isInstalled(null)) { Silverlight.onSilverlightInstalled(); } else { setTimeout(Silverlight.WaitForInstallCompletion, 3000); } } }
Silverlight.__startup = function() {
    Silverlight.isBrowserRestartRequired = Silverlight.isInstalled(null); //(!window.ActiveXObject || Silverlight.isInstalled(null));
    if (!Silverlight.isBrowserRestartRequired) { Silverlight.WaitForInstallCompletion(); }
    if (window.removeEventListener) { window.removeEventListener('load', Silverlight.__startup, false); } else { window.detachEvent('onload', Silverlight.__startup); }
}
if (window.addEventListener) { window.addEventListener('load', Silverlight.__startup, false); } else { window.attachEvent('onload', Silverlight.__startup); }

///////////////////////////////////////////////////////////////////////////////
// createObject();  Params:
// parentElement of type Element, the parent element of the Silverlight Control
// source of type String
// id of type string
// properties of type String, object literal notation { name:value, name:value, name:value},
//     current properties are: width, height, background, framerate, isWindowless, enableHtmlAccess, inplaceInstallPrompt:  all are of type string
// events of type String, object literal notation { name:value, name:value, name:value},
//     current events are onLoad onError, both are type string
// initParams of type Object or object literal notation { name:value, name:value, name:value}
// userContext of type Object
/////////////////////////////////////////////////////////////////////////////////
Silverlight.createObject = function(source, parentElement, id, properties, events, initParams, userContext) {
    var slPluginHelper = new Object(); var slProperties = properties; var slEvents = events;
    slPluginHelper.version = slProperties.version; slProperties.source = source; slPluginHelper.alt = slProperties.alt;
    if (initParams) slProperties.initParams = initParams;
    if (slProperties.isWindowless && !slProperties.windowless) slProperties.windowless = slProperties.isWindowless;
    if (slProperties.framerate && !slProperties.maxFramerate) slProperties.maxFramerate = slProperties.framerate;
    if (id && !slProperties.id) { slProperties.id = id; }
    delete slProperties.ignoreBrowserVer; delete slProperties.inplaceInstallPrompt;
    delete slProperties.version; delete slProperties.isWindowless;
    delete slProperties.framerate; delete slProperties.data;
    delete slProperties.src; delete slProperties.alt;
    if (Silverlight.isInstalled(slPluginHelper.version)) {
        for (var name in slEvents) {
            if (slEvents[name]) {
                if (name == "onLoad" && typeof slEvents[name] == "function" && slEvents[name].length != 1) {
                    var onLoadHandler = slEvents[name];
                    slEvents[name] = function(sender) { return onLoadHandler(document.getElementById(id), userContext, sender) };
                }
                var handlerName = Silverlight.__getHandlerName(slEvents[name]);
                if (handlerName != null) { slProperties[name] = handlerName; slEvents[name] = null; }
                else { throw "typeof events." + name + " must be 'function' or 'string'"; }
            }
        }
        slPluginHTML = Silverlight.buildHTML(slProperties);
    }
    else { slPluginHTML = Silverlight.buildPromptHTML(slPluginHelper); }
    if (parentElement) { parentElement.innerHTML = slPluginHTML; } else { return slPluginHTML; }
}

///////////////////////////////////////////////////////////////////////////////
//  create HTML that instantiates the control
///////////////////////////////////////////////////////////////////////////////
Silverlight.buildHTML = function(slProperties) {
    var htmlBuilder = [];
    htmlBuilder.push('<object type=\"application/x-silverlight\" data="data:application/x-silverlight,"');
    if (slProperties.id != null) { htmlBuilder.push(' id="' + slProperties.id + '"'); }
    if (slProperties.width != null) { htmlBuilder.push(' width="' + slProperties.width + '"'); }
    if (slProperties.height != null) { htmlBuilder.push(' height="' + slProperties.height + '"'); }
    htmlBuilder.push(' >');
    delete slProperties.id; delete slProperties.width; delete slProperties.height;
    for (var name in slProperties) { if (slProperties[name]) { htmlBuilder.push('<param name="' + Silverlight.HtmlAttributeEncode(name) + '" value="' + slProperties[name] + '" />'); } }
    htmlBuilder.push('<\/object>');
    return htmlBuilder.join('');
}
// createObjectEx, takes a single parameter of all createObject parameters enclosed in {}
Silverlight.createObjectEx = function(params) { var parameters = params; var html = Silverlight.createObject(parameters.source, parameters.parentElement, parameters.id, parameters.properties, parameters.events, parameters.initParams, parameters.context); if (parameters.parentElement == null) { return html; } }

///////////////////////////////////////////////////////////////////////////////////////////////
// Builds the HTML to prompt the user to download and install Silverlight
///////////////////////////////////////////////////////////////////////////////////////////////
Silverlight.buildPromptHTML = function(slPluginHelper) {
    var slPluginHTML = ""; var urlRoot = Silverlight.fwlinkRoot; var shortVer = slPluginHelper.version;
    if (slPluginHelper.alt) { slPluginHTML = slPluginHelper.alt; }
    else {
        if (!shortVer) { shortVer = ""; }
        slPluginHTML = "<a href='javascript:Silverlight.getSilverlight(\"{1}\");' style='text-decoration: none;'><img src='{2}' alt='Get Microsoft Silverlight' style='border-style: none'/></a>";
        slPluginHTML = slPluginHTML.replace('{1}', shortVer); slPluginHTML = slPluginHTML.replace('{2}', urlRoot + '108181');
    }
    return slPluginHTML;
}
Silverlight.getSilverlight = function(version) {
    if (Silverlight.onGetSilverlight) { Silverlight.onGetSilverlight(); }
    var shortVer = ""; var reqVerArray = String(version).split("."); var verArg = "";
    if (reqVerArray.length > 1) { var majorNum = parseInt(reqVerArray[0]); if (isNaN(majorNum) || majorNum < 2) { shortVer = "1.0"; } else { shortVer = reqVerArray[0] + '.' + reqVerArray[1]; } }
    if (shortVer.match(/^\d+\056\d+$/)) { verArg = "&v=" + shortVer; }
    Silverlight.followFWLink("114576" + verArg);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// Navigates to a url based on fwlinkid
///////////////////////////////////////////////////////////////////////////////////////////////
Silverlight.followFWLink = function(linkid) { top.location = Silverlight.fwlinkRoot + String(linkid); }

///////////////////////////////////////////////////////////////////////////////////////////////
/// Encodes special characters in input strings as charcodes
///////////////////////////////////////////////////////////////////////////////////////////////
Silverlight.HtmlAttributeEncode = function(strInput) {
    var c; var retVal = '';
    if (strInput == null) { return null; }
    for (var cnt = 0; cnt < strInput.length; cnt++) { c = strInput.charCodeAt(cnt); if (((c > 96) && (c < 123)) || ((c > 64) && (c < 91)) || ((c > 43) && (c < 58) && (c != 47)) || (c == 95)) { retVal = retVal + String.fromCharCode(c); } else { retVal = retVal + '&#' + c + ';'; } }
    return retVal;
}
///////////////////////////////////////////////////////////////////////////////
//  Default error handling function to be used when a custom error handler is
//  not present
///////////////////////////////////////////////////////////////////////////////
Silverlight.default_error_handler = function(sender, args) {
    var iErrorCode; var errorType = args.ErrorType; iErrorCode = args.ErrorCode;
    var errMsg = "\nSilverlight error message     \n"; errMsg += "ErrorCode: " + iErrorCode + "\n"; errMsg += "ErrorType: " + errorType + "       \n"; errMsg += "Message: " + args.ErrorMessage + "     \n";
    if (errorType == "ParserError") { errMsg += "XamlFile: " + args.xamlFile + "     \n"; errMsg += "Line: " + args.lineNumber + "     \n"; errMsg += "Position: " + args.charPosition + "     \n"; }
    else if (errorType == "RuntimeError") { if (args.lineNumber != 0) { errMsg += "Line: " + args.lineNumber + "     \n"; errMsg += "Position: " + args.charPosition + "     \n"; } errMsg += "MethodName: " + args.methodName + "     \n"; }
    alert(errMsg);
}

///////////////////////////////////////////////////////////////////////////////////////////////
/// Releases event handler resources when the page is unloaded
///////////////////////////////////////////////////////////////////////////////////////////////
Silverlight.__cleanup = function() {
    for (var i = Silverlight._silverlightCount - 1; i >= 0; i--) { window['__slEvent' + i] = null; }
    Silverlight._silverlightCount = 0;
    if (window.removeEventListener) { window.removeEventListener('unload', Silverlight.__cleanup, false); } else { window.detachEvent('onunload', Silverlight.__cleanup); }
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// Releases event handler resources when the page is unloaded
///////////////////////////////////////////////////////////////////////////////////////////////
Silverlight.__getHandlerName = function(handler) {
    var handlerName = "";
    if (typeof handler == "string") { handlerName = handler; }
    else if (typeof handler == "function") {
        if (Silverlight._silverlightCount == 0) { if (window.addEventListener) { window.addEventListener('onunload', Silverlight.__cleanup, false); } else { window.attachEvent('onunload', Silverlight.__cleanup); } }
        var count = Silverlight._silverlightCount++; handlerName = "__slEvent" + count; window[handlerName] = handler;
    }
    else { handlerName = null; }
    return handlerName;
}
////////////////////////////////////// Embed Code Do not Remove  ////////////////////////////////////////////////
//function EmbedPlayer(surl, burl, mid , ) {
//    Silverlight.createObjectEx({ source: burl,
//        parentElement: document.getElementById('mashupstudio'),
//        id: "ctl00_MainContentPlaceHolder_playerhost",
//        properties: {
//            width: window.screen.availWidth,
//            height: "600",
//            inplaceInstallPrompt: true,
//            version: "3.0.40624.0",
//            enableGPUAcceleration: "true",
//            isWindowless: "false",
//            enableHtmlAccess: "true",
//            autoUpgrade: "true"
//        },
//        initParams: "WebServiceBasePathUrl=" + surl + ",Embed=#$####,######=" + mid + "",
//        events: {}
//    });
//}

function EmbedPlayerNew(surl, burl, mid, width, height, isLMS) {
    Silverlight.createObjectEx({ source: burl,
        parentElement: document.getElementById('mashupstudio'),
        id: PlayerClientID,
        properties: {
            width: width > 0 ? width : 1024,
            height: height > 0 ? height : 500,
            inplaceInstallPrompt: true,
            version: "3.0.40624.0",
            isWindowless: "false",
            enableGPUAcceleration: "true",
            enableHtmlAccess: "true",
            autoUpgrade: "true"
        },
        initParams: "WebServiceBasePathUrl=" + surl + "," + (isLMS != undefined ? "LMS" : "Embed") + "=#$####,######=" + mid + "",
        events: {}
    });
}

//function EmbedPlayerNew(surl, burl, mid, width, height , isLMS) {
//    Silverlight.createObjectEx({ source: burl,
//        parentElement: document.getElementById('mashupstudio'),
//        id: "ctl00_MainContentPlaceHolder_playerhost",
//        properties: {
//            width: width > 0 ? width : 1024,
//            height: height > 0 ? height : 500,
//            inplaceInstallPrompt: true,
//            version: "3.0.40624.0",
//            isWindowless: "false",
//            enableGPUAcceleration: "true",
//            enableHtmlAccess: "true",
//            autoUpgrade: "true"
//        },
//        initParams: "WebServiceBasePathUrl=" + surl + ",LMS=#$####,######=" + mid + "",
//        events: {}
//    });
//}

/////////////////////////////////////////////  End //////////////////////////////////////////////
function PlayerUnloadFromSilverLight(url) { var silverlight = document.getElementById(PlayerClientID); if (silverlight) { var strmsg = silverlight.Content.PlayerControl.PlayerUnload(url); } else { window.location = url; } }
function TrackingNavigation() {
    var hyperLinks = document.getElementsByTagName("a"); for (var i = 0; i < hyperLinks.length; i++) {
        try {
            if (hyperLinks[i].getAttribute('target') != "_blank") {
                hyperLinks[i].onclick = function() {
                    PlayerUnloadFromSilverLight(this.href); return false;
                }
            }
        } catch (ex) { }
    }
}
function StopPopup() { ShowPopup = false; }
function StartPopup() { ShowPopup = true; }
window.onbeforeunload = function(e) { try { e = e || window.event; if (ShowPopup) { var silverlight = document.getElementById(PlayerClientID); if (silverlight) { silverlight.Content.PlayerControl.PageBeforeUnload(e); } !alert("You have requested to navigate away from this page. \r\n Current state of your activity has been saved in our log. Thank you") } } catch (ex) { /*alert(ex);*/ } }

function CreateActiveX() {
    var script = "<object id='sdk' type='application/cab' codebase='Admin/VB6000SDK.cab#Version=3,3,0,1'";
    script += " height='1' width='1' classid='clsid:DCC1943B-CBFF-43E2-8E5A-67545F7ACF15'></object>";

    document.getElementById('createActiveXForClientSide').innerHTML = script;
}


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    //return navigator.userAgent;
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
function checkVersion() {
    var msg = "You're not using Internet Explorer.";
    var ver = getInternetExplorerVersion();

    //    if (ver > -1) {
    //        if (ver >= 8.0)
    //            msg = "You're using a recent copy of Internet Explorer."
    //        else
    //            msg = "You should upgrade your copy of Internet Explorer.";
    //    }
    //alert(ver);
}

if (typeof (UltraLearn) == 'undefined')
    UltraLearn = {};

var userAgent = navigator.userAgent.toLowerCase();  // Figure out what browser is being used
UltraLearn.Browser = {
    Version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
    Chrome: /chrome/.test(userAgent),
    Safari: /webkit/.test(userAgent),
    Opera: /opera/.test(userAgent),
    IE: /msie/.test(userAgent) && !/opera/.test(userAgent),
    Mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
    Check: function() { alert(userAgent); }
};

function PlayerHostManager() { }
PlayerHostManager.HostId = "";
PlayerHostManager.Width = 0;
PlayerHostManager.Height = 0;
PlayerHostManager.Counter = 1;
PlayerHostManager.isFirstTime = true;
PlayerHostManager.ViewportWidth = 0;
PlayerHostManager.ViewportHeight = 0;


PlayerHostManager.SetHost = function() {
    //alert("SetHost");
    var viewportWidth;
    var viewportHeight;
    var padVertical = 0;
    var padHorizontal = 0;
    var setViewPortPercentage = 100;
    try {
        //do the same for Chrome and Mozilla
        if (UltraLearn.Browser.Chrome || UltraLearn.Browser.Mozilla) {
            padHorizontal = 17;   //#### Browser ScrollBar ####//
            padVertical = 2;
            viewportWidth = window.innerWidth - padHorizontal;
            viewportHeight = window.innerHeight - padVertical;
        }
        else if (UltraLearn.Browser.IE) {
            var ver = getInternetExplorerVersion();
            padHorizontal = 22; //#### Browser ScrollBar:: 22px in IE ####//

            if (ver > 7) {
                viewportWidth = document.documentElement.offsetWidth - padHorizontal;
                viewportHeight = document.documentElement.clientHeight - padVertical;
            }
            else {
                viewportWidth = screen.width - 22;
                viewportHeight = document.documentElement.offsetHeight - padVertical;
            }
        }
        else {
            padHorizontal = 22;   //#### Browser ScrollBar ####//
            padVertical = 2;
            viewportWidth = window.innerWidth - padHorizontal;
            viewportHeight = window.innerHeight - padVertical;
        }
    } catch (ex) {
        //alert(ex.message);
    }
    //setViewPortPercentage = viewportHeight * setViewPortPercentage / 100;

    //debug("PlayerHostManager.ViewportHeight: " + PlayerHostManager.ViewportHeight);
    //if (PlayerHostManager.ViewportHeight != viewportHeight) {
    debug("PlayerHostManager.ViewportHeight: " + PlayerHostManager.ViewportHeight);
    debug("viewportHeight: " + viewportHeight);
    PlayerHostManager.ViewportHeight = viewportHeight;
    PlayerHostManager.setHostSize(viewportWidth, viewportHeight);
    //}

    //PlayerHostManager.ViewportWidth = viewportWidth;
}


PlayerHostManager.ResizePresenter = function() {
    debug("function called: ResizePresenter");

    try {
        var eleId = PlayerHostManager.HostId;
        var Host = document.getElementById(eleId);

        if (!Host) {
            return;
        }

        var presenterHeight = Host.Content.HostControl.GetPresenterHeight();
        Host.style.height = parseInt(presenterHeight) + "px";
        //alert('test');
    }
    catch (e) {
        debug("Exception in ResizePresenter: " + e.message);
    }
}

PlayerHostManager.ResizePlayerContent = function(Width, Height) {
    debug("function called: ResizePlayerContent");

    var eleId = PlayerHostManager.HostId;
    var Host = document.getElementById(eleId);
    if (!Host) {
        debug("Host is null --- ResizePlayerContent");
        return;
    }

    Host.Content.HostControl.ResizePlayer(Width, Height);
    //setTimeout(PlayerHostManager.ResizePresenter, 1000);
    PlayerHostManager.Counter = PlayerHostManager.Counter + 1;
}



PlayerHostManager.setHostSize = function(Width, Height) {
    debug("function called: setHostSize");
    var eleId = PlayerHostManager.HostId;
    var Host = document.getElementById(eleId);
    //alert(Host);
    if (!Host) {
        return;
    }
    //alert(Height);
    //alert(parseInt(Height));
    var headerHeight = findPos(Host)[1];
    var newHeight = Height - headerHeight;
    //alert(parseInt(Width));


    Host.style.width = parseInt(Width) + "px";
    Host.style.height = parseInt(newHeight) + "px";

    debug("width: " + Width);
    debug("height: " + newHeight);

    try {
        //document.getElementById("Divdifference").innerHTML += "<br> Host Width: " + Host.style.width;
        //alert(" Host.style.height: " + Host.style.height + "\nHost.style.width: " + Host.style.width);
        PlayerHostManager.ResizePlayerContent(Width, newHeight);
    }
    catch (e) {
        //alert("Error in: Host.Content.HostControl.ResizePlayer() --- " + e.message);
        setTimeout(function() { PlayerHostManager.setHostSize(Width, Height) }, 1000);
        debug("Exception in setHostSize: " + e.message);
    }
}

PlayerHostManager.AdjustSilverlightHost = function(hostId) {
    PlayerHostManager.HostId = hostId;
    PlayerHostManager.SetHost();
}
function findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); return [curleft, curtop]; } }
function debug(text) {
    if (window.console) {
        if (window.console.log) {
            window.console.log(text);
        }
    }
}