disabled = false;

flakeCount = 1;
updateSpeed = 60;
snowSpeed = 1;

realUpdateSpeed = 1000/updateSpeed;

snowFlakes = new Array();
snowElem = null;
snowContext = null;

snowOldFlakes = new Array();

windowWidth = 0;
windowHeight = 0;
viewWidth = 0;
viewHeight = 0;

// getPageScroll() by quirksmode.com
function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}
// End getPageScroll()

function createFlakes(count) {
	var curFlakes = 0;

	while(curFlakes < count) {
		var snowRadius = Math.random()*4+4;
		var centerX = Math.random()*windowWidth;
		var centerY = Math.random()*windowHeight;
		var speedX = 0;
		var speedY = 0;

		speedX = (Math.random()*160-80)/updateSpeed*snowSpeed;
		speedY = (Math.random()*240)/updateSpeed*snowSpeed;

		if(speedY < 0.2) speedY = 0.2;

		snowFlakes.push(new Array(centerX, centerY, snowRadius, speedX, speedY));
		curFlakes += 1;
	}

	updateFlakes();
}

function updateFlakes() {
	snowContext.clearRect(0, 0, windowWidth, windowHeight);

	for(var i = 0; i < snowFlakes.length; i++) {
		snowContext.beginPath();
		snowFlakes[i][0] += snowFlakes[i][3];
		snowFlakes[i][1] += snowFlakes[i][4];

		if(snowFlakes[i][0] > windowWidth+snowFlakes[i][2]) snowFlakes[i][0] = -snowFlakes[i][2];
		if(snowFlakes[i][0] < -snowFlakes[i][2]) snowFlakes[i][0] = windowWidth+snowFlakes[i][2];
		if(snowFlakes[i][1] > windowHeight+snowFlakes[i][2]) snowFlakes[i][1] = -snowFlakes[i][2];

		snowContext.arc(snowFlakes[i][0], snowFlakes[i][1], snowFlakes[i][2], 0, 2 * Math.PI, false);

		snowContext.fillStyle = "#ffffff";
		snowContext.fill();

		snowContext.lineWidth = 1;
		snowContext.strokeStyle = "#aaaaaa";
		snowContext.stroke();
	}

	setTimeout("updateFlakes();", realUpdateSpeed);
}

function createOldFlakes(count) {
	var curFlakes = 0;

	while(curFlakes < count) {
		var snowRadius = Math.random()*4+4;
		var centerX = Math.random()*windowWidth;
		var centerY = Math.random()*windowHeight;
		var speedX = (Math.random()*160-80)/updateSpeed*snowSpeed;
		var speedY = (Math.random()*240)/updateSpeed*snowSpeed;
		var snowId = 'fl_'+curFlakes;

		if(speedY < 0.4) speedY = 0.4;

		$('body').append('<img id="'+snowId+'" class="flake" width="'+(snowRadius*2)+'" height="'+(snowRadius*2)+'" alt="" src="/media/flake.png" />');
		snowOldFlakes.push(new Array(centerX, centerY, snowRadius, speedX, speedY, snowId));

		curFlakes += 1;
	}

	updateOldFlakes();
}

function updateOldFlakes() {
	pageScroll = getPageScroll();
	for(var i = 0; i < snowOldFlakes.length; i++) {
		snowOldFlakes[i][0] += snowOldFlakes[i][3];
		snowOldFlakes[i][1] += snowOldFlakes[i][4];

		if(snowOldFlakes[i][0] > windowWidth-20) snowOldFlakes[i][0] = -snowOldFlakes[i][2];
		if(snowOldFlakes[i][0] < -snowOldFlakes[i][2]) snowOldFlakes[i][0] = windowWidth-snowOldFlakes[i][2]-20;
		if(snowOldFlakes[i][1] > windowHeight-20) snowOldFlakes[i][1] = -snowOldFlakes[i][2];

		if(snowOldFlakes[i][1] < pageScroll[1]-snowOldFlakes[i][2] || snowOldFlakes[i][1] > pageScroll[1]+viewHeight+snowOldFlakes[i][2]) $('img#'+snowOldFlakes[i][5]).css('display', 'none');
		else $('img#'+snowOldFlakes[i][5]).css('display', 'inline');

		$('img#'+snowOldFlakes[i][5]).css('top', snowOldFlakes[i][1]).css('left', snowOldFlakes[i][0]);
	}

	setTimeout("updateOldFlakes();", realUpdateSpeed)
}

$(document).ready(function() {
	windowWidth = $(document).width();
	windowHeight = $(document).height();
	viewWidth = $(window).width();
	viewHeight = $(window).height();

	var c_disabled = $.cookies.get('snow_disabled');
	var c_count = $.cookies.get('snow_count');
	var c_updateSpeed = $.cookies.get('snow_updateSpeed');
	var c_speed = $.cookies.get('snow_speed');

	if(c_disabled != null) disabled = c_disabled;
	if(c_count != null) flakeCount = c_count;
	if(c_updateSpeed != null) realUpdateSpeed = 1000/c_updateSpeed;
	if(c_speed != null) snowSpeed = c_speed;

	flakeCount = Math.round(windowHeight/100)*flakeCount;
	
	if(c_disabled == 1) return;
	if(navigator.userAgent.search('/Mobile/i') != -1) return

	if($.browser.mozilla && parseFloat($.browser.version) > 4) {
		$('body').append('<canvas id="snow" width="'+windowWidth+'" height="'+windowHeight+'"></canvas>');

		snowElem = document.getElementById('snow');
		snowContext = snowElem.getContext("2d");
		createFlakes(flakeCount);
	} else {
		createOldFlakes(flakeCount);
	}
});
