// JavaScript Document

/*

Example implementation:

var carousel_1 = new Carousel({
	obj: $("#featured-row ul.carousel"),
	animSpeed: 1000,
	tilesPerSlide: 5
});

carousel_1.next();
carousel_1.prev();

*/

Carousel = function (params)
{
	
	var obj = params.obj;
	var length = obj.children().length;
	var itemWidth = $(obj.children()[0]).width();
	var animSpeed = params.animSpeed;
	var tilesPerSlide = params.tilesPerSlide || 1;
	var offset = params.offset || 0;
	
	var me = this;
	
	this.length = length;
	
	this.animSpeed = function (speed) { animSpeed = speed; }
	
	var index = 0;
	
	this.next = function () {
		if (index < Math.ceil(length / tilesPerSlide) - 1)
			me.animTo(index+1);
		else
			me.animTo(0);
	}
	
	this.isOnFirst = function () { return index == 0; }
	this.isOnLast = function () { return (index * tilesPerSlide) >= length-tilesPerSlide; }
	
	this.prev = function () {
		if (index > 0)
			me.animTo(index-1);
		else
			me.animTo(Math.ceil(length / tilesPerSlide) - 1);
	}
	
	this.index = function () { return index; }
	
	this.animTo = function (eq) {
		if (eq > -1 && eq < length)
		{
			index = eq;
			obj.animate({"left":"-" + ((100 * index) + offset) + "%"}, animSpeed);
			changeFn.call(obj,index);
		}
	}
	
	var changeFn = function () {}
	
	this.change = function (fn) { changeFn = fn; }
	
	obj.css("left","-" + offset + "%");
}
