// Logging APIs - typically you just use ShowPane, log
// Copyright (c) 2003-2005 Move Networks

MN.Log =
{
    _pane : null, // the actual div with log messages
    _paneVisible : false,
    MAX_ROWS : 0, // by default, no logging will show up
    
    IsVisible : function() { return MN.Log._paneVisible; },

    MakePane : function()
    {   // Creates an invisible logging pane - inspired by MochiKit
        if (MN.Log._pane)
            return;
        MN.Log._pane = document.createElement('div');
        MN.Update(MN.Log._pane.style, {'display':'block', 'width':'100%', 'overflow':'auto',
                                'position':'absolute', 'left':'0px', 'bottom':'0px',
                                'font':'8pt Verdana,sans-serif', 'height':'100px', 'color':'black', 'backgroundColor':'white',
                                'borderTop':'2px solid black', 'z-index':'1000'});

        // display the version number in the log pane
        var s = document.createElement('span');
        s.innerHTML = '::: Release %s :::<br>'.format(MN.RELEASE_VERSION);
        MN.Log._pane.appendChild(s);
    },

    ShowPane : function(maxRows)
    {   // Shows or hides the logging pane - if maxRows is 0, hides the pane. If maxRows > 0,
        // displays the pane and makes logging code keep only the last 'maxRows' rows
        MN.Log.MakePane();
        if (maxRows == null)
            maxRows = 500;

        if (maxRows > 0 && !MN.Log._paneVisible)
        {
            MN.Log.MAX_ROWS = maxRows;
            document.body.appendChild(MN.Log._pane);
            MN.Log._paneVisible = true;
        }
        else if (maxRows <= 0 && MN.Log._paneVisible)
        {
            MN.Log._pane.parentNode.removeChild(MN.Log._pane);
            MN.Log._paneVisible = false;
        }
    },

    log : function()
    {   // write out one or more args to the logging pane
        var MAX_ROWS = MN.Log.MAX_ROWS;
        if (MAX_ROWS <= 0)
            return;

        if (!MN.Log._pane)
            MN.Log.MakePane();

        var pane = MN.Log._pane;
        if (pane.childNodes.length >= MAX_ROWS)
        {   // prevent it from growing without bounds
            var kids = pane.childNodes;
            var ax = kids.length - MAX_ROWS;
            for (var i=0; i < ax; i++)
                pane.removeChild(kids[i]);
        }

        var params = [];
        for (var i = 0; i < arguments.length; i++)
        {
            var arg = arguments[i];
            if (arg == null)
                arg = 'null';
            else
                arg = arg.toString();
            params.push(arg);
        }

        var s = document.createElement('span');
        s.innerHTML = MN.GetTimestamp() + ' : ' + params.join(' ') + '<br>';
        pane.appendChild(s);
        pane.scrollTop = pane.scrollHeight;
    },

    logError : function()
    {   // like log, but log with an ERROR prefix
        var params = MN.ToArray(arguments);
        params.unshift('<font color="red">ERROR</font>:');
        log.apply(this, params);
    }
};

// For convenience, make these globals
var log = MN.Log.log;
var logError = MN.Log.logError;


