// Filename: float.js
//
// Description: Floating images, not just snow!
//
//    Can use: float_time.js (so script only runs between specified dates)
//		float_0.gif, float_1.gif, float_2.gif, float_3.gif
//		float_4.gif, float_5.gif
//
// Tested in: Internet Explorer, Opera, Mozilla, Mozilla Firefox, Safari
//

/* fall Script Kurt Grigg - http://www.btinternet.com/~kurt.grigg/javascript */

//
// User Configurable Variables
//
// Stop After x seconds
var qTime = 180; // 3 minutes

//
// Flake Settings
//
// Number of Flakes - Number of actual files
var flakes = 2;

// Number of flakes on screen..
var sFlake = 6;

// Path to Flakes
//
// Note: If using on phpBB forum put a . infront of 1st /
//
var iPath = '/images/floaters/valentine/';
//
// Flake Images
//
var fImage = 'float_'; // the snowflake image postfixed with a number
var ImgExt = '.gif'; // ext of image

// Width and Height of Biggest Image used (THIS IS IMPROTANT)
var iWidth = 160;
var iHeight = 200;

// End User configuration
//
// Do not alter anything below here, unless you know what your doing.. ;)
//
//// Start Script
//
// Using IE?
//
// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();

// check for MS Internet Explorer
//
// Not tested in IE4 therefore not used
//var IE4 = (agt.indexOf('msie 4') != -1) ? 1 : 0;
var IE5 = (agt.indexOf('msie 5.0') != -1) ? 1 : 0;
var IE55 = (agt.indexOf('msie 5.5') != -1) ? 1 : 0;
var IE6 = (agt.indexOf('msie 6') != -1) ? 1 : 0;
var MSIE = (IE5 || IE55 || IE6) ? 1 : 0;

//
// co-ordinate and client variables...
var xp, yp, stx, sty;
var am, i;
var scr_iW, scr_iH;

// 
// 
iWidth = Math.round( ((iWidth / 4) * 1.5) + iWidth );
iHeight = Math.round( ((iHeight / 4) * 1.5) + iHeight );

// Convert time to milli-seconds
//
qTime *= 1000;
timerID = null;

// co-ordinate arrays
yp = new Array();
xp = new Array();
am = new Array();
stx = new Array();
sty = new Array();

//Pre-load images!
pics=new Array();
lpics = new Array();

for(i=0; i < flakes; i++) {
	pics[i] = iPath + fImage + i + ImgExt;
	lpics[i] = new Image();
	lpics[i].src = pics[i];
}

if (document.layers){
	for (i=0; i < sFlake; i++){
		randomFlake = pics[Math.floor(Math.random()*pics.length)];
		document.write('<LAYER NAME="flake' + i + '" LEFT="0" TOP="0"><img src="' + randomFlake + '" border="0"></LAYER>');
	}
} else if (document.all){
	document.write('<div style="position:absolute;top:0px;left:0px;z-index: 10"><div style="position:relative;z-index: 10">');

	for (i=0; i < sFlake; i++){
		randomFlake = pics[Math.floor(Math.random()*pics.length)];
		document.write('<img id="flake' + i + '" src="' + randomFlake + '" style="position:absolute;top:0px;left:0px">');
	}

	document.write('</div></div>');
} else {
	for (i=0; i < sFlake; i++){
		randomFlake = pics[Math.floor(Math.random()*pics.length)];
		document.write('<div id="flake' + i + '" style="position:absolute;z-index: 10"><img src="' + randomFlake + '" border="0"></div>');
	}
}

// Obtain width x height of client area of browser
scr_iW = (document.all) ? document.body.clientWidth : window.innerWidth;
scr_iH = (document.all) ? document.body.clientHeight : window.innerHeight;

// Populate co-ordinate arrays
for (i=0; i < sFlake; i++){                                                                
	yp[i] = Math.round(Math.random() * scr_iH);
	xp[i] = Math.round(Math.random() * scr_iW);
	am[i] = Math.random() * 5 + 3;
	stx[i] = 0;
	sty[i] = Math.random() * 0.1 + 0.05;
}

// the snow function
function fall(){
	scr_iW=(document.all) ? document.body.clientWidth : self.innerWidth;
	scr_iH=(document.all) ? document.body.clientHeight : self.innerHeight;
	scr_Yoff=(document.all) ? document.body.scrollTop : self.pageYOffset;
	scr_Xoff=(document.all) ? document.body.scrollLeft : self.pageXOffset;

	for (i=0; i < sFlake; i++){
		sy = am[i] * Math.sin(90*Math.PI/180);
		sx = am[i] * Math.cos(stx[i]);
		yp[i] += sy;
		xp[i] += sx; 

		stx[i] += sty[i];

		if(!MSIE)
		{
			if (yp[i] > (scr_iH - iHeight)){
				yp[i] = -(iHeight);
				xp[i] = Math.round(Math.random() * scr_iW);
				am[i] = Math.random() * 5 + 3;
			}

			if(xp[i] > (scr_iW - iWidth)) {
				xp[i] = 0;
			}
		}
		else
		{
			if (yp[i] > scr_iH){
				yp[i] = -(iHeight);
				xp[i] = Math.round(Math.random() * scr_iW);
				am[i] = Math.random() * 5 + 3;
			}
		}
		
		if (document.all)
		{
 			document.all["flake"+i].style.pixelLeft = xp[i];
			document.all["flake"+i].style.pixelTop = yp[i] + scr_Yoff;
		}
		else if(document.layers)
		{
			document.layers["flake"+i].left=xp[i];
			document.layers["flake"+i].top=yp[i] + scr_Yoff;
		}
		else
		{
			if(document.getElementById("flake"+i))
			{
				document.getElementById("flake"+i).style.left=xp[i];
				document.getElementById("flake"+i).style.top=yp[i] + scr_Yoff;
			}
		}

	}

	timerID = setTimeout('fall()',150);
}

// make it snow...
fall();

// make it not snow...
function dsbl(){
	for (i=0; i < sFlake; i++){

		if (document.layers) {
			document.layers["flake"+i].visibility="hide";
		}
 		else if (document.all)
 		{
 			document.all["flake"+i].style.visibility="hidden";
 		}
 		else
 		{
 			document.getElementById("flake"+i).style.visibility="hidden";
 		}
	}

	clearTimeout(timerID);
}

// set timeout for timer
setTimeout('dsbl()',qTime);