function snowstorm (id, scalefactor, intensity, settle, debug) {
	var SNOW = [],
		SETTLED_SNOW = 0,
		MAX_SNOW,
		SETTLE = !!settle,
		SNOWSTORM = document.getElementById(id),
		WIDTH = $(window).width(),
		HEIGHT = $("#" + id).height(),
		SCALE_FACTOR = scalefactor,
		PROBABILITY = intensity,
		SNOW_MACHINE_ON = true,
		SNOW_IMAGES = [],
		timer,
		publicmethods = {
			start: function () {
				if (!timer) {
					timer = setTimeout (loop, 1000/30);
				}
			},
			stop: function () {
				clearTimeout (timer);
				timer = undefined;
			},
			addImage: function (src) {
				var image = new Image ();
				image.src = src;
				SNOW_IMAGES.push (image);
			}
		};

	function random (min, max) {
		return Math.random () * (max - min) + min;
	}

	function Vector (x, y) {
		if (!(this instanceof Vector)) {
			return new Vector (x, y);
		}
		this.x = x;
		this.y = y;
	}
	Vector.prototype = {
		add: function (vector) {
			this.x += vector.x;
			this.y += vector.y;
		}
	};

	function snowflake (image) {
		if (!(this instanceof snowflake)) {
			return new snowflake ();
		}

		var position = new Vector (random (0, WIDTH), 0),
			velocity = new Vector (random (-1, 1), random(2,3)),
			scale = SCALE_FACTOR * random (50, 200) / 100,
			publicmethods,
			tag;

		this.settled = false;
		tag = document.createElement("img");
		tag.src = image.src;
		tag.width = image.width * scale;
		tag.height = image.height * scale;
		tag.style.position = "absolute";
		tag.style.transform = "rotate(" + random(0, 359) + "deg)";
		SNOWSTORM.appendChild(tag);

		publicmethods = {
			update: function () {
				if (!this.settled) {
					position.add(velocity);
				}
				if (position.x < 0 - tag.width) {
					position.x = WIDTH;
				} else if (position.y > WIDTH) {
					position.x = 0;
				}
				if (position.y > HEIGHT - tag.height/2) {
					if (!settle || SETTLED_SNOW > 50) {
						SNOW_MACHINE_ON = false;
						position.y = 0;
					} else {
						this.settled = true;
						SETTLED_SNOW++;
					}
				}
			},
			draw: function (context) {
				tag.style.top = (~~position.y) + 'px';
				tag.style.left = (~~position.x) + 'px';
			}
		};


		return publicmethods;
	}


	function addSnow () {
		if (SNOW_MACHINE_ON && random(0, 100) < PROBABILITY ) {
			var rand = ~~random(0, SNOW_IMAGES.length);
			var flake = new snowflake (SNOW_IMAGES[rand]);
			SNOW.push (flake);
		}
	}

	function loop () {
		var flake,
			length = SNOW.length,
			i;
		addSnow ();
		for (i = 0; i < length; i++) {
			flake = SNOW[i];
			if (flake) {
				if (flake.settled) {
					SNOW.splice(i, 1);
					continue;
				}
				flake.update ();
				flake.draw ();
			}
		}
		timer = setTimeout (loop, 1000/30);
	}

	$(window).resize (function (e) {
		WIDTH = $(window).width();
	});

	return publicmethods;

}




$(function () {
	if ($("body").hasClass('home')) {
		if (document.getElementById('background')) {
			var storm = snowstorm ("background", 0.2, 10);
			storm.addImage ('/_assets/images/snow/blur.png');
			storm.addImage ('/_assets/images/snow/star.png');
			storm.start();
		}
		if (document.getElementById("background-2")) {
			var storm2 = snowstorm ("background-2", 0.3, 15, true);
			storm2.addImage ('/_assets/images/snow/blur.png');
			// storm2.addImage ('/_assets/images/snow/star.png');
			storm2.start();
		}
		if (document.getElementById("background-3")) {
			var storm3 = snowstorm ("background-3", 0.3, 10, true);
			storm3.addImage ('/_assets/images/snow/blur.png');
			// storm3.addImage ('/_assets/images/snow/star.png');
			storm3.start();
			var storm4 = snowstorm ("background-3", 0.5, 5, true);
			storm4.addImage ('/_assets/images/snow/blur.png');
			// storm4.addImage ('/_assets/images/snow/star.png');
			storm4.start();
		}
		if (document.getElementById("cityscape")) {
			var storm5 = snowstorm ("cityscape", 0.2, 20);
			storm5.addImage ('/_assets/images/snow/blur.png');
			storm5.start();

		}
	}

});


