/* Author:kumquats */
var canvas;
var context;
var bounds;
var balls = [];
var mousePosition = {
	x: 0,
	y: 0
};
var createBallInterval;

var params = {
	globalAlpha: 1,
	clearRect: false,
	shadowEnabled: false,
	shadowColor: '#F00',
	shadowBlur: 2,
	initAngleVariation: 1,
	speed: 1,
	maxSpeedBoost: 5,
	mouseGravityArea: 200,
	strokeStyle: '#000',
	end:1
};

var defaultParams = {
	  "preset": "bulles",
	  "remembered": {
	    "Default": {
	      "0": {
	        "globalAlpha": 1,
	        "clearRect": false,
	        "shadowEnabled": false,
	        "shadowColor": "#F00",
	        "shadowBlur": 2,
	        "initAngleVariation": 1,
	        "maxSpeedBoost": 5,
	        "mouseGravityArea": 200,
	        "strokeStyle": "#000"
	      }
	    },
	    "cells": {
	      "0": {
	        "globalAlpha": 1,
	        "clearRect": true,
	        "shadowEnabled": true,
	        "shadowColor": "#a33c10",
	        "shadowBlur": 1.5,
	        "initAngleVariation": 1,
	        "maxSpeedBoost": 15,
	        "mouseGravityArea": 700,
	        "strokeStyle": "#ffffff"
	      }
	    },
	    "bulles": {
	      "0": {
	        "globalAlpha": 1,
	        "clearRect": false,
	        "shadowEnabled": true,
	        "shadowColor": "#ff8900",
	        "shadowBlur": 5.5,
	        "initAngleVariation": 0,
	        "maxSpeedBoost": 5.863078427891359,
	        "mouseGravityArea": 287.42539475294814,
	        "strokeStyle": "#ffffff"
	      }
	    },
	    "bulles_mini": {
	      "0": {
	        "globalAlpha": 1,
	        "clearRect": true,
	        "shadowEnabled": true,
	        "shadowColor": "#ff8900",
	        "shadowBlur": 1.6541083088065844,
	        "initAngleVariation": 0,
	        "speed": 1.5,
	        "maxSpeedBoost": 7.572323680324828,
	        "mouseGravityArea": 221.32722673303704,
	        "strokeStyle": "#ffffff"
	      }
	    }
	  },
	  "closed": false,
	  "folders": {
	    "filters": {
	      "preset": "Default",
	      "closed": false,
	      "folders": {}
	    },
	    "particules": {
	      "preset": "Default",
	      "closed": false,
	      "folders": {}
	    }
	  }
	};

function init()
{
	if ( ! Modernizr.canvas )
	{
		return;
	}
	canvas = $('#bgCanvas');
	bounds = {
		width: canvas.attr('width'),
		height: canvas.attr('height')
	};
	context = canvas[0].getContext('2d');

	createBallInterval = setInterval( createBall, 350 );
	$(document).mousemove( mousemoveHandler );
	setInterval( render, 50 );
	setInterval( changeDirection, 500 );

	if ( window.dat != undefined )
	{
		var gui = new dat.GUI( { load: defaultParams } );
		gui.remember( params );
		var guiFilters = gui.addFolder( 'filters' );
		guiFilters.add( params, 'globalAlpha', 0, 1 ); //0.2
		guiFilters.add( params, 'clearRect' ); //
		guiFilters.add( params, 'shadowEnabled' );
		guiFilters.addColor( params, 'shadowColor' );
		guiFilters.add( params, 'shadowBlur', 0, 50 );
		var guiParticles = gui.addFolder( 'particules' );
		guiParticles.add( params, 'initAngleVariation', 0, 1 ).name('initAngleVariation');
		guiParticles.add( params, 'speed', 1, 50 ); //35.5
		guiParticles.add( params, 'maxSpeedBoost', 1, 150 ).name('mouseSpeed'); //35.5
		guiParticles.add( params, 'mouseGravityArea', 1, 1000 ).name('mouseSize'); //705
		guiParticles.addColor( params, 'strokeStyle' ).name( 'Color' );
		guiParticles.open();
	}
	else
	{
		for ( var it in defaultParams.remembered.bulles[ 0 ] )
		{
			params[it] = defaultParams.remembered.bulles[ 0 ][ it ];
		}
	}
}
function createBall()
{
	if ( balls.length < 35 )
	{
		balls.push( initBall( {} ) );
	}
	else
	{
		clearInterval( createBallInterval );
	}
}
function initBall( ball )
{
	ball.x = bounds.width * Math.random();
	ball.y = bounds.height-2;
	ball.angle = - Math.PI/2 + ( 0.5 - Math.random() ) * Math.PI * params.initAngleVariation;
	ball.v = /*Math.random() * */0.5;
	ball.direction = Math.random() - 0.5;
	ball.t = 0;
	return ball;
}

function mousemoveHandler( event )
{
	mousePosition.x = event.pageX - canvas.position().left - parseInt(canvas.css( 'margin-left'));
	mousePosition.y = event.pageY - canvas.position().top;
}

function changeDirection()
{
	var ball;
	for ( var i = 0; i < balls.length; i++) 
	{
	    ball = balls[i];
	    if ( ball.t == 0 && Math.random() < 0.2 )
	    {
	    	ball.t = 1;
		}
	    if ( Math.random() > 0.9 )
		{
			ball.direction *= -1;
		}
	}
}
function drawBall( ball, movementDelta )
{
	context.beginPath();
	context.strokeStyle = params.strokeStyle;
	context.lineCap = "round";
	context.lineWidth = 2;
	context.moveTo( 0, 0 );
	context.lineTo( movementDelta.x*3, movementDelta.y*3 );
	context.stroke();
	context.closePath();
//	context.rotate( ball.angle );
//	var speed = 5*Math.sqrt( movementDelta.x * movementDelta.x + movementDelta.y * movementDelta.y );
//	var mx = speed/2;
//	var my = 1;
//	context.beginPath();
//	context.strokeStyle="#FC0";   
//	context.lineWidth="0.5";   
//	context.fillStyle= "#FFF";
//	context.moveTo(0,my);
//	context.quadraticCurveTo(0, 0, mx, 0);
//	context.quadraticCurveTo(speed, 0, speed, my);
//	context.quadraticCurveTo(speed, my*2, mx, 2);
//	context.quadraticCurveTo(0, my*2, 0, my);
//	context.stroke();
//	context.fill();
//	context.closePath();
}
function render()
{
	if ( params.clearRect )
	{
		context.clearRect( 0, 0, bounds.width, bounds.height );
	}
	else
	{
		context.fillStyle ='rgba(255,255,255,0.1)';
		context.fillRect( 0, 0, bounds.width, bounds.height );
	}
	context.save();
	if ( params.shadowEnabled )
	{
		context.shadowColor = params.shadowColor;
		context.shadowBlur = params.shadowBlur;
	}
	context.globalAlpha = params.globalAlpha;
	
	var mouseDistance;
	var movementDelta;
	var speedBoost;
	for ( var i = 0; i < balls.length; i++) {
		var ball = balls[i];
		context.save();
		context.translate( ball.x, ball.y );
		
		if ( ball.t > 0 )
		{
			ball.angle += ball.direction * 0.05;
			ball.t ++;
			if ( ball.t > 10 )
			{
				ball.t = 0;
			}
		}
		else
		{
			ball.angle += ball.direction * 0.01;
		}
		mouseDistance = Math.sqrt( Math.pow( ball.x - mousePosition.x, 2 ) + Math.pow( ball.y - mousePosition.y, 2 ) );
		speedBoost = Math.max( 1, Math.min( params.maxSpeedBoost, params.mouseGravityArea / Math.max( 1, mouseDistance ) ) );
		
		movementDelta = {
			x: ball.v * params.speed * speedBoost * Math.cos( ball.angle ),
			y: ball.v * params.speed * speedBoost * Math.sin( ball.angle )
		};
		ball.x += movementDelta.x;
		ball.y += movementDelta.y;
		
		if ( ball.x < 0 || ball.x > bounds.width || ball.y < 0 || ball.y > bounds.height )
		{
			initBall(ball);
		}
		drawBall( ball, movementDelta );
		context.restore();

    }
	context.restore();
	
	context.save();
	var hratio = bounds.height / bounds.width;
	context.scale(1, hratio * 2);
	var grd=context.createRadialGradient(
			bounds.width/2, bounds.width/2, 0, 
			bounds.width/2, bounds.width/2, bounds.width/2);
	grd.addColorStop(0,"rgba(255,255,255,0)");
//	grd.addColorStop(0,"rgba(255,170,0,0.1)");
//	grd.addColorStop(0.3,"rgba(255,255,255,0)");
	grd.addColorStop(0.6,"rgba(255,255,255,0.1)");
//	grd.addColorStop(0.8,"rgba(255,255,255,0)");
	grd.addColorStop(1,"rgba(255,255,255,1)");
	context.fillStyle=grd;
	context.fillRect(0,0,bounds.width,bounds.width);
	context.restore();
}

(function() {

	$(document).ready(function() {
		init();
    });
	
}).call(this);



