/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 * Modal dialog module
 */
ModalDialog = function() {

    /**
     * Current dialog reference
     *
     * @var jQuery
     */
    var dialog_object;

    // Let's return public interface object
    return {

        /**
         * Show modal dialog
         *
         *
         * @param String name
         * @param String title
         * @param mixed body
         * @param mixed settings
         */
        show : function(name, title, body, settings) {
            // dialog options
            var options = {
                modal     : true,
                draggable : false,
                resizable : true,
                title     : title,
                id        : name,
                position  : 'top',
                bgiframe  : true,
                close     : function (type,data) {
                    if (settings.close) {
                        settings.close();
                    } // if
                    dialog_object.dialog('destroy').remove();
                },
                resizeStart : function (type,data) {

                }
            };

            if (settings) {
                // width and height settings
                options.width = settings.width ? settings.width : 410;
                options.height = settings.height ? settings.height : 'auto';
                // additional buttons
                options.buttons = {};
                if (settings && settings.buttons) {
                    for (var x = 0; x < settings.buttons.length; x++) {
                        if (settings.buttons[x].callback) {
                            var callback_function = settings.buttons[x].callback;
                            options.buttons[settings.buttons[x].label] = function () {
                                callback_function();
                                dialog_object.dialog('close');
                            } // function
                        } else {
                            options.buttons[settings.buttons[x].label] = function () {
                                dialog_object.dialog('close');
                            } // function
                        } // if
                    } // if
                } // if
            } // if

            options.maxWidth = options.width;
            options.minWidth = options.width;

            dialog_object = $(body).dialog(options);
            

            var counter = 0;
            dialog_object.parent().parent().find('.ui-dialog-buttonpane button').each(function () {
                var button = $(this);
                button.removeClass('ui-state-default').removeClass('ui-corner-all');

                var label = button.html();
                button.html('<span><span>' + label + '</span></span>');
                if (counter != 0) {
                    button.addClass('alternative');
                } // if
                counter++;
            });
        },

        /**
         * Example: App.ModalWindow.close()
         * Close the dialog
         */
        close : function() {
            if(typeof(dialog_object)=="undefined"){ return; }
            dialog_object.dialog('destroy').remove();
        },

        /**
         * sets width of dialog
         */
        setWidth : function (width_px) {
            var dom_dialog = $('.ui-dialog');
            var position = dom_dialog.position();
            var new_left_offset = position.left - ((width_px - dom_dialog.width())/2);
            dom_dialog.css('width' , width_px+'px').css('left', new_left_offset+'px');
        },

        /**
         * Sets dialog title
         */
        setTitle : function (title) {
            var dom_dialog = $('.ui-dialog .ui-dialog-titlebar span.ui-dialog-title').html(title);
        },

        setBody : function (body) {
            var dom_dialog_body = $('.ui-dialog .ui-dialog-content').html(body);
        },


        /**
         * Checks if dialog is open
         */
        isOpen : function () {
            if ($('.ui-dialog').length > 0) {
                return true;
            } else {
                return false;
            }
        }
    };

}();



