// Stars effect - Tracker Hero
// Carlos Peris 2009


var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;  
var divName = 'mydiv'; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position
var numParticles  = 64;
var angle = 1.0;
var offset = 0;
var amouseX = 1;
var amouseY = 1;

particleArrayX = new Array();
particleArrayY = new Array();
particleArraySX = new Array();
particleArraySY = new Array();
particleArrayTTL = new Array();
particleArraySpeed = new Array();


function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}

function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}



function addParticle(i, shape)
{
   var obj = document.getElementById(divName+i).style;
	
	particleArrayX[i] = 380; 
	particleArrayY[i] = 280;
	if (i%4 == 0)
	{
		particleArrayX[i] += Math.random()*20 - Math.random()*20;	
		particleArrayY[i] += Math.random()*20 - Math.random()*20;
	}
	else
	{
		particleArrayX[i] = particleArrayX[i - 1];
		particleArrayY[i] = particleArrayY[i - 1];
	}
	var speed = 3 + Math.random()*4;
	speed = speed / 2;
	var oSpeed = speed;
	
	if (shape > 0)
	{
		oSpeed = particleArraySpeed[i - shape];
		speed = oSpeed - (shape/5);		
	}
	particleArraySpeed[i] = speed;
	particleArraySX[i] = speed*Math.cos(angle);
	particleArraySY[i] = speed*Math.sin(angle);
	
	
	particleArrayTTL[i] = 2500  / oSpeed;
	
	obj.visibility = 'visible';
	obj.left = particleArrayX[i]+'px';
	obj.top = particleArrayY[i]+'px';
	
}

function addDivs()
{
	if (is_chrome)
	{
		numParticles = 256;
	}


	var lastShape = 0;
	
   for (var i = 0; i < numParticles; i++)
   {
		var shape = i%4;
		
		if (i%4 == 0)
		{
			shape = Math.random()*4;
			shape = shape + 0.8;
			lastShape = shape;
		}
		else
		{
			shape = lastShape + (i%4);			
		}
		
		if (shape <= 1)
			document.write("<div id='mydiv" + i + "' style='position: absolute;top: 0px;left: 20px;VISIBILITY: visible;'><img id=im src='gfx/particle_001.png'></div>");
		else if (shape <= 2)
			document.write("<div id='mydiv" + i + "' style='position: absolute;top: 0px;left: 20px;VISIBILITY: visible;'><img id=im src='gfx/particle_002.png'></div>");
		else if (shape <= 3)
			document.write("<div id='mydiv" + i + "' style='position: absolute;top: 0px;left: 20px;VISIBILITY: visible;'><img id=im src='gfx/particle_003.png'></div>");			
		else
			document.write("<div id='mydiv" + i + "' style='position: absolute;top: 0px;left: 20px;VISIBILITY: visible;'><img id=im src='gfx/particle_004.png'></div>");
			
		addParticle(i, i%4);
		
		if (i%4 == 3)
			angle = angle + 0.8;
	
   }
   
}

function follow(evt)
{
	amouseX = mouseX(evt);
	amouseY = mouseY(evt);	
	var dx = amouseX - 380;
	var dy = amouseY - 280;
	angle = Math.atan2(dy , dx);

	//document.write(parseInt(angle));
}

function update()
{	
   for (var i = 0; i < numParticles; i++)
   {
   
		var obj = document.getElementById(divName+i).style;
		obj.visibility = 'visible';
		obj.left = particleArrayX[i]+'px';
		obj.top = particleArrayY[i]+'px';
		
		particleArrayX[i] = particleArrayX[i] + particleArraySX[i];
		particleArrayY[i] = particleArrayY[i] + particleArraySY[i];
		
		particleArrayTTL[i] = particleArrayTTL[i] - 10;
		if (particleArrayTTL[i] < 0)
		{
			addParticle(i, i%4)
		
			if (i%4 == 3)
			{
				if (Math.random()*4 < 1)
				{
					angle = angle + (Math.random()*3.14);
				}
				else
				{
					angle = angle + (Math.random()*0.2) - (Math.random()*0.2);
				}
			}
			
		}
		
	}
	offset += 16;
	if (offset > numParticles - 16)
		offset = 0;
	
	//document.onmousemove = follow;	
		
	setTimeout("update()", 1);		
}

addDivs();
//document.onmousemove = follow;


                    


