// JavaScript Document
function Scroll(_name,_div,_window,_steps,_rate,_active,_elements,_current)
{
	this.name	 = _name;
	this.div	 = _div;
	this.window	 = _window;
	this.steps	 = _steps;
	this.step	 = 0;
	this.halfStep= this.steps;//2;
	this.rampStep= 3*this.steps;//4;
	this.rate	 = _rate;
	this.active	 = _active;
	this.element = new Array();
	this.stop = new Array();
	this.elementCount = _elements;
	this.current = _current;
	this.h	= 360;
	this.x0 = 0;
	this.x1 = 0;
	this.x  = 0;
	this.dx = 0.33;
	this.maxRate = 12;
	this.blockSize=640+2*0;
	this.width=0;
	this.AdvanceTo=AdvanceTo;
}

scrollDivs = new Scroll("scrollDivs","divScroll",650,10,10,true,6,0);
Start(scrollDivs);

function Start(s){
	s.active = true;
	stopPx=0;
	s.stop[0]=0;
	for (j=1;j<=s.elementCount;j++)
	{
		d = getObject("portada_"+j);
//		s.element[j-1]=d.scrollWidth;
		s.element[j-1]=s.blockSize;
		stopPx+=s.blockSize;
		s.stop[j]=stopPx;
	}
	s.width = stopPx;
	s.x0 = s.stop[s.current];
}

function Move(s){
	d = getObject(s.div);
	x = parseInt(d.offsetLeft);
	if (s.x != s.x1)
	{
		w = d.Width;
		if (Math.abs(s.x-s.x0) < 30) {
			dx = Math.abs(s.x-s.x0)*s.dx; 
		} else if (Math.abs(s.x-s.x1) < 30) {
			dx = Math.abs(s.x-s.x1)*s.dx; 
		} else {
			dx = s.maxRate;
		}
		dx = Math.min(s.maxRate,dx)*s.dir;
		s.x += dx;
		if ( ((s.x-s.x1)*s.dir > 0) || (Math.abs(s.x-s.x1)<0.5)) s.x=s.x1;
		x = -Math.floor(s.x);
		d.style.left = x;
		d.style.clip="rect(0,"+(s.window-x)+","+s.h+","+(0-x)+")";
		s.mID = setTimeout("Move("+s.name+");",s.rate);
	}
	else
	{
		if (s.current == s.elementCount-1)
		{
			s.x1=0;s.current=0;s.x=0;x=0;d.style.left = x;d.style.clip="rect(0,"+(s.window-x)+","+s.h+","+(0-x)+")";
		}
		s.x0=s.x1;
	}
}

function AdvanceTo(e){
	s=this;
	s.current=e;
	if (e<0) s.current=0;
	if (e>=s.elements) s.current = s.elements-1;
	s.x1 = s.stop[s.current];
	if (s.x1 != null)
	{
		s.dir = (s.x1>s.x0)?1:-1;
		s.x = s.x+s.dir;
		Move(s);
	}
}

function getObject(o){
	if (typeof o == 'object') return o
	else return document.getElementById(o);
}
