/************************************************************
						Roller object
	See roller_test for usage
 ************************************************************/
function Roller (div,speed,direction /*'up'|'down'*/)  {
	/********************************
				ATTRIBUTES
	*********************************/
	// Keep a trace of ourself
	var self = this;

	// keep a trace of content node
	this.d1=document.getElementById(div);	
	// Clone of dom node
	this.clones = [];
	// Parent of dom node (and its clone)
	this.container = null;
	// keep a trave of desired speed (1 if not set)
	this.speed = typeof(speed) !== "undefined" ? speed : 1;
	// keep a trace of current y offset and move factor
	switch (direction) {
		case "up" : this.op = -1;  this.y = 0; break;
		case "down" : this.op = 1; this.y = -self.d1.offsetHeight; break;
		default : this.op = -1; this.y = 0;
	}
	// Keep a trace of current move interval (roller started == (timer <> null))
	this.timer = null;
	
	/********************************
				METHODS
	*********************************/	
	/**
		Init the roller (clone dom node content)
	**/
	this.init = function() {
		// already initialized
		if (self.d1 && self.container) return;
		
		// Init dom node in which we will apply roller
		if (!self.d1) {
			// Throw an error
			alert("Invalid node for roller initialization !");
			return;
		}
		// Init container
		self.container = self.d1.parentNode;
		
		// Add listener for mouse events in order to stop roller when mouse is over
		self.d1.onmouseover = function() { self.stop(); };
		self.d1.onmouseout = function() { self.start(); };
		
		// Add some style
		self.container.style.overflow = "hidden";
		self.container.style.position = "relative";
		self.d1.style.position = "relative";
		self.d1.style.top = "0 px";	

		// Compute number of clone needed ( container height / content height )
		var nb_clone = Math.ceil((self.container.getBoundingClientRect().bottom  - self.container.getBoundingClientRect().top )/
								(self.d1.getBoundingClientRect().bottom - self.d1.getBoundingClientRect().top));
		
		//console.log((self.container.getBoundingClientRect().bottom  - self.container.getBoundingClientRect().top )+" / "+(self.d1.getBoundingClientRect().bottom - self.d1.getBoundingClientRect().top)+ " --> "+nb_clone);

		// Create and append clones to dom container
		self.clones = [];
		for(var i =0; i < nb_clone; i++) {
			self.clones[i] = self.d1.cloneNode(true);
			// Re-add listener (cloneNode doesn't keep it)
			self.clones[i].onmouseover = function() { self.stop(); };
			self.clones[i].onmouseout = function() { self.start(); };
			self.clones[i].id = self.d1.id+i;
			// Add the clone to parent node of d1
			self.container.appendChild(self.clones[i]);	
		}
	};
	
	/**
		Start the timer
	**/
	this.start = function() {
		if (self.isStarted()) {
			//Stop before restart
			self.stop();
		}
		// Go
		self.timer = setInterval(self.move, 100 / self.speed);
	};
	/**
		Move one time the roller
	**/
	this.move = function() {

		self.y= self.y + self.op;
		
		//self.d1.style.paddingTop = self.y + " px";
		//self.d2.style.paddingTop = self.y + " px";
		self.d1.style.top = self.y + "px";
		
		for(var i =0; i < self.clones.length; i++) {
			self.clones[i].style.top = self.y + "px";
		}
		// If direction is up
		if (self.op <0 && self.y< self.op * self.d1.offsetHeight) {
			self.y=0;
		// Else if down
		} else if (self.op >0 && self.y > 0) {
			self.y = -self.d1.offsetHeight;
		}
	};
	/**
		Stop it
	**/
	this.stop = function() {
		if (self.isStarted()) {
			clearInterval(self.timer);
			self.timer = null;
		}
	};
	/**
		State function
	**/
	this.isStarted = function() {
		return self.timer !== null;
	}
	
	
	// Init roller
	this.init();
}