/*
 * Class Butterfly extends Object
 */
JSFX.Butterfly = function(n)
{
	JSFX.Butterfly.running = true;

	var i;
	for(i=0 ; i<n ; i++)
	{
		mySprite = new JSFX.Sprite("fun/JS-FX/Butterfly/images/butterfly_rest.gif", 0, 0);

		mySprite.x = Math.random() * (JSFX.Browser.getMaxX()-75);
		mySprite.y = Math.random() * (JSFX.Browser.getMaxY()-75);
		mySprite.dx = Math.random() * 4 - 2;
		mySprite.dy = Math.random() * 8 - 4;
		mySprite.ax = Math.random() * 4 - 2;
		mySprite.ay = Math.random() * 8 - 4;
		mySprite.running = true;
		mySprite.animate = JSFX.Butterfly.animate_rest;
		mySprite.addEventHandler("onmousedown",JSFX.Butterfly.down);
		mySprite.addEventHandler("onmouseover",JSFX.Butterfly.over);

		mySprite.moveTo(mySprite.x,mySprite.y);
		mySprite.show();
	}
	JSFX.Sprite.startSprites();
}
JSFX.Butterfly.down = function(layr, ev)
{
	layr.hide();
	layr.moveTo(-100,-100);
	layr.running = false;
}
JSFX.Butterfly.over = function(layr, ev)
{
	layr.dx = Math.random()*20 - 10;
	layr.dy = Math.random()*20 - 10;
	layr.setImg("fun/JS-FX/Butterfly/images/butterfly_fly.gif");
	layr.animate = JSFX.Butterfly.animate_fly;
}
JSFX.Butterfly.animate_rest = function()
{
	if(Math.random() >.99)
	{
		this.dx = 0;
		this.dy = 0;
		this.ax = 0;
		this.ay = 0;
		this.setImg("fun/JS-FX/Butterfly/images/butterfly_fly.gif");
		this.animate = JSFX.Butterfly.animate_fly;
	}
}
JSFX.Butterfly.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("fun/JS-FX/Butterfly/images/butterfly_rest.gif");
		this.animate = JSFX.Butterfly.animate_rest;
	}
}
