/* © Zooven, Inc 2006-2007 - ALL RIGHTS RESERVED */

/**
 * Dialog - a javascript class for popup dialogs that appear over the window.
 *
 * TODO: change Dialog objects to use CSS classes instead of IDs.
 */
zooven.ui.Dialog = Class.create();
// the id for the "opaque" object that obscures the rest of the screen.
zooven.ui.Dialog.OPAQUE_ID = 'opaque';
zooven.ui.Dialog.prototype = {

/**
 * constructor for a Dialog.
 * dialogId     : the id for the HTML element with the dialog content
 * toggleId     : the id for the "toggle button" (defaults to dialogId + "Toggle")
 * closeId      : the id for the "close button" (defaults to dialogId + "Close")
 */
    initialize : function(
            dialogId,
            toggleId,
            closeId
            ) {
        // set member variables for the dialog and the buttons
        this.dialogObject = $(dialogId);
        this.toggleButton = $(toggleId ? toggleId : dialogId + 'Toggle');
        this.closeButton = $(closeId ? closeId : dialogId + 'Close');

        // create the callback function as a closure. 
        var dialog = this;
        function toggleFunc() {
            dialog.toggle();
            return false;
        }

        // if there are buttons, attach the handler
        if (this.toggleButton) {
            this.toggleButton.onclick = toggleFunc;
        }
        if (this.closeButton) {
            this.closeButton.onclick = toggleFunc;
        }

        // the top and left have to be set dynamically, because they are relative to the window size
        this.dialogObject.style.top = ((screen.height - this.dialogObject.offsetHeight) / 3.5) + "px";
        this.dialogObject.style.left = ((screen.width - this.dialogObject.offsetWidth) / 3.5) + "px";
    },

/**
 * toggle - toggle the display of this dialog.
 */
    toggle : function() {
        // scroll to the top - because the objects are positioned relative to the upper-left corner
        window.scrollTo(0, 0);

        // if the opaque div is visible, hide it - if not, show it.
        var opaque = $(zooven.ui.Dialog.OPAQUE_ID);
        var display = (opaque.style.display == 'block') ? "none" : "block";
        opaque.style.display = display;
        this.dialogObject.style.display = display;
    }
}
