var kk = {};
kk.Innerfade = new Class({
  Implements: [Chain, Events, Options],
  
  options: {
			timeout: 2000,
			type: 'sequence',
			containerheight: 'auto',
			transition: Fx.Transitions.linear,
			duration: 500
	},

	initialize: function(selector, options){
		var params = $A(arguments).associate({'options': 'object', 'element': true});
		this.element = this.element || $E(params.element);
		this.setOptions(params.options);
    
    this.element.getParent().setStyle('position', 'relative');
    this.element.setStyle('height', this.options.containerheight);
    
    
    this.elements = $(this.element).getElements('li');
    $each(this.elements, function(el, i, a){
			this.build($(el), i);
	  }, this);
	  
	  var current = 0;
    var last = 0;
    
	  if (this.elements.length > 1) {
  		if ( this.options.type == 'sequence' ) {
    		current = 1;
        
        this.elements[current].setStyle('visibility', 'visible');
  		}
  		else if ( settings.type == 'random' ) {
  			current = $random(0, this.elements.length-1);
        do { last = $random(0, this.elements.length-1); } while (last == current);
  		}
  		else {
  			alert('Type must either be \'sequence\' or \'random\'');
  		}
    }
    
    this.currentidx = last;
    
    this.lastdelaydesc = this.next.delay(this.options.timeout, this, [current, last]);
		this.elements[last].setStyle('visibility', 'visible');
  	  
		if (this.options.initialize) this.options.initialize.call(this);
	},
	
	build: function(el, i) {
  	el.setStyle('z-index', this.elements.length - i).setStyle('position', 'absolute').setStyle('visibility', 'hidden');
    el.fader = el.fader || el.effect('opacity', {duration: this.options.duration,transition: this.options.transition, wait: false});    
  },
  
  fadeout: function(el) {
    el.fader.start(el.getStyle('opacity'), 0);
  },
  
  fadein: function(el) {
    el.fader.start(el.getStyle('opacity'), 1);
  },
  
  getcurrent: function() {
    return this.currentidx;
  },
  
  switchto: function(idx) {
    this.next(idx, this.getcurrent());
  },

	next: function (current, last) {
	  this.currentidx = current;
	  
    if (this.lastdelaydesc)
      clearTimeout(this.lastdelaydesc); 
	  
	  if (current != last) {
      this.fadein(this.elements[current]);
      this.fadeout(this.elements[last]);
    }
    
    if ( this.elements.length > 1) {
      if ( this.options.type == 'sequence' ) {
    		if ( ( current + 1 ) < this.elements.length ) {
    			current = current + 1;
    			last = current - 1;
    		}
    		else {
    			current = 0;
    			last = this.elements.length - 1;
    		}
    	}
    	else if ( this.options.type == 'random' ) {
    		last = current;
  
    		while (	current == last ) {
    			current = $random(0, this.elements.length-1)
    		}
    	}
    	else {
    		alert('Type must either be \'sequence\' or \'random\'');
    	}
  	}
  	else {
  	  current = 0;
  	  last = 0;
  	}
  	  
  	this.lastdelaydesc = this.next.delay(this.options.timeout, this, [current, last]);    
  }
});
// kk.Innerfade.implement(new Options);

kk.Innerfades = {
  initRotator: function(uid, atimeout, adisplay, aheight) {
    this[uid] = new kk.Innerfade('ul#'+uid, {timeout: atimeout, type: adisplay, containerheight: aheight});
  },
  
  switchImages: function(uid, idx) {
  if ($defined(this[uid]))
    this[uid].switchto(idx);
}
};
