// Snowflake script, transformed to PrototypeJS 1.6
// Found various resources, where the snow code always looks similar
// December 2010

var ProtoSnow =
{
	imgSrc:			"res/img/snowflakes/snow0.gif",

	sfCount:		48,		// number of snowflakes
	sfSpeed:		30,		// speed, smaller numbers are faster (20 - 50)
	sfRndSize:		true,	// random size of snowflakes

	sfOrgWidth:		0,		// original size of selected image
	sfOrgHeight:	0,

	sfPrefId:		"sn_fl_",
	sfContId:		"sn_fl_cont",
	sfCode:			"",

	brIEoNS:		0,
	brMOZ:			0,
	brIE7:			0,

	brWidth:		0,		// browser window size
	brHeight:		0,

	dx:				[],		// for SIN
	xp:				[],		// pos X
	yp:				[],		// pos Y
	am:				[],		// amplitude
	stx:			[],		// step variables
	sty:			[],

	timerObj:		null,
	timerFunc:		"ProtoSnowTimer()",


	setup: function()
	{
		this.getWindowSize();

		var sfCont = document.createElement("div");
		Element.extend(sfCont);
		sfCont.writeAttribute("id", this.sfContId);
		document.body.appendChild(sfCont);

		this.sfCode = "";

		for(var i = 0; i < this.sfCount; i++)
		{
			this.dx[i] = 0;
			this.xp[i] = (Math.random() * (this.brWidth - 50)) + 15;
			this.yp[i] = Math.random() * this.brHeight;
			this.am[i] = Math.random() * 20;
			this.stx[i] = 0.02 + Math.random() / 10;
			this.sty[i] = 0.7 + Math.random();

			this.sfCode += '<div id="' + this.sfPrefId + i + '" style="position:absolute; z-index:' + (500+i);
			this.sfCode += '; visibility:visible; top:' + this.yp[i] + 'px; left:' + this.xp[i] + 'px;">';
			this.sfCode += '<img src="' + this.imgSrc + '" border="0" /></div>';
		}
	},

	fallDown: function()
	{
		if(!this.timerObj) return;

		for(var i = 0; i < this.sfCount; i++)
		{
			this.yp[i] += this.sty[i];

			if(this.yp[i] > this.brHeight - 40)
			{
				this.xp[i] = Math.random() * (this.brWidth - this.am[i] - 30);
				this.yp[i] = 0;
				this.stx[i] = 0.02 + Math.random() / 10;
				this.sty[i] = 0.7 + Math.random();

				if(this.sfRndSize) this.resizeFlake(this.sfPrefId + i);
			}

			this.dx[i] += this.stx[i];

			$(this.sfPrefId + i).setStyle({
				top:	this.yp[i] + "px",
				left:	(this.xp[i] + this.am[i] * Math.sin(this.dx[i])) + "px"
			});
		}

		this.timerObj = setTimeout(this.timerFunc, this.sfSpeed);
	},

	start: function()
	{
		if(!this.timerObj && this.sfCode) {
			$(this.sfContId).update(this.sfCode);
			this.timerObj = setTimeout(this.timerFunc, this.sfSpeed);
		}
	},

	stop: function()
	{
		if(this.timerObj) clearTimeout(this.timerObj);
		this.timerObj = null;
		$(this.sfContId).update("");
	},

	getWindowSize: function()
	{
		this.brIEoNS = (document.body.clientHeight) ? 1 : 0;
		this.brMOZ = (self.innerWidth) ? 1 : 0;
		this.brIE7 = (document.documentElement.clientHeight) ? 1 : 0;

		if(this.brIEoNS) {
			this.brWidth = document.body.clientWidth;
			this.brHeight = document.body.clientHeight;

		} else if (this.brMOZ) {
			this.brWidth = self.innerWidth - 20;
			this.brHeight = self.innerHeight;

		} else if(this.brIE7) {
			this.brWidth = document.documentElement.clientWidth;
			this.brHeight = document.documentElement.clientHeight;
		}
	},

	resizeFlake: function(id)
	{
		var sf = $(id).firstDescendant();	// get img

		if(!this.sfOrgWidth)
		{
			this.sfOrgWidth = sf.getWidth();
			this.sfOrgHeight = sf.getHeight();
		}

		var f = 0.5 + Math.random() / 2;
		if(f > 0.9) f = 1;

		sf.writeAttribute("width", f * this.sfOrgWidth);
		sf.writeAttribute("height", f * this.sfOrgHeight);
	}
};


function ProtoSnowTimer() { ProtoSnow.fallDown(); }


// wait until the page has been loaded
Event.observe(window, 'load', function(ev){
	ProtoSnow.setup();
	ProtoSnow.start();
});

