/// class PD.Web.UI.Dialog
/// using PD.Web.UI.Controls.Control

PD.Web.UI.Dialog =
PD.Web.UI.Controls.Control.extend({

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

    _window: null,
    
    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {
        
        options = $.extend(true, {
            config: {
                resizable: false
            }
        }, this.getDefaultOptions(), options);

        this._createWindow(options);

        options = $.extend(true, {
            container: this._window.get_content()
        },
            options);
        
        this._super(options);

        this._window._kendo.setOptions(options);

    },

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

    get_context: function () {
        return PD.context.getContextById(this.get_id());
    },

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

    template: function (e) {
        return "<div></div>";
    },

    onRender: function () {
        this.container().empty();
        this.container().append(this._$node);
    },

    onResize: function () {
        //overridable    
    },

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

    getDefaultOptions: function (options) {
        return _.extend({
            resizable: false
        }, options);
    },

    close: function (options) {
        this.remove();
        this._destroyContext();
        this._window.close(options);
    },

    center: function () {
        this._window._kendo.center();
    },

    setTitle: function (value) {
        this._window._kendo.setOptions({
            title: value
        });
    },

    setDimensions: function (height, width) {
        this._window._kendo.wrapper.css({
            width: width,
            height: height
        });
        this.center();
    },

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

    onConstruction: function (options) {
        this._createContext(options.config);
    },
    
    _createContext: function (config) {
        
        PD.context.createContext({
            id: this.get_id(),
            name: config != null ? config.title : null
        });

    },

    _destroyContext: function () {
        
        PD.context.destroyContext({
            id: this.get_id()
        });

    },

    _createWindow: function (options) {

        var that = this;

        this._window = new PD.Web.UI.Window($.extend(true, {
            events: {
                resize: function (e) {
                    if (that.get_node() != null) {
                        that.onResize(e);
                    }
                },
                close: function (e) {
                    if (_.isFunction(options.close)) {
                        that.close(e);
                    }
                }
            }
        }, options));

    }

});

PD.Web.UI.Dialog.open = function (options) {
    return new PD.Web.UI.Dialog({
        config: {
            height: "200px",
            width: "500px"
        }
    });
};
