var Rainbow = new Class({
	initialize: function() {
		// Set veriables
		this.morph = new Array();
		this.count = new Array();
		this.pos = 0;
		// Set colors
		this.colors1 = new Array(
			[196, 30, 36], [135, 20, 23], [195, 22, 38], [178, 16, 43], [217, 22, 64], 
			[161, 25, 58], [219, 20, 75], [176, 20, 66], [147, 0, 53], [175, 15, 83], 
			[149, 0, 61], [174, 36, 91], [119, 22, 60], [208, 1, 106], [159, 0, 94], 
			[194, 21, 138], [155, 24, 128], [134, 19, 118], [98, 5, 95], [111, 44, 144], 
			[84, 39, 133], [59, 35, 123], [27, 20, 100], [12, 77, 162], [0, 82, 156], 
			[20, 50, 92], [0, 113, 187], [0, 71, 130], [0, 82, 133], [0, 118, 183], 
			[0, 122, 178], [0, 169, 232], [0, 141, 170], [0, 113, 134], [0, 149, 158],
			[0, 135, 137], [0, 109, 101], [0, 156, 118], [0, 142, 93], [0, 129, 73], 
			[0, 105, 50], [31, 163, 69], [64, 153, 59], [83, 145, 51], [80, 120, 35]
		);
		
		this.colors2 = new Array(
			[138, 93, 25], [221, 117, 29], [147, 82, 36], [203, 99, 35], [183, 85, 24], 
			[221, 82, 31], [201, 53, 30], [127, 44, 27], [178, 53, 31], [148, 9, 13], 
			[166, 28, 33], [226, 26, 45], [207, 21, 51], [157, 15, 40], [195, 34, 72], 
			[134, 22, 52], [214, 29, 81], [194, 9, 73], [216, 25, 100], [195, 4, 82], 
			[158, 0, 68], [144, 33, 74], [237, 6, 119], [189, 6, 113], [158, 0, 93], 
			[171 ,32, 141], [148, 27, 129], [121, 24, 119], [86, 9, 95], [90, 45, 142], 
			[63, 40, 131], [36, 35, 119], [15, 33, 101], [0, 86, 163], [26, 58, 102], 
			[0, 62, 115], [1, 79, 140], [0, 98, 155], [0, 121, 188], [0, 125, 183], 
			[0, 116, 156], [0, 150, 182], [0, 131, 156], [0, 153, 163], [0, 140, 142]
		);
		
		// Extend rainbow (should make a rainbow with a width of 2250px)
		this.colors1.extend(this.colors1);
		this.colors2.extend(this.colors2);
		
		// Set colors and prepare tween effects
		this.prepare();
	},
	
	prepare: function() {
		var obj = this;
		// Place colors
		this.colors1.each(function(value, key) {
			// Create color element
			var div = new Element('div', {'styles': {'background-color': 'rgb('+value+')', 'left': (key*25)}}).inject($(document.body).getElement('div.rainbow'));
			// Prepare tween
			obj.morph.extend([new Fx.Tween(div, {duration: 500})]);
		});
		// Start change if billboard is not present
		if (typeof billboard === 'undefined')
			obj.change.delay(5000, this);
	},
	
	change: function() {
		var obj = this;
		// End of loop
		if (!this.count.length) {
			this.pos++;
			switch(this.pos) {
				case 1: this.newcolors = $A(this.colors1).reverse(); break;
				case 2: this.newcolors = $A(this.colors2); break;
				case 3: this.newcolors = $A(this.colors2).reverse(); break;
				case 4: this.newcolors = $A(this.colors1); this.pos=0; break;
			}
			// Reset keys
			this.colors1.each(function(value, key) { obj.count.extend([key]); });
		}
		// Get a random number in the first 5 remaining
		var rdm = $random(0, (this.count.length>9 ? 9 : this.count.length-1));
		// Alter color
		this.morph[this.count[rdm]].start('background-color', 'rbg('+this.newcolors[this.count[rdm]]+')');
		// Remove key
		this.count.splice(rdm, 1);
		// Continue loop
		if (this.count.length) this.change.delay(20, this);
		else if (typeof billboard === 'undefined') this.change.delay(10000, this);
	}
});

var rainbow;
window.addEvent('domready', function() { rainbow = new Rainbow(); });
