/// <reference path="../../ScriptReference/jquery-1.7.1-vsdoc.js" />
/// <reference path="../../Scripts/Mediasite.Core.js"/>
/// <reference path="../../Scripts/Mediasite.Player.Core.js"/>
/// <reference path="../../Scripts/Mediasite.Player.Localization.js"/>
/*global Mediasite, Silverlight*/

Mediasite.namespace("MediaPlayer.Views");
Mediasite.namespace("MediaPlayer.UserActivation");

(function (module) {
    var userActivationModules = [];
    var defaultUserActivationModule = {
        constructor: $.noop,
        usable: $.noop,
        mediaPlayer: false
    }

    var contentContainer = '#PlayerContent';

    var currentUserActivationModule;

    module.Register = function (userActivationModule) {
        userActivationModule = $.extend({}, defaultUserActivationModule, userActivationModule);
        userActivationModules.push(userActivationModule);
    };

    module.Create = function (mediaPlayerView, options) {
        var mediaContainer = $(mediaPlayerView.Wrapper);
        options = $.extend({}, options, { contentContainer: contentContainer });
        var constructor;

        for (var i = 0, length = userActivationModules.length; i < length; i++) {
            var userActivationModule = userActivationModules[i];
            if (options.mediaPlayerType == userActivationModule.mediaPlayerType
                && userActivationModule.usable()) {
                currentUserActivationModule = userActivationModule;
                break;
            }
        }

        if (currentUserActivationModule) {
            var constructor = userActivationModule.constructor;
            var instance = new constructor(mediaContainer, options);
            return instance;
        }
    };


    module.ContainerCss = {
        background: 'rgb(119, 119, 119)',
        display: 'block',
        position: 'fixed',
        top: '0',
        left: '0',
        right: '0',
        bottom: '0',
        zIndex: 2147483600
    };

    // Keys must match DOMElement.style keys
    module.MediaElementCss = {
        position: 'absolute',
        zIndex: 2147483601,
        top: '0',
        right: '0',
        left: '0',
        bottom: '0',
        width: '100%',
        height: '100%',
        cursor: 'pointer'
    };

    module.MessageCss = {
        zIndex: 2147483602,
        pointerEvents: 'none',
        display: 'block',
        position: 'absolute',
        top: '0',
        left: '0',
        right: '0',
        padding: '1em',
        background: 'rgba(100,100,100,0.2)',
        textAlign: 'center',
        fontSize: '30px',
        color: '#ddd'
    };

    var showMessageTimeout;
    module.ShowPrompt = function (messageCss, onShowMessage) {
        messageCss = $.extend({}, module.MessageCss, messageCss);
        clearTimeout(showMessageTimeout);
        showMessageTimeout = setTimeout(module.ShowMessage.bind(module, messageCss, onShowMessage), 1000);
    }

    module.RemovePrompt = function () {
        showMessageTimeout = null;
        if (messageContainer) {
            messageContainer.remove();
        }
    };

    var messageContainer;
    module.ShowMessage = function (messageCss, onShowMessage) {
        if (messageContainer) { return; }

        messageContainer = $("<div class='clickToActivate'>");
        messageContainer.appendTo(contentContainer);
        messageContainer.css(module.ContainerCss);

        var message = $("<div />").appendTo(messageContainer);
        message.text(Mediasite.Player.Localization.MediaPlayer.ClickToActivatePlugin);
        message.css(messageCss);

        if (onShowMessage) {
            onShowMessage(messageContainer);
        }
    };

})(Mediasite.MediaPlayer.UserActivation);

