/*******************************************************************
*
* File    : JSFX_Butterfly.js © JavaScript-FX.com
*
* Created : 2001/03/17
*
* Author  : Roy Whittle www.Roy.Whittle.com
*           
* Purpose : To create butterfly sprites that can be animated in a page
*
* History
* Date         Version        Description
*
* 2001-03-17	1.0		Created for javascript-fx
* 2001-12-09	1.1		Remove the need for JSFX_Sprite.js
***********************************************************************/
/*
 * Static Class Butterfly
 */
JSFX.Butterfly = function(n)
{
	return new JSFX.ButterflyObj(n);
}
/*
 * Class ButterflyObj extends Object
 */
JSFX.ButterflyObj = function(numSprites)
{
	this.id = "JSFX_ButterflyObj_"+JSFX.ButterflyObj.count++;
	this.sprites = new Array();
	for(i=0 ; i<numSprites; i++)
		this.sprites[i]=new JSFX.ButterflySprite();

	window[this.id]=this;
	this.animate();
}
JSFX.ButterflyObj.count = 0;
JSFX.ButterflyObj.prototype.animate = function()
{
	setTimeout("window."+this.id+".animate()", 10);

	for(i=0 ; i<this.sprites.length ; i++)
		this.sprites[i].animate();
}

/*
 * Class ButterflySprite extends Layer
 */
JSFX.ButterflySprite = function(n)
{
	this.running = true;
	
	var imgName = "JSFX_bfs_" + JSFX.ButterflySprite.count++;

	var theHtml = "<IMG SRC='http://www.thespanishfly.com/images/fly8.gif' NAME='"+imgName+"'>";
	//Call the superclass constructor
	this.superC	= JSFX.Layer;
	this.superC(theHtml);

	this.imgName = imgName;
	this.x = Math.random() * (JSFX.Browser.getMaxX()-75);
	this.y = Math.random() * (JSFX.Browser.getMaxY()-75);
	this.dx = Math.random() * 4 - 2;
	this.dy = Math.random() * 8 - 4;
	this.ax = Math.random() * 4 - 2;
	this.ay = Math.random() * 8 - 4;
	this.running = true;
	this.animate = this.animate_rest;
	this.addEventHandler("onmousedown",this.down);
	this.addEventHandler("onmouseover",this.over);

	this.moveTo(this.x,this.y);
	this.show();
}
JSFX.ButterflySprite.prototype = new JSFX.Layer;

JSFX.ButterflySprite.count = 0;

JSFX.ButterflySprite.prototype.down = function(layr, ev)
{
	layr.hide();
	layr.moveTo(-100,-100);
	layr.running = false;
}
JSFX.ButterflySprite.prototype.over = function(layr, ev)
{
	layr.dx = Math.random()*20 - 10;
	layr.dy = Math.random()*20 - 10;
	layr.setImg("http://www.thespanishfly.com/images/fly8.gif");
	layr.animate = layr.animate_fly;
}
JSFX.ButterflySprite.prototype.animate_rest = function()
{
	if(Math.random() >.99)
	{
		this.dx = 0;
		this.dy = 0;
		this.ax = 0;
		this.ay = 0;
		this.setImg("http://www.thespanishfly.com/images/fly8.gif");
		this.animate = this.animate_fly;
	}
}
JSFX.ButterflySprite.prototype.animate_fly = function()
{
	if(!this.running)
		return;

	this.x += this.dx;
	this.y += this.dy;

	if( (this.x > JSFX.Browser.getMaxX()-80)
	 || (this.x < JSFX.Browser.getMinX()) )
	{
		this.x -= this.dx;
		this.dx = -this.dx;
		this.ax = -this.ax;
	}
	if( (this.y > JSFX.Browser.getMaxY()-80)
	 || (this.y < JSFX.Browser.getMinY()) )
	{
		this.y -= this.dy;
		this.dy = -this.dy;
		this.ay = -this.ay;
	}
	this.moveTo(this.x, this.y);

	this.dx += this.ax;
	this.dy += this.ay;

	this.ax += Math.random() * 2 - 1;
	this.ay += Math.random() * 2 - 1;

	this.ax = Math.abs(this.ax) > 5 ? 0 : this.ax;
	this.ay = Math.abs(this.ay) > 5 ? 0 : this.ay;

	this.dx = Math.abs(this.dx) > 20 ? 0 : this.dx;
	this.dy = Math.abs(this.dy) > 20 ? 0 : this.dy;

	if(Math.random() >.995)
	{
		this.setImg("http://www.thespanishfly.com/images/fly8.gif");
		this.animate = this.animate_rest;
	}
}
JSFX.ButterflySprite.prototype.setImg = function(theImage)
{
	this.images[this.imgName].src = theImage;
}


