/**
 * @author Nicholas Wellcome
 * Chaotically Colorful Mosaic Mouse Trail
 * for use with a transparent cursor. 
 */
var initop = new Array();
var clicked = false;
var xpos = 0;
var ypos = 0;
var posx = 0;
var posy = 0;
var offsety;
var offsetx;
var arraypos = 0;
var xpixels = new Array();
var ypixels = new Array();
var docinit;
var tol = 10;
var boxnum;

function _hex(a,b){
	var c="";
	for(var d=0;d<b;d++)c+="0";
	c+=a.toString(16);
	return c.substr(c.length-b,b).toUpperCase();
}
function _rnd(a){
	return Math.floor(Math.random()*a);
}

function moveit(x) {
	document.getElementById(x).style.top = ypixels[x] + 20 + "px";
}
function updateboxnum() {
	for (var x=0;x<=boxnum;x++) {
		document.getElementById("body").removeChild(document.getElementById(x));
	}
	boxnum = document.getElementById("boxnum").value;
	arraypos = 0;
	for (var x=0;x<=boxnum;x++) {
		var newDiv = document.createElement("div");
			newDiv.id = x;
			newDiv.style.cssText = "overflow:hidden;background-color:#444;width:"+tol+"px;height:"+tol+"px;position:absolute;top:0px;left:0px;";
			if ((!document.all)&&(document.getElementById)){ newDiv.setAttribute("onmouseover","moveit("+x+");"); }    
			if ((document.all)&&(document.getElementById)){	newDiv["onmouseover"]=new Function("moveit("+x+");"); }
			document.getElementById("body").appendChild(newDiv);
	}
}
			
function doSomethingElse(e) {
	if (docinit==true) {	
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		if(posx>xpos+tol){xpos = xpos+tol;changer();}
		if(posx<xpos-tol){xpos = xpos-tol;changer();}
		if(posy>ypos+tol){ypos = ypos+tol;changer();}
		if(posy<ypos-tol){ypos = ypos-tol;changer();}
		if(clicked) {
			var mult = (parseInt(document.getElementById(clicked+'div').style.height)-parseInt(document.getElementById(clicked+'parent').style.height))/(parseInt(document.getElementById(clicked+'parent').style.height)-parseInt(document.getElementById(clicked+'scroll').style.height));
			if((posy-offsety>=parseInt(document.getElementById(clicked+'parent').style.top))&&(posy-offsety<=parseInt(document.getElementById(clicked+'parent').style.top)+parseInt(document.getElementById(clicked+'parent').style.height)-parseInt(document.getElementById(clicked+'scroll').style.height))) {
				document.getElementById(clicked+'scroll').style.top = posy-offsety+"px";
				document.getElementById(clicked+'div').style.top = ((initop[clicked]-parseInt(document.getElementById(clicked+'scroll').style.top))*mult)+"px";
				document.getElementById(clicked+'scroll').style.left = posx-offsetx+"px";
			}
		}
	}
}

function change() {
		document.getElementById(arraypos).style.backgroundColor = "#"+_hex(_rnd(256),2)+_hex(_rnd(256),2)+_hex(_rnd(256),2);
		ypixels[arraypos] = ypos + ((_rnd(3)*10)-(_rnd(1)*(_rnd(3)*10))); 
		document.getElementById(arraypos).style.top = ypixels[arraypos]+"px"; 
		xpixels[arraypos] = xpos + ((_rnd(3)*10)-(_rnd(1)*(_rnd(3)*10))); 
		document.getElementById(arraypos).style.left = xpixels[arraypos]+"px";
		if(arraypos<boxnum) { arraypos++; } else { arraypos = 0;}
}
function changer() {
		change();
		setTimeout("change()",100);
		setTimeout("change()",200);
		//setTimeout("change()",200);
		//setTimeout("change()",250);
}

window.onload=function(){
	boxnum = document.getElementById("boxnum").value;
	for (var x=0;x<=boxnum;x++) {
		var newDiv = document.createElement("div");
			newDiv.id = x;
			newDiv.style.cssText = "overflow:hidden;background-color:#444;width:"+tol+"px;height:"+tol+"px;position:absolute;top:0px;left:0px;";
			if ((!document.all)&&(document.getElementById)){ newDiv.setAttribute("onmouseover","moveit("+x+");"); }    
			if ((document.all)&&(document.getElementById)){	newDiv["onmouseover"]=new Function("moveit("+x+");"); }
			document.getElementById("body").appendChild(newDiv);
	}
	docinit = true;
	document.onmousemove = doSomethingElse;
}
