
var sliders = new Array();
var fps = 20;
setInterval("sliderIntervalHandler()", 1000/fps);

function sliderIntervalHandler(){
	for(var i = 0; i < sliders.length; i++){
		sliders[i].tick();
	}
}

function Slider (obj, nodes, nodeSize, horizontal) {
	
	this.obj = obj;
	this.crop = this.obj.parentNode;
	this.parent = this.crop.parentNode;
	this.totalNodes = nodes;
	this.nodeSize = nodeSize;
	this.horizontal = horizontal;
	this.currentNode = 1;
	
	this.currentX = 0;
	this.currentY = 0;
		
	if(this.horizontal){
		this.obj.style.width = this.totalNodes * this.nodeSize + "px";		
	} else {
		this.obj.style.height = this.totalNodes * this.nodeSize + "px";
	}
	
	this.tick = function() {
		var target = - (this.currentNode - 1) * this.nodeSize;
		
		if(this.horizontal){
			if(this.currentX != target){
				this.currentX += (target - this.currentX) * .25;
				if(this.currentX > target) this.currentX = Math.floor(this.currentX);
				if(this.currentX < target) this.currentX = Math.ceil(this.currentX);
				this.updateCSS();
			}
		} else {
			if(this.currentY != target){
				this.currentY += (target - this.currentY) * .25;
				if(this.currentY > target) this.currentY = Math.floor(this.currentY);
				if(this.currentY < target) this.currentY = Math.ceil(this.currentY);
				this.updateCSS();
			}
		}
	}
	this.updateCSS = function() {
		this.obj.style.left = this.currentX + "px";
		this.obj.style.top  = this.currentY + "px";
		
		var tempclass = "";

		if(this.currentNode == 1) tempclass += "slideBeg";
		if(this.currentNode == this.totalNodes) tempclass += " slideEnd";
		if(tempclass == "") tempclass = "slideMid";
		
		tempclass += " slide"+this.currentNode;		
		this.parent.className = tempclass;
	}
	this.prev = function() {
		if(this.currentNode > 1) this.currentNode --;
		return this.currentNode;
	}
	this.next = function() {
		if(this.currentNode < this.totalNodes) this.currentNode ++;
		return this.currentNode;
	}
	this.slideTo = function(num) {
		if(num >= 1 & num <= this.totalNodes) this.currentNode = num;
	}
	
	this.updateCSS();
	
	this.fps = 2;
	sliders.push(this);
}
