var Notice_overlay = function(){

    return {

        /**
         * Notice properties
         */
        title                                       : '',
        subtitle                                    : '',
        details                                     : '',
        debug                                       : '',
        showDebug                                   : false,
        footer                                      : '',

        /**
         * Sets default notice messages to use if no message is passed to function
         */
        defaultSuccessMsg                           : 'Completed successfully',
        defaultErrorMsg                             : 'An error has occurred',
        defaultWarningMsg                           : 'Warning',
        defaultDebuggingMsg                         : 'Debugging',

        timer                                       : 0,
        timeout                                     : 15, // in seconds
        defaultTimeout                              : 15,

        /**
         * Options for modal dialog
         */
        MODAL_OPTIONS : {
            opacity                                 : 60,
            overlayId                               : 'notice-overlay',
            containerId                             : 'notice-overlay-container',
            position                                : ['30%',null],
            closeClass                              : 'close'
        },

        /**
         * Options for modal dialog in debug mode
         */
        MODAL_DEBUG_OPTIONS : {
            opacity                                 : 60,
            overlayId                               : 'notice-overlay',
            containerId                             : 'notice-overlay-container-debug',
            position                                : ['5%',null],
            closeClass                              : 'close'
        },

        /**
         * Displays success notice
         *
         * @param string message The success message to display
         */
        success : function(title){

            /* Use default message if none is specified */
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                this.title                          = this.defaultSuccessMsg;
            }
            this.show('success');
        },

        /**
         * Displays error notice
         *
         * @param string message The error message to display
         */
        error : function(title){

            // Use default message if none is specified
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                this.title                          = this.defaultErrorMsg;
            }
            this.show('error');
        },

        /**
         * Displays info notice
         *
         * @param string message The info message to display
         */
        info : function(title){

            // Don't show message if none is specified
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                return;
            }
            this.show('info');
        },

        /**
         * Displays warning notice
         *
         * @param string message The warning message to display
         */
        warning : function(title){

            // Use default message if none is specified
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                this.title                          = this.defaultWarningMsg;
            }
            this.show('warning');
        },

        /**
         * Displays help notice
         *
         * @param string message The help message to display
         */
        help : function(title){

            // Don't show message if none is specified
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                return;
            }
            this.show('help');
        },

        /**
         * Displays debug notice
         *
         * @param string message The debug message to display
         */
        debugging : function(title){

            // Use default message if none is specified
            if (title != undefined){
                this.title                          = title;
            }
            else if (isEmpty(this.title)){
                this.title                          = this.defaultDebuggingMsg;
            }
            this.show('debug');
        },

        /**
         * Shows notice
         *
         * @param string mode The mode the notice is being displayed in (i.e. success, error, debug)
         */
        show : function(mode) {

            // Reset timer and overlay
            this.hide();

            // Switch notice styles based on mode
            $('#notice').attr('class',mode);

            // Add title
            $('#notice-title').html(this.title);

            // Add subtitle and display if it has content
            $('#notice-subtitle').html(this.subtitle);

            if (this.subtitle.length > 0){
                $('#notice-subtitle').show();
            }

            // Check whether details is an object. If so, convert to unordered list
            if (this.details.length > 0 && typeof(this.details) == 'object'){

                if (this.details.length == 1){
                    var details                     = '<p>'+decodeURIComponent(this.details[0])+'</p>';
                }
                else {
                    // Add list of details
                    var details                     = '<ul>';
                    for (detail in this.details) {
                        if (this.details[detail] != '' && this.details[detail] != undefined) {
                            details                += '<li>' + decodeURIComponent(this.details[detail]) + '</li>';
                        }
                    }
                    details                        += '</ul>';
                }

                // Show details
                $('#notice-details').html(details);
                $('#notice-details').show();

            }
            // If details isn't an object, but does have content, display it
            else if (this.details.length > 0){
                $('#notice-details').html(this.details);
                $('#notice-details').show();
            }

            // If debugging is turned on, show the debug console
            if (this.showDebug == true || mode == 'debug'){

                // Check whether debug is an object. If so, convert to unordered list
                if (this.debug.length > 0 && typeof(this.debug) == 'object'){

                    // Add list of debug items
                    var debug                       = '<ul>';
                    for (thisDebug in this.debug){
                        if (!isEmpty(this.debug[thisDebug])){
                            debug                  += '<li>' + decodeURIComponent(this.debug[thisDebug]) + '</li>';
                        }
                    }
                    debug                          += '</ul>';

                    // Show debug
                    $('#notice-debug').html(debug);
                    $('#notice-view-debug-link').show();
                }
                // If debug isn't an object, but does have content, display it
                else if (this.debug.length > 0) {
                    $('#notice-debug').html(this.debug);
                    $('#notice-view-debug-link').show();
                }
                // Otherwise, hide debug
                else {
                    $('#notice-debug-container fieldset').hide();
                    $('#notice-view-debug-link').hide();
                }
            }
            // Otherwise, hide debug
            else {
                $('#notice-debug-container fieldset').hide();
                $('#notice-view-debug-link').hide();
            }

            // Make debug window larger and show by default
            if (mode == 'debug'){
                $('#notice-debug-container fieldset').show();
                $('#notice-view-debug-link').hide();
            }

            // Add footer and display if it has content
            $('#notice-footer').html(this.footer);

            if (this.footer.length > 0){
                $('#notice-footer').show();
            }

            // Show modal overlay
            if (mode == 'debug'){
                $('#notice').modal(this.MODAL_DEBUG_OPTIONS);
            }
            else {
                $('#notice').modal(this.MODAL_OPTIONS);
            }

            // Set timer to close it, unless debugging
            if (this.showDebug == false && mode != 'debug') {
                this.hide(this.timeout);
            }
        },

        /**
         * Hides notice
         *
         * @param integer timeout The number of seconds to delay before hiding notice
         */
        hide : function(timeout){

            clearTimeout(this.timer);
            this.timer                              = 0;

            // If a timeout has been specified, set timer
            if (timeout != undefined){
                this.timer                          = setTimeout('$.modal.close();Notice_overlay.timer = 0;',timeout * 1000);
            }
            // If no timeout has been specified, close modal dialog immediately
            else {
                $.modal.close();
            }
        },

        /**
         * Clears any previous notice content and hides everything but the (required) title.
         *
         * Also resets timeout to default.
         */
        clear : function(){

            this.title                              = '';
            this.subtitle                           = '';
            this.details                            = '';
            this.debug                              = '';
            this.showDebug                          = false;
            this.footer                             = '';

            this.timeout                            = this.defaultTimeout;

            $('#notice-title').html('');
            $('#notice-subtitle').html('');
            $('#notice-details').html('');
            $('#notice-debug').html('');

            $('#notice-subtitle').hide();
            $('#notice-details').hide();
            $('#notice-debug-container fieldset').hide();
            $('#notice-view-debug-link').hide();
            $('#notice-footer').hide();
        },

        /**
         * INIT
         */
        init : function(){

            // "View Debug" onclick
            $('#notice-view-debug-link').click(function(){
                $('#notice-debug-container fieldset').toggle();
            });
        }
    }
}();
$(document).ready(Notice_overlay.init);

