function ImageCarousel(elementID) {

  this.options = {
    SLIDE_TIMEOUT:             5,
    CLICK_TIMEOUT_INTERVAL:    20000,
    SINGLE_ITEM_WIDTH:         300,
    START_DELAY:               1000
  };

  var container = $(elementID);
  var previousButton = container.down('a.previous-item');
  var nextButton = container.down('a.next-item');
  var list = container.down('ul');
  this.items = list.childElements();
  
  if(this.items.length < 2) return;
  if(this.items.length < 3) { 
    for(var i = 0; i < 2; i++) {
      list.appendChild(list.appendChild(this.items[i].cloneNode(true)));
    };
  };

  this.isAnimating = false;
  this.allowSlide = true;
  this.pauseInterval = null;

  list.style.left = -300 + "px";

  this.pe = null;
  this.count = 0;
  var o = this;

  if(nextButton && previousButton) {
    nextButton.onclick = previousButton.onclick = function(event) {
      if(o.isAnimating == false) {
        clearInterval(o.pauseInterval);
        o.allowSlide = false;
        var e = event || window.event;
        var el = Element.extend(e.target || e.srcElement);
        o.stop();
        o.doSlide(el.hasClassName("next-item") ? 0 : 1);
        o.pauseInterval = setInterval(function() {
          o.allowSlide = true;
          o.doSlide();
          o.start();
        }, o.options.CLICK_TIMEOUT_INTERVAL);
      };
      return false;
    };
  };
  
  this.doSlide = function(direction) {
    clearInterval(this.pauseInterval);
    var d = direction || 0;
    var currentLeft = parseInt(list.style.left, 10);
    this.isAnimating = true;
    new Effect.Move(list, {
      x: (d < 1 ? -600 : 0),
      y: 0, 
      mode: 'absolute', 
      transition: Effect.Transitions.spring,
      afterFinish: this.onFinishAnim,
      duration: 1
    });
    o.count++;
  };

  this.onFinishAnim = function() {
    o.isAnimating = false;
    o.reset();
  };

  this.reset = function() {
    var offset = parseInt(list.style.left, 10);
    var indexLast = list.childElements().length - 1;
    if(offset == 0) {
      var itemToMove = list.childElements()[indexLast];
      list.insertBefore(itemToMove, list.childElements()[0]);
    } else if(offset == -600) {
      var itemToMove = list.childElements()[0];
      list.appendChild(itemToMove);
    };
    list.style.left = -300 + "px";
  };

  setTimeout(function() {
    o.doSlide();
    o.start();
  }, this.options.START_DELAY);
};

ImageCarousel.prototype.start = function() {
  var o = this;
  this.pe = new PeriodicalExecuter(function(pe) {
    o.doSlide();
  }, this.options.SLIDE_TIMEOUT);
};

ImageCarousel.prototype.stop = function() {
  if(this.pe) this.pe.stop();
  this.pe = null;
};

var cc;
var mbc;
Event.addBehavior({ 'div#channel-carousel' : function() { cc = new ImageCarousel(this.id) } });
Event.addBehavior({ 'div#micro-banner-carousel' : function() { mbc = new ImageCarousel(this.id) } });
