var snowflakes;
var mouseAdd = 0;  // this specifies direction of snowing, either left or right according to mouse position

function moveSnowflakes() {
	for (var i in snowflakes.items) {
		snowflakes.items[i].move(i);
	}
}

function classSnowflake() {
	return {
		x: null,
		originX: null,
		y: null,
		step: 0,
		speed: null,
		img: null,
		
		init: function (x, y) {
			this.x = this.originX = x;
			this.y = y;
			this.speed = Math.random()*1 + 1;
			this.step = Math.random() * Math.PI / 2;
			
			this.img = document.createElement('img');
			
			YAHOO.util.Dom.setAttribute(this.img, 'src', 'http://hosting.icontio.com/~vyvoj/jezisek/img/snowflake' + (Math.round(Math.random() * 2) + 1) + '.png');
			YAHOO.util.Dom.addClass(this.img, 'snowflake');
			YAHOO.util.Dom.setStyle(this.img, 'left', this.x + 'px');
			YAHOO.util.Dom.setStyle(this.img, 'top', this.y + 'px');
			
			document.body.appendChild(this.img);
		},
		
		move: function (index) {
			this.originX += mouseAdd;
			this.y += this.speed;
			this.x = this.originX + Math.sin(this.step) * 10;
			this.step += 0.1;
			YAHOO.util.Dom.setStyle(this.img, 'left', this.x + 'px');
			YAHOO.util.Dom.setStyle(this.img, 'top', this.y + 'px');
			if (this.y >= snowflakes.border[3] || this.x >= snowflakes.border[2]) {
				document.body.removeChild(this.img);
				snowflakes.items.splice(index, 1);
				
				var snowflake = new classSnowflake();
				snowflake.init(
					Math.random() * (snowflakes.border[2] - snowflakes.border[0]) + snowflakes.border[0],
					0
				);
				snowflakes.items.push(snowflake);
			}
		}
	}
}

YAHOO.util.Event.onDOMReady(
	function() {
		// global variable, use next lines to configure snowing
		snowflakes = {
			// collection of falling snowflakes
			items: [],
			// area we want snowflakes at
			border: [0, 0, YAHOO.util.Dom.getDocumentWidth() - 20, 290],
			// amount of snowflakes on screen
			amount: 100,
			// aprox. speed of snowflakes, the smaller the faster
			speed: 100
		};
		
		// creation of snowflakes at the begining
		for (var i = 0; i < snowflakes.amount; i++) {
			var snowflake = new classSnowflake();
			snowflake.init(
				Math.random() * (snowflakes.border[2] - snowflakes.border[0]) + snowflakes.border[0],
				Math.random() * (snowflakes.border[3] - snowflakes.border[1]) + snowflakes.border[1]
			);
			snowflakes.items.push(snowflake);
		}
		
		// let's move it
		setInterval('moveSnowflakes()', snowflakes.speed);
		
		YAHOO.util.Event.addListener(
			document,
			'mousemove',
			function(e) {
				var width = YAHOO.util.Dom.getDocumentWidth();
				var posX = YAHOO.util.Event.getPageX(e);
				mouseAdd = (posX - width/2) / (width/2) * 2;
			}
		);
	}
);
