//
//	snows.js
//
//
//	 Snows will fall if you load this file at page top and call Start()
//	at page bottom.
//
//	 Checked browsers are: IE6 / Mozilla1.7.3 / Opera7.53.
//
//	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html4/loose.dtd">
//


// Configuration Variables
MaxLayer = 16;								// number of snows
TimeSlice = 41;								// time slice of updating snows
Frequency = 0.015;							// frequency of fluctuation of snows
Amplitude = 0.5;							// amplitude of fluctuation of snows
SnowTypes = 4;								// number of snow types
SnowImage = new Array();
SnowImage[0] = "./snow0.gif";				// snow image file / slow
SnowImage[1] = "./snow1.gif";				// snow image file
SnowImage[2] = "./snow2.gif";				// snow image file
SnowImage[3] = "./snow3.gif";				// snow image file / fast

// Variable Arrays
snow_element = new Array();					// snow layer element
snow_type = new Array();					// snow type
snow_left = new Array();					// snow left position
snow_top = new Array();						// snow top position
snow_width = new Array();					// snow layer width
snow_height = new Array();					// snow layer height
snow_radian = new Array();					// snow radian
snow_amplitude = new Array();				// snow amplitude
snow_frequency = new Array();				// snow frequency


// Varaibles
var loop;									// temporary for loop
var client_width, client_height;			// client screen size
var holizontal_offse, vertical_offse;		// scroll offset positions
var timer;									// timer handler


document.open();
for (loop = 0; loop < MaxLayer; loop++)
{
	snow_type[loop] = Math.floor(Math.random() * SnowTypes);
	document.write('<div id="Lay' + loop + '" style="position: absolute; margin: 0px; padding: 0px; visibility: hidden;">');
	document.write('<img id="Img' + loop + '" src="' + SnowImage[snow_type[loop]] + '" border="0">');
	document.write('</div>');
}
document.close();


// Start snows falling !
function Snows()
{
	client_width = GetWidth();
	client_height = GetHeight();

	for (loop = 0; loop < MaxLayer; loop++)
	{
		snow_element[loop] = document.getElementById('Lay' + loop);
		snow_width[loop] = snow_element[loop].clientWidth;
		snow_height[loop] = snow_element[loop].clientHeight;
		snow_left[loop] = Math.floor(Math.random() * (client_width - snow_width[loop]));
		snow_top[loop] = Math.floor(Math.random() * (client_height - snow_height[loop]));
		snow_radian[loop] = Math.random() * (Math.PI * 2);
		snow_frequency[loop] = Frequency * (SnowTypes - snow_type[loop]);
		snow_amplitude[loop] = Amplitude * (snow_type[loop] + 1);
		snow_element[loop].style.visibility = 'visible';
		snow_element[loop].style.zIndex = snow_type[loop];
		snow_element[loop].style.left = snow_left[loop] + "px";
		snow_element[loop].style.top = snow_top[loop] + "px";
	}

	window.onresize = GetWindowSize;
	timer = setInterval("MoveSnows()", TimeSlice);
}


// Moving snows
function MoveSnows()
{
	holizontal_offse = document.documentElement.scrollLeft;
	vertical_offse = document.documentElement.scrollTop;

	for (loop = 0; loop < MaxLayer; loop++)
	{
		snow_radian[loop] += snow_frequency[loop];
		snow_left[loop] += Math.sin(snow_radian[loop]) * snow_amplitude[loop];
		snow_top[loop] += snow_type[loop] + 1;

		if (snow_top[loop] < vertical_offse) {
			snow_top[loop] += (client_height - snow_height[loop]);
			snow_left[loop] = Math.floor(Math.random() * (client_width - snow_width[loop])) + holizontal_offse;
		}
		if (snow_top[loop] >= (client_height + vertical_offse - snow_height[loop]))
		{
			snow_top[loop] -= (client_height - snow_height[loop]);
			snow_left[loop] = Math.floor(Math.random() * (client_width - snow_width[loop])) + holizontal_offse;
		}

		if (snow_left[loop] < holizontal_offse) {
			snow_left[loop] += (client_width - snow_width[loop]);
		}
		if (snow_left[loop] >= (client_width - snow_width[loop] + holizontal_offse)) {
			snow_left[loop] -= (client_width - snow_width[loop]);
		}

		snow_element[loop].style.left = snow_left[loop] + "px";
		snow_element[loop].style.top = snow_top[loop] + "px";
	}
}


// WindowResize Event Callback
function GetWindowSize()
{
	client_width = GetWidth();
	client_height = GetHeight();

	for (loop = 0; loop < MaxLayer; loop++)
	{
		snow_left[loop] = Math.floor(Math.random() * (client_width - snow_width[loop])) + holizontal_offse;
		snow_top[loop] = Math.floor(Math.random() * (client_height - snow_height[loop])) + vertical_offse;

		snow_radian[loop] = Math.random() * Math.PI;
		snow_element[loop].style.left = snow_left[loop] + "px";
		snow_element[loop].style.top = snow_top[loop] + "px";
	}
}


// Get client width
function GetWidth() {
	if (window.opera)
		return document.body.clientWidth;
	else
		return document.documentElement.clientWidth;
}


// Get client height
function GetHeight() {
	if (window.opera)
		return document.body.clientHeight;
	else
		return document.documentElement.clientHeight;
}
