var canvas;
var ctx;
	
var images=new Array ();
var flakes=new Array ();
var nrFlakes=50;
var ySpread=100;

function createCanvasOverlay()
{
  myCanvas = document.createElement('canvas');
  document.body.appendChild(myCanvas);
  myCanvas.id="myCanvas";
  myCanvas.style.position = 'absolute';
  myCanvas.style.left="0px";
  myCanvas.style.top="0px";
  myCanvas.style.zIndex="100";
  myCanvas.style.width="100%";
  myCanvas.style.height="100%";
  //myCanvas.width=myCanvas.offsetWidth;
  //myCanvas.height=myCanvas.offsetHeight;
  myCanvas.width=window.innerWidth;
  myCanvas.height=window.innerHeight;  
  
  //alert (window.innerWidth);
}

function getRandomFlake ()
{
	return (Math.floor(Math.random()*3)+1);
}

function getRandomX ()
{
	placeX=Math.floor(Math.random()*(ctx.canvas.width+1))-30;
	//alert (placeX);
	return (placeX);
}

function getRandomSwirve ()
{
	return (Math.floor(Math.random()*(4)));
}

function getRandomSpeed ()
{
	return (Math.floor(Math.random()*(15))+3);
}

function getRandomY ()
{
	return (Math.floor(Math.random()*(ctx.canvas.height+1))-ctx.canvas.height);
}

function getRandomY2 ()
{
	return (-20);
}

function start()
{   
	var d = new Date();
	if ((d.getMonth()==10) || (d.getMonth()==11))
	{
	  // We're in the snow season
	}
	else
	{
		return;
    }
				
	createCanvasOverlay ();

	canvas=document.getElementById("myCanvas");
	ctx=canvas.getContext("2d");	
	//ctx.canvas.width  = window.innerWidth;
	//ctx.canvas.height = window.innerHeight;
	
	images [0]=new Image();
	images [0].src = 'snowflake1.png'; 

	images [1]=new Image();
	images [1].src = 'snowflake2.png'; 

	images [2]=new Image();
	images [2].src = 'snowflake3.png'; 	
		
	//ctx.globalCompositeOperation = 'destination-over';  		
    //ctx.fillStyle = 'rgba(0,0,0)';   
    //ctx.strokeStyle = 'rgba(0,0,0)';   
	//ctx.fillRect(0,0,canvas.width,canvas.height);

	for (i=0;i<nrFlakes;i++)
	{
		name='snowflake'+getRandomFlake ()+'.png'; 
		//alert (name);
		flakes [i]=new Array ();
		flakes [i][0]=new Image();
		flakes [i][0].src=name;
		flakes [i][1]=getRandomX ();
		flakes [i][2]=getRandomY ();
		flakes [i][3]=getRandomSpeed ();
	}	
	
	for (i=0;i<nrFlakes;i++)
	{
		//alert ("Flake "+i+": "+flakes [i][0].src+": "+flakes [i][1]+","+flakes [i][2]);
		//alert ("Flake "+i+": "+flakes [i].src);
	}	
		
	if (canvas.getContext)
	{ 	
		setInterval (drawShapes,100);
	} 
	else 
	{
		alert ('You need Safari or Firefox 1.5+ to see this demo.');
	}
} 

function drawShapes()
{ 	
	ctx.clearRect(0,0,canvas.width,canvas.height);
	
	//ctx.drawImage (flakes [0][0],ctx.canvas.width/2,ctx.canvas.height/2);
	
	for (i=0;i<nrFlakes;i++)
	{
		ctx.drawImage (flakes [i][0],
		               flakes [i][1]+=getRandomSwirve (),
					   flakes [i][2]+=flakes [i][3]);
					   
		if (flakes [i][2]>ctx.canvas.height)
		{
			flakes [i][1]=getRandomX ();
			flakes [i][2]=getRandomY2 ();
			flakes [i][3]=getRandomSpeed ();
		}	
	}
}
