function loadAll()
{
    if (!document.body)
    {   // wait for it...
        setTimeout(loadAll, 10);
    }
    else
    {   // go!
        // just news for now
        initNews();
    }
} // end loadAll()

/* NEWS POP-UP ***************************************************************/
/**
Attaches a pop-up function to #newsaggregator link. If JavaScript is disabled
the link will function as a simple _blank target link. With JavaScript enabled,
but styles disabled a sized window will open. If, however, JavaScript and styles
are enabled an in-line pop-up is added to the document and will appear on top
the current content. The news content is retrieved with an xmlhttprequest.
*/
function initNews()
{
    var na = document.getElementById('newsaggregator'),
        nac,        // newsaggregator container (new element)
        nad,        // newsaggregator data holder (new element)
        nac_close;  // newsaggregator container closer (new element)

    // remove default link functionality
    na.href = 'javascript:void(0);';
    na.target = '';

    // and replace with javascript function
    na.onclick = popNews;

    if (stylesOn() === true)
    {   // styles are one, add in-line pop-up container in advance
        // TODO: ...
        nac = document.createElement('div');
        nac.id = 'newsaggregator_container';
        document.getElementsByTagName('body')[0].appendChild(nac);

        // data holder
        nad = document.createElement('span');
        nad.id = 'newsaggregator_data';
        nac.appendChild(nad);

        // add close button
        nac_close = document.createElement('img');
        nac_close.id = 'newsaggregator_closer';
        nac_close.src = 'etc/img/close.gif';
        nac_close.onclick = popNews; // function also pops down
        nac.appendChild(nac_close);
    }
} // initNews();


function popNews()
{
    if (stylesOn() === false)
    {   // styles off pop-up "plain" new window
        window.open('news-aggregator.php', 'newsaggregator', 'width=400,height=540,scrollbars=yes,resizable=yes');
    }
    else
    {   // styles on
        // TODO: show fancy "in-line/in-page" pop-up

        // test
        //window.open('news-aggregator.php', 'newsaggregator', 'width=400,height=540,scrollbars=yes,resizable=yes');

        var nac = document.getElementById('newsaggregator_container'),
            nad = document.getElementById('newsaggregator_data');

        if (nac.style.display == '')
        {   // first time, get content (once)
            nac.style.display = 'block';

            // start synchronous content fetch
            req = false;

            if(window.XMLHttpRequest)
            {	// branch for native XMLHttpRequest object
                try
                {
                    req = new XMLHttpRequest();
                }
                catch(e)
                {
                    req = false;
                }
            }
            else if (window.ActiveXObject)
            {	// branch for IE/Windows ActiveX version
                try
                {
                    req = new ActiveXObject('Msxml2.XMLHTTP');
                }
                catch(e)
                {
                    try
                    {
                        req = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    catch(e)
                    {
                        req = false;
                    }
                }
            }

            if(req)
            {
                // set wait message
                nad.innerHTML = 'Loading...';

                // get data
                req.open('GET', 'news-aggregator.php?inline=1', false);
                req.send(null);

                if (req.status == 200)
                {
                    // fill data holder with response (replacing wait message)
                    nad.innerHTML = req.responseText;
                }
                else
                {
                    // XXX catch error?
                }
            }
            else
            {	// XXX catch error?
                //alert('uhoh, req not ok');
            }
        }
        else
        {   // content already loaded, simply display or hide container
            nac.style.display = (nac.style.display == 'none') ? 'block' : 'none';
        }
    }
} // end popNews()

/* STYLECHECKER **************************************************************/
function stylesOn()
{
   return (document.getElementById('sidebar').offsetLeft > 20);
} // end stylesOn

/* ************************************************************************* */
loadAll();
/* ************************************************************************* */