// JavaScript Document
if(typeof(AC)=="undefined")AC={};

AC.ColorPicker=Class.create();
AC.ColorPicker.prototype={
	colors: null,
	bodyEl: null,
	currentColor: null,
	
	initialize: function(container, colorList, initial, colors, options) {
		this.initialColor = initial;
		this.colors = colors;
		this.colorList = colorList;
		this.container = $(container);
		this.options = options || {};
		
		this.addPickerEvents();
		
		if (this.initialColor) {
			var id = 'cs-' + this.initialColor;
			this.swapColor($(id));
		}
		
	},
	
	addPickerEvents: function() {
		this.colorList.each(function(colorLink, linkIndex) {
			$(colorLink).observe('click', function(evt) {
				Event.stop(evt);
				var colorLink = Event.element(evt);
				this.swapColor(colorLink);
			}.bind(this))
		}.bind(this));
	},
	
	swapColor: function(colorLink) {
		var colorClassName = colorLink.id;
		var newColor = colorClassName.split('cs-')[1];
		
		if (this.options.onColorChange && typeof(this.options.onColorChange) == "function") {
			this.options.onColorChange(newColor);
		}
		
		if(newColor != this.currentColor) {
			this.container.toggleClassName(this.currentColor);
			this.container.toggleClassName(newColor);
			this.currentColor = newColor;
		}
	}

}
