
var myCarousels = new Array();

function carousel( imgsArr, newBlockNr ) {
  var objInstance = this;

  this.imgs       = imgsArr;
  this.blockNr    = newBlockNr;
  this.curImg     = 0;
  this.intervalId = 0;

  this.showPrevImg = function() {
    // Reset autoplay
    objInstance.resetAutoPlay();

    // Add one to current image
    objInstance.curImg--;

    // Check if current image isnt higher then max image
    if( objInstance.curImg < 0 ) {
      objInstance.curImg = (objInstance.imgs.length-1);
    }

    // Go
    objInstance.fadeImage();
  };

  this.showNextImg = function() {

    // Reset autoplay
    objInstance.resetAutoPlay();

    // Add one to current image
    objInstance.curImg++;

    // Check if current image isnt higher then max image
    if( (objInstance.curImg+1) > objInstance.imgs.length) {
      objInstance.curImg = 0;
    }

    // Go
    objInstance.fadeImage();
  };

  this.resetAutoPlay = function() {
    clearInterval(objInstance.intervalId);
    objInstance.intervalId = setInterval(objInstance.showNextImg, 4000);
  };

  this.fadeImage = function() {
    // Place the next image
    $('#block'+objInstance.blockNr)
    .find('.box')
    .find('a')
    .append('<img />')
    .find(':last-child')
    .attr('src', objInstance.imgs[objInstance.curImg][2])
    .css('display', 'none');

    // Fade to next image please
    $('#block'+objInstance.blockNr).find('.box').find('a').find(':first-child').stop().fadeOut(1000, function() { $(this).remove(); });
    $('#block'+objInstance.blockNr).find('.box').find('a').find(':last-child').stop().fadeIn(1000);

    // Change the title
    $('#block'+objInstance.blockNr).find('.box').find('.actionHeader').find('h3').html(objInstance.imgs[objInstance.curImg][0]);

    // Change the title url
    //$('.blockTitle').find('a').attr("href", this.imgs[this.curImg][1]);

    // Change the image url
    $('#block'+objInstance.blockNr).find('.box').find('a').attr("href", objInstance.imgs[objInstance.curImg][1]);

    $('#block'+objInstance.blockNr).find('.box').find('.actionHeader').find('h3').removeClass("sIFR-replaced")

    // Update sIFR
    sIFR.replace(din, {
      selector: 'h3.carousel',
      css: '.sIFR-root { color: #ffffff; }',
      wmode: 'transparent'
    });
  }

  // Start
  this.resetAutoPlay();

}