/*******************************************************************
*
* File    : JSFX_Ghosts.js © JavaScript-FX.com
*
* Created : 2001/03/16
*
* Author  : Roy Whittle www.Roy.Whittle.com
*           
* Purpose : To create animated Ghosts on the page using any user 
*		supplied image. Uses the JSFX_Sprite library.
*
* History
* Date         Version        Description
*
* 2001-03-17	1.0		Created for javascript-fx
***********************************************************************/
//!!!! First thing we need to do is disable opacity for Netscape 6 to
//!!!! stop it crashing in the GKGFXWIN.DLL !!!!!!!!!!!
if(navigator.appName.indexOf("Netscape") != -1)
{
	JSFX.Layer.prototype.setOpacity = function(pc) {return 0;}
}
//!!!! Try taking the above lines out and watch NS6 CHOKE!!!

JSFX.AddGhost = function(img)
{
	var myGhost = null;

	myGhost = new JSFX.Sprite(img);
	myGhost.op = 0;
	myGhost.x = Math.random()*JSFX.Browser.getMaxX();
	myGhost.y = Math.random()*JSFX.Browser.getMaxY();
	myGhost.dx = 0;
	myGhost.dy = 0;
	myGhost.w = 30;
	myGhost.h = 30;
	myGhost.targetX = Math.random()*JSFX.Browser.getMaxX();
	myGhost.targetY = Math.random()*JSFX.Browser.getMaxY();
	myGhost.state = "off"
	myGhost.animate = JSFX.animateGhosts;
	myGhost.hide();
	myGhost.setOpacity(this.op);
	myGhost.moveTo(myGhost.x,myGhost.y);
	JSFX.Sprite.startSprites();
}
JSFX.animateGhosts = function()
{
	if(this.state == "off")
	{
		if(Math.random() > .99)
		{
			this.state="up";
			this.show();
		}
	}
	else if(this.state == "on")
	{
		if(Math.random() > .98)
			this.state="down";
	}
	else if(this.state == "up")
	{
		this.op += 10;
		this.setOpacity(this.op);
		if(this.op>=90)
			this.state = "on";
	}
	else if(this.state == "down")
	{
		this.op -= 10;
		if(this.op==0)
		{
			this.hide();
			this.state = "off";
		}
		else
			this.setOpacity(this.op);
	}

	m = this;
	var X = (this.targetX - m.x);
	var Y = (this.targetY - m.y);
	var len = Math.sqrt(X*X+Y*Y);
	if(len < 1) len = 1;
	var dx = 20 * (X/len);
	var dy = 20 * (Y/len);
	var ddx = (dx - this.dx)/10;
	var ddy = (dy - this.dy)/10;
	this.dx += ddx;
	this.dy += ddy;
	m.x += this.dx;
	m.y += this.dy;
	m.moveTo(m.x, m.y);
	if(Math.random() >.95 )
	{
		this.targetX = Math.random()*(JSFX.Browser.getCanvasWidth()-150);
		this.targetY = Math.random()*(JSFX.Browser.getCanvasHeight()+JSFX.Browser.getMinY()-150);
	}
}
