var Carousel = function() {}

Carousel.prototype.initEventNode = function(leftButton, rightButton) {
	this.leftButton = leftButton;
	this.rightButton = rightButton;	
}

Carousel.prototype.initMoveNode = function(cNode, ulId, textCNode) {
	this.cNode = cNode;		
	this.textCNode = textCNode;
	this.ulId = ulId;
}

Carousel.prototype.initNodeParams = function(lenght, fotoAmount, viewAmount, leftLimit, rightLimit, step, currentPosition) {
	this.lenght = lenght;
	this.fotoAmount = fotoAmount;
	this.viewAmount = viewAmount;
	this.leftLimit = leftLimit;
	this.rightLimit = fotoAmount - rightLimit;
	this.step = step;
	this.currentPosition = currentPosition;
	
	this.i = 0;
	this.j = 0;
}

Carousel.prototype.setTitle = function(string) {
	this.string = string;
}

Carousel.prototype.appendTitle = function(string) {
	$(this.textCNode).empty().append(this.string[this.currentPosition + 1]);	
}

Carousel.prototype.run = function() {

	// Scale first element.
	$(this.cNode + 1).animate({"marginTop":"-=20px", "width":"+=" + this.step + "px", "opacity":"1"}, "slow");	
	
	this.appendTitle();	

	var object = this;
	
	$(this.leftButton).click(function() {
	    object.moveLeft();
	})

	$(this.rightButton).click(function() {
	    object.moveRight();
	})

}

Carousel.prototype.moveRight = function() {
    if (this.currentPosition < this.rightLimit) {
		this.i = this.currentPosition + 3;
		this.j = this.currentPosition + 2;

		// Move list.
		$(this.ulId).animate({"left":"-=" + this.lenght + "px"}, "slow");
 	    $(this.cNode + this.i).animate({"marginTop":"-=20px", "width":"+=" + this.step + "px", "opacity":"1"}, "slow");
		$(this.cNode + this.j).animate({"marginTop":"+=20px","width":"-=" + this.step + "px", "opacity":"0.6"}, "slow");

		this.currentPosition++;
        
		this.appendTitle();	
	} else {
		// Come back.
		$(this.ulId).animate({"left":"150px"}, "slow");

		// Scale last element.
		$(this.cNode + 1).animate({"marginTop":"-=20px", "width":"+=" + this.step + "px", "opacity":"1"}, "slow");

		// Scale first element.
		$(this.cNode + this.fotoAmount).animate({"marginTop":"+=20px", "width":"-=" + this.step + "px", "opacity":"0.6"}, "slow");

		this.currentPosition = -1;
		
		// Append text for first element.
		this.appendTitle();	
	}
}

Carousel.prototype.moveLeft = function() {
	if (this.currentPosition > this.leftLimit) {
		this.i = this.currentPosition + 2;
		this.j = this.currentPosition + 1;
		
		// Move list.
		$(this.ulId).animate({"left":"+=" + this.lenght + "px"}, "slow");

		$(this.cNode + this.i).animate({"marginTop":"+=20px", "width":"-=" + this.step + "px", "opacity":"0.6"}, "slow");
		$(this.cNode + this.j).animate({"marginTop":"-=20px","width":"+=" + this.step + "px", "opacity":"1"}, "slow");

		this.currentPosition--;
		
		this.appendTitle();
	} 
}

