(function () {

    var extensionName = 'Mediasite.Player.PollNotification';
    var timedEventType = 'mediasite/poll';

    var pollsWindowOptions = 'height=768, width=1024, scrollbars=yes, resizable=yes';

    var elementTemplate = '<div class="PollNotification">   \
                                <div class="PopupBackground"></div> \
                                <div class="PopupClose"></div>  \
                                <div class="PopupButton"></div> \
                            </div>  \
                            ';

    function getSelect(element) {
        var select = Mediasite.ElementSelector(element);

        select.addSelectorMethods({
            closeButton: ".PopupClose",
            actionButton: '.PopupButton',
            title: '.PopupButton'
        });

        return select;
    }


    var defaultDbData = {
        Message: Mediasite.Player.Localization.Buttons.ShowPolls
    };

    var logger = Mediasite.Logging.LoggerFactory(extensionName);

    function PollNotification(mediasiteAPI, dbExtension) {
        var self = this;

        var data = {};
        if (dbExtension.Data) {
            try {
                data = JSON.parse(dbExtension.Data);
            } catch (e) {
                logger.Warn("Could not parse config data");
            }
        }
        $.extend(data, defaultDbData, {
            pollsUri: mediasiteAPI.getPollsUri()
        });

        var notificationElement;
        var select;
        var notificationElementVisible = false;

        var liveStatus = mediasiteAPI.getLiveStatus();
        var isLive = (liveStatus != Mediasite.Player.PresentationStatus.OnDemand && liveStatus != Mediasite.Player.PresentationStatus.NotAvailable);

        if (isLive && data.pollsUri) {
            load();
        }



        function load() {
            loadCSS();
            addAPIEventHandlers();
        }

        function addAPIEventHandlers() {
            mediasiteAPI.addHandler('timedeventreached', function (eventData) {
                if (eventData.timedEventType == timedEventType) {
                    showUI(eventData.timedEventPayload);
                }
            });
        }
/*
        self.showNotification = function(notificationData) {
            showUI(notificationData);
        }
*/
        function showUI(payload) {
            createUI();

            if (notificationElementVisible) {
                select.element().fadeOut('slow', function () {
                    select.element().fadeIn('slow');
                });
            } else {
                select.element().fadeIn();
                notificationElementVisible = true;
            }
        }


        function hideUI() {
            select.element().fadeOut();
            notificationElementVisible = false;
        }


        function createUI() {
            if (notificationElement) return;

            notificationElement = $(elementTemplate).appendTo(document.body);
            select = getSelect(notificationElement);

            select.element().hide();

            select.title().attr('title', data.Message);

            select.closeButton().click(function (event) {
                event.stopPropagation();
                hideUI();
            });


            select.element().click(function () {
                hideUI();
                openPolls();
            });
        }

        function openPolls() {
            window.open(data.pollsUri, new Date(), pollsWindowOptions);
        }

        function loadCSS() {
            var stylesheet = dbExtension.ExtensionBasePath + "Extension.css";
            Mediasite.Css.LoadStyleSheets([stylesheet], addAPIEventHandlers);
        }

    }

    Mediasite.Player.RegisterExtension(extensionName,
    {
        Constructor: PollNotification
    });


})();