var eBDScrollerID=1;
var eBDScrollerTimer=20;

function Scroller(vertical,width,height,base_width,base_height)
{
	this.vertical=vertical;
	this.width=width;
	this.height=height;
	this.base_width=base_width;
	this.base_height=base_height;
	this.id = eBDScrollerID++;
	this.pause=3000;
	this.speed=100;
	this.items=new Array();

	this.first=0;
	this.count=0;
	
	this.addItem=Scroller_addItem;
	this.setPause=Scroller_setPause;
	this.setSpeed=Scroller_setSpeed;
	this.paint=Scroller_paint;
	this.start=Scroller_start;
	this.scroll=Scroller_scroll;
}


function Scroller_addItem(text)
{
	this.items[this.items.length] = text;
}


function Scroller_setPause(p)
{ this.pause=p; }

function Scroller_setSpeed(s)
{ this.speed=s; }

function Scroller_paint()
{
	if (this.items.length == 0) { return; }
	
	// primero calculo el contenido...
	var content_width = (this.vertical) ? this.width : 0;
	var content_height = (this.vertical) ? 0 : this.height;
	var content_html = '';
	var itemcount=0;
	
	var itemid=1;
	for (var i in this.items)
	{
		content_html += '<div id="eBDScroller'+this.id+'_item'+itemid+'" class="eBDScroller_item"' +
			 ' style="width:'+this.width+'px; height:'+this.height+'px; float:left;">' +
			 this.items[i] +
			 '</div>';
		itemid++;
		itemcount++;
		
		if (this.vertical)
		{ content_height += this.height; }
		else
		{ content_width += this.width; }
	}

	// si despues de poner todos los items, todavia no cubrimos la zona visible,
	// los repetimos hasta que la podamos cubrir...
	
	var lleno=false;
	if (this.vertical) { lleno = (content_height >= this.base_height + this.height); }
	else { lleno = (content_width >= this.base_width + this.width); }
	
	var i=0;
	while (!lleno)
	{
		content_html += '<div id="eBDScroller'+this.id+'_item'+itemid+'" class="eBDScroller_item"' +
			 ' style="width:'+this.width+'px; height:'+this.height+'px; float:left;">' +
			 this.items[i] +
			 '</div>';
		itemid++;
		itemcount++;
		i = (i+1) % this.items.length;
		
		if (this.vertical) { content_height += this.height; lleno = (content_height >= this.base_height + this.height); }
		else { content_width += this.width; lleno = (content_width >= this.base_width + this.width); }
	}
	
	// generamos los divs del contenedor y contenido
	var html='<div id="eBDScroller'+this.id+'_container" ' +
			 ' style="position:relative; overflow:hidden; width:'+this.base_width+'px; height:'+this.base_height+'px;">';
	html += '<div id="eBDScroller'+this.id+'_content" ' +
	 		' style="position:absolute; top:0px; left:0px; width:'+content_width+'px; height:'+content_height+'px;">' +
			content_html +
			'</div>';
	html += '</div>';

	this.count = itemcount;
	if (this.count > 0) { this.first=1; }

	document.write(html);
}

function Scroller_start(varname)
{
	if (this.count > 0)
	{ setTimeout(varname+".scroll('"+varname+"');",this.pause); }
}

function Scroller_scroll(varname)
{
	var cnt = document.getElementById('eBDScroller'+this.id+'_content');
	
	if (cnt == null) { return; }

	// -------------------------------------------
	// realizamos el movimiento de la capa content
	
	// calculamos el incremento que hay que mover...
	var maxincr = (this.vertical) ? this.height : this.width;
	var incr = this.speed / (1000 / eBDScrollerTimer);
	incr = Math.min(maxincr, incr);
	incr = parseInt(incr);
	if(incr<1){ incr=1; }
	

	// para mirar si hemos llegado al principio...
	var principio=false;

	if (this.vertical)
	{
		var t = cnt.style.top;
		t=t.replace('px','');
		t -= incr;
		cnt.style.top = t+'px';
		
		if (t*-1 >= this.height)
		{
			principio=true;
			
			// Si hemos llegado al principio, tenemos que quitar el primero 
			// y ponerlo al final

			var item = document.getElementById('eBDScroller'+this.id+'_item'+this.first);
			if (item != null)
			{
				cnt.removeChild(item);
				t += this.height;
				cnt.style.top = t+'px';
				cnt.appendChild(item);
			}
			this.first++;
			if (this.first > this.count) { this.first=1; }
		}
	}
	else
	{
		var t = cnt.style.left;
		t=t.replace('px','');
		t -= incr;
		cnt.style.left = t+'px';

		if (t*-1 >= this.width)
		{
			principio=true;
			
			// Si hemos llegado al principio, tenemos que quitar el primero 
			// y ponerlo al final

			var item = document.getElementById('eBDScroller'+this.id+'_item'+this.first);
			if (item != null)
			{
				cnt.removeChild(item);
				t += this.width;
				cnt.style.left = t+'px';
				cnt.appendChild(item);
			}
			this.first++;
			if (this.first > this.count) { this.first=1; }
		}
	}
	
	
	// -----------------------------------------------------------------------
	// Si hemos llegado al principio, tenemos que realizar la pausa programada
	var wait = (principio) ? this.pause : eBDScrollerTimer;
	setTimeout(varname+".scroll('"+varname+"');",wait);
}
