var RealNetworks_Temp = {};
var RealNetworks_Debug_Enable = false;

if (!RealNetworks)
    var RealNetworks = {};

RealNetworks.Debug =
{
    debugWindow: null,
    windowObject: null,
    queuedMessages: null,
    oddStatement: true,
    
    fInit: function()
    {
        if (RealNetworks.Debug.debugWindow == null && RealNetworks.Debug.windowObject && RealNetworks_Debug_Enable)
        {
            RealNetworks.Debug.debugWindow = RealNetworks.Debug.windowObject.open('www.google.com','RealNetworks_DebugWindow','height=800,width=800,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes');
            oddStatement = true;
            RealNetworks.Debug.fPrintQueuedMessages();
        }
    },
    
    fTerminate: function()
    {
        if (RealNetworks.Debug.debugWindow != null)
        {
            RealNetworks.Debug.debugWindow.close();
            RealNetworks.Debug.debugWindow = null;
        }
    },
    
    fPrint: function(statement)
    {
        if (RealNetworks_Debug_Enable)
        {
            if (!RealNetworks.Debug.queuedMessages && !RealNetworks.Debug.debugWindow)
            {
                RealNetworks.Debug.queuedMessages = new Array();
            }
            
            if (RealNetworks.Debug.queuedMessages  && !RealNetworks.Debug.debugWindow)
            {
                RealNetworks.Debug.queuedMessages.push([statement, new Date()]);
            }

            if (RealNetworks.Debug.debugWindow)
            {
                RealNetworks.Debug.fPrintQueuedMessages();
                RealNetworks.Debug.fPrintHelper(statement, new Date());
            }
        }
    },
    
    fPrintQueuedMessages: function()
    {
        if (RealNetworks.Debug.debugWindow && RealNetworks.Debug.queuedMessages && RealNetworks.Debug.queuedMessages.length>0)
        {
            RealNetworks.Debug.queuedMessages.reverse();
            var message = RealNetworks.Debug.queuedMessages.pop();
            while(message)
            {
                try
                {
                    RealNetworks.Debug.fPrintHelper(message[0], message[1]);
                }
                catch (e)
                {
                    alert('An error occured @ fPrint');
                }
                message = RealNetworks.Debug.queuedMessages.pop()
            }
        }
    },
    
    fPrintHelper: function(statement, date)
    {
        var cstrStatementListIdValue = "_statement_table";
        if (RealNetworks.Debug.debugWindow)
        {
            var doc = RealNetworks.Debug.debugWindow.document;
            
            var currentDate = null;
            if (!date)
                currentDate = new Date();
            else
                currentDate = date;
            
            var statementTable = doc.getElementById(cstrStatementListIdValue);
            if (!statementTable)
            {
                statementTable = doc.createElement("table");
                statementTable.name = cstrStatementListIdValue;
                statementTable.id = cstrStatementListIdValue;
                doc.body.appendChild(statementTable);
            }
            
            preTag = doc.createElement("pre");
            preTag.appendChild(doc.createTextNode(statement));
            tdStatementTag = doc.createElement("td");
            tdStatementTag.appendChild(preTag);
            trTag = doc.createElement("tr");
            
            if (oddStatement)
                trTag.style.setProperty("background-color","#CCCCCC",null);
            
            tdDateTag = doc.createElement("td");
            
            tdDateTag.appendChild(doc.createTextNode(""+currentDate.getTime()));
            
            trTag.appendChild(tdDateTag);
            trTag.appendChild(tdStatementTag);
            
            if (statementTable.firstChild)
            {
                statementTable.insertBefore(trTag, statementTable.firstChild);
            }
            else
            {
                statementTable.appendChild(trTag);
            }
            
            oddStatement = !oddStatement;
        }
    }
};

RealNetworks_Temp.fOnLoadDocument = function(aEvent)
{
    RealNetworks.Debug.windowObject = window;
    RealNetworks.Debug.fInit();
}

RealNetworks_Temp.fOnLoadChrome = function()
{
    var appcontent = document.getElementById("appcontent");
    if (appcontent)
    {
        appcontent.addEventListener("load", RealNetworks_Temp.fOnLoadDocument, true);
    }
}

if (RealNetworks_Debug_Enable)
{
    window.addEventListener("load", RealNetworks_Temp.fOnLoadChrome, false);
}
