////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Snowfall script by Silas Landricombe
// December 2009
// currently supports: IE versions 6, 7, 8 : Firefox : Safari
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// EDIT VARIABLES //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// these arrays control the style of the snowfall use as many or as few as options as you like
var snowchars = new Array('*');
var snowfont = new Array('Arial Black','Arial','Times');
var snowcols = new Array('#cccccc','#999999');

// sets the min and max font size to be used this also controls speed as the snow falls at a speed related to it's size
var maxsize = 24;
var minsize = 8;

//  this controls how heavy the snow falls a lower number will increase the heaviness of the fall. you will need to adjust the maxflakes variable as well so I recommend leaving it at 50 
var weight = 50;

// this set's the maximum number of flakes cycling, I wouldn't advise setting abvoe 40 / 45
var maxflakes = 45;

// use this to set a constraining element for the snow. element used must have width and height set and position must be set to absolute or relative. If you want fullscreen leave it empty
var constrain = '';

////////////////////////////////////////////////////
// DO NOT EDIT BELOW THIS LINE /////////////////////

// get page dimensions
var pagewidth = window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
var pageheight = window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
// offset for scrollbar
pagewidth = pagewidth - 20;
pageheight = pageheight - 36;

// fixed vars
var snow = new Array();
var timer;
var startpoint = '0px';

// adjusts points if a constraining element is set
if(constrain != ''){
	pagewidth = document.getElementById(constrain).offsetWidth;
	pageheight = document.getElementById(constrain).offsetHeight;
}

// creates snow array and sets controlling styles
function startsnowing() {
	for (i = 0; i <= maxflakes; i++) {
		snow[i] = document.getElementById("s" + i);
		
		snow[i].style.fontFamily = snowfont[randomise(snowfont.length - 1)];
		snow[i].style.fontSize = randomise(maxsize, minsize) + 'px';
		snow[i].style.color = snowcols[randomise(snowcols.length - 1)];
		
		snow[i].style.display = 'none';
		snow[i].style.left = randomise(pagewidth) + 'px';
		snow[i].style.top = startpoint;
	}
	
	// starts snowfall
	movesnow();
}

// snow movement functions creates random left and right movement as well as controlling the fall
function movesnow() {
	for (i = 0; i <= maxflakes; i++) {
		if(parseInt(snow[i].style.top) == parseInt(startpoint) && randomise(weight) == 1 || parseInt(snow[i].style.top) != parseInt(startpoint)){
			
			if(parseInt(snow[i].style.left) < (pagewidth - maxsize) - 2){
				snow[i].style.left = (randomise(parseInt(snow[i].style.left) + 2, parseInt(snow[i].style.left) - 2)) + 'px';
			} else {
				snow[i].style.left = (randomise(parseInt(snow[i].style.left), parseInt(snow[i].style.left) - 2)) + 'px';
			}
			
			if (parseInt(snow[i].style.top) < pageheight){
				snow[i].style.display = 'block';
				snow[i].style.top = (parseInt(snow[i].style.top) + (parseInt(snow[i].style.fontSize) / 4)) + 'px';
			} else {
				snow[i].style.top = startpoint;
				snow[i].style.left = randomise(pagewidth) + 'px';;
				snow[i].style.display = 'none';
			}
		}
	}
	
	// loops function every 50 milliseconds
	timer = setTimeout("movesnow()", 50);
}

// creates physical sprites on stage
function createSnow(){
	for (i = 0; i <= maxflakes; i++) {
		var snowflake = document.createElement("span");
		snowflake.id = 's' + i; snowflake.style.display = 'none'; snowflake.style.position = 'absolute';
		snowflake.innerHTML = snowchars[randomise(snowchars.length - 1)];
		if(constrain != ''){
			document.getElementById(constrain).appendChild(snowflake);
		} else {
			document.body.appendChild(snowflake);
		}
	}
	startsnowing();
}

// initiates the function on page load or as a click function - delete as appropriate
/////////////////////////////////////////////////////////////////////////////////////////////////
//window.onload=createSnow;
/////////////////////////////////////////////////////////////////////////////////////////////////
var isnowing = 0;
function toggleSnow(){
	if(isnowing == 0){
		createSnow();
		isnowing = 1;
	} else {
		clearTimeout(timer);
		isnowing = 0;
		for (i = 0; i <= maxflakes; i++) {
			snow[i] = document.getElementById("s" + i);
			snow[i].style.display = 'none';
			snow[i].style.left = randomise(pagewidth) + 'px';
			snow[i].style.top = startpoint;
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////

// returns a random number between set range
function randomise(high, low) {	
	if(!low){ low = 0; }
	rand = Math.floor(Math.random() * (high - low + 1) + low)
	return rand;
}
