Hsa.createCarousels = function() {
    jQuery(".js-carousel").each(function(){
        new Hsa.carousel(this);
    });
}

Hsa.carousel = function(carousel) {

    this.carouselElem = carousel;
    this.contentWrapperElem = jQuery(this.carouselElem).find(".carousel-content-wrapper");
	this.itemsLength = 0;
	this.allItemsWidth = 0;
	this.currentItemIndex = 0;
	this.previousItemIndex = -1;
	this.nextItemIndex = 1;
	this.STEP_WIDTH = 142;
	this.STEP_WIDE_WIDTH = 284;

	this.init = function(){

		this.itemsLength = jQuery(this.carouselElem).find(".carousel-content").length;

        if(this.itemsLength > 4){
            var controls = '<a class="carousel-prev" href="#">&lt; edellinen</a><a class="carousel-next" href="#">seuraava &gt;</a>';
            jQuery(this.carouselElem).append(controls);
			
			//clone all items to the beginning and end for looping
			var allItems = jQuery(this.carouselElem).find(".carousel-content");
			allItems.clone().appendTo( this.contentWrapperElem );
			allItems.clone().prependTo( this.contentWrapperElem );
			
			//move to first "real" item and adjust the width of wrapper based on the amount of child elements
			this.allItemsWidth = ( this.STEP_WIDTH * (this.itemsLength - 1) + this.STEP_WIDE_WIDTH );
			jQuery(this.contentWrapperElem).css({'left':'-'+ this.allItemsWidth +'px', 'width': this.allItemsWidth * 3 + 'px'})
			
			this.currentItemIndex = 0;
			
				this.bindEvents();
        }

	}

	this.bindEvents = function(){

        var self = this;

		jQuery(this.carouselElem).find(".carousel-prev").click(
			function (e) {
				e.preventDefault();
				self.switchToPreviousItem();
			}
		);
		jQuery(this.carouselElem).find(".carousel-next").click(
			function (e) {
				e.preventDefault();
				self.switchToNextItem();
			}
		);
	}

	this.switchToPreviousItem = function (){
		if( this.currentItemIndex == 2 || this.currentItemIndex == (- this.itemsLength + 1) ){
				this.previousItemIndex = this.currentItemIndex-2; //for the first (wide) item
		} else {
				this.previousItemIndex = this.currentItemIndex-1;
		}
		this.toggleItem( this.previousItemIndex );
	}

	this.switchToNextItem = function (){
		if( this.currentItemIndex == ( this.itemsLength - 4) || this.currentItemIndex == (- this.itemsLength + 1) ){
				this.nextItemIndex = this.currentItemIndex+2;
		} else {
				this.nextItemIndex = this.currentItemIndex+1;
		}
		this.toggleItem( this.nextItemIndex );
	}

	this.toggleItem = function(itemIndex){
		//console.log(itemIndex + "/" + this.itemsLength + " - " + (- this.STEP_WIDTH * itemIndex) );
		var self = this;
		var leftCoordinate = - this.allItemsWidth - (this.STEP_WIDTH * itemIndex );
		
		jQuery(this.contentWrapperElem).animate({
				left: leftCoordinate
		}, 400, 'swing', function (){
				//animation complete
				//console.log("animaatio valmis, indeksi: " + itemIndex);
				if( itemIndex == ( - self.itemsLength - 1 )  || itemIndex == ( self.itemsLength + 1 ) ){
						jQuery(self.contentWrapperElem).css('left','-'+ self.allItemsWidth +'px')
						self.currentItemIndex = 0;
				} else {
						self.currentItemIndex = itemIndex;
				}
		});
		
	}

    this.init();

}

jQuery(function(){
		Hsa.createCarousels();
});

