Array.prototype.random = function () {
	return this[Math.floor(Math.random() * this.length)];
}

RandomMovingBoxes = {
	create: function(amount, elements, options)
	{
		this.options = Object.extend({
			delay: true,
			delaymax: 40,
			startx: 10,
			starty: 10,
			yscope: 'bottom',
			endx: 1000,
			endy: 20,
			deviation: 100,
			effectduration: 90,
			effectdeviation: 20,
			container: false
		}, (options || {}));
		if (elements && amount > 0) {
			for (i = 0; i < amount; i++) {
				var el = $(elements.random()).clone(true).setStyle({position: 'absolute', left: this.options.startx + 'px', top: this.options.starty + 'px'}).hide();
				if (this.options.container) 
					this.options.container.insert(el);
				else
					document.body.appendChild(el);
				//alert((this.endx));// - (Math.random() * this.options.deviation)));
				var movingbox = new MovingBox(el, {
					x: (this.options.endx - (Math.random() * this.options.deviation).floor()),
					y: (this.options.endy - (Math.random() * this.options.deviation).floor()),
					duration: (this.options.effectduration - (Math.random() * this.options.effectdeviation).floor())
				});
				if (this.options.delay)
					movingbox.start.bind(movingbox).delay((Math.random() * this.options.delaymax).floor());
				else
					movingbox.start();
			}
		}
	},
	resizeContainer: function()
	{
		var viewport = document.viewport.getDimensions();
		//var height = ((viewport.width / this.dimensions.width) * this.dimensions.height);
		this.options.container.setStyle({height: viewport.height+'px'});
	}
};

MovingBox = Class.create();
MovingBox.prototype = {
	initialize: function(element, options)
	{
		this.options = Object.extend({
			x: 0,
			y: 0,
			duration: 60,
			startdelay: 0
		}, (options || {}));
		this.el = $(element);
		this.originalLeft = parseFloat(this.el.getStyle('left') || '0');
		this.originalTop  = parseFloat(this.el.getStyle('top')  || '0');
	},
	start: function()
	{
		//alert(this.el.identify());
		new Effect.Appear(this.el);
		new Effect.Wave(this.el, {x: this.options.x, y: this.options.y, mode: 'absolute', duration: this.options.duration, queue: { position: 'front', scope: this.el.identify() }});
		new Effect.Fade(this.el, {queue: {position: 'end', scope: this.el.identify()}, afterFinish: this.restart.bind(this)});
	},
	restart: function()
	{
		this.el.setStyle({top: this.originalTop+'px', left: this.originalLeft+'px'});
		this.start();
	}
};
