/*

	Snowscreens logic
	Author:              thunder::tech inc.
	License:             ADDITIONAL PARTY is defined as anyone other than thunder::tech.
						 No right is granted to ADDITIONAL PARTY to sell, distribute, modify or otherwise transfer the following source code without explicit written permission by thunder::tech.

*/

/*  ================
     Initialization
    ================ */

var Snowscreens = { };

Snowscreens.pageLoaded = function()
{
	thunder.client.modify.selfLabelFields();
	$(window).resize(Snowscreens.getSize);
	Snowscreens.getSize();
	Snowscreens.screenInterval = setInterval(Snowscreens.screenFrame, 30);
	var i, weather = '';
	for(i = 0; i < 10; i++)
	{
		weather += '<img src="images/flake.gif" alt="" style="left: ' + Snowscreens.randomX() + 'px; top: ' + Snowscreens.randomY() + 'px" class="snowflake" velocity="'+Snowscreens.randomVelocity(1)+'" />';
	}
	for(i = 0; i < 10; i++)
	{
		weather += '<img src="images/day-flake.gif" alt="" style="left: ' + Snowscreens.randomX() + 'px; top: ' + Snowscreens.randomY() + 'px" class="snowflake dual-snowflake" velocity="'+Snowscreens.randomVelocity(1)+'" />';
	}
	$('.weather').html(weather);
	Snowscreens.weather = $('.weather').children();
}

Snowscreens.screenFrame = function()
{
	Snowscreens.weather.each(Snowscreens.weatherLogic);
}

Snowscreens.weatherLogic = function()
{
	qthis = $(this);
	var p, l, t;
	p = qthis.position();
	v = parseInt(qthis.attr('velocity'));
	l = p.left + Math.floor(v/2);
	t = p.top + v;
	if((t > Snowscreens.uiHeight - 20)||(l>Snowscreens.uiWidth - 20))
	{
		t = 0;
		l = Snowscreens.randomX();
	}
	qthis.css({left: l, top: t});
}

Snowscreens.getSize = function()
{
	Snowscreens.uiWidth = $(window).width();
	Snowscreens.uiHeight = $(window).height();
	$('.screen-middle').css({top: (Snowscreens.uiHeight / 2) - 150});
	$('.screen-top-middle').css({top: (Snowscreens.uiHeight / 2) - 260});
}

Snowscreens.randomX = function()
{
	x = (Math.random() * (Snowscreens.uiWidth + 50)) - 50;
	return x;
}

Snowscreens.randomVelocity = function()
{
	return Math.floor(Math.random() * 5) + 1;
}

// Return a random viewport-visible Y position
Snowscreens.randomY = function()
{
	return Math.random() * Snowscreens.uiHeight;
}

$(Snowscreens.pageLoaded);
