$(function() {


	var canvas = $("#c");
	var canvasHeight;
	var canvasWidth;
	var ctx;
	var dt = 0.1;
	
	var pointCollection;
	
	function init() {
		updateCanvasDimensions();
		
		var g = [new Point(14, 20, 0.0, 12, "#3d65e7"),
		new Point(14, 47, 0.0, 12, "#507bf2"),
		new Point(14, 74, 0.0, 12, "#5681f5"),
		new Point(14, 101, 0.0, 12, "#5683f7"),
		new Point(14, 128, 0.0, 12, "#4779f7"),
		new Point(37, 74, 0.0, 12, "#5681f5"),
		//new Point(60, 20, 0.0, 12, "#3d65e7"),
		//new Point(60, 47, 0.0, 12, "#507bf2"),
		new Point(60, 74, 0.0, 12, "#5681f5"),
		new Point(60, 101, 0.0, 12, "#5683f7"),
		new Point(60, 128, 0.0, 12, "#4779f7"),
		
		//new Point(92, 20, 0.0, 12, "#d82038"),
		//new Point(92, 47, 0.0, 12, "#e74653"),
		new Point(92, 74, 0.0, 12, "#f0696c"),
		new Point(92, 101, 0.0, 12, "#f4716e"),
		new Point(92, 128, 0.0, 12, "#ef5c5c"),
		//new Point(115, 20, 0.0, 12, "#d82038"),
		new Point(115, 74, 0.0, 12, "#f0696c"),
		new Point(115, 128, 0.0, 12, "#ef5c5c"),
		//new Point(138, 20, 0.0, 12, "#d82038"),
		new Point(138, 74, 0.0, 12, "#f0696c"),
		new Point(138, 128, 0.0, 12, "#ef5c5c"),
		
		//new Point(169, 20, 0.0, 12, "#ee9a1e"),
		//new Point(169, 47, 0.0, 12, "#f9b125"),
		new Point(169, 74, 0.0, 12, "#fac652"),
		new Point(169, 101, 0.0, 12, "#facb5e"),
		new Point(169, 128, 0.0, 12, "#f8c247"),
		//new Point(192, 20, 0.0, 12, "#ee9a1e"),
		new Point(192, 74, 0.0, 12, "#fac652"),
		new Point(192, 101, 0.0, 12, "#facb5e"),
		new Point(192, 128, 0.0, 12, "#f8c247"),
		//new Point(215, 20, 0.0, 12, "#ee9a1e"),
		new Point(215, 74, 0.0, 12, "#fac652"),
		//new Point(215, 101, 0.0, 12, "#facb5e"),
		new Point(215, 128, 0.0, 12, "#f8c247"),
		
		//new Point(246, 20, 0.0, 12, "#3d65e7"),
		new Point(246, 74, 0.0, 12, "#5681f5"),
		//new Point(269, 20, 0.0, 12, "#3d65e7"),
		//new Point(269, 47, 0.0, 12, "#507bf2"),
		new Point(269, 74, 0.0, 12, "#5681f5"),
		new Point(269, 101, 0.0, 12, "#5683f7"),
		new Point(269, 128, 0.0, 12, "#4779f7"),
		//new Point(292, 20, 0.0, 12, "#3d65e7"),
		new Point(292, 74, 0.0, 12, "#5681f5"),
		
		//new Point(323, 20, 0.0, 12, "#36b641"),
		//new Point(323, 47, 0.0, 12, "#10a11d"),
		new Point(323, 74, 0.0, 12, "#0b991a"),
		new Point(323, 101, 0.0, 12, "#1f9e2c"),
		new Point(323, 128, 0.0, 12, "#269230"),
		//new Point(346, 20, 0.0, 12, "#36b641"),
		new Point(346, 101, 0.0, 12, "#1f9e2c"),
		new Point(346, 128, 0.0, 12, "#0b991a"),
		new Point(346, 74, 0.0, 12, "#269230"),
		//new Point(369, 20, 0.0, 12, "#36b641"),
		new Point(369, 74, 0.0, 12, "#0b991a"),
		new Point(369, 128, 0.0, 12, "#269230"),
		
		//new Point(400, 20, 0.0, 12, "#d82038"),
		//new Point(400, 47, 0.0, 12, "#e74653"),
		new Point(400, 74, 0.0, 12, "#f0696c"),
		new Point(400, 101, 0.0, 12, "#f4716e"),
		new Point(400, 128, 0.0, 12, "#ef5c5c"),
		//new Point(423, 20, 0.0, 12, "#d82038"),
		new Point(423, 74, 0.0, 12, "#f0696c"),
		new Point(423, 128, 0.0, 12, "#ef5c5c"),
		//new Point(446, 20, 0.0, 12, "#d82038"),
		new Point(446, 74, 0.0, 12, "#f0696c"),
		new Point(446, 128, 0.0, 12, "#ef5c5c"),
		
		new Point(478, 20, 0.0, 12, "#3d65e7"),
		new Point(478, 47, 0.0, 12, "#507bf2"),
		new Point(478, 74, 0.0, 12, "#5681f5"),
		new Point(478, 101, 0.0, 12, "#5683f7"),
		new Point(478, 128, 0.0, 12, "#4779f7"),
		new Point(501, 74, 0.0, 12, "#5681f5"),
		//new Point(524, 20, 0.0, 12, "#3d65e7"),
		//new Point(524, 47, 0.0, 12, "#507bf2"),
		new Point(524, 74, 0.0, 12, "#5681f5"),
		new Point(524, 101, 0.0, 12, "#5683f7"),
		new Point(524, 128, 0.0, 12, "#4779f7")
		
		];
		
		gLength = g.length;
		for (var i = 0; i < gLength; i++) {
			g[i].curPos.x = (canvasWidth/2 - 180) + g[i].curPos.x;
			g[i].curPos.y = (canvasHeight/2 - 65) + g[i].curPos.y;
			
			g[i].originalPos.x = (canvasWidth/2 - 180) + g[i].originalPos.x;
			g[i].originalPos.y = (canvasHeight/2 - 65) + g[i].originalPos.y;
		};
		
		pointCollection = new PointCollection();
		pointCollection.points = g;
		
		initEventListeners();
		timeout();
	};
	
	function initEventListeners() {
		$(window).bind('resize', updateCanvasDimensions).bind('mousemove', onMove);
		
		canvas.get(0).ontouchmove = function(e) {
			e.preventDefault();
			onTouchMove(e);
		};
		
		canvas.get(0).ontouchstart = function(e) {
			e.preventDefault();
		};
	};
	
	function updateCanvasDimensions() {
		//canvas.attr({height: $(window).height(), width: $(window).width()});
		canvasWidth = canvas.width();
		canvasHeight = canvas.height();

		draw();
	};
	
	function onMove(e) {
		if (pointCollection)
			pointCollection.mousePos.set(e.pageX-290, e.pageY+50);
	};
	
	function onTouchMove(e) {
		if (pointCollection)
			pointCollection.mousePos.set(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
	};
	
	function timeout() {
		draw();
		update();
		
		setTimeout(function() { timeout() }, 30);
	};
	
	function draw() {
		var tmpCanvas = canvas.get(0);

		if (tmpCanvas.getContext == null) {
			return; 
		};
		
		ctx = tmpCanvas.getContext('2d');
		ctx.clearRect(0, 0, canvasWidth, canvasHeight);
		
		if (pointCollection)
			pointCollection.draw();
	};
	
	function update() {		
		if (pointCollection)
			pointCollection.update();
	};
	
	function Vector(x, y, z) {
		this.x = x;
		this.y = y;
		this.z = z;
 
		this.addX = function(x) {
			this.x += x;
		};
		
		this.addY = function(y) {
			this.y += y;
		};
		
		this.addZ = function(z) {
			this.z += z;
		};
 
		this.set = function(x, y, z) {
			this.x = x; 
			this.y = y;
			this.z = z;
		};
	};
	
	function PointCollection() {
		this.mousePos = new Vector(0, 0);
		this.points = new Array();
		
		this.newPoint = function(x, y, z) {
			var point = new Point(x, y, z);
			this.points.push(point);
			return point;
		};
		
		this.update = function() {		
			var pointsLength = this.points.length;
			
			for (var i = 0; i < pointsLength; i++) {
				var point = this.points[i];
				
				if (point == null)
					continue;
				
				var dx = this.mousePos.x - point.curPos.x;
				var dy = this.mousePos.y - point.curPos.y;
				var dd = (dx * dx) + (dy * dy);
				var d = Math.sqrt(dd);
				
				if (d < 150) {
					point.targetPos.x = (this.mousePos.x < point.curPos.x) ? point.curPos.x - dx : point.curPos.x - dx;
					point.targetPos.y = (this.mousePos.y < point.curPos.y) ? point.curPos.y - dy : point.curPos.y - dy;
				} else {
					point.targetPos.x = point.originalPos.x;
					point.targetPos.y = point.originalPos.y;
				};
				
				point.update();
			};
		};
		
		this.draw = function() {
			var pointsLength = this.points.length;
			for (var i = 0; i < pointsLength; i++) {
				var point = this.points[i];
				
				if (point == null)
					continue;

				point.draw();
			};
		};
	};
	
	function Point(x, y, z, size, colour) {
		this.colour = colour;
		this.curPos = new Vector(x, y, z);
		this.friction = 0.8;
		this.originalPos = new Vector(x, y, z);
		this.radius = size;
		this.size = size;
		this.springStrength = 0.1;
		this.targetPos = new Vector(x, y, z);
		this.velocity = new Vector(0.0, 0.0, 0.0);
		
		this.update = function() {
			var dx = this.targetPos.x - this.curPos.x;
			var ax = dx * this.springStrength;
			this.velocity.x += ax;
			this.velocity.x *= this.friction;
			this.curPos.x += this.velocity.x;
			
			var dy = this.targetPos.y - this.curPos.y;
			var ay = dy * this.springStrength;
			this.velocity.y += ay;
			this.velocity.y *= this.friction;
			this.curPos.y += this.velocity.y;
			
			var dox = this.originalPos.x - this.curPos.x;
			var doy = this.originalPos.y - this.curPos.y;
			var dd = (dox * dox) + (doy * doy);
			var d = Math.sqrt(dd);
			
			this.targetPos.z = d/100 + 1;
			var dz = this.targetPos.z - this.curPos.z;
			var az = dz * this.springStrength;
			this.velocity.z += az;
			this.velocity.z *= this.friction;
			this.curPos.z += this.velocity.z;
			
			this.radius = this.size*this.curPos.z;
			if (this.radius < 1) this.radius = 1;
		};
		
		this.draw = function() {
			ctx.fillStyle = this.colour;
			ctx.beginPath();
			ctx.arc(this.curPos.x, this.curPos.y, this.radius, 0, Math.PI*2, true);
			ctx.fill();
		};
	};
	
	init();
});
