/// class ProspectDirector.Web.UI.MessageDialog
/// using ProspectDirector.Web.UI.Dialog

/**
@class MessageDialog
@constructor
@namespace Web.UI
@extends Web.UI.Dialog
**/
ProspectDirector.Web.UI.MessageDialog = Class.extend({

    //================================
    // FIELDS
    //================================

    _options: null,
    _windowOptions: {
        animation: false,
        modal: true,
        title: "Confirmation",
        content: null,
        minHeight: 50,
        visible: false,
        resizable: false
    },
    _id: null,
    _icon: null,
    _window: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {
        
        this._options = $.extend({
            title: "Confirmation",
            message: "",
            icon: null,
            buttons: {
                ok: "OK"
            }
        }, options);

        this._id = uuid.v4();

        this._create();
    },

    //================================
    // PROPERTIES
    //================================

    get_options: function () {
        return this._options;
    },

    get_windowOptions: function () {
        return this._windowOptions;
    },

    get_window: function () {
        return this._window;
    },

    get_windowDomNode: function () {
        return this.get_window().element;
    },

    //================================
    // EVENT HANDLERS
    //================================

    //================================
    // PUBLIC METHODS
    //================================

    hide: function (options) {
        this.get_window().destroy(options);
    },

    //================================
    // PRIVATE METHODS
    //================================

    _create: function () {

        this._window = $("<div>" + this._render() + "</div>").appendTo("BODY DIV[data-role=windows]").kendoWindow(this.get_windowOptions()).data("kendoWindow");

        this._attachButtonEvents();
        this.get_windowDomNode().parent().attr("ID", this._options.id);
        this.get_window().center();
        this.get_window().open();
        this.get_window().title(this._options.title);
        this._setFocus();

    },

    _render: function () {

        var button = null,
            content = $("<div class=\"-pd-confirmation-dialog\"><table><tbody></tbody></table></div>"),
            contentBody = content.find("TBODY");

        contentBody.append(this._generateContentRow());

        if (this.get_options().buttons) {
            contentBody.append(this._generateButtonRow());
        }

        return content.wrap("<div></div>").parent().html();

    },

    _generateContentRow: function () {

        var result = $("<tr></tr>");

        if (this.get_options().icon) {
            result.append("<td class=\"icon\"><img src=\"" + this._getIconUrl() + "\"></td>");
        }

        result.append("<td class=\"text\">" + this.get_options().message + "</td>");

        return result;

    },

    _generateButtonRow: function () {

        var that = this,
            id = null,
            buttons = this.get_options().buttons,
            result = $("<tr><td class=\"buttons\"></td></tr>");

        if (this.get_options().icon) {
            result.find("TD").attr("colspan", "2");
        }

        for (id in buttons) {
            result.find("TD").append(this._generateButton(id, buttons[id]));
        }

        return result;

    },

    _generateButton: function (id, data) {

        var result = $("<input type=\"button\" />");

        result.attr("value", data.label ? data.label : data);
        result.attr("data-command", id);
        result.addClass("button");

        return result;

    },

    _attachButtonEvents: function () {

        var id = null,
            buttons = this.get_options().buttons;

        for (id in buttons) {
            this._attachButtonEvent(id, buttons[id]);
        }

    },

    _attachButtonEvent: function (id, data) {

        var that = this,
            node = this._getButton(id);

        node.on("click", function () {
            if ($.isFunction(data.callback)) {
                data.callback({
                    dialog: that,
                    window: that._window,
                    content: that.get_windowDomNode()
                });
            }
            that.hide();
        });

    },

    _getButton: function (id) {
        return this.get_windowDomNode().find("input[type=button][data-command=" + id + "]");
    },

    _getIconUrl: function () {

        var result = null,
            iconUrl = this.get_options().icon || "";

        if (iconUrl.indexOf("~") === 0) {
            result = ProspectDirector.RUNTIME.resolveUrl(iconUrl);
        }
        else {
            result = ProspectDirector.RUNTIME.resolveUrl("~/application/icons/32x32/" + iconUrl);
        }

        return result;

    },

    _setFocus: function () {

        var id = this.get_options().focus,
            buttons = this.get_options().buttons,
            data = null;

        if (id) {
            this._getButton(id).focus();
        }
        else {
            this.get_windowDomNode().find("input[type=button]:first").focus();
        }

    }

});

//================================
// STATIC METHODS
//================================

ProspectDirector.Web.UI.MessageDialog.show = function (options) {
    return new ProspectDirector.Web.UI.MessageDialog(options);
};

ProspectDirector.Web.UI.MessageDialog.warn = function (options) {
    return new ProspectDirector.Web.UI.MessageDialog($.extend(true, {
        title: "Application Warning",
        icon: "SIGN_WARNING.PNG"
    }, 
    options));
};

ProspectDirector.Web.UI.MessageDialog.confirm = function (options) {
    return new ProspectDirector.Web.UI.MessageDialog($.extend(true, {
        title: "Application Confirmation",
        icon: "QUESTION_AND_ANSWER.PNG"
    }, 
    options));
};

ProspectDirector.Web.UI.MessageDialog.error = function (options) {
    return new ProspectDirector.Web.UI.MessageDialog($.extend(true, {
        title: "Application Error",
        icon: "ERROR.PNG"
    }, 
    options));
};

ProspectDirector.Web.UI.MessageDialog.notify = function (options) {
    return new ProspectDirector.Web.UI.MessageDialog($.extend(true, {
        title: "Application Notification",
        icon: "INFORMATION.PNG"
    }, 
    options));
};
