
function BxAlertsLive (sIdTarget, sIdBuffer)
{
    this._sIdTarget = sIdTarget;
    this._sIdBuffer = sIdBuffer;
    this._iTimeout = 2000;
    this._iPerPage = 12;
    this._iTs = 0;
    this._iStopped = 0;
    this._iStoppedSave = 0;
    this._aModules = {};
    this._hTimer = null;

    this._c = {'R' : '238', 'G' : '238', 'B' : '0'};
}

BxAlertsLive.prototype.run = function ()
{
    this._hTimer = null;

    if (this._iStopped) return;

    var eBuffer = $(this._sIdBuffer);
    var eTarget = $(this._sIdTarget);


    // remove text nodes    

    while (undefined != eBuffer.lastChild && 1 != eBuffer.lastChild.nodeType) // remove text nodes
        eBuffer.removeChild (eBuffer.lastChild);        

    while (undefined != eTarget.lastChild && 1 != eTarget.lastChild.nodeType) // remove text nodes
        eTarget.removeChild (eTarget.lastChild);        

    // try to get from buffer

    if (undefined != eBuffer.lastChild)
    {        
        var e = eBuffer.lastChild;

        e.style.backgroundColor = 'rgb(' + this._c.R + ',' + this._c.G + ',' + this._c.B + ')';        

        eTarget.insertBefore (e, eTarget.firstChild);
        if (eTarget.childNodes.length > this._iPerPage)
            eTarget.removeChild (eTarget.lastChild);
    
        this.fade (e.id, this._c.R, this._c.G, this._c.B);

        this._hTimer = setTimeout ('gBxAlertsLive.run()', this._iTimeout);
        return;
    }

    // try to get online

    var $this = this;
    var h = function (r)
    {
        if (this._iStopped) return;

        eBuffer.innerHTML = r;

        while (undefined != eBuffer.lastChild && 1 != eBuffer.lastChild.nodeType) // remove text nodes
            eBuffer.removeChild (eBuffer.lastChild);        

        if (undefined != eBuffer.lastChild) // update last time
            $this.setTs(eBuffer);

        this._hTimer = setTimeout ('gBxAlertsLive.run()', $this._iTimeout);
    }
    var oDate = new Date(); 
    var m = this.getModules();
    if (m.length)
        new BxXslTransform (aBxConfig['urlRoot'] + "alerts/live/?ts=" + this._iTs + "&modules=" + m + "&date=" + oDate.toGMTString(), aBxConfig['urlRoot'] + 'modules/Alerts/layout/default/xsl/live.xsl', h);
}


BxAlertsLive.prototype.firstRun = function ()
{
    this._iStoppedSave = this._iStopped;
    this.pause();

    if (null != this._hTimer)
    {
        clearTimeout(this._hTimer);        
    }

    this._iTs = 0;
    var eBuffer = $(this._sIdBuffer);
    var eTarget = $(this._sIdTarget);

    eBuffer.innerHTML = '';
    eTarget.innerHTML = 'loading...';

    // try to get online

    var $this = this;
    var h = function (r)
    {
        eTarget.innerHTML = r;

        if (undefined != eTarget.lastChild) // update last time
            $this.setTs(eTarget);

        if (!$this._iStoppedSave) $this.play();
        $this._iStopped = $this._iStoppedSave;

        //this._hTimer = setTimeout ('gBxAlertsLive.run()', $this._iTimeout);
    }
    var oDate = new Date(); 
    var m = this.getModules();
    new BxXslTransform (aBxConfig['urlRoot'] + "alerts/live/?ts=" + this._iTs + "&modules=" + m + "&date=" + oDate.toGMTString(), aBxConfig['urlRoot'] + 'modules/Alerts/layout/default/xsl/live.xsl', h);
}

BxAlertsLive.prototype.setTs = function (e)
{
    while (undefined != e.firstChild && 1 != e.firstChild.nodeType) // remove text nodes
        e.removeChild (e.firstChild);

    if (undefined == e.firstChild) return;

    var eDiv = e.firstChild;
    var eTs = eDiv.getElementsByTagName('u')[0];
    this._iTs = parseInt(eTs.innerHTML);    
}

BxAlertsLive.prototype.pause = function ()
{
    this._iStopped = 1;
}

BxAlertsLive.prototype.play = function ()
{
    this._iStopped = 0;
    this.run();
}

BxAlertsLive.prototype.isPaused = function ()
{
    return this._iStopped;
}

BxAlertsLive.prototype.module = function (name, bool)
{
    this._aModules[name] = bool;
}

BxAlertsLive.prototype.getModules = function ()
{
    var s = '';
    for (var name in this._aModules) 
        if (1 == parseInt(this._aModules[name]))
            s += name + ":";
    return s.length ? s.substr(0, s.length-1) : '';
}

BxAlertsLive.prototype.fade = function (id, r, g, b)
{
	r += 5;
	g += 5;
	b += 5;

	if (r > 255) r = 255;
	if (g > 255) g = 255;
	if (b > 255) b = 255;

	var e = $(id);
    if (undefined == e) return;
	e.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b +')';

	if (r < 255 || g < 255 || b < 255) 
		setTimeout('gBxAlertsLive.fade(\'' + id + '\','+r+','+g+','+b+')', 100);
}

BxAlertsLive.prototype.debug = function (e)
{
    var s = '';
    for (var i in e) s += i + "\n";
    alert (s);
}

