/*******************************************************************
*
* File    : JSFX_Squidie.js
*
* Created : 2001/05/17
*
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
*
* Purpose : To create a Squidie or eel animation
*
* History
* Date         Version        Description
* 2001-05-17	1.0		Converted for JavaScript-FX
***********************************************************************/
if(!window.JSFX)
	JSFX = new Object();
/*
 * Class SquidieMass extends Layer
 */
JSFX.SquidieMass = function(htmlStr, x, y, z)
{
	if(!htmlStr)
		return;

	//Call the super constructor
	this.superC = JSFX.Layer;
	this.superC(htmlStr, x, y);

	this.x=x;
	this.y=y;
	this.show();
	this.setzIndex(z);
}
JSFX.SquidieMass.prototype = new JSFX.Layer;

JSFX.Squidie = function(args)
{
	if(args==null) return;

	var n  = args[0];
	this.followers = new Array();
	this.followers[0] = new JSFX.SquidieMass (args[1], 100, 100, n );

	for(i=1 ; i<n ; i++)
	{
		var img
		if(args.length == 2)
			img = args[1];
		else
			img = args[(i%(args.length-2)) + 2];

		this.followers[i] = new JSFX.SquidieMass (img, 100, 100, n-i );
	}

	this.targetX	= 200;
	this.targetY	= 200;
	this.x		= 100;
	this.y		= 100;
	this.dx		= 0;
	this.dy		= 0;
}

JSFX.Squidie.prototype.animate = function()
{
	var m;
	for(i=this.followers.length-1; i>0 ; i--)
	{
		m = this.followers[i];
		m.x = this.followers[i-1].x+1;
		m.y = this.followers[i-1].y+1;
		m.moveTo(m.x, m.y);
	}
	m = this.followers[0];
	var X = (this.targetX - m.x);
	var Y = (this.targetY - m.y);
	var len = Math.sqrt(X*X+Y*Y);
	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()-40);
		this.targetY = Math.random()*(JSFX.Browser.getCanvasHeight()+JSFX.Browser.getMinY()-40);
	}
	
}

JSFX.MakeSquidie = function()
{
	var squidie = new JSFX.Squidie(JSFX.MakeSquidie.arguments);
	JSFX.MakeSquidie.squidies[JSFX.MakeSquidie.squidies.length] = squidie;

	if(!JSFX.MakeSquidie.theTimer)
		JSFX.MakeSquidie.theTimer = setInterval("JSFX.MakeSquidie.animate()", 40);

	return(squidie);
}
JSFX.MakeSquidie.squidies = new Array();
JSFX.MakeSquidie.animate = function()
{
	var i;
	for(i=0 ; i<JSFX.MakeSquidie.squidies.length ; i++)
		JSFX.MakeSquidie.squidies[i].animate();
}
/*** 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;
}


