if(typeof(AC)=="undefined")AC={};

AC.RandomColorPicker=Class.create();
AC.RandomColorPicker.prototype={
	colors: null,
	bodyEl: null,
	currentColor: null,
	
	initialize: function() {
		this.colors = new Array('pink', 'blue', 'green', 'orange', 'silver');
		this.bodyEl = document.getElementsByTagName('BODY')[0];

		this.randomColor();
		
		this.addPickerEvents();
	},
	
	randomColor: function() {
		var randNum = Math.floor(Math.random()*(this.colors.length-2));
		this.currentColor = this.colors[randNum];
		Element.addClassName(this.bodyEl, this.currentColor);
	},
	
	addPickerEvents: function() {
		var colorPicker = document.getElementsByClassName('colorpicker')[0];
		var colorLinks = $A(colorPicker.getElementsByTagName('LI'));
		colorLinks.each(function(colorLink, linkIndex) {
			colorLink.onclick = this.swapColor.bindAsEventListener(this);
		}.bind(this));
	},
	
	swapColor: function(evt) {
		var colorLink = Event.element(evt);
		var colorClassName = colorLink.className;
		var newColor = colorClassName.split('cp-')[1];
		
		if(newColor != this.currentColor) {
			Element.toggleClassName(this.bodyEl, this.currentColor);
			Element.toggleClassName(this.bodyEl, newColor);
			this.currentColor = newColor;
		}
	}

}

function initRandomColor() {new AC.RandomColorPicker();}
Event.observe(window, 'load', initRandomColor);
