/* usemedia.com . joes koppers . 04.2004 */
/* thnx for reading this code */


function init()
{
	line = new Object();

	line.x = 0;
	line.y = 100;
	line.w = 1;
	line.h = 1;
	line.index = 0;
	line.speed = 10;
	
	line.growing = false;
	line.turning = 0;
	line.next_turn = 100;
	
	line.direction = 1; //start going left
	
	//and start growing..
	grow();
	
	document.onmouseup = click;
}

function click()
{
	if (line.growing) window.clearTimeout(line.growing);
	turn(false);
}

function create()
{
	/* make new <div> */

	line.index++;
	
	var newLine = document.createElement('DIV');	
	document.getElementById('master').appendChild(newLine);
	
	//set properties
	newLine.className = 'line';
	newLine.id = 'line'+line.index;
	newLine.innerHTML = '<p>'; //to get 1px height on m$ exploder 

	//update line object
	switch(line.direction)
	{
		case 1: line.x = line.x + line.w; break;
		case 2: line.y = line.y + line.h; break;
	}
	line.w = 1;
	line.h = 1;
}


function turn(direction)
{
	//create a new line
	create();

	//new direction
	if (!direction)
	{
		var rnd = Math.round(Math.random());
		
		//random new direction
		switch (line.direction)
		{
			case 1:
			case 4:	
				//up or down
				line.direction = (rnd)? 2:3;
				//always down if top of screen
				if (line.y<=2) line.direction = 2;
				break;
				
			case 2:
			case 3:
				//left or right
				line.direction = (rnd)? 1:4;
				//always right if left of screen
				if (line.x<=2) line.direction = 1;
				break;
		}
	}
	else line.direction = direction;

	//continue turning..	
	line.turning = 0;
	line.next_turn = (Math.round(Math.random()*10)*10)+10;
	
	//continue growing..
	grow();
}

function grow()
{
	//window.status = line.turning+" : "+line.next_turn+" : "+line.direction;

	/* 1=right, 2=down, 3=up, 4=left */

	line.turning++;
	
	//turn now?
	if (line.turning==line.next_turn) turn(false);
	else
	{
		/* turn at screen edges */

		if (line.direction==4 && line.x<=2)
		{
			//random up or down..
			var rnd = Math.round(Math.random());
			var direction = (rnd)? 2:3;
			turn(direction);
		}
		else if (line.direction==3 && line.y<=2)
		{
			//random left or right
			var rnd = Math.round(Math.random());
			var direction = (rnd)? 1:4;
			turn(direction);
		}
		else
		{
			
			/* grow */
			
			switch (line.direction)
			{
				case 1: line.w++; break;
				case 2:	line.h++; break;
				case 3: line.h++; line.y--; break;
				case 4:	line.w++; line.x--;	break;
			}
	
			//apply to div
			document.getElementById('line'+line.index).style.left = line.x +"px";
			document.getElementById('line'+line.index).style.top = line.y +"px";
			document.getElementById('line'+line.index).style.width = line.w +"px";
			document.getElementById('line'+line.index).style.height = line.h +"px";
			
			//continue growing..
			line.growing = window.setTimeout('grow()',line.speed);
		}
	}
}