Sanoma.Site.carousel = {

  currentItem : 0,

  timerId: 0,
  itemsLength: 0,
  nextItemIndex: 1,

  init: function() {
    if(jQuery('#fr_carousel').length == 0) { return; }
    this.bindEvents();

	this.itemsLength = jQuery("#fr_carouselContainer .fr_carouselContent").length;
	this.timerHandler();

	jQuery('#fr_carouselControls ul li:first a').addClass('active');
  },

  bindEvents: function() {
	jQuery("#fr_carouselControls ul a").one("click", function(){ //clear timer on first user click
		clearInterval (Sanoma.Site.carousel.timerId);
	});
    jQuery('#fr_carouselControls ul a').click(
      function(e) {
        e.preventDefault();
        jQuery('#fr_carouselControls ul a').removeClass('active');
        jQuery(this).addClass('active');
        var $index = jQuery('#fr_carouselControls ul a').index(this);
        Sanoma.Site.carousel.positionElement($index);
      }
    );
  },

  timerHandler: function (){
    if(this.itemsLength > 1){
      this.timerId = setInterval ( "Sanoma.Site.carousel.switchToNextTab()", 7000 );
    }
  },

  switchToNextTab: function (){

        jQuery('#fr_carouselControls ul a').removeClass('active');
        jQuery('#fr_carouselControls ul li:eq('+this.nextItemIndex+') a').addClass('active');


     this.positionElement(this.nextItemIndex);
    //this.toggleTab( jQuery('.carouselTeaser ul.steps li a:eq('+this.nextItemIndex+')') );
  },

  positionElement: function(index) {
    var $position = jQuery('#fr_carouselContainer').position();
    var top = $position.top;
    jQuery('#fr_carouselContainer').stop().animate({
      top: -354 * index + 'px'
    }, 800);

	if(this.nextItemIndex < (this.itemsLength-1)){
		this.nextItemIndex++;
	} else {
		this.nextItemIndex = 0;
	}

  }

}

