ProspectDirector.Types.Core.Confirmation = Class.extend({

    //=================================
    //  constructor
    //=================================

    init: function (options) {

        var that = this,
            index = 0;

        this.options = $.extend({
            id: ProspectDirector.RUNTIME.guid(),
            title: "Message from application",
            icon: null,
            message: "[message]",
            commands: []
        }, options);

        for (index; index < this.options.commands.length; index++) {
            this.options.commands[index] = this.getCommand(this.options.commands[index]);
        }

        this.dialog = $(this.html(options)).appendTo("body DIV[data-role=windows]");

        this.setFocus();

        this.window = this.dialog.kendoWindow({
            animation: false,
            modal: true,
            resizable: false,
            title: this.options.title,
            actions: [],
            minHeight: 55,
            minWidth: 200,
            refresh: function () {
                this.center();
            },
            visible: false
        }).data("kendoWindow");

        this.window.center();
        this.window.open();

        $("[data-confirmation-id=" + this.options.id + "]").parent().show();

        $(window).on("resize", function () {
            if (that.window) {
                that.window.center();
            }
            else {
                $(window).off("resize", this);
            }
        });

        this.addEventHandlers();

    },

    //=================================
    //  private methods
    //=================================

    html: function (options) {

        var result = "",
            icon = null,
            index = 0,
            command = null;

        result += "<div data-confirmation-id=\"" + this.options.id + "\" data-role=\"dialog\" style=\"padding:10px;\">" +
                "<table style=\"width:400px;\">" +
                "<tr>";

        if (this.options.icon) {

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

            result += "<td style=\"width:40px;\" align=\"middle\" valign=\"top\"><img src=\"" + icon + "\"></td>";

        }

        result += "<td class=\"standard-text\">" + this.options.message + "</td>" +
                "</tr>";

        if (this.options.commands.length > 0) {
            result += this.htmlButtons();
        }

        result += "</table>" +
                "</div>";

        return result;

    },

    htmlButtons: function () {

        var result = ""

        result += "<tr>" +
                "<td colspan=\"" + (this.options.icon ? 2 : 1) + "\" align=\"right\" style=\"padding-top:10px;\">";

        for (index = 0; index < this.options.commands.length; index++) {
            result += "&nbsp;<input type=\"button\" value=\"" + this.options.commands[index].label + "\" data-command=\"" + this.options.commands[index].command + "\" class=\"app-button\" />";
        }

        result += "</td>" +
                "</tr>";

        return result;

    },

    getCommand: function (command) {

        var result = null;

        switch ($.type(command)) {
            case "string":
                result = {
                    label: command,
                    command: command.toLowwerCase(),
                    callback: this.getCallback(command),
                    isDefault: false
                }
                break;
            case "object":
                result = {
                    label: command.label,
                    command: command.command,
                    callback: command.callback ? command.callback : this.getCallback(command.command),
                    isDefault: command.isDefault
                };
                break;
        }

        return result;

    },

    getCallback: function (command) {

        var that = this,
            result = null;

        if ($.type(command) === "string") {

            result = $.proxy(function (e) {
                that.close({
                    command: command
                });
            }, this);

        }
        else if ($.type(command) === "object") {
            result = command.callback;
        }

        return result;

    },

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

    addEventHandlers: function () {

        var index = 0;

        this.removeEventHandlers();

        for (index; index < this.options.commands.length; index++) {

            this.addButtonClickHandler(this.options.commands[index]);

        }

    },

    addButtonClickHandler: function (command) {

        var that = this,
            button = this.getButton(command.command);

        if (button.length > 0 && command !== null) {

            button.on("click.confirmation", function () {

                command.callback({
                    command: command.command,
                    dialog: that.dialog
                });

                that.close({
                    command: command
                });

            });

        }

    },

    removeEventHandlers: function () {

        var index = 0,
            button = null,
            commandInfo = null;

        for (index; index < this.options.commands.length; index++) {

            commandInfo = this.options.commands[index];
            button = this.getButton(commandInfo.command);
            button.off("click.confirmation");

        }

    },

    close: function () {

        var that = this,
            win = that.dialog.data("kendoWindow");

        setTimeout(function () {
            if (win) {
                win.close();
                win.destroy();
            }
        }, 50)

    },

    setFocus: function () {

        var index = 0,
            button = null,
            commandInfo = null;

        for (index; index < this.options.commands.length; index++) {

            commandInfo = this.options.commands[index];
            if (commandInfo.isDefault) {
                button = this.getButton(commandInfo.command);
            }

        }

        if (button) {

            setTimeout(function () {
                button.focus();
            }), 100;

        }

    }

});

//=================================
//  static methods
//=================================

ProspectDirector.Types.Core.Confirmation.show = function (options) {

    var result = new ProspectDirector.Types.Core.Confirmation(options);



    return result;

};