/**
 * @author Twusk
 * @copyright 2009-2010
 */

// CONFIG
var effect = 'continuousmove';
var timedPath = false;
var constantDir = true;
var initialDir = 2;

var timer = 40;
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);

	document.body.onclick = celloration.click;
};

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) {
		var nx = isNaN(nx) ? c.x : nx;
		var ny = isNaN(ny) ? c.y : ny;
		var nw = isNaN(nw) ? c.w : nw;
		var nh = isNaN(nh) ? c.h : nh;
		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];

			c.clicked = c.o.className == 'clicked';

			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;
				case 'continuousmove':
					//var mod = 1;
					//var dx = (Math.random() * mod) * vx;
					//var dy = (Math.random() * mod) * vy;
					var axy = 1;
					var dx = axy * vx;
					var dy = axy * vy;
					var nx = c.x + dx;
					var ny = c.y + dy;
					var nw = c.w;
					var nh = c.h;
					if (typeof(c.a) == "number") {
						var am = c.a < 0 ? -1 : 1;
						if (c.a * am <= axy) {
							axy = c.a * am;
							c.a = null;
						} else {
							c.a = c.a - (am * axy);
						}
						if (c.a > 0) { // increase in size
							nx = c.x - (c.d == 3 ? am * axy : 0);
							ny = c.y - (c.d == 0 ? am * axy : 0);
						} else if (c.a < 0) { // decrease in size
							nx = c.x - (c.d == 1 ? am * axy : 0);
							ny = c.y - (c.d == 2 ? am * axy : 0);
						}
						nw = c.w + (c.d == 1 || c.d == 3 ? am * axy : 0);
						nh = c.h + (c.d == 0 || c.d == 2 ? am * axy : 0);
					}
					if (this.updatePos(c, nx, ny, nw, nh) != 3) { // Cannot move up or down
						if (c.c) {
							if (c.a === null) {
								document.getElementById('cells').removeChild(c.o);
								this.celldata.splice(i, 1);
							}
						} else {
							c.ow = c.w;
							c.oh = c.h;
							c.a = c.d == 0 || c.d == 2 ? -c.h : -c.w;
							c.c = true;
							var nc = {
								o: document.createElement('div'),
								x: vx == 0 ? c.x : (c.d == 1 ? 0 : pageWidth - 1),
								y: vy == 0 ? c.y : (c.d == 2 ? 0 : pageHeight - 1),
								ow: c.w,
								oh: c.h,
								w: vx == 0 ? c.w : 1,
								h: vy == 0 ? c.h : 1,
								a: -c.a - 1,
								d: c.d,
								t: c.t
							};
							this.celldata.push(nc);
							nc.o.style.width = nc.w == 0 ? 0 : nc.w + 'px';
							nc.o.style.height = nc.h == 0 ? 0 : nc.h + 'px';
							nc.o.style.left = nc.x == 0 ? 0 : nc.x + 'px';
							nc.o.style.top = nc.y == 0 ? 0 : nc.y + 'px';
							nc.o.style.backgroundColor = c.o.style.backgroundColor;
							document.getElementById('cells').appendChild(nc.o);
						}
					}
					break;
			}
		}
	},
	click: function(e) {
		var e = e ? e : window.event;
		var t = e.target ? e.target : e.srcElement;
		if (t.tagName == 'A') { return; }
		//if (t.parentNode.id == 'cells') {
			for (var i=0; i<celloration.celldata.length; i++) {
				var c = celloration.celldata[i];
				if (c.a) {
					c.c = c.c ? false : true;
					if (c.a < 0) { c.a = c.a + (c.d == 0 || c.d == 2 ? c.oh : c.ow); }
					else { c.a = (c.d == 0 || c.d == 2 ? -c.h : -c.w); }
				}
				c.d = (c.d + 2) % 4;
			}
			//t.className = 'clicked';
		//}
	}
};
