/******************************
 * Simple notification system *
 * @author jasse              *
 ******************************/

/**
 * Opens a dialog window
 * 
 * @param String title
 * @param String content
 * @param Object options (optional)
 */
var openDialog = function(title, content) {

    var options = Object.extend({
        title:          title,

        closable:       true,
        destroyOnClose: true,

        width:          PrototypeWindowSettings.dialog.width, 
        height:         PrototypeWindowSettings.dialog.height,
        className:      PrototypeWindowSettings.theme,
        
        showEffect:     PrototypeWindowSettings.dialog.showEffect,
        hideEffect:     PrototypeWindowSettings.dialog.hideEffect,
        effectOptions:  PrototypeWindowSettings.effectOptions,
        
        callback:       null,
        
        error:          false
        
    }, arguments[2] || {});
    
    if (options.error) {
        Dialog.alert(content, options);
    } else {
        Dialog.info(content, options);
    }

    if (PrototypeWindowSettings.dialog.staysVisible > 0) {
        setTimeout(function() {
            Dialog.closeInfo();
            
            if (options.callback) 
                options.callback();
            
        }, PrototypeWindowSettings.dialog.staysVisible);
    }
}

$(document).observe('dom:loaded', function(event) {

    var getHeight = function(messages) {
        
        var height   = 20;
        for(i = 0; i < messages.length; i++) {
            height += 20;
        }

        return height;
    }
    
    var getContent = function(messages) {
        
        var msg = '';
        for (i = 0; i < messages.length; i++) {
            msg += messages[i] + '<br/>';
        }
        
        return msg;
    }
    
    var chain = [];
    
    if(notifications.errors && notifications.errors.length > 0) {
        chain.push([
            'Fehler', 
            '<div style="color: #98141B; font-weight: bold; padding-top: 10px; text-align: center; vertical-align: middle">#{msg}</div>'.interpolate({
                msg: getContent(notifications.errors)
            }), 
            { height: getHeight(notifications.errors) }
        ]);
    }
    
    if(notifications.messages && notifications.messages.length > 0) { // Errors (implication: if there are messages, there are no errors)
        chain.push([
            'Hinweis', 
            '<div style="color: black; font-weight: bold; padding-top: 10px; text-align: center; vertical-align: middle">#{msg}</div>'.interpolate({
                msg: getContent(notifications.messages)
            }), 
            { height: getHeight(notifications.messages) }
        ]);
    }
    
    if(chain.length == 2) {
        openDialog(
            chain[0][0], 
            chain[0][1],
            Object.extend(chain[0][2], { 
                callback: function() { openDialog(chain[1][0], chain[1][1], chain[1][2]); } 
            }) 
        )
    } else if(chain.length == 1) {
        openDialog(chain[0][0], chain[0][1], chain[0][2]);
    }
});
