

		
	
		
	



createCarousels = function() {
    $(".carousel").each(function(){
        new carousel(this);
    });
}

carousel = function(carousel) {

    this.carouselElem = carousel;
	this.timerId = 0;
	this.itemsLength = 0;
	this.currentItemIndex = 0;
	this.previousItemIndex = 0;
	this.nextItemIndex = 1;

	this.init = function(){

		$(this.carouselElem).find(".carousel-content").hide(); //hide all content

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

		$(this.carouselElem).find(".carousel-content:first").show(); //show first
        if(this.itemsLength > 1){
            var controls = '<div class="carousel-nav clearfix"><!--a class="carousel-prev" href="#">&lt; edellinen</a><a class="carousel-next" href="#">seuraava &gt;</a--><div class="carousel-steps">';
            for(i=0; i < this.itemsLength; i++){
                controls += '<a href="#">'+(i+1)+'</a>'; 
            }
            controls += '</div></div>';
            $(this.carouselElem).append(controls);

		    $(this.carouselElem).find(".carousel-steps a:first").addClass('active');
            this.previousItemIndex = this.itemsLength - 1;

		    this.timerHandler();
        }

		this.bindEvents();

	}

	this.bindEvents = function(){

        var self = this;

		$(this.carouselElem).one("click", function(){ //clear timer on first user click
			clearInterval (self.timerId);
		});

		$(this.carouselElem).find(".carousel-steps a").click(
			function (e) {
				e.preventDefault();
                var stepList = $(this).parent().children();
                var index = $(stepList).index(this);
				self.toggleTab(index);
			}
		);
/*
		$(this.carouselElem).find(".carousel-prev").click(
			function (e) {
				e.preventDefault();
				self.switchToPreviousTab();
			}
		);
		$(this.carouselElem).find(".carousel-next").click(
			function (e) {
				e.preventDefault();
				self.switchToNextTab();
			}
		);
*/
	}

	this.timerHandler = function (){
        var self = this;
        this.timerId = setInterval ( function(){ self.switchToNextTab(); }, 5000 );
	}

/*
	this.switchToPreviousTab = function (){
		if(this.currentItemIndex > 0){
			this.previousItemIndex = this.currentItemIndex-1;
		} else {
			this.previousItemIndex = this.itemsLength-1;
		}
		this.toggleTab( this.previousItemIndex );
	}
*/

	this.switchToNextTab = function (){
		if(this.currentItemIndex < (this.itemsLength-1)){
			this.nextItemIndex = this.currentItemIndex+1;
		} else {
			this.nextItemIndex = 0;
		}
		this.toggleTab( this.nextItemIndex );
	}

	this.toggleTab = function(index){
		var contentsWrapper = $(this.carouselElem).find(".carousel-content-wrapper");
		$(this.carouselElem).find(".carousel-steps a").removeClass("active");
		$(this.carouselElem).find(".carousel-steps a:eq("+index+")").addClass("active");
		$(contentsWrapper).find(".carousel-content").hide();
		$(contentsWrapper).find(".carousel-content:eq("+index+")").fadeIn();
        this.currentItemIndex = index;
	}

    this.init();

}

