$.widget("ui.messagedisplay", {

    options: {
        title: "",
        message: "",
        iserror: true,
        draggable: false,
        modal: true,
        dialogClass: "MessageDisplay",
        allowClose: true,
        height: 'auto',
        width: 'auto',
        appendTo: 'body',
        minHeight: '0px',
        position: { my: "center", at: "center", of: window }
    },

    _resizeTriggered: false,

    _create: function () {
        var self = this;

        self.element.addClass('messagedisplayinit');
        this._msgDialogInterval = 0;
    },

    _init: function () {

        try {
            this.element.dialog('destroy');
        } catch (e) {

        }

        this._buildHtml();
    },

    _buildHtml: function () {

        var self = this;
        this.element.empty();

        var buttons = [
            {
                text: Mediasite.Player.Localization.Common.OK,
                click: $.proxy(self.close, self)
            }];

        if (!this.options.buttons) {
            this.options.buttons = buttons;
        }

        var calcedWidth = this._calcWidth();
        this.element.html(this.options.message);
        this.element.dialog(
        {
            title: this.options.title,
            close: $.proxy(self.close, self),
            modal: this.options.modal,
            draggable: this.options.draggable,
            dialogClass: this.options.dialogClass,
            closeOnEscape: this.options.allowClose,
            height: this.options.height,
            width: calcedWidth,
            position: this.options.position,
            buttons: this.options.buttons,
            appendTo: this.options.appendTo,
            minHeight: this.options.minHeight
        });

        if (!this.options.allowClose) {
            var closeButton = $('.' + this.options.dialogClass).find('.ui-dialog-titlebar-close');
            closeButton.hide();
        }

        if (this.options.position) {
            $(window).bind('resize', $.proxy(self._repositionDialog, self));
        }

        //destroy the widget if it becomes hidden
        this._msgDialogInterval = setInterval(function () {
            if (!self.element.is(':visible')) {
                self.close();
            }
        }, 500);
    },

    close: function () {
        this.destroy();

        if ($.isFunction(this.options.close)) {
            this.options.close();
        }
    },

    destroy: function () {
        // call the base destroy function
        clearInterval(this._msgDialogInterval);
        $(window).unbind('resize', this._repositionDialog);
        this.element.dialog('destroy');
        this.element.empty();
        this.element.removeClass('messagedisplayinit');

        $.Widget.prototype.destroy.call(this);
    },


    _resizeThrottle: function (event) {
        clearTimeout(this._resizeTimer);
        this._resizeTimer = setTimeout($.proxy(this._repositionDialog, this), 300);
    },

    _resizeDialog: function () {
        this._resizeTriggered = true;
        $(window).trigger('resize');
    },

    _repositionDialog: function (event) {
        if (this.element.is(':visible')) {
            this.element.dialog('option', "width", this._calcWidth());
            this.element.dialog('option', "position", this.options.position);
        }

        if (!this._resizeTriggered) {
            setTimeout($.proxy(this._resizeDialog, this), 100);
            setTimeout($.proxy(this._resizeDialog, this), 200);
        } else {
            this._resizeTriggered = false;
        }
    },

    _calcWidth: function () {
        var width = this.options.width;
        var parentWidth = $(document).outerWidth(true) - 60;

        if (parentWidth <= this.options.width) {

            width = parentWidth - 60;
        }

        return width;
    }
});