//Copyright 2001 DPA Software

var ns4up, ie4up, ns6up;
var AnimateTimerId;

// only if browser is not N3, scan for the other browsers. otherwise N3 shows error
if ( navigator.appName == "Netscape" && parseInt(navigator.appVersion,10) == 3 )
{
	ns4up = ie4up = ns6up = 0;
}
else
{
	var ie4up = (document.all) ? 1 : 0;
	ns6up = (document.getElementById && !ie4up)? 1 : 0;
	if ( ! ie4up && ! ns6up ) ns4up = 1;  // browser sniffer
}

var Direction = 'Movement';

var pi_4 = (Math.PI/4);

var e = 0.392699;
var se = e + pi_4;
var s = se + pi_4;
var sw = s + pi_4;
var w = sw + pi_4;
var nw = w + pi_4;
var n = nw + pi_4;
var ne = n + pi_4;

// define number of divs
var nDots = 2;

// define starting position of image
var Xpos = 0;
var Ypos = 0;

var OldXpos = 0, OldYpos = 0;

// define speed of moving image
var DELTAT = 0.1;

// define how far the image drops from the cursor
var SEGLEN = 10;

// define how big is the loop the image makes around the cursor
var SPRINGK = 10;

// the greater the mass the slower the speed of image
var MASS = 1;

// the greater the gravity the smaller is the loop
var GRAVITY = 50;

// the greater the resistance the slower the speed of image
var RESISTANCE = 10;

var STOPVEL = 0.1 //Terminal velocity 0.1

var STOPACC = 0.1 // Stop Acceleration 0.1;
var DOTSIZE = 11;

var Timeout = 0;

var XOffset = 0;
var YOffset = 0;

// define the size of the bounces of the image
var BOUNCE = 0.75;
var isNetscape = navigator.appName=="Netscape";
var followmouse = true;
var dots = new Array();

var C_N = 2, C_NE = 3, C_E = 4, C_SE = 5, C_S = 6, C_SW = 7, C_W = 8, C_NW = 9  

var ImageArray = new Array();
var theImages = new Array();

function StopEffect ()
{
	var i, img;
	
	clearInterval ( AnimateTimerId );
	
	for ( i = 0 ; i < nDots ; i++)
	{
		img = "dot_ce_" + i
		if (ie4up)
		{
			document.all[img].style.visibility = "hidden";
		}
		else if (ns4up)
		{
			document.layers[img].visibility = "hidden";
		}
		else if(ns6up)
		{
			var elm = document.getElementById(img);
			elm.style.visibility = "hidden";
		}
	}
}

function init()
{
	var i = 0;
	for (i = 0; i < nDots; i++)
	{
		dots[i] = new dot(i);
	}

	for (i = 0; i < nDots; i++)
	{
		dots[i].obj.left = dots[i].X;
		dots[i].obj.top = dots[i].Y;
		if (ie4up)
		{
			dots[i].obj.pixelWidth = dots[i].objImg.width;
		}
		if (ns4up)
		{
			dots[i].obj.width = eval("document.imgdot_ce_" + i).width;
		}
	}
	if (ie4up)
	{
		setTimeout("startanimate()", 3000);
	}
	else if(ns4up || ns6up)
	{
		startanimate();
	}
}

function dot(i)
{
	this.X = Xpos;
	this.Y = Ypos;
	this.dx = 0;
	this.dy = 0;
	this.index = i;
	if (ie4up)
	{
		this.obj = eval("dot_ce_" + i + ".style") ;
		this.objImg = eval("imgdot_ce_" + i);
	}
	else if(ns4up)
	{
		this.obj = eval(document.layers["dot_ce_" + i]);
		this.objImg = eval("document.imgdot_ce_" + i);
	}
	else if	(ns6up)
	{
		this.obj = eval(document.getElementById("dot_ce_" + i ).style);
		this.objImg = eval(document.images["imgdot_ce_" + i]);
	}
}

function startanimate()
{
    AnimateTimerId = setInterval("animate()", 20);
}

function MoveHandler(e)
{
	if (ns4up)
	{
		Xpos = e.pageX + (XOffset);
		Ypos = e.pageY + (YOffset);
	}
	else if(ns6up)
	{
		Xpos = e.pageX + (XOffset);
		Ypos = e.pageY + (YOffset);
	}
	return true;
}

function MoveHandlerIE()
{
	Xpos = window.event.x + document.body.scrollLeft + (XOffset);
	Ypos = window.event.y + document.body.scrollTop + (YOffset);
}

if (ie4up)
{
	document.onmousemove = MoveHandlerIE;
}
else if (ns4up)
{
	document.captureEvents(Event.MOUSEMOVE);
	document.onMouseMove = MoveHandler;
}
else if (ns6up)
{
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = MoveHandler;
}

function vec(X, Y)
{
	this.X = X;
	this.Y = Y;
}

function springForce(i, j, spring)
{
	var dx = (dots[i].X - dots[j].X);
	var dy = (dots[i].Y - dots[j].Y);
	var len = Math.sqrt(dx*dx + dy*dy);
	if (len > SEGLEN)
	{
		var springF = SPRINGK * (len - SEGLEN);
		spring.X += (dx / len) * springF;
		spring.Y += (dy / len) * springF;
	}
}

function AssignDirectionalImage(xnew,ynew,xold,yold)
{
	var angle = Math.atan(((ynew - yold) / (xnew - xold)));
	var NewVisibleImage = 0;
	if ( angle < 0 ) 
	{
		if ( (xnew-xold) < 0 )
			angle = Math.PI + angle;
		else
			angle = ( 2 * Math.PI ) + angle;
	}
	else
	{
		if ( (ynew-yold) < 0 )
			angle += Math.PI;
	}

	if (angle >= e && angle <= se )
	{
		NewVisibleImage = C_SE;
	}
	else if (angle >= se && angle <= s)
	{
		NewVisibleImage = C_S;
	}
	else if (angle >= s && angle <= sw)
	{
		NewVisibleImage = C_SW;
	}
	else if (angle >= sw && angle <= w)
	{
		NewVisibleImage = C_W;
	}
	else if (angle >= w && angle <= nw)
	{
		NewVisibleImage = C_NW;
	}
	else if (angle >= nw && angle <= n)
	{
		NewVisibleImage = C_N;
	}
	else if (angle >= n && angle <= ne)
	{
		NewVisibleImage = C_NE;
	}
	else 
	{
		NewVisibleImage = C_E;
	}
	return NewVisibleImage;
}

function animate()
{
	var start = 1;

	if (followmouse)
	{
		dots[0].X = Xpos;
		dots[0].Y = Ypos;
		start = 1;
	}
	for (i = start ; i < 2 ; i++ )
	{
		var NewVisibleImage = 0, SavedObj;

		OldXpos = dots[i].X
		OldYpos = dots[i].Y
		
		var spring = new vec(0, 0);

		if (i > 0)
		{
			springForce(i-1, i, spring);
		}
		if (i < (nDots - 1))
		{
			springForce(i+1, i, spring);
		}
		var resist = new vec(-dots[i].dx * RESISTANCE,-dots[i].dy * RESISTANCE);
		var accel = new vec((spring.X + resist.X)/ MASS,(spring.Y + resist.Y)/ MASS + GRAVITY);
		dots[i].dx += (DELTAT * accel.X);
		dots[i].dy += (DELTAT * accel.Y);
		if (Math.abs(dots[i].dx) < STOPVEL && Math.abs(dots[i].dy) < STOPVEL &&
			Math.abs(accel.X) < STOPACC &&
			Math.abs(accel.Y) < STOPACC)
		{
			dots[i].dx = 0;
			dots[i].dy = 0;
		}
	 	dots[i].X += dots[i].dx;
		dots[i].Y += dots[i].dy;
		var height, width;

		if (isNetscape)
		{
			height = self.innerHeight + self.scrollTop;
			width = self.innerWidth + self.scrollLeft;
		}
		else
		{
			height = document.body.clientHeight + document.body.scrollTop;
			width = document.body.clientWidth + document.body.scrollLeft;
		}
		if (dots[i].Y >=  height - DOTSIZE - 1)
		{
			if (dots[i].dy > 0)
			{
				dots[i].dy = BOUNCE * -dots[i].dy;
			}
			dots[i].Y = height - DOTSIZE - 1;
		}
		if (dots[i].X >= width - DOTSIZE)
		{
			if (dots[i].dx > 0)
			{
				dots[i].dx = BOUNCE * -dots[i].dx;
			}
			dots[i].X = width - DOTSIZE - 1;
		}
		if (dots[i].X < 0)
		{
			if (dots[i].dx < 0)
			{
				dots[i].dx = BOUNCE * -dots[i].dx;
			}
			dots[i].X = 0;
		}
		if (Direction == "Movement")
		{
			if (Math.abs(dots[i].X - OldXpos ) > 5 || Math.abs(dots[i].Y - OldYpos) > 5)
			{
				 NewVisibleImage = AssignDirectionalImage(dots[i].X,dots[i].Y,OldXpos,OldYpos)
			}
			else
			{
				NewVisibleImage = 1;
			}
		}
		else if (Direction == "Cursor")
		{
			OldXpos = dots[i].X + (dots[i].objImg.width/2);
			OldYpos = dots[i].Y + (dots[i].objImg.height/2);
			NewVisibleImage = AssignDirectionalImage(Xpos-(XOffset), Ypos-(YOffset), OldXpos, OldYpos);
		}
		dots[i].obj.left = dots[i].X;
		dots[i].obj.top =  dots[i].Y;
		if ( dots[i].objImg.src != theImages[NewVisibleImage].src )
		{
			dots[i].objImg.src = theImages[NewVisibleImage].src; 
		}
	}
	OldXpos = Xpos
	OldYpos =Ypos
}

function StartCE ( Direction, Acceleration, Elasticity, Mass, Timeout, 
	StationaryImage, NImage, NEImage, EImage, SEImage, SImage, SWImage, WImage, NWImage,
	x_offset, y_offset )
{
	Direction = Direction;
	DELTAT = Acceleration/1000;
	SPRINGK = Elasticity;
	MASS = Mass;
	Timeout = Timeout;
	Timeout = Timeout * 60 * 1000;
	ImageArray[0] = StationaryImage;
	ImageArray[1] = StationaryImage;
	ImageArray[C_N] = NImage;
	ImageArray[C_NE] = NEImage;
	ImageArray[C_E] = EImage;
	ImageArray[C_SE] = SEImage;
	ImageArray[C_S] = SImage;
	ImageArray[C_SW] = SWImage;
	ImageArray[C_W] = WImage;
	ImageArray[C_NW] = NWImage;

	XOffset = x_offset;
	YOffset = y_offset;

	for(i = 1; i <= 9; i++) 
	{	
		theImages[i] = new Image();   
		theImages[i].src = ImageArray[i];
	}

	for ( i = 0 ; i < nDots ; i++)
	{
		if (ie4up)
		{
			document.write("<div id=\"dot_ce_"+ i +"\" style=\"POSITION: ");
			document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
			document.write("hidden; TOP: 0px; LEFT: 0px;\"><img src=\"");
			document.write(ImageArray[i]+ "\" name=\"imgdot_ce_" + i + "\" border=\"0\"></div>");
		}
		else if(ns4up)
		{
			document.write("<layer name=\"dot_ce_"+ i +"\" left=\"0\" ");
			document.write("top=\"0\" visibility=\"hidden\"><img src=\"");
			document.write(ImageArray[i]+ "\" name=\"imgdot_ce_" + i +  "\" border=\"0\"></layer>");
		}
		else if(ns6up)
		{
			var elm;
			elm = document.createElement('img');
			elm.id = "dot_ce_"+ i;
			elm.name = "imgdot_ce_"+ i;
			elm.style.position = 'absolute';
			elm.src = ImageArray[i];
			elm.style.visibility = "hidden";
			elm.style.top = '0' + 'px';
			elm.style.left = '0' + 'px';
			elm.style.border = '0'
			document.body.appendChild ( elm );
		}
	}

	if (ie4up)
	{
		document.all["dot_ce_1"].style.visibility = "visible";
	}
	else if (ns4up)
	{
		document.layers["dot_ce_1"].visibility = "show";
	}
	else if(ns6up)
	{
		var elm = document.getElementById("dot_ce_1");
		elm.style.visibility = "visible";
	}

	if ( ns4up || ie4up || ns6up )
	{
		init();

		if (Timeout != 0)
		{
			setTimeout ( "StopEffect()", Timeout );
		}
	}
}
