/// <reference path="3rdParty/jquery.js"/>

if (!Mediasite) {
    var Mediasite = {};
}
Mediasite.Player = Mediasite.Player || {};

Mediasite.Player.WaitingOverlay = function (options) {

    var overlay;
    var message;
    var timeToStart;
    var status;
    var countDownInterval;

    init();


    function init() {

        $('.WaitingOverlay').remove();

        if (options.Destroy) {
            return;
        }

        overlay = $('<div class="WaitingOverlay"><div class="WaitingOverlayMessage"></div></div>');
        message = overlay.find('.WaitingOverlayMessage');
        //message.text("Waiting for presentation to begin...");

        loadPresentationInfo();

        overlay.appendTo(options.Content);

        repositionMessage();
        overlay.fadeIn();


        $(window).resize(repositionMessage);
    }

    function loadPresentationInfo() {

        var screentitle = $('<div class="WaitingOverlayTitle"></div>');
        screentitle.text(options.LocalizedStrings.NotStarted);
        screentitle.appendTo(message);

        var presenationInfo = $('<div class="WaitingOverlayPresentationInfo"></div>');
        presenationInfo.appendTo(message);

        var title = $('<div class="WaitingOverlayPresentationTitle"></div>');
        title.text(options.Presentation.Title);
        title.appendTo(presenationInfo);

        var time = $('<div class="WaitingOverlayPresentationTime"></div>');
        time.html(options.LocalizedStrings.ScheduledDate + options.Presentation.Date + " - " + options.Presentation.Time);
        time.appendTo(presenationInfo);

        var description = $('<div class="WaitingOverlayPresentationDescription"></div>');
        description.text(options.Presentation.Description);
        description.appendTo(presenationInfo);



        timeToStart = $('<div class="WaitingOverlayPresentationTimeToStart"></div>');
        timeToStart.appendTo(presenationInfo);

        countDownInterval = setInterval(function () {
            var timeTillStart = Mediasite.Player.TimeHelper.getDisplayFromDuration(new Date(options.Presentation.UnixTime) - new Date());

            if (timeTillStart && timeTillStart.length > 0) {
                timeToStart.text(options.LocalizedStrings.StartingIn + timeTillStart);
                status.text(options.LocalizedStrings.Status + Mediasite.Player.MediaState.GetDisplayName(options.Presentation.MediaState));
            }

        }, 1000);



        status = $('<div class="WaitingOverlayPresentationMediaStatus"></div>');
        status.appendTo(presenationInfo);

    }

    function repositionMessage() {

        var windowHeight = $(window).height();
        var windowWidth = $(window).width();
        var messageHeight = message.outerHeight(true);
        var messageWidth = message.outerWidth(true);

        var top = (($(window).height() - message.outerHeight(true)) / 2) + 'px';
        var left = (($(window).width() - message.outerWidth(true)) / 2) + 'px';
        message.css({ top: top, left: left });
    }



    this.Destroy = function () {
        overlay.remove();
        countDownInterval.clearInterval();
    };


};