
var UI_DIALOG = null;

var TOP_OFFSET = 50;

var UI_DIALOG_OPTS = {
    autoOpen: false,
    modal: true,
    position: ['center', TOP_OFFSET],
    width: 500
//     height: 300
};

function initDialog()
{
    UI_DIALOG.dialog(UI_DIALOG_OPTS);
}

function openDialog(content_container, dialog_opts)
{
    if (dialog_opts) {
        UI_DIALOG.dialog("option", dialog_opts);
    }
    if (!dialog_opts || !dialog_opts["disableOverlayClose"]) {
        jQuery(".ui-widget-overlay").click(closeDialog);
    }
    
    UI_DIALOG.dialog("open");
    UI_DIALOG.html(jQuery(content_container).html());
}

function fitDialogToHeight()
{
    var winHeight = jQuery(window).height();
    if (UI_DIALOG.height() > (winHeight - TOP_OFFSET)) {
        UI_DIALOG.dialog( "option", "height", winHeight - TOP_OFFSET );
    }
}

function openAjaxDialog(dlg_opener, url, dialog_opts, request_opts)
{
    var get_params = ["_popup=1"];
    if (request_opts != undefined) {
        var i = get_params.length;
        for (var key in request_opts) {
            get_params[i] = (key + "=" + request_opts[key]);
            i++;
        }
    }
    
    jQuery.getJSON(url, function(json_data, textStatus) {
        if (json_data["completed"] != undefined && json_data["completed"]) {
            var dlg_opener_j = jQuery(dlg_opener);
            var box = dlg_opener_j.closest(".sys-reload");
            if (dlg_opener_j.hasClass("sys-page_reload")) {
                if (json_data["new_location_url"]) {
                    window.location.href = json_data["new_location_url"];
                    return;
                } else {
                    window.location.reload(true);
                    return;
                }
            } else if (dlg_opener_j.hasClass("sys-ajax_delete")) {
                box.fadeOut("slow");
//                 box.remove();
            } else {
                reload_box(box);
                var after_reload_callback = dlg_opener_j.data("after_reload_callback");
                if (after_reload_callback) {
                    after_reload_callback(json_data, dlg_opener_j, box);
                }
            }
            return;
        }
        
        if (dialog_opts) {
            UI_DIALOG.dialog("option", dialog_opts);
        }
        UI_DIALOG.dialog("open");
        UI_DIALOG.opener = dlg_opener;
        
        UI_DIALOG.html(json_data["html"]);
        
        fitDialogToHeight();
        
        if (json_data["dialog_opts"]) {
            UI_DIALOG.dialog( "option", json_data["dialog_opts"] );
        }
        UI_DIALOG.find("form #button_dialog_close").click(closeDialog);
        if (json_data["title"] != undefined && json_data["title"]) {
            UI_DIALOG.dialog( "option", "title", json_data["title"] );
        } else {
            UI_DIALOG.dialog( "option", "title", "" );
        }
        
        UI_DIALOG.dialog("option", "close", function(event, ui) {
            if (removeValidator != undefined) {
                UI_DIALOG.find("form").each(function(i, e) {
                    removeValidator(jQuery(e).attr("id"));
                });
            }
            UI_DIALOG.dialog("destroy");
            initDialog();
            UI_DIALOG.html("");
        });
        
        jQuery(".ui-widget-overlay").click(closeDialog);
    });
    
    return false;
}

function refreshAjaxDialog(json_data, callback)
{
    if (!json_data["completed"]) {
        //var body = jQuery(json_data["html"]).find("#content");
        //UI_DIALOG.html(body.html());
        //alert(json_data["html"]);
        if (removeValidator != undefined) {
            UI_DIALOG.find("form").each(function(i, e) {
                removeValidator(jQuery(e).attr("id"));
            });
        }
        UI_DIALOG.html(json_data["html"]);
        
        fitDialogToHeight();
    } else {
        UI_DIALOG.dialog("close");
        var dlg_opener_j = jQuery(UI_DIALOG.opener);
        var box = dlg_opener_j.closest(".sys-reload");
        if (dlg_opener_j.hasClass("sys-page_reload")) {
            if (json_data["new_location_url"]) {
                window.location.href = json_data["new_location_url"];
                return;
            } else {
                window.location.reload(true);
                return;
            }
        } else if (dlg_opener_j.hasClass("sys-ajax_delete")) {
            box.fadeOut("slow");
            //box.remove();
        } else {
            reload_box(box);
        }
        if (typeof(callback) != "undefined") {
            callback(json_data);
        }
        var after_reload_callback = dlg_opener_j.data("after_reload_callback");
        if (after_reload_callback) {
            after_reload_callback(json_data, dlg_opener_j, box);
        }
        jQuery(".sys-reload-global").each(function(i, e) { reload_box(e); });
    }
}

function closeDialog()
{
    UI_DIALOG.dialog("close");
    return false;
}

function getDialogOpener()
{
    return UI_DIALOG.opener;
}

function initDefaultAjaxDialog(opts, css_class, after_reload_callback)
{
    if (!css_class) {
        css_class = "sys-ajax_dialog";
    }
    if (!opts) {
        opts = {};
    }

    opts = jQuery.extend({
        width: 800 
//        height: 600
    }, opts);
    jQuery("." + css_class).each(function(i, e) {
        jQuery(e).click(function() {
            var href = this.href;
            if (href.substr(0, 11) == "javascript:") {
                href = eval(href.substr(11));
            }
            if (href == null) {
                return false;
            } else {
                if (after_reload_callback) {
                    jQuery(this).data("after_reload_callback", after_reload_callback);
                }
                return openAjaxDialog(this, href, opts);
            }
        });
    });
    jQuery("." + css_class).removeClass(css_class);
}

jQuery(document).ready(function() {
    UI_DIALOG = jQuery('<div style="display: none;"></div>').appendTo("body");
    initDialog();
//     initAjaxDialog();
});

