//////////////////////////////////////////////////////////////////////////
//
// Flaps.js script for generic multi-image flapping up or down
// the screen. Can be used for any rising or falling pairs of images,
// like bats flying or fish swimming.
//
// Script written by Craig Taverner based on "Wobbles.js" script
// by Craig Taverner, Kurt Grigg and others.
// Date: 16/03/2002

// These settings are defaulted to good values for falling autumn leaves
// Change them to use for other objects that fall differently
Amount=8;		// Number of images to place on screen
			// Smoothness depends on image file size,
			// the smaller the size the more you can use!
BaseSpeed=3;		// larger numbers increase speed of object movement
SpeedRange=5;		// larger numbers increase range of different speeds
Amplitude=1.0;		// ratio of x-wobble to y-speed
BaseWobble=0.05;	// larger numbers give tighter wobble
WobbleRange=0.1;	// larger numbers give wider wobble range
TimeGap=20;		// time to wait between redraws (larger number slows animation)
Rise=0;			// set to 0 for falling, and 1 for rising

// Initialize internal arrays
Ypos=new Array();
Xpos=new Array();
Speed=new Array();
Step=new Array();
Cstep=new Array();
ns=(document.layers)?1:0;
ns6=(document.getElementById&&!document.all)?1:0;

// Pre-load your image below!
graphicsA=new Array()
graphicsB=new Array()
WinHeight=(ns||ns6)?window.innerHeight:window.document.body.clientHeight;
WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth;
hscrll=(ns||ns6)?window.pageYOffset:document.body.scrollTop;
wscrll=(ns||ns6)?window.pageXOffset:document.body.scrollLeft;

// Add a new image to the array of loaded images
function addImage(imageURLA,imageURLB){
	var arraySize=graphicsA.length;
	var ImageBufA=new Image();
	ImageBufA.src=graphicsA[arraySize]=imageURLA;
	var ImageBufB=new Image();
	ImageBufB.src=graphicsB[arraySize]=imageURLB;
}

// Animate images
function runAnimation(){
	// recalculate window information
	// in case user has resized, or scrolled
	WinHeight=(ns||ns6)?window.innerHeight-50:window.document.body.clientHeight-50;
	WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth;
	hscrll=(ns||ns6)?window.pageYOffset:document.body.scrollTop;
	wscrll=(ns||ns6)?window.pageXOffset:document.body.scrollLeft;
	// Step through number of independant animations
	for(i=0;i<Amount;i++){
		sy = Speed[i]*Math.sin(90*Math.PI/180);
		sx = Amplitude*Speed[i]*Math.cos(Cstep[i]);
		var flap = Math.floor(Math.random()*2.0);
		var showChar="A";
		var hideChar="B";
		if(flap){
			showChar="B";
			hideChar="A";
		}
		if(Rise) Ypos[i]-=sy;
		else Ypos[i]+=sy;
		Xpos[i]+=sx;
		if(Ypos[i]>WinHeight){
			Ypos[i]=-60;
			Xpos[i]=Math.round(Math.random()*WinWidth);
			Speed[i]=Math.random()*SpeedRange+BaseSpeed;
		}else if(Ypos[i]<-70){
			Ypos[i]=+WinHeight;
			Xpos[i]=Math.round(Math.random()*WinWidth);
			Speed[i]=Math.random()*SpeedRange+BaseSpeed;
		}
		if(ns){
			document.layers['sn'+showChar+i].left=Xpos[i];
			document.layers['sn'+showChar+i].top=Ypos[i]+hscrll;
			document.layers['sn'+hideChar+i].left=0;
			document.layers['sn'+hideChar+i].top=0;
		}else if(ns6){
			document.getElementById("si"+showChar+i).style.left=Math.min(WinWidth,Xpos[i]);
			document.getElementById("si"+showChar+i).style.top=Ypos[i]+hscrll;
			document.getElementById("si"+hideChar+i).style.left=0;
			document.getElementById("si"+hideChar+i).style.top=0;
		}else{
			eval("document.all.si"+showChar+i).style.left=Xpos[i];
			eval("document.all.si"+showChar+i).style.top=Ypos[i]+hscrll;
			eval("document.all.si"+hideChar+i).style.left=0;
			eval("document.all.si"+hideChar+i).style.top=0;
		}
		Cstep[i]+=Step[i];
	}
	setTimeout('runAnimation()',TimeGap);
}

// Place images in HTML
function initAnimation(inAmount,inBaseSpeed,inSpeedRange,inAmplitude,inBaseWobble,inWobbleRange,inTimeGap,inRise){
	Amount=inAmount;		// Number of images to place on screen
	BaseSpeed=inBaseSpeed;		// larger numbers increase speed of object movement
	SpeedRange=inSpeedRange;	// larger numbers increase range of different speeds
	Amplitude=inAmplitude;		// ratio of x-wobble to y-speed
	BaseWobble=inBaseWobble;	// larger numbers give tighter wobble
	WobbleRange=inWobbleRange;	// larger numbers give wider wobble range
	TimeGap=inTimeGap;		// time to wait between redraws (larger number slows animation)
	Rise=inRise;			// set to 0 for falling, and 1 for rising
	// Initial image positions
	for(i=0;i<Amount;i++){
		Ypos[i] = Math.round(Math.random()*WinHeight);
		Xpos[i] = Math.round(Math.random()*WinWidth);
		Speed[i]= Math.random()*SpeedRange+BaseSpeed;
		Cstep[i]=0;
		Step[i]=Math.random()*WobbleRange+BaseWobble;
	}
	// Place images in HTML document in <DIV> or <LAYER> tags
	if(ns){
		for(i=0;i<Amount;i++){
			var P=Math.floor(Math.random()*graphicsA.length);
			document.write("<LAYER NAME='snA"+i+"' LEFT=0 TOP=0><img src="+graphicsA[P]+"></LAYER>");
			document.write("<LAYER NAME='snB"+i+"' LEFT=0 TOP=0><img src="+graphicsB[P]+"></LAYER>");
		}
	}else{
		document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
		for(i=0;i<Amount;i++){
			var P=Math.floor(Math.random()*graphicsA.length);
			document.write('<img id="siA'+i+'" src="'+graphicsA[P]+'" style="position:absolute;top:0px;left:0px">');
			document.write('<img id="siB'+i+'" src="'+graphicsB[P]+'" style="position:absolute;top:0px;left:0px">');
		}
		document.write('</div></div>');
	}
}

// Put this in your HTML head or body
//<script language="JavaScript1.2" src="Flaps.js"></script>
// Put this in your HTML body
//<script>
//	addImage("Bat.gif","BatClosed.gif");
//	addImage("BatSmall.gif","BatClosedSmall.gif");
//	addImage("BBat.gif","BBatClosed.gif");
//	addImage("BBatSmall.gif","BBatClosedSmall.gif");
//	initAnimation(8,2,5,0.3,0.1,0.1,20,1);	// example good for flying bats
//	window.onload=runAnimation;
//</script>
