  /**
 * Caroussel 
 * Rotator, between all elements in its array of elements
 */
var Caroussel = new Class({
	
	/**
	 * Elementstack
	 */
	elements: [],
	
	/**
	 * Caroussel-options
	 */
	options: {
		'timeout' : 5000
	},
	
	/*
	 * Is the caroussel running?
	 */
	running: false,
	
	/**
	 * Current element
	 */
	activeEl: 0,
	
	/**
	 * Init the class
	 */
	init: function(options){
		this.setOptions(options);
	},
	
	/**
	 * Add an element to the stack
	 */
	push: function(id){
		if ($(id)){
			var found = false;
			for (i = 0; i<this.elements.length; i++){
				if (this.elements[i] == id){
					found = true;
				}
			}
			if (!found){
				this.elements.push(id);
				$(id).setStyle('display', 'none');
			}
		}
	},
	
	/**
	 * Start running the rotation 
	 */
	startRunning: function (){
		for (i = 0 ; i < (this.elements.length - 1); i++){
			$(this.elements[i]).setStyle('display', 'none');
		}
		this.activeEl = 0;
		this.running = true;
		this.exec();
	},
	
	/**
	 * Stop the process 
	 */
	stopRunning: function(){
		this.running = false;
	},
	
	/**
	 * If the process is not stopped using the stopRunning-method, 
	 * it will show the next item.
	 */
	exec: function(){
		if (this.running){
			$(this.elements[this.activeEl]).setStyle('display', 'none');
			this.activeEl = (this.activeEl == (this.elements.length -1)) ? 0 : (this.activeEl + 1);
			$(this.elements[this.activeEl]).setStyle('display', 'block');
			setTimeout(this.exec.bind(this), this.options.timeout);
		}
	}
});
			
Caroussel.implement(new Options);

/**
 * Stack for all Caroussels
 */
var CarousselCollection = new Class({
	
	/**
	 * Associative array of caroussels and their names
	 */
	elements: [],
	
	/**
	 * Init the class
	 */
	init: function(){
	
	},
	
	/**
	 * If the stackName does not exist, a Caroussel is instantiated and added to the stack 
	 * Then, the id is added to that Caroussels' elementstack
	 *
	 * @param string stackName
	 * @param string id
	 */
	push: function (stackName, id){
		if (!$chk(this.elements[stackName])){
			this.elements[stackName] = new Caroussel();
		}
		this.elements[stackName].push(id);
	},
	
	/**
	 * Start running Caroussel stackName. If no stackName is given, all caroussels are started
	 * 
	 * @param string stackName
	 */
	startRunning: function(stackName){
		
		if (!stackName){
			for (i in this.elements){
				if ($type(this.elements[i]) ==  'object'){
					if(this.elements[i].elements != undefined){
						if(this.elements[i].elements[0].substr(0,6) == 'banner' || this.elements[i].elements[0].substr(0,5) == 'quote'){
							this.elements[i].startRunning();
						}
					}
				}
			}
		} else {
			if ($chk(this.elements[stackName]) && $type(this.elements[stackName]) == 'Caroussel'){
				this.elements[stackName].startRunning();
			}
		}
	},
	
	/**
	 * Stop running Caroussel stackName. If no stackName is given, all caroussels are stopped
	 * 
	 * @param string stackName
	 */
	stopRunning: function(stackName){
		if (!stackName){
			for (i in this.elements){
				if ($type(this.elements[i]) ==  'object'){
					this.elements[i].stopRunning();
				}
			}
		} else {
			if ($chk(this.elements[stackName]) && $type(this.elements[stackName]) == 'Caroussel'){
				this.elements[stackName].stopRunning();
			}
		}
	}
});

var ccol = new CarousselCollection();