
function BxBlgLive (sIdTarget, sIdBuffer)
{
    this._sIdTarget = sIdTarget;
    this._sIdBuffer = sIdBuffer;
    this._iTimeout = 3000;
    this._iPerPage = 20;
    this._iTs = 1;
    this._iStopped = 0;
    this._iStoppedSave = 0;
    this._hTimer = null;
    
    this._c = {'R' : '238', 'G' : '238', 'B' : '0'};
}

BxBlgLive.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;
        
        var h = e.clientHeight;        
        e.style.height = "0px";

        eTarget.insertBefore (e, eTarget.firstChild);
        if (eTarget.childNodes.length > this._iPerPage)
            eTarget.removeChild (eTarget.lastChild);
   
        //e.style.backgroundColor = 'rgb(' + this._c.R + ',' + this._c.G + ',' + this._c.B + ')';
        //this.fade (e.id, this._c.R, this._c.G, this._c.B);

        this.roll (e.id, h);

        this._hTimer = setTimeout ('gBxBlgLive.run()', this._iTimeout);
        return;
    }

    // try to get online

    var $this = this;
    var h = function (r)
    {
        if (this._iStopped) return;

        r = r.replace(/&#160;/gm, ' ');
        r = r.replace(/\x26gt;/gm, '\x3e');
        r = r.replace(/\x26lt;/gm, '\x3c');

        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 ('gBxBlgLive.run()', $this._iTimeout);
    }

    var oDate = new Date(); 
    new BxXslTransform (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "live/?ts=" + this._iTs + "&date=" + oDate.toGMTString(), aBxConfig['urlRoot'] + 'modules/' + aBxConfig['moduleName'] + '/layout/default/xsl/snippet_live_inc.xsl', h);
}


BxBlgLive.prototype.firstRun = function ()
{
    var eTarget = $(this._sIdTarget);
    this.setTs(eTarget);
    this.run ();
}

BxBlgLive.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);    
}


BxBlgLive.prototype.pause = function ()
{
    this._iStopped = 1;
}

BxBlgLive.prototype.play = function ()
{
    this._iStopped = 0;
    this.run();
}

BxBlgLive.prototype.isPaused = function ()
{
    return this._iStopped;
}


BxBlgLive.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('gBxBlgLive.fade(\'' + id + '\','+r+','+g+','+b+')', 100);
}

BxBlgLive.prototype.roll = function (id, h)
{
	var st = 15;
	var d = 100;
	
	var e = $(id);
	var hh = parseInt(e.style.height);
	
	hh += st;
	if (hh < h)
	{		
		e.style.height = hh + 'px'			
		setTimeout ("gBxBlgLive.roll('"+id+"',"+h+")", d);
	}
	else
	{
		hh = h;		
		e.style.height = hh + 'px'	
	}
	
}

BxBlgLive.prototype.setPerPage = function (i)
{
    this._iPerPage = i;
}

BxBlgLive.prototype.debug = function (e)
{
    var s = '';
    for (var i in e) s += i + "\n";
    alert (s);
}


