// JavaScript Document

//==========================================
$(document).ready(function() {
    /* All site-specific javascript */
	canvasApp();
	//alert('Hey!');
});
//==========================================


function canvasApp() {
//====================
	//var theCanvas = document.getElementById("bgcanvas");
	var theCanvas = $("#bgcanvas").get(0);
	if (!theCanvas) {
		return;
	}
	var context = theCanvas.getContext("2d");
	if (!context) {
		return;
	}
	
	
	
	theCanvas.width = $(theCanvas).parent().width();
	theCanvas.height = $(theCanvas).parent().height();
	
	var numFlakes = 175;
	var maxSize = 3;
	var minSize = 1;
	var maxSpeed = maxSize+2;
	var snowflakes = new Array();
	var tempFlake;
	var tempX;
	var tempY;
	var tempSpeed;
	var tempAngle;
	var tempRadius;
	var tempRadians;
	var tempvelocityx;
	var tempvelocityy;
	
	//create flakes;
	for (var i = 0; i < numFlakes; i++) {
		tempRadius = Math.floor(Math.random()*maxSize) + minSize;
		tempX = tempRadius*2 + (Math.floor(Math.random()*theCanvas.width) - tempRadius * 2);
		tempY = tempRadius*2 + (Math.floor(Math.random()*theCanvas.height) - tempRadius * 2);
		//tempSpeed = maxSpeed-tempRadius;
		tempSpeed = tempRadius/3;
		tempAngle = (Math.floor(Math.random()*40) + 70);
		tempRadians = tempAngle * Math.PI / 180;
		tempvelocityx = Math.cos(tempRadians) * tempSpeed;
		tempvelocityy = Math.sin(tempRadians) * tempSpeed;
		tempFlake = {x:tempX, y:tempY, radius:tempRadius, speed:tempSpeed, angle:tempAngle, velocityx:tempvelocityx, velocityy:tempvelocityy};
		
		
		snowflakes.push(tempFlake);		
	}
	
	
	
	
	//drawScreen();
	setInterval(drawScreen,33);





	function drawScreen() {
		//alert("drawScreen");
		//background
		//context.fillStyle = "#cfe9f7";
		
		
		context.fillStyle = "#eeeeee";
		context.fillRect(0,0,theCanvas.width,theCanvas.height);
		

		//text shadow... kind of
		context.globalAlpha = .03;
		context.fillStyle    = "#888888";
		context.font         = "150px Impact";
		context.fontWeight   = "bold";
		context.textBaseline = "middle";
		context.textAlign = "center";
		context.fillText ("HAPPY HOLIDAYS", theCanvas.width/2 + 3, (theCanvas.height/2) + 11 );
		//text
		context.globalAlpha = .4;
		context.fillStyle    = "#ffffff";
		context.font         = "150px Impact";
		context.fontWeight   = "bold";
		context.textBaseline = "middle";
		context.textAlign = "center";
		context.fillText ("HAPPY HOLIDAYS", theCanvas.width/2, (theCanvas.height/2) + 8 );
		context.globalAlpha = 1;
		

		//place snowflakes
		context.fillStyle = "#ffffff";
		context.shadowOffsetX = 1;
		context.shadowOffsetY = 1;
		context.shadowColor = '#dddddd';
		context.shadowBlur = 1;
		
		var snowflake;
		
		for (var i = 0; i < snowflakes.length; i++){
			snowflake = snowflakes[i];
			snowflake.x += snowflake.velocityx;
			snowflake.y += snowflake.velocityy;
			
			
			//Snowflake as ball
			context.beginPath();
			context.arc(snowflake.x, snowflake.y, snowflake.radius, 0, Math.PI*2, true);
			context.closePath();
			context.fill();


			if(snowflake.x > (theCanvas.width) ) {
				//ball.angle = 180 - ball.angle;
				snowflake.x -= theCanvas.width;
				updateFlake(snowflake);
			}
			else if(snowflake.y > theCanvas.height) {
				//ball.angle = 360 - ball.angle;
				snowflake.y = 0 - snowflake.radius * 2;
				snowflake.x = (Math.floor(Math.random()*theCanvas.width) - snowflake.radius * 2);
				snowflake.angle = (Math.floor(Math.random()*40) + 70);
				updateFlake(snowflake);
			}

		}
		
	}// end of drawScreen()
	
	function updateFlake(snowflake) {
		//alert("updateBall");
		snowflake.radians = snowflake.angle * Math.PI / 180;
		snowflake.velocityx = Math.cos(snowflake.radians) * snowflake.speed;
		snowflake.velocityy = Math.sin(snowflake.radians) * snowflake.speed;
		//snowflake.x = (snowflake.x += snowflake.velocityx);
		//snowflake.y = (snowflake.y += snowflake.velocityy);
		
	}
	
	
	
	
		
	
	
	
	
	
}// end of canvasApp()

