/*******************************************************************
*
* File    : JSFX_Droplets.js © JavaScript-FX.com
*
* Created : 2001/10/26
*
* Author  : Roy Whittle www.Roy.Whittle.com
*
* Purpose : To create a mouse trailer that looks like water
*		droplets coming from the mouse pointer.
*
* Requires	: JSFX_Layer.js - for layer creation, movement
*		: JSFX_Mouse.js - to track the mouse x,y coordinates
*
* History
* Date         Version        Description
*
* 2001-10-26	1.0		Created for javascript-fx
* 2003-03-02	1.1		Added a stop() function
***********************************************************************/

var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function dec2hex(dec)
{
	return(hexDigit[dec>>4]+hexDigit[dec&15]);
}
function hex2dec(hex)
{
	return(parseInt(hex,16))
}

/*** Class DropletSpark extends Layer ***/
JSFX.DropletSpark = function(max)
{
	//Call the superclass constructor
	this.superC	= JSFX.Layer;
	this.superC("X");

	this.dx	= 0;
	this.dy	= 0;
	this.ay	= .1;
	this.step   = 0;
	this.x	= 100;
	this.y	= 100;
	this.max	= max;
	this.hide();
}
JSFX.DropletSpark.prototype = new JSFX.Layer;
JSFX.DropletSpark.prototype.animate = function()
{
	if(this.isVisible())
	{
		this.dy += this.ay;
		this.x += this.dx;
		this.y += this.dy;
		this.moveTo(this.x, this.y);
		this.step++;
		if(this.step > this.max)
		{
			this.hide();
			this.step = 0;
			this.moveTo(0,0);
		}
	}
}
JSFX.DropletSpark.prototype.changeColour = function()
{
	var colour="";

	r2= 0;
	g2= Math.random()*255;
	b2= g2;

	if(r2==0 && g2==0 && b2==0)
	{
		r2=255;
		g2=255;
		b2=255;
	}

	colour = "#" + dec2hex(r2) + dec2hex(g2) + dec2hex(b2);
	this.setBgColor(colour);
}
/*** END Class DropletSpark ***/

/*** Class DropletObj extends Object ***/
JSFX.DropletObj = function(numSparks, anim)
{
	this.id = "JSFX_DropletObj_"+JSFX.DropletObj.count++;
	this.sparks = new Array();
	for(i=0 ; i<numSparks; i++)
	{
		this.sparks[i]=new JSFX.DropletSpark(numSparks);
		this.sparks[i].clip(0,0,2,2);
		this.sparks[i].setBgColor("yellow");
	}
	this.oldX=0;
	this.oldY=0;
	this.newX=0;
	this.newY=0;
	if(anim == 0)
		this.setSpeed = this.setSpeed0;
	else if(anim == 1)
		this.setSpeed = this.setSpeed1;
	else if(anim == 2)
		this.setSpeed = this.setSpeed2;
	else if(anim == 3)
		this.setSpeed = this.setSpeed3;
	else
		this.setSpeed = this.setSpeed0;

	window[this.id]=this;
	this.animate();
}
JSFX.DropletObj.count = 0;
JSFX.DropletObj.prototype.findIdle = function()
{
	for(i=0 ; i<this.sparks.length ; i++)
		if(! this.sparks[i].isVisible() )
			return this.sparks[i];

	return this.sparks[0];
}
JSFX.DropletObj.prototype.animate = function()
{
	this.timerId = setTimeout("window."+this.id+".animate()", 30);
	
	this.newX = JSFX.Browser.mouseX;
	this.newY = JSFX.Browser.mouseY-2;

	if(this.newX != this.oldX || this.newY != this.oldY)
	{
		var s = this.findIdle();
		this.setSpeed(s);
		s.x = this.newX;
		s.y = this.newY;
		s.changeColour();
		s.show();
	}
	this.oldX = this.newX;
	this.oldY = this.newY;

	for(i=0 ; i<this.sparks.length ; i++)
		this.sparks[i].animate();
}
JSFX.DropletObj.prototype.stop = function()
{
	clearTimeout(this.timerId);
	for(i=0 ; i<this.sparks.length ; i++)
		this.sparks[i].hide();
	
}
JSFX.DropletObj.prototype.setSpeed0 = function(s)
{
	s.dx=0;
	s.dy=0;
}
JSFX.DropletObj.prototype.setSpeed1 = function(s)
{
	s.dx=0;
	s.dy=-3;
}
JSFX.DropletObj.prototype.setSpeed2 = function(s)
{
	s.dx = this.newX-this.oldX;
	s.dy = this.newY-this.oldY;
}
JSFX.DropletObj.prototype.setSpeed3 = function(s)
{
	s.dx = this.oldX-this.newX;
	s.dy = this.oldY-this.newY;
}
/*** END Class DropletObj ***/

/*** Static method for creating droplet objects ***/
JSFX.Droplets = function(num, anim)
{
	return new JSFX.DropletObj(num, anim);
}

/*** If no other script has added it yet, add the ns resize fix ***/
if(navigator.appName.indexOf("Netscape") != -1 && !document.getElementById)
{
	if(!JSFX.ns_resize)
	{
		JSFX.ow = outerWidth;
		JSFX.oh = outerHeight;
		JSFX.ns_resize = function()
		{
			if(outerWidth != JSFX.ow || outerHeight != JSFX.oh )
				location.reload();
		}
	}
	window.onresize=JSFX.ns_resize;
}



