/**
 * @author Twusk
 * @copyright 2009-2010
 */

// CONFIG
var effect = 'fall';
var timedPath = false;
var constantDir = true;
var initialDir = 2;

var timer = 2;
var pageWidth = 640;
var pageHeight = 480;
var maxWidth = 640;
var maxHeight = 480;
var minMoveTime = 500;
var maxMoveTime = 2500;

var timeMod = 1 / timer;

window.onload = function(){
	celloration.init();
	setInterval('celloration.cellerate()', timer);
};

var celloration = {
	celldata: [],
	init: function() {
		var cells = document.getElementById('cells').childNodes;
		for (var i=0; i<cells.length; i++) {
			var o, x, y, w, h;
			o = cells[i];
			x = parseInt(o.style.left);
			y = parseInt(o.style.top);
			w = parseInt(o.style.width);
			h = parseInt(o.style.height);
			this.celldata[i] = {o:o,x:x,y:y,w:w,h:h,d:initialDir,t:0};
		}
	},
	checkBoundry: function(x, y, w, h) {
		var b = 0;
		if (x >= 0 && x + w <= pageWidth && w >= 1 && w <= maxWidth) b += 1;
		if (y >= 0 && y + h <= pageHeight && h >= 1 && h <= maxHeight) b += 2;
		return b;
	},
	updatePos: function(c, nx, ny, nw, nh) {
		nx = nx ? nx : c.x;
		ny = ny ? ny : c.y;
		nw = nw ? nw : c.w;
		nh = nh ? nh : c.h;
		var b = this.checkBoundry(nx, ny, nw, nh);
		if (!b) {
			c.t = 0;
			return 0;
		}
		if (b & 1) {
			c.x = nx;
			c.o.style.left = Math.floor(nx) + 'px';
			c.w = nw;
			c.o.style.width = Math.floor(nw) + 'px';
		}
		if (b & 2) {
			c.y = ny;
			c.o.style.top = Math.floor(ny) + 'px';
			c.h = nh;
			c.o.style.height = Math.floor(nh) + 'px';
		}
		return b;
	},
	cellerate: function() {
		for (var i=0; i<this.celldata.length; i++) {
			var c = this.celldata[i];

			var vx = (c.d==1 ? 1 : (c.d==3 ? -1 : 0));
			var vy = (c.d==2 ? 1 : (c.d==0 ? -1 : 0));

			if (timedPath) {
				if (c.t > 0) { c.t--; }
				else { c.t = (Math.floor(Math.random() * (maxMoveTime - minMoveTime)) + minMoveTime) * timeMod; }
			}
			if (!constantDir && c.t <= 0) {
				c.d = Math.floor(Math.random() * 4);
			}

			switch (effect) {
				case 'wetpaint':
					var nh = c.h + Math.round(Math.random() - .40);
					if (nh + c.y < pageHeight)
						c.o.style.height = (nh) + 'px';
					break;
				case 'wiggle':
					var mod = .25;
					var dx = (Math.random() * mod) * vx;
					var dy = (Math.random() * mod) * vy;
					var nx = c.x + cx;
					var ny = c.y + cy;
					this.updatePos(c, nx, ny, c.w, c.h);
					break;
				case 'reshape':
					var mod = 1;
					var dx = (Math.random() * mod) * vx;
					var dy = (Math.random() * mod) * vy;
					var nx = (Math.round(Math.random()) ? c.x - dx : c.x);
					var ny = (Math.round(Math.random()) ? c.y - dy : c.y);
					var nw = c.w + dx;
					var nh = c.h + dy;
					this.updatePos(c, nx, ny, nw, nh);
					break;
				case 'fall':
					var mod = 1;
					var dx = (Math.random() * mod) * vx;
					var dy = (Math.random() * mod) * vy;
					if (!(this.updatePos(c, c.x + dx, c.y + dy, c.w, c.h) & 2)) { // Cannot move down
						//var nh = h - cy;
						//if (!(this.updatePos(c, c.x, ny, c.w, nh) & 2)) { // Cannot resize
							//c.o.parentNode.removeChild(c.o);
							//this.celldata.splice(i, 1);
						//}
					}
					break;
			}
		}
	}
};