var iCarousel = new Class({
	options: {
		animation: {
			direction: "left",
			transition: Fx.Transitions.Cubic.easeInOut,
			duration: 500
		},

		idPrevious: "previous",
		idNext: "next",
		idToggle: "toggle",

		onClickPrevious: Class.empty,
		onClickNext: Class.empty,
		onPrevious: Class.empty,
		onNext: Class.empty
	},

	initialize: function(container, options) {

		this.setOptions(options);
		this.container = $(container);
		this.widthVisibleWindow = this.container.parentNode.clientWidth;
		this.widthContainerWindow = this.container.clientWidth;
		this.leftShift = 0;

		this.currentPage = 0;
		this.pageSize = 420;
		this.pageCount = 4;
		/*this.pageSize = this.widthContainerWindow - this.widthVisibleWindow;
		if (this.pageSize > this.widthVisibleWindow)
		this.pageSize = this.widthVisibleWindow
		*/
		if (this.options.idPrevious != "undefined" && $(this.options.idPrevious))
			$(this.options.idPrevious).addEvent("click", function(event) {
				new Event(event).stop();
				this._previous();
				this.fireEvent("onClickPrevious", this, 20);
			} .bind(this)); // check if value is not "undefined" before start search the dom with $()
		if (this.options.idNext != "undefined" && $(this.options.idNext) && this.widthContainerWindow > this.widthVisibleWindow) {
			$(this.options.idNext).style.display = "";
			$(this.options.idNext).addEvent("click", function(event) {
				new Event(event).stop();
				this._next();
				this.fireEvent("onClickNext", this, 20);
			} .bind(this));
		}
		var oAn = this.options.animation; // short hand
		this.fx = this.container.effects({ duration: oAn.duration, transition: oAn.transition, wait: false });
		this.atScreen = 0;
		this.container.setStyle(oAn.direction, 0);
	},

	_previous: function() {
		var begin = true;
		for (i = this.container.getElements("li").length - 1; i >= 0; i--) {
			var elem = this.container.getElements("li")[i];
			if (elem.offsetLeft < this.leftShift) {
				this.leftShift -= this.leftShift - elem.offsetLeft;
				if (i > 0)
					begin = false;
				break;
			}
		}
		$(this.options.idNext).style.display = "";
		if (begin)
			$(this.options.idPrevious).style.display = "none";
		this._animate(this.leftShift);
		//this.fireEvent("onPrevious", this, 20);
	},

	_next: function() {
		var end = true;
		var prev;
		for (i = 0; i < this.container.getElements("li").length; i++) {
			if ((this.container.getElements("li")[i].offsetLeft - this.leftShift) >= this.widthVisibleWindow - 18) {
				var stump = (prev.offsetLeft + prev.clientWidth) - this.leftShift - this.widthVisibleWindow + 18;
				this.leftShift += stump + this.container.getElements("li")[i].clientWidth;
				if (i < this.container.getElements("li").length - 1)
					end = false;
				break;
			}
			prev = this.container.getElements("li")[i];
		}
		$(this.options.idPrevious).style.display = "";
		if (end)
			$(this.options.idNext).style.display = "none";
		this._animate(this.leftShift);
		//this.fireEvent("onNext", this, 20);
	},

	_animate: function(a) {
		var that = this;
		that.fx.start({ "left": -this.leftShift });
	}
});
iCarousel.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn)
iCarousel.implement(new Options);// Implements setOptions(defaults, options)