/*
Flying Butterfly script (By BGAudioDr@aol.com)
Modified slightly/ permission granted to Dynamic Drive to feature script in archive
*/

var Ymax=8;			//MAX # OF PIXEL STEPS IN THE "X" DIRECTION
var Xmax=8;			//MAX # OF PIXEL STEPS IN THE "Y" DIRECTION
var Tmax=5000;		//MAX # OF MILLISECONDS BETWEEN PARAMETER CHANGES

//FLOATING IMAGE URLS FOR EACH IMAGE. ADD OR DELETE ENTRIES. KEEP ELEMENT NUMERICAL ORDER STARTING WITH "0" !!

var floatimages=new Array();
floatimages[0]='./templates/subSilver/images/item0.gif';
floatimages[1]='./templates/subSilver/images/item1.gif';
floatimages[2]='./templates/subSilver/images/item2.gif';
floatimages[3]='./templates/subSilver/images/item3.gif';
floatimages[4]='./templates/subSilver/images/item4.gif';
floatimages[5]='./templates/subSilver/images/item5.gif';

//*********DO NOT EDIT BELOW***********
var IE4 = (document.all) ? true : false;
var wind_w, wind_h, t = '', IDs = new Array(), startfly = new Array();

function moveimage(num)
{
	if ( getidleft(num) + IDs[num].W + IDs[num].Xstep >= wind_w + getscrollx() - 20 )
	{
		IDs[num].Xdir = false;
	}
	if ( getidleft(num) - IDs[num].Xstep <= getscrollx() )
	{
		IDs[num].Xdir = true;
	}
	if ( getidtop(num) + IDs[num].H + IDs[num].Ystep >= wind_h + getscrolly() )
	{
		IDs[num].Ydir=false;
	}
	if ( getidtop(num) - IDs[num].Ystep <= getscrolly() )
	{
		IDs[num].Ydir=true;
	}
	moveidby(num, (IDs[num].Xdir) ? IDs[num].Xstep :  -IDs[num].Xstep, (IDs[num].Ydir) ? IDs[num].Ystep: -IDs[num].Ystep);
}

function hidebutterfly(num)
{
	document.getElementById("pic"+num).style.visibility = 'hidden';
	clearInterval(startfly[num]);
	//document.getElementById("bgsound").src = "./halloween.wav";
}

function getnewprops(num)
{
	IDs[num].Ydir	= Math.floor(Math.random() * 2) > 0;
	IDs[num].Xdir	= Math.floor(Math.random() * 2) >0;
	IDs[num].Ystep	= Math.ceil(Math.random() * Ymax);
	IDs[num].Xstep	= Math.ceil(Math.random() * Xmax);
	setTimeout('getnewprops('+num+')', Math.floor(Math.random() * Tmax));
}

function getscrollx(){
if(IE4)return document.body.scrollLeft;
return window.pageXOffset;
}

function getscrolly(){
if(IE4)return document.body.scrollTop;
return window.pageYOffset;
}

function getid(name){
return document.getElementById(name);
}

function moveidto(num,x,y){
IDs[num].style.left=x+'px';
IDs[num].style.top=y+'px';
}

function getidleft(num){
return parseInt(IDs[num].style.left);
}

function getidtop(num){
return parseInt(IDs[num].style.top);
}

function moveidby(num,dx,dy){
IDs[num].style.left=(getidleft(num)+dx)+'px';
IDs[num].style.top=(getidtop(num)+dy)+'px';
}

function getwindowwidth(){
if(IE4)return document.body.clientWidth;
return window.innerWidth;
}

function getwindowheight(){
if(IE4)return document.body.clientHeight;
return window.innerHeight;
}

function initfly(){
}

for ( i = 0; i < floatimages.length; i++)
{
	t += '<div id="pic'+i+'" style="position:absolute; visibility:hidden;">';
	t += '<img src="'+floatimages[i]+'" name="p'+i+'" border="0" onclick="hidebutterfly('+i+');">';
	t += '</div>';
}
document.write(t);

wind_w = getwindowwidth();
wind_h = getwindowheight();

for ( i = 0; i < floatimages.length; i++)
{
	IDs[i]		= getid('pic'+i);
	IDs[i].W	= document.images["p"+i].width;
	IDs[i].H	= document.images["p"+i].height;
	getnewprops(i);
	moveidto(i , Math.floor(Math.random()*(wind_w-IDs[i].W)), Math.floor(Math.random()*(wind_h-IDs[i].H)));
	IDs[i].style.visibility = "visible";
	startfly[i]	= setInterval('moveimage('+i+')', Math.floor(Math.random()*100)+100);
}

window.onresize=function(){ wind_w=getwindowwidth(); wind_h=getwindowheight(); }
