var SnowFx = new Object();

	SnowFx.timer = null;
	SnowFx.moveDeltaMilliseconds = 35;
	SnowFx.flakeImageUrl = 'http://james.j-servers.net/media/snowflake.gif';
	SnowFx.flakeParent = null;
	SnowFx.realDocumentWidth = 0;
	SnowFx.realDocumentHeight = 0;
	
	SnowFx.numFlakes = 65;
	SnowFx.minOpacity = 0.85;
	SnowFx.maxOpacity = 0.97;
	SnowFx.minSize = 18.0;
	SnowFx.maxSize = 25.0;
	SnowFx.minSpeed = 2.0;
	SnowFx.maxSpeed = 3.5;
	SnowFx.minWindDisplacement = 10.0;
	SnowFx.maxWindDisplacement = 90.0;

	SnowFx.opacityRange = 0;
	SnowFx.sizeRange = 0;
	SnowFx.speedRange = 0;
	SnowFx.windDisplacementRange = 0;
	
	SnowFx.flakes = new Array();
	
	SnowFx.init = function() {
		var i, flake, flakeCount;
		
		this.flakeParent = document.body;
		this.realDocumentWidth = document.body.clientWidth;
		this.realDocumentHeight = document.body.clientHeight;
		
		this.opacityRange = SnowFx.maxOpacity - SnowFx.minOpacity;
		this.sizeRange = SnowFx.maxSize - SnowFx.minSize;
		this.speedRange = SnowFx.maxSpeed - SnowFx.minSpeed;
		this.windDisplacementRange = SnowFx.maxWindDisplacement - SnowFx.minWindDisplacement;
		
		flakeCount = this.numFlakes;
		for(i = 0; i < flakeCount; i++) {
			flake = new Snowflake();
			flake.init();
			this.flakes.push(flake);
		}
		
		SnowFx.timer = setTimeout(SnowFx.move, SnowFx.moveDeltaMilliseconds);
	}
	
	SnowFx.move = function() {
		var i, flake, flakeCount;
		
		flakeCount = SnowFx.numFlakes;
		for(i = 0; i < flakeCount; i++) {
			flake = SnowFx.flakes[i];
			flake.y += flake.yDelta;
			flake.x += flake.xDelta;
			if (flake.y > SnowFx.realDocumentHeight) {
				flake.x = (Math.random() * (SnowFx.realDocumentWidth + (2 * SnowFx.maxWindDisplacement))) - SnowFx.maxWindDisplacement;
				flake.y = -30;
			}
			flake.render();
		}

		SnowFx.timer = setTimeout(SnowFx.move, SnowFx.moveDeltaMilliseconds);
	}
	
	SnowFx.end = function() {
		var i;
		
		for(i = 0; i < this.flakes.length; i++) {
			this.flakes[i].destroy();
		}

		this.flakes = new Array();
		
		if (this.timer) {
			clearTimeout(this.timer);
		}
	}
		
var Snowflake = function() {
	this.x = 0.0;
	this.y = 0.0;
	this.size = 0;
	this.opacity = 0.0;
	this.element = null;
	
	this.xDelta = 0.3;
	this.yDelta = 0;
}
	
	Snowflake.prototype.init = function() {
		this.x = (Math.random() * (SnowFx.realDocumentWidth + (2 * SnowFx.maxWindDisplacement))) - SnowFx.maxWindDisplacement;
		this.y = Math.random() * SnowFx.realDocumentHeight * (-1);
		this.size = SnowFx.minSize + (Math.random() * SnowFx.sizeRange);
		this.opacity = SnowFx.minOpacity + (((this.size - SnowFx.minSize)/(SnowFx.sizeRange)) * (SnowFx.opacityRange));
		
		this.element = document.createElement('img');
		SnowFx.flakeParent.appendChild(this.element);
		this.element.style.position = 'absolute';
		this.element.src = SnowFx.flakeImageUrl;
		
		this.element.style.height = this.size + 'px';
		this.element.style.width = this.size + 'px';
		
		this.xDelta = 0.3;
		this.yDelta = SnowFx.minSpeed + (((this.size - SnowFx.minSize)/SnowFx.sizeRange) * SnowFx.speedRange);
		
		this.element.style.opacity = this.opacity;
		this.element.style.MozOpacity = this.opacity;
		this.element.style.KHTMLOpacity = this.opacity;
		this.element.style.filter = 'alpha(opacity:' + (this.opacity * 100) + ')';
		
		this.render();
	}
	
	Snowflake.prototype.render = function() {
		if ( (this.x > (0 - this.size)) && (this.x < SnowFx.realDocumentWidth) ) {
			this.element.style.display = 'block';
			this.element.style.left = this.x + 'px';
		}
		else {
			this.element.style.display = 'none';
		}
		
		if ( (this.y > (0 - this.size)) && (this.y < SnowFx.realDocumentHeight) ) {
			this.element.style.display = 'block';
			this.element.style.top = this.y + 'px';
		}
		else {
			this.element.style.display = 'none';
		}
	}
	
	Snowflake.prototype.destroy = function() {
		this.element.parentNode.removeChild(this.element);
		this.element = null;
	}
