var snow = true;

var xCoords = new Array();
var yCoords = new Array(); 

// Animate.
function flakeFall() {
    if (!getRefToDivNest('snFlkDiv0')) { 
	return; 
    }
    var scrWidth = 0;
    var scrHeight = 0;
    var scrollHeight = 0;
    var scrollWidth = 0;
    // Find screen settings for all variations. doing this every time allows for resizing and scrolling.
    if (typeof(window.innerWidth) == 'number') { 
	scrWidth = window.innerWidth; 
	scrHeight = window.innerHeight; 
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	scrWidth = document.documentElement.clientWidth; 
	scrHeight = document.documentElement.clientHeight; 
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
	scrWidth = document.body.clientWidth; 
	scrHeight = document.body.clientHeight; 
    }
    if (typeof(window.pageYOffset) == 'number') { 
	scrollHeight = pageYOffset; 
	scrollWidth = pageXOffset; 
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { 
	scrollHeight = document.body.scrollTop; 
	scrollWidth = document.body.scrollLeft; 
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { 
	scrollHeight = document.documentElement.scrollTop; 
	scrollWidth = document.documentElement.scrollLeft;
    }
    // Move the snowflakes to their new position.
    for (var x = 0; x < numFlakes; x++) {
	if (yCoords[x] * scrHeight > scrHeight - pictureHeight) { 
	    yCoords[x] = 0; 
	}
	var divRef = getRefToDivNest('snFlkDiv' + x); 
	if (!divRef) { 
	    return; 
	}
	if (divRef.style) { 
	    divRef = divRef.style; 
	} 
	var oPix = document.childNodes ? 'px' : 0;
	divRef.top = (Math.round(yCoords[x] * scrHeight) + scrollHeight) + oPix;
	divRef.left = (Math.round(((xCoords[x] * scrWidth) - (pictureWidth / 2)) + ((scrWidth / ((numFlakes + 1) * 4)) * (Math.sin(lrFlakes * yCoords[x]) - Math.sin(3 * lrFlakes * yCoords[x])))) + scrollWidth) + oPix;
	yCoords[x] += downSpeed;
    }

    document.getElementById("flakes").style.visibility = "visible";
}

// DHTML handlers.
function getRefToDivNest(divName) {
    if (document.layers) { // NS4
	return document.layers[divName]; 
    } 
    if (document[divName]) { // NS4 also
	return document[divName]; 
    } 
    if (document.getElementById) { // DOM (IE5+, NS6+, Mozilla0.9+, Opera)
	return document.getElementById(divName); 
    } 
    if (document.all) { // Proprietary DOM - IE4
	return document.all[divName]; 
    } 
    return false;
}

var intervalId = null;

function startFlakes() {
    if (!snow) return;

    // Safety checks. Browsers will hang if this is wrong. If other values are wrong there will just be errors.
    if ((typeof(numFlakes) != 'number') || (Math.round(numFlakes) != numFlakes) || (numFlakes < 1)) { 
	numFlakes = 10; 
    }
    
    // Draw the snowflakes.
    var html = "";
    var i = 0;
    for (var x = 0; x < numFlakes; x++) {
	pictureSrc = pictures[i];
	i = (i + 1) % pictures.length;
	if (document.layers) { // Releave NS4 bug
	    html += '<layer id="snFlkDiv'+x+'" style="background-color: transparent;"><img src="'+pictureSrc+'" height="'+pictureHeight+'" width="'+pictureWidth+'" alt="*" border="0"></layer>';
	} else {
	    html += '<div style="position:absolute;z-index:20;background-color: transparent;" id="snFlkDiv'+x+'"><img src="'+pictureSrc+'" height="'+pictureHeight+'" width="'+pictureWidth+'" alt="*" border="0"></div>';
	}
    }
    document.getElementById("flakes").innerHTML = html;

    // Calculate initial positions (in portions of browser window size).
    var snFlkTemp;
    for (var x = 0; x < numFlakes; x++) {
	xCoords[x] = (x + 1) / (numFlakes + 1);
	do { 
	    snFlkTemp = Math.round((numFlakes - 1) * Math.random());
	} while (typeof(yCoords[snFlkTemp]) == 'number');
	yCoords[snFlkTemp] = x / numFlakes;
    }

    window.setInterval('flakeFall();', 50);
}

function stopFlakes() {
    if (!snow) return;

    try {
	window.clearInterval(intervalId);
	document.getElementById("flakes").innerHTML = "";
    } catch (error) {}    
}