/* HelpDialog content */
HelpDialog.content = function(params) {
    var paramsDefault = {
        url: null,
        postQuery: null,
        header: '',
        html: ''
    };
    this.params = $.extend(paramsDefault,params);
    this._html = '';
    this._dialog = null;
    this._buttons = [];
    this._html_content = null;
    return this;
};
HelpDialog.content.prototype.setDialog = function (d) {
    this._dialog = d;
};
HelpDialog.content.prototype.isUrl = function() {
    return this.params.url ? true : false;
};
HelpDialog.content.prototype.getUrl = function() {
    return this.params.url;
};
HelpDialog.content.prototype.setHtmlContent = function (html_content) {
    this._html_content = html_content;
};
HelpDialog.content.prototype._createHtml = function() {
    var self = this;
    if (this._html_content != null) {
        this._html = $(this._html_content);
    }
    else {
        this._html = $('<div></div>');
        if (this.params.header) {
            this._html.append('<h3>' + this.params.header + '</h3>');
        }
        this._html.append(this.params.html);
        if (this._buttons.length > 0) {
            var buttons = $('<div></div>').css({
                paddingTop: '30px',
                paddingBottom: '20px',
                textAlign: 'center',
                "_height": '1%',
                minHeight: '1%'
            });
            $.each(this._buttons,function (idx,btn) {
                var aBtn = $('<a>'+btn.label+'</a>').addClass('button');
                if (typeof(btn.onClick) == 'function') {
                    aBtn.click(function () {
                        return btn.onClick(self._dialog);
                    });
                }
                buttons.append($('<span></span>').append(
                    $('<span></span>').append(aBtn).addClass('buttonEnd')
                ).addClass('button'));
                buttons.append('&nbsp;');
            });
            this._html.append(buttons);
        }
    }
};
HelpDialog.content.prototype.addButton = function (label,onclick) {
    this._buttons[this._buttons.length] = {
        label: label,
        onClick: onclick
    };
    return this;
};
HelpDialog.content.prototype.get = function () {
    this._createHtml();
    return this._html;
};

/* HelpDialog */

function HelpDialog(id,params) {
    this.id = id;
    var paramsDefault = {
        content: null,
        onContentLoad: null,
        onShow: null,
        onClose: null,
        overlay: true
    };
    this.params = $.extend(paramsDefault,params);
    this._dialog = null;
    this._content = null;
    this._setContent(this.params.content);
    this._createHtml();
}

HelpDialog.prototype._getParam = function (paramName) {
    return this.params[paramName];
};

HelpDialog.prototype._createHtml = function () {
    if ($('#' + this.id).length) {
        return true;
    }
    var self = this;
    // var closeBtn = $('<span class="closeHelpDialog"><a href="javascript:void(0);"><span>X</span></a></span>').click(function () {
    //     self.close();
    // });
    // var content = $('<div class="hdc"></div>');
    // this._dialog = $('<div class="helpDialog"></div>').attr('id',this.id).append(
    //     $('<div class="hdt"></div>').append(
    //         $('<div class="hdb"></div>').append(content)
    //     )
    // ).append(closeBtn);
    
    // var content = $('<div class="hdc"></div>');
    // this._dialog = $('<div class="helpDialog"></div>').attr('id',this.id).append(
    //     $('<div class="hdt"></div>').append(
    //         $('<div class="hdb"></div>').append(content)
    //     )
    // );
    var content = $('<div class="hdc"></div>');
    this._dialog = $('<div class="helpDialog"></div>').attr('id', this.id)
        .append('<div class="hdt"></hdt>')
        .append(content)
        .append('<div class="hdb"></hdb>');
    
    if (this._content.isUrl()) {
        content.ajaxProgress();
        if (this._content.params.postQuery) {
            $.post(this._content.getUrl(), this._content.params.postQuery, function (dataLoaded) {
                content.html(dataLoaded);
                if (typeof(self.params.onContentLoad) == 'function') {
                    self.params.onContentLoad(self,dataLoaded);
                }
            });
        }
        else {
            content.load(this._content.getUrl(),function (dataLoaded) {
                if (typeof(self.params.onContentLoad) == 'function') {
                    self.params.onContentLoad(self,dataLoaded);
                }
            });
        }
    } else {
        content.append(this._content.get());
    }
};

HelpDialog.prototype.show = function () {
    //overlay
    // var zIndex = $('div.popupDialogOverlay').length + 100;
    // if (this.params.overlay) {
    //     var overlay = $('<div class="popupDialogOverlay"></div>').css({
    //         position: 'absolute',
    //         top: '0px',
    //         left: '0px',
    //         width: $(document).width() + 'px',
    //         height: $(document).height() + 'px',
    //         zIndex: zIndex
    //     }).attr('id',this.id + '_overlay');
    //     $(document.body).append(overlay);
    // }
    this._dialog.css('z-index', 1001);
    this._dialog.appendTo(document.body);
    if (typeof(this.params.onShow) == 'function') {
        this.params.onShow(this);
    }
};

HelpDialog.prototype.close = function (noEvent) {
    if (typeof(noEvent) == 'undefined') {
        noEvent = false;
    }
    var close = true;
    if (!noEvent) {
        if (typeof(this.params.onClose) == 'function') {
            close = this.params.onClose(this);
        }
    }
    if (close) {
        this._dialog.remove();
        // $('#' + this.id + '_overlay').remove();
    }
};

HelpDialog.prototype.center = function (x, y) {
    var l = x - this._dialog.width() + 20;
    var t = y + 20;
    
    l = l < 0 ? 0 : l;
    t = t < 0 ? 0 : t;
    
    this._dialog.css({
        left: l + 'px',
        top: t + 'px'
    });
};

HelpDialog.prototype._setContent = function (c) {
    this._content = c;
    this._content.setDialog(this);
    return this;
};

HelpDialog.prototype.getHtml = function (c) {
    return this._dialog;
};

