/*
	Author - http://asapdesign.de/index.php?page=Projects&projid=1
*/
Color.implement({
	changeBrightness: function(value) {
		return new Color([this.hsb[0], this.hsb[1], this.hsb[2]+value], 'hsb');
	}
});

// my first class
var PulsateFx = new Class({
	options: {
		pulselength:250,
		pulses:3,
		transitionIn:Fx.Transitions.Cubic.easeIn,
		transitionOut:Fx.Transitions.Quad.easeOut,
		toColor:false, toAlpha:.8
	},
	initialize: function(pulsatingElement, options) {
		this.setOptions(options);
		var originalBgColor = $(pulsatingElement).getStyle('background-color');
		var fxIn = $(pulsatingElement).effects({
			duration:this.options.pulselength,
			transition:this.options.transitionIn
		});
		var fxOut = $(pulsatingElement).effects({
			duration:this.options.pulselength,
			transition:this.options.transitionOut
		});
		this.pulsate(fxIn, fxOut, originalBgColor);
	},
	pulsate: function(fxIn, fxOut, originalBgColor) {
		var extraTime = 10;
		var p = function() {
			if (this.options.toColor != false) {
				if (this.options.toColor == 'lighter' || this.options.toColor == 'darker') {
					brighterBgColor = new Color(originalBgColor);
					brighterBgColor = brighterBgColor.changeBrightness(this.options.toColor == 'lighter' ? 10 : -10).rgbToHex(false);
					fxIn.start({ 'background-color': brighterBgColor }).chain(function() {
						fxOut.start({ 'background-color': originalBgColor });
					});
				} else {
					fxIn.start({ 'background-color': this.options.toColor }).chain(function() {
						fxOut.start({ 'background-color': originalBgColor });
					});
				}
			} else {
				fxIn.start({ 'opacity':this.options.toAlpha }).chain(function() {
					fxOut.start({ 'opacity':1 });
				});
			}
		}.bind(this).periodical(this.options.pulselength * 2 + extraTime);
		(function() { $clear(p); }).delay(this.options.pulses * 2 * this.options.pulselength + this.options.pulselength + (this.options.pulses * extraTime));
	}
});
PulsateFx.implement(new Options, new Events);