
var SilverlightObjectId = 'PlayerXaml';

function GetStandAloneApi() {
    var silverlightObject = document.getElementById(SilverlightObjectId);
    return silverlightObject.content.StandAloneScriptApi;
}

function LoadMyManifest() {
    var manifestXml = unescape(StandAloneManifest);
    GetStandAloneApi().LoadManifest(manifestXml);
}

function LoadMyPlayerOptions() {
    var optionsXml = unescape(StandAlonePlayerOptions);
    GetStandAloneApi().LoadOptions(optionsXml);
}

function onError(sender, args) {
    if (Mediasite.Urls.ContainsUnicode(document.location.href) && !Mediasite.BrowserDetect.InternetExplorer()) {
        $('body').empty().text(Mediasite.Player.Localization.MediaPlayer.SilverlightLoadErrorNonAsciiPath);
        return;
    }


    var silverlightObject = document.getElementById(SilverlightObjectId);
    if (silverlightObject.content.PlayerScriptApi) {
        silverlightObject.content.PlayerScriptApi.ReportError(args.ErrorType, args.ErrorMessage, args.ErrorCode);
    }
}

function createPlayerObjectIn(containerId) {
    Silverlight.createObjectEx({
        source: 'Layout.xap',
        parentElement: document.getElementById(containerId),
        id: SilverlightObjectId,
        properties: {
           width: "100%",
           height: "100%",
           minRuntimeVersion: "5.0.61118.0",
           windowless: "False"
        }, 
        events: {
            onError: 'onError',
            onLoad: 'focusPlayer'
        },
        initParams: 'autoplay=True,showCover=False,IsStandAlone=True,fileServer=stand-alone,identityTicket=stand-alone'
    });
}

function focusPlayer() {
    if (window === window.top) {
        var silverlightObject = document.getElementById(SilverlightObjectId);
        if (silverlightObject) {
            silverlightObject.focus();
        }
    }
}
function bindEvent(element, events, handler) {
    /// <summary>Attach event listeners to a DOM element</summary>
    /// <param name="element" type="DOMElement">DOM element to attach events to</param>
    /// <param name="events" type="string">List of events to bind to, space-delimited</param>
    /// <param name="handler" type="function">Function to handle events</param>

    if (element && typeof (events) == "string" && handler) {
        var eventList = events.split(" ");

        for (var el = 0, len = eventList.length; el < len; el++) {
            var event = eventList[el];
            if (element.addEventListener) {
                element.addEventListener(event, handler);
            } else if (element.attachEvent) {
                element.attachEvent("on" + event.toLowerCase(), handler);
            }
        }
    }
}

function getPlayerApi() {
    var silverlightObject = document.getElementById(SilverlightObjectId);
    return silverlightObject.Content.PlayerScriptApi;
}

function onMouseEnter(evt) {
    var api = getPlayerApi(evt.target);
    if (api) api.OnMouseEnter();
}

function onMouseLeave(evt) {
    var api = getPlayerApi(evt.target);
    if (api) api.OnMouseLeave();
}

function loadScreenReader() {
    window.location.href = 'ScreenReader.html'
}
