﻿var DialogControl = new function() {
    this._control = function() { return $("#dialogControl"); }
    this._mask = function() { return $("#dialogControlMask"); }
    this._titlebar = function() { return this._control().children(".ui-dialog-titlebar"); }

    this.init = function() {
        this._control().addClass("ui-dialog ui-widget").hide();
        this._mask().hide();
    }    

    this.show = function(settings) {
        var content = this._control().children(".ui-dialog-content");
        content.html("<iframe class='dialogLoading' src='../Pages/" + settings.page + "' frameborder='0'></iframe>")

        if (undefined != settings.width)
            content.width(settings.width);
        if (undefined != settings.height)
            content.height(settings.height);

        if (undefined == settings.left)
            settings.left = "350px";

        this._control().css("left", settings.left);

        if (undefined != settings.top)
            this._control().css("top", settings.top);

        if (undefined != settings.bottom)
            this._control().css("bottom", settings.bottom);

        this._mask().show();
        this._control().show();

        if (undefined != settings.right)
            this._control().css("right", settings.right);
        else
            this._control().width(this._control().children(".ui-dialog-content").outerWidth());

        this._titlebar().text(Resources_Dialog_LoadingTitle);
    }

    this.contentLoaded = function(aTitle) {
        this._control().find("iframe").removeClass("dialogLoading");

        if (aTitle) {
            this._titlebar().text(aTitle);
        }
    }

    this.close = function() {
        this._control().slideUp();
        this._mask().hide();
    }
}

function setupDialog(aObject, aSaveClickFunction, aModifyDialogCloseFunction) {
    setupMessageBox("#savingDialog", null);

    setupMessageBox("#addedDialog, #updatedDialog", {
        Resources_Global_Ok: function() {
            $(this).dialog('close');
            window.top.DialogControl.close();

            if (undefined != aModifyDialogCloseFunction) {
                aModifyDialogCloseFunction();
            }
        }
    });
    
    $("#cancelButton").iconButton({
        iconType: "trash",
        onClick: function() {
            window.top.DialogControl.close();
        }
    });

    $("#closeButton").iconButton({
        iconType: "check",
        onClick: function() {
            window.top.DialogControl.close();
        }
    });
    
    if (undefined == aSaveClickFunction) {
        aSaveClickFunction = function() {
            if (validateGroup(ValidationGroup))
                openSavingDialog();
        }
    }
    
    $("#saveButton").iconButton({
        iconType: "disk",
        onClick: aSaveClickFunction                
    });
    
    localizeButtons(aObject);
}

function openSavingDialog() {
    $("#savingDialog").dialog("open");
    onStoreData();
}

function closeSavingDialog() {
    $("#savingDialog").dialog("close");
}

function closeSavingOpenConfirmationDialog() {
    closeSavingDialog();
    $("#" + (HasEditId ? "updated" : "added") + "Dialog").dialog("open");
}

function onStorageCompleted() {
    closeSavingOpenConfirmationDialog();
    window.top.SidebarControl.reloadItems();
}
