/*******************************************************************
*
* File    : halloween2.js (NOTE: requires animate2.js!!)
*
* Created : 2000/06/05
*
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
*
* Purpose : To create animated ghosts in your document
*
* History
* Date         Version        Description
*
* 2000-10-02	1.0		Another halloween already!!
*
***********************************************************************/
/*** Start - xLayer - a cross browser layer object by www.Roy.Whittle.com ***/
var xLayerNo=0;
function xLayer(xHtml)
{
	var xName="xLayer" + xLayerNo++;

	if(document.layers)
	{
		this.layer=new Layer(100);
		this.layer.document.open()
		this.layer.document.write(xHtml)
		this.layer.document.close()
	}
	else
	if(document.all)
	{
		txt =   "<DIV ID='" + xName
			+ "' STYLE=\"position:absolute;left:100;top:100;width:100;height:100;visibility:hidden\">"
			+ xHtml + "</DIV>";
		document.body.insertAdjacentHTML("BeforeEnd",txt);
		this.element = document.all[xName];
		this.layer   = document.all[xName].style;
	}

	this.show       = show;
	this.moveTo     = moveTo;
	this.setContent = setContent;

	return(this);
}
function show()
{
	if(document.layers)
		this.layer.visibility="show";
	else
	if(document.all)
		this.layer.visibility="visible";
}
function moveTo(x,y)
{
	if(document.layers)
		this.layer.moveTo(x,y);
	else
	if(document.all)
	{
		this.layer.pixelLeft = x;
		this.layer.pixelTop  = y;
	}	
}
function setContent(xHtml)
{
	if(document.layers)
	{
		this.layer.document.open()
		this.layer.document.write(xHtml)
		this.layer.document.close()
	}
	else 
	if(document.all)
	{
		this.element.innerHTML=xHtml;
	}
}
/*** End  - xLayer - a cross browser layer object by www.Roy.Whittle.com ***/

var fwLayer = new Array();
var ow;
var oh;
var theTimer;
var layerNo=0;
var imgBase="";
var MAX_GHOSTS=8;

function moveIt()
{
	var z=Math.floor(Math.random()*5);
	var myLayer=fwLayer[layerNo];

	myLayer.x  = Math.floor(Math.random()*scrWidth());
	myLayer.y  = Math.floor(Math.random()*scrHeight());
	if(z<3)
	{
		myLayer.dx = Math.random()*20 - 10;
		myLayer.dy = Math.random()*20 - 10;
	}
	else
	{
		myLayer.dx = 0;
		myLayer.dy = 0;
	}

	myLayer.moveTo(myLayer.x,myLayer.y);
	myLayer.show();

	turn_on("fw"+layerNo, "ghost"+z);
	turn_off("fw"+layerNo, "ghost"+z);
	
	layerNo=(layerNo+1)%MAX_GHOSTS;

	theTimer=setTimeout("moveIt()", Math.floor(Math.random()*(10000/MAX_GHOSTS) ) );
}
function ani()
{
	for(i=0 ; i<fwLayer.length ; i++)
	{
		var b = fwLayer[i];
		b.x += b.dx;
		b.y += b.dy;

		b.moveTo(b.x, b.y);

		if(b.dx > 0)
		{
			if(b.x > scrWidth()-110)
				b.x=0;
		}
		else
			if(b.x < 5)
				b.x=scrWidth()-110;

		if(b.dy > 0)
		{
			if(b.y > scrHeight()-110)
				b.y = 0;
		}
		else
			if(b.y < 5)
				b.y = scrHeight()-110;
	}
	setTimeout("ani()", 25);
}
function scrWidth()
{
	if(document.layers)
		return window.innerWidth;
	else if(document.all)
		return document.body.clientWidth;
}
function scrHeight()
{
	if(document.layers)
		return window.innerHeight;
	else if(document.all)
		return document.body.clientHeight;
}
function setSize()
{
	if(document.layers)
	{
		ow=window.outerWidth;
		oh=window.outerHeight;
	}
}
AnimationFrames("ghost0", 7, ".gif");
AnimationFrames("ghost1", 7, ".gif");
AnimationFrames("ghost2", 7, ".gif");
AnimationFrames("ghost3", 7, ".gif");
AnimationFrames("ghost4", 7, ".gif");
FrameInterval=75;

function ghosts()
{
	var i=0;
	setSize();

	//Make a duplicate for ghost 3
//	AniFrame["ghost3"]=AniFrame["ghost0"];

	for(i=0 ; i<MAX_GHOSTS; i++)
	{
		AnimatedImage("fw"+i, "ghost0");
		fwLayer[i]=new xLayer("<IMG SRC='"+imgBase+"images/ghost0/0.gif' NAME='fw"+i+"'>");
		fwLayer[i].fwNo=0;
		fwLayer[i].x  = Math.floor(Math.random()*scrWidth());
		fwLayer[i].y  = Math.floor(Math.random()*scrHeight());
		fwLayer[i].dx = Math.random()*20 - 10;
		fwLayer[i].dy = Math.random()*20 - 10;
	}

	theTimer=setTimeout("moveIt()", 1000 );
	ani();
}
function handle_resize()
{
	if(   document.layers 
	   && (   ow != window.outerWidth
	       || oh != window.outerHeight) )
		location.reload();
	setSize();
}
window.onload=ghosts;
window.onresize=handle_resize;
