window.requestAnimFrame = (function(){
  	return  window.requestAnimationFrame       || 
			window.webkitRequestAnimationFrame || 
			window.mozRequestAnimationFrame    || 
			window.oRequestAnimationFrame      || 
			window.msRequestAnimationFrame     || 
			function( callback ){
				window.setTimeout(callback, 1000 / 60);
			};
})();

var flakes;

function falling_flakes(c){
	function e(t){throw new Error(t);}
	var crop=false;
	if(!c.container){e('FallingFlakes: Container not defined');return;}
	if(!c.flake.image){e('FallingFlakes: Flake image not defined');return;}
	if(!c.flake.wind_direction) c.flake.wind_direction='random';
	if(!c.flake.sizes) c.flake.sizes = {};
		if(c.flake.sizes.crop) crop=true;
	if(!c.canvas) c.canvas = {};
		if(!c.canvas.width) c.canvas.width=parseInt(document.getElementById(c.container).offsetWidth);
		if(!c.canvas.height) c.canvas.height=parseInt(document.getElementById(c.container).offsetHeight);

	if(crop){
		var big_image = new Image();
		big_image.onload = function(){
			var pseudo_canvas = document.createElement('canvas'),
				pseudo_ctx = pseudo_canvas.getContext('2d');
			pseudo_canvas.width = c.flake.sizes.crop.width;
			pseudo_canvas.height = c.flake.sizes.crop.height;
			pseudo_ctx.drawImage(
				big_image,
				c.flake.sizes.crop.x,
				c.flake.sizes.crop.y,
				c.flake.sizes.crop.width,
				c.flake.sizes.crop.height,
				0,
				0,
				c.flake.sizes.crop.width,
				c.flake.sizes.crop.height
			);
			flakes(pseudo_canvas.toDataURL());

		};
		big_image.src = c.flake.image;
	}else
		flakes(c.flake.image);
	function flakes(src){
		var image = new Image();
		image.onload = function(){

			if(!c.flake.sizes.width) c.flake.sizes.width=image.width;
			if(!c.flake.sizes.height) c.flake.sizes.height=image.height;
			if(!c.flake.number) c.flake.number = Math.floor(((c.canvas.width/c.flake.sizes.width)*(c.canvas.height/c.flake.sizes.height))/16);

			var canvas = document.createElement('canvas'),
				ctx = canvas.getContext('2d');
				flakes = [];

			canvas.width = c.canvas.width;
			canvas.height = c.canvas.height;
			canvas.style.position = 'absolute';
			canvas.style.top = '0px';
			canvas.style.left = '0px';
			document.getElementById(c.container).appendChild(canvas);

			var mouse = {
				x: c.canvas.width,
				y: c.canvas.height
			};

			console.log(c);

			var wd=(c.canvas.width/2)/(-200);

			window.addEventListener('mousemove',function(e){
				mouse.x = e.clientX;
				mouse.y = e.clientY;
				wd=(mouse.x-c.canvas.width/2)/(-200);
			},false);
			window.addEventListener('resize',function(e){
				canvas.width = c.canvas.width = parseInt(document.getElementById(c.container).offsetWidth);
				canvas.height = c.canvas.height = parseInt(document.getElementById(c.container).offsetHeight);
			},false);

			function set_flake(p){
				var r = Math.random();
					r = r<0.2?0.2:(r>0.8?0.8:r);
				var y = c.flake.sizes.height*Math.random()*(-2);
				flakes[p] = [
					Math.random()*c.canvas.width,                     //x
					y > -c.flake.sizes.height?-c.flake.sizes.height:y,//y
					Math.random()*6-3,                                //wind speed
					r*5,                                              //fall speed 
					r                                                 //opacity
				];
			}

			function draw_flakes(){
				ctx.clearRect(0,0,c.canvas.width,c.canvas.height);
				for(var i=0;i<c.flake.number;i++){
					if(c.flake.wind_direction === 'random')
						flakes[i][0]+=flakes[i][2];
					else if(c.flake.wind_direction === 'mousecontrol')
						flakes[i][0]+=wd;
					flakes[i][1]+=flakes[i][3];
					ctx.globalAlpha = flakes[i][4];
					if(flakes[i][0]+c.flake.sizes.width < 0 || flakes[i][0] > c.canvas.width || flakes[i][1] > c.canvas.height) set_flake(i);
					ctx.drawImage(
						image,
						flakes[i][0],
						flakes[i][1],
						c.flake.sizes.width,
						c.flake.sizes.height
					);
				}
			}

			for(var i=0;i<c.flake.number;i++)
				set_flake(i);

			(function anim_loop(){
				window.requestAnimFrame(anim_loop);
				draw_flakes();
			})();
		};
		image.error = function(a){
			e('FallingFlakes: Image not loaded');
		};
		image.src = src;
	}

}