/**
 * Gestor de scroll.
 * creación:
 *   var mgr = new ScrollManager();
 * asociar con el elemento a hacer scroll, su container y la velocidad:
 *   mgr.init(objeto_a_mover, container, velocidad);
 * activar y desactivar:
 *   mgr.start('up'); o mgr.start('dn'); 
 *   mgr.stop();
 */


ScrollManager = function() {
	this.capa = null;
	this.container = null;
	this.speed = 1;
	this.milisegundos = 20;
	this.event = null;	
	this.maxy = 300;
} 

update(ScrollManager.prototype, {

	"init": function(capa, container, velocidad) {
		// inicializa la capa que vamos a hacer scroll y se
		// asegura que tiene posicionamiento relativo
		this.capa = getElement(capa);
		this.container = getElement(container);
		this.speed = velocidad;
		this.maxy = getElementDimensions(container).h - getElementDimensions(capa).h;
		updateNodeAttributes(capa, {'style' : { 'position' : 'relative', 'top' : 0 }});
//	updateNodeAttributes(capa, {'style' : { 'background-color' : 'cyan'}});
	},
	
	"_scroll" : function(direccion) {
		// funcion interna que realiza la accion de scroll
        return method(this, function () {
			pos = getElementPosition(this.capa, this.container);
			ok = false;
			if (direccion == 'dn' && pos.y > this.maxy) {
				pos.y -= this.speed;
				ok = true;
			}
			if (direccion == 'up' && pos.y < 0) {
				pos.y += this.speed;
				ok = true;
			}
			if (ok) {
				setElementPosition(this.capa, pos);
			}
        });
	},
	
	"start": function(direccion) {
		if (this.event == null) {
			this.event = setInterval(this._scroll(direccion), this.milisegundos);
		}
	},
	
	"stop": function() {
		if (this.event != null) {
			clearInterval(this.event);
			this.event = null;
		}
	}
});


