/// <reference path="../ScriptReference/jquery-1.5.2.js" />

function onSilverlightError(sender, args) {
    
    var appSource = "";
    if (sender !== null && sender !== 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;
    
    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType === "ParserError") {
        errMsg += "File: " + 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";
    }

    try {
        jQuery('#player').get(0).content.PlayerScriptApi.ReportError(errorType, errMsg, iErrorCode);
    }
    catch (error) { }
}

function focusPlayer() {
    if (window == window.top) {
        jQuery('#player').get(0).focus();
    }
}

function loadScreenReader() {
    if (window == window.top) {
        $('body').focus();  // Avoid focus issues on reload

        // Set cookie for screen reader mode
        var cookie = new Mediasite.Cookie("MediasitePlayerOptions");
        cookie.SetValue(Mediasite.Player.Options.UseScreenReader, true);

        // Reload page (in screen reader mode)
        window.location.reload();
    }
}

function onPageClose() {
    try {
        var mediaView = jQuery('#player').get(0).content.PlayerScriptApi.GetLastMediaView();
        var endpoint = mediaView.ServiceEndpoint.replace(/\/sl$/i, "/json/ReportMediaView");
        var jsonData = JSON.stringify({ playbackTicket: mediaView.PlaybackTicket, startTime: mediaView.StartTime, duration: mediaView.Duration });

        jQuery.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: endpoint,
            async: false,
            data: jsonData
        });        
    }
    catch (error) { }    
}

function getPlayerApi(container) {
    var api = null;

    var silverlightObjectSelector = 'object[type*="silverlight"]';
    var $container = container ? $(container) : $(document);
    var $sl = $container.is(silverlightObjectSelector)
                    ? $container
                    : $container.find(silverlightObjectSelector);

    if ($sl.length) {
        var slContent = $sl[0].Content;
        var api = slContent.PlayerScriptApi;
    }

    return api;

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

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