﻿ErrorDialog = function (title, message, options) {
	this.init(title, message, options);
}

ErrorDialog.prototype.Bind = function (fnMethod) {
	var objSelf = this;
	return (
		function () {
			return (fnMethod.apply(objSelf, arguments));
		}
	);
}

jQuery.extend(ErrorDialog.prototype, {
    options: { show: true, onDialogClose: function () { } },
    htmlObject: null,
    texts: { title: '', message: '' },
    overlayApi: null,

    init: function (title, message, options) {
        this.options = jQuery.extend(this.options, options);
        this.texts.title = title;
        this.texts.message = message;

        this.generateHtmlObject();
        this.createOverlayApi();

        if (this.options.show) {
            this.show();
        }
    },

    generateHtmlObject: function () {
        if (this.texts.title == '') {
            this.texts.title = GetTranslation('42fc94bc-98a8-417a-aba6-8e5f41d97d80');
        }
        if (this.texts.message == '') {
            this.texts.message = GetTranslation('c97532ca-0238-4ee9-9ea1-8acd59128866');
        }
        var closeButton = jQuery('<a href="javascript:void(0);" class="button-green" id="closeButton">' + GetTranslation('b30ab296-b395-4bed-97ca-6a5aff6ae31f') + '</a>');
        closeButton.click(this.Bind(this.close));
        var html = '<div class="box-overlay" style="width:400px;display:none;">';
        html += '<h2>' + this.texts.title + '</h2>';
        html += '<div class="m10">' + this.texts.message + '</div>';
        html += '<div id="closeContainer"></div>';
        html += '</div>';

        this.htmlObject = jQuery(html);
        jQuery(document.body).append(this.htmlObject);
        jQuery('#closeContainer').append(closeButton);
    },

    createOverlayApi: function () {
        this.overlayApi = this.htmlObject.overlay({ closeOnClick: false, fixed: false, api: true, onClose: this.Bind(this.onClose) });
    },

    show: function () {
        this.overlayApi.load();
    },

    onClose: function () {
        this.options.onDialogClose();
        this.cleanUp();
    },

    close: function () {
        this.overlayApi.close();
    },

    cleanUp: function () {
        this.htmlObject.remove();
    }
});
