/// class ProspectDirector.Web.UI.Canvas.Charts.PreviewDialog
/// using ProspectDirector.Web.UI.Dialog

/**
@constructor
@class PreviewDialog
@namespace Web.UI.Canvas.Charts
@extends Web.UI.Dialog
**/
ProspectDirector.Web.UI.Canvas.Charts.PreviewDialog = ProspectDirector.Web.UI.Dialog.extend({

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

    init: function (options) {

        this.options = $.extend({
            config: {
                modal: true,
                height: 400,
                width: 550,
                iframe: false,
                resizable: false,
                title: "Chart Preview",
                content: ProspectDirector.RUNTIME.resolveUrl("~/areas/hub/assets/internal/New/Web.UI.Canvas.Charts/Chart/Dialogs/PreviewDialog.htm")
            }
        }, options);

        this._super(this.options);

    },

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

    get_chartDomNode: function () {
        return this.get_container().find("DIV[data-id=chart]");
    },

    get_closeButton: function () {
        return this.get_container().find("BUTTON[data-id=close]");
    },

    //================================
    // INITIALIZERS
    //================================

    _init_dialog: function () {
        this.get_closeButton().on("click", $.proxy(this._closeButton_onClick, this));
        this._getChartPreview();
    },

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

    _closeButton_onClick: function (e) {
        this.hide();
    },

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

    _getChartPreview: function () {

        PD.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: ProspectDirector.RUNTIME.resolveUrl("~/Chart/Preview"),
            data: JSON.stringify(this.options.data.context),
            success: $.proxy(this._getChartPreview_onSuccess, this),
            error: $.proxy(this._getChartPreview_onError, this)
        });

    },

    _getChartPreview_onSuccess: function (e) {

        var renderer = new ProspectDirector.Web.UI.Canvas.Charts.RenderBase({
            data: e,
            domNode: this.get_chartDomNode(),
            isDesign: true
        });

        renderer.render();

    },

    _getChartPreview_onError: function (e) {
        this.get_chartDomNode().append("ERROR");
    }

});

ProspectDirector.Web.UI.Canvas.Charts.PreviewDialog.open = function (options) {
    return new ProspectDirector.Web.UI.Canvas.Charts.PreviewDialog(options);
};