var Cycle = function(options) {
  
  options = options || {};
  
  if(typeof options.container == 'undefined' || options.container.nodeName.toLowerCase() != 'ul') {
    throwError('Fatal error for options.container');
    return;
  }
  
  options.timer = typeof options.timer != 'undefined' ? options.timer : 7000;
  
  var container = options.container,
      items     = options.container.getChildren(),
      length    = options.container.getChildren().length,
      timeout   = null,
      paused    = 0,
      current   = 0
  
  /*
  ** Private methods.
  */
  
  function initStyles() {
    
    container.setStyles({
      'position' : 'relative'
    });
    
    items.setStyles({
      'position' : 'absolute',
      'top'      : 0,
      'left'     : 0
    });
    
    var length2 = length - 1;
    items.each(function(el, index) {
      el.setStyles({
        'z-index' : Math.abs(index - length2)
      });
    });
    
  };
  
  function paint() {
    
    var oldItem = items[current];
    current = (current + 1) < length ? current + 1 : 0;
    var newItem = items[current];
    
    var oldFx = new Fx.Morph(oldItem, { duration: 1000 });
    var newFx = new Fx.Morph(newItem);
    
    oldFx.addEvent('complete', function(e) {
      
      var others = items.filter(function(el) {
        return el.getStyle('z-index') < (length - 1);
      });
      
      others.each(function(el, index) {
        el.setStyle('z-index', +el.getStyle('z-index') + 1);
      });
      
      oldItem.setStyles({
        'opacity' : 1,
        'z-index' : 0
      });
      
    });
    
    oldFx.start({
      'opacity': 0
    });
    
    
    
    // containers.cycle.children(':visible').animate(styles.hidden).css('zIndex', 0);
    //     containers.cycle.children().eq(items.current).animate(styles.visible).css('zIndex', 1);
    
    // set active class on controls
    /*if(options.controls) {
      $(containers.controls).find('ol li').removeClass('active');
      $(containers.controls).find('ol li').eq(items.current).addClass('active');
    }*/
    
  }
  
  function play() {
    
    clearInterval(timeout);
    paused = 0;
    
    /*if(options.controls) {
      containers.controls.find('.pause').removeClass('active');
    }
    if(options.pausedOverlay) {
      containers.pausedOverlay.animate({ opacity: 0 });
    }*/
    
    timeout = setInterval(function() {
      paint();
    }, options.timer);
    
  }
  
  function throwError(msg) {
    
    console.log(msg);
    
  };
  
  
  /*
  ** Public methods.
  */
  
  this.fixDimensions = function() {
    
    var height = 0;
    items.each(function(el, index) {
      var h = parseInt(el.getStyle('height'));
      height = h > height ? h : height;
    });
    
    container.setStyles({
      'height' : height + 'px'
    });
    
  }
  
  
  /*
  ** Init.
  */
  
  function init() {
    
    /* Apply initial styling. */
    initStyles();
    
    /* Play. */
    play();
    
  };
  
  /* Set things in motion. */
  init();
  
  
}