// JS Lines (c) 2000 by Greg Cunningham
// http://curioustech.home.insightbb.com/

function intRand(n){return Math.round(Math.random()*n)}

var hexChars="0123456789ABCDEF";
function D2H(d) {
	var a=d % 16;
	var b=(d - a)/16;
	hex="" + hexChars.charAt(b) + hexChars.charAt(a);
	return hex;
}

function smax(n,max){
	if(n<-max) n=-max // limit the rotation speed
	if(n>max) n=max
	return n
}

var numlines=13
var x=1,y=1,dx=3,dy=3,size=40,ds=0,rot=0,dr=1
var lp
var ln=0
var r,g,b,rn,gn,bn
var Xpos=0, Ypos=0;

function updateLines(){
	var hit=0
	// check for wall collision
	n=y+dy

	// object size
	ds=smax(ds,14)
	size+=ds
	if(size<2){ size=2; ds=1;} // limit the line size
	max=Math.min(WinWidth(),WinHeight())/3
	if(size>max){ size=max; ds=-1;}

	if(n<=0 | n>=WinHeight()-size){
		dy = -dy + (1-intRand(2))
		hit=1
	}
	n=x+dx
	if(n<=0 | n>=WinWidth()-size){
		dx = -dx + (1-intRand(2))
		hit=1
	}
	if(hit | intRand(3)==0){
		ds += 2-intRand(4)
		dx += 1-intRand(2)
		dy += 1-intRand(2)
		dr += 1-intRand(2)
	}
	dr=smax(dr,5)
	dx=smax(dx,4)
	dy=smax(dy,4)

	if( dx < 0 && Xpos > x)
		dx = -dx
	if( dx > 0 && Xpos < x)
		dx = -dx

	if( dy < 0 && Ypos > y)
		dy = -dy
	if( dy > 0 && Ypos < y)
		dy = -dy

	x+=dx
	y+=dy

	rot+=dr
	rot%=180 // uh, full circle for a line
	lp[ln].r.rotation = rot;

	// color cycler
	r+=rn; g+=gn; b+=bn;

	if(r<12 | r>244){
		rn=-rn
		if(rn>0) rn=1+intRand(10)
	}
	if(g<12 | g>244){
		gn=-gn
		if(gn>0) gn=1+intRand(10)
	}
	if(b<12 | b>244){
		bn=-bn
		if(bn>0) bn=1+intRand(10)
	}
	if(r>255) r=255
	if(g>255) g=255
	if(b>255) b=255

	lp[ln].c.color='#' + D2H(r) + D2H(g) + D2H(b)

	lp[ln].left = x + document.body.scrollLeft -(size/2)
	lp[ln].top = y + document.body.scrollTop -(size/2)
	lp[ln].pixelWidth = size
	lp[ln].pixelHeight = size
	ln++; ln%=numlines
}

if (document.all&&window.print){
	window.attachEvent("onload", initVMLines);
}

function initVMLines() {
	lp = new Array(numlines);

	r=4+intRand(250)
	g=4+intRand(250)
	b=4+intRand(250)
	rn=5-intRand(10)
	gn=5-intRand(10)
	bn=5-intRand(10)

	for(var i=0; i<numlines; i++){
	  lp[i]=eval('line'+i+'.style')
	  lp[i].r=eval('l'+i+'.style')
	  lp[i].c=eval('l'+i+'.childNodes.item(0).childNodes.item(0)')
	}
	window.setInterval("updateLines()", 40);
}

function MoveHandler(e)
{
    Xpos = e.pageX;
    Ypos = e.pageY;
    return true;
}

// just save mouse position for animate() to use
function MoveHandlerIE() {
    Xpos = window.event.x// + document.body.scrollLeft;
    Ypos = window.event.y// + document.body.scrollTop;	  
}

if (document.all) {
    document.onmousemove = MoveHandlerIE;
} else {
    document.captureEvents(Event.MOUSEMOVE);
    document.onMouseMove = MoveHandler;
}

