/// class ProspectDirector.Types.Core.Dialog

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

    init: function (id, title, url, options) {

        this.options = $.extend(true, {
            id: id,
            config: {
                content: {
                    type: "GET",
                    url: url
                },
                modal: true,
                resizable: false,
                title: title,
                actions: ["Close"],
                animation: false,
                close: $.proxy(function (e) {
                    this.hide({
                        command: "cancel"
                    });
                }, this),
                minHeight: 55,
                minWidth: 200,
                refresh: function () {
                    this.center();
                },
                visible: false
            }

        }, options);

        this.callbacks = {};

    },

    get_element: function () {
        return $("#" + this.options.id)
    },

    get_window: function () {
        return this.get_element().data("kendoWindow");
    },

    trigger: function (command, args) {

        if (command !== null
            && this.callbacks !== null
            && typeof this.callbacks === "object") {
            if (this.callbacks.hasOwnProperty(command)) {
                var callback = this.callbacks[command];
                if (callback !== undefined
                    && typeof callback === "function") {
                    callback(this, args);
                }
            }
        }

    },

    show: function (options) {

        options = $.extend(true, { params: {} }, this.options, options);

        this.callbacks = options.callbacks;

        if (options.config.content.type === "POST") {

            options.config.content.url = options.config.content.url + "?dialogId=" + options.id;
            options.config.content.data = options.params;

        }
        else {

            options.config.content = {
                url: this.options.config.content.url,
                data: $.extend({
                    dialogId: options.id
                }, options.params)
            };

        }

        this.open(options);

        if (this.get_element().data("dialog")) {
            this.get_element().data("dialog").params = options.params;
        }

        if (options.content !== undefined && options.content !== null) {
            this.get_element().data("kendoWindow").content(options.content);
        }

    },

    hide: function (options) {

        var window = this.get_element().data("kendoWindow");

        options = $.extend({
            id: this.options.id
        }, options);

        this.get_element().off("keydown.dialog");

        window.unbind("close", this.options.config.close);

        this.trigger(options.command, options.data);

        setTimeout(function () {
            window.destroy();
        }, 50);

        window.bind("close", this.options.config.close);

    },

    open: function (options) {

        var dialog = null;

        if (this.get_element().length == 0) {
            this.create(options);
        };

        if (this.get_element().length == 1) {

            this.get_element().data("dialog", this);

            dialog = this.get_element().data("kendoWindow");

            if (dialog) {

                dialog.open();

                dialog.center();

                $(window).resize(function () {
                    if (!dialog.options.isMaximized) {
                        dialog.center();
                    }
                });

            }
            else {
                dialog = null;
            }

        };

    },

    create: function (options) {

        var element = $("<div id=\"" + options.id + "\" data-role=\"dialog\"></div>"),
            dialog = element.appendTo("body DIV[data-role=windows]").kendoWindow(options.config).data("kendoWindow");

        element.append(this.getLoadingMessage());
        element.css({
            overflow: "hidden"
        });

        element.on("keydown.dialog", function (e) {

            if (e.keyCode === 8) {
                if (!$(e.target).is("INPUT,TEXTAREA")) {
                    e.preventDefault();
                }
            }

        });

        dialog.center();

    },

    getLoadingMessage: function () {

        var imageUrl = ProspectDirector.RUNTIME.resolveUrl("~/application/icons/misc/ICON_CIRCLE_ANIMATED_36X36.GIF"),
            image = "<img src=\"" + imageUrl + "\" style=\"vertical-align:middle;padding-right:20px;\" />",
            message = "<table style=\"height:100%;width:100%;\"><tr><td style=\"padding:10px;text-align:center;vertical-align:middle;\">" + image + "Loading...</div></td></tr></table>";

        return $(message);

    }


});