/**
 * Script schnee.js
 * Author: Andreas Damm
 * Date: 2009-01-11
 * Description: let the snowflakes fall!
 * Usage: Schnee.init() inside document.body, Schnee.run() after document.body has loaded
 */
var Schnee = {
	number: 40,	// number of snowflakes
	img_src: 'images/snow.gif',
	doc_width: 800,
	doc_height: 600,
	t: {},		// falling timer
	dt: {},		// falling speed
	x0: {},		// x-position origin
	y0: {},		// y-position origin
	amp: {},	// swinging amplitude
	freq: {},	// swinging frequence
	wind: 0,	// wind strength
	timer_run: null,	// timer reference run
	timer_wind: null,	// timer reference wind
	
	init: function() {
		// write snowflakes to document
		for (var i = 0; i < this.number; i++) {
			document.write('<img id="dot'+ i +'" style="opacity:.6;-moz-opacity:.6;filter:alpha(opacity=60);position:absolute;z-index:'+ (i * 1000) +';visibility:hidden;left:10px;top:10px;" src="'+ this.img_src +'" border="0">');
		}
	},
	run: function() {
		var dot = document.getElementById('dot0');
		// test initialisation
		if (!dot) return;
		// store document size
		this.doc_width  = document.body.offsetWidth;
		this.doc_height = document.body.offsetHeight;
		// show snowflakes
		for (var i = 0; i < this.number; i++) {  
			this.reset_flocke(i);
			this.y0[i] = Math.random() * this.doc_height;
			dot = document.getElementById('dot'+ i);
			dot.style.visibility = 'visible';
			dot.style.left = parseInt(this.x0[i]) +'px';
			dot.style.top  = parseInt(this.y0[i]) +'px';
		}
		// start snowflake falling
		this.timer_run = window.setInterval('Schnee.iterate()',40);
		// set wind strength
		Schnee.set_wind();
		// start wind changing
		this.timer_wind = window.setInterval('Schnee.set_wind()',20000);
	},
	reset_flocke: function(i) {
		this.t[i] = this.y0[i] = 0; //reset time, y-position
		this.x0[i] = Math.random() * (this.doc_width - 200) + 80; //reset x-position
		this.dt[i] = .5 + 1 * Math.random(); //reset falling speed
		this.amp[i] = 5 + 5 * Math.random(); // reset swinging amplitude
		this.freq[i] = 20 + 10 * Math.random(); // reset swinging frequence
	},
	set_wind: function() {
		var newWind = 1 * Math.random() - .5; // new wind strength
		var changetime = 5000; // time to change to new wind strength
		var steps = 100; // steps to change to new wind strength
		var step = 0; // current step
		// change wind strength over time
		while (step++ < steps) {
			window.setTimeout('Schnee.wind='+ ((newWind - this.wind) * (1 - Math.cos(step * Math.PI / steps)) / 2 + this.wind), (changetime / steps) * step);
		}
	},
	iterate: function() {
		// store document size
		this.doc_width  = document.body.offsetWidth;
		this.doc_height = document.body.offsetHeight;
		// let the snowflakes fall
		for (var i = 0; i < this.number; i++) {
			var dot = document.getElementById('dot'+ i);
			if (parseInt(dot.style.top) > this.doc_height - 2 * dot.offsetHeight
				|| parseInt(dot.style.left) > this.doc_width - 2 * dot.offsetWidth - 50
				|| parseInt(dot.style.left) < 2 * dot.offsetWidth
			) {
				// reset snowflake if it is out of left, right or bottom border
				this.reset_flocke(i);
			} else {
				// increase falling timer by falling speed
				this.t[i] += this.dt[i];
			}
			// set new snowflake position [x, y] = [ amplitude * sin(PI / frequence * time) + x-position-origin + windstrength * time, time + y-position-origin ]
			dot.style.left = parseInt(this.amp[i] * Math.sin(Math.PI / this.freq[i] * this.t[i]) + this.x0[i] + this.wind * this.t[i]) +'px';
			dot.style.top  = parseInt(this.t[i] + this.y0[i]) +'px';
		}
	},
	stop_wind: function() {
		if(this.timer_wind != null) window.clearInterval(this.timer_wind);
	},
	stop: function() {
		this.stop_wind();
		if(this.timer_run != null) window.clearInterval(this.timer_run);
	},
	hide: function() {
		this.stop();
		for (var i = 0; i < this.number; i++) {
			dot = document.getElementById('dot'+ i);
			dot.style.visibility = 'hidden';
		}
	}
}
