// Init Menu
var FaderClass = new Class({

	initialize: function(options){
	
		// Get options from the passed options object
		this.Container = $(options.container);
		this.Rotate = options.rotate;
		this.elements = options.elements;
		this.Interval = options.interval;
		this.RotateLocation = 0;
		this.fadeLength = options.fadeLength;
		
		window.addEvents({
			"domready" : function(){
			
				// Get the container location, i.e. x, y and also include scroll height
				this.getContainerLocation();
				if(this.Rotate) {
					this.startRotation();		
				}
			
			}.bind(this),
			"resize" : function(){
		
				this.getContainerLocation();
		
			}.bind(this),
			"scroll" : function(){
		
				this.getContainerLocation();
		
			}.bind(this)
		});			
		
	},
	
	getContainerLocation : function() {
		this.ContainerLocation = this.Container.getCoordinates();
		this.ContaienrScroll = window.getScroll();
	},
	
	startRotation : function() {
		
		(function() {
		
			// Increment location
			if((this.RotateLocation+1)==this.elements.length){
				this.RotateLocation=0;
			} else {
				this.RotateLocation++;
			}
			
			// Make temp element and insert
			var NewImageDiv = this.elements[this.RotateLocation].object;
			NewImageDiv.setStyle('opacity', 0);
			NewImageDiv.injectInside(this.Container);

			
			// Fade in and then get rid of overlay temp element
			var myFadeFx = new Fx.Tween(NewImageDiv, {duration: this.fadeLength, onComplete : function() {
				// Delete old div's
				this.DeleteOld();			
			}.bind(this)});
			myFadeFx.start('opacity', 0, 1);
		
		}).periodical(this.Interval, this);
	
	},
	
	DeleteOld : function(iImage) {
		if(this.Container.getElements('div').length > 1) {
			this.Container.getFirst().dispose();
		}
	}
	
});
