$(function() {
	var canvas = $("#emeks_logo");
	var canvasHeight;
	var canvasWidth;
	var ctx;
	var dt = 0.1;
	
	var pointCollection;
	
	function init() {
		updateCanvasDimensions();

		var p_size = 8;
		var g = [new Point(0, 0, 0.0, p_size, "#ff6500"), new Point(16, 0, 0.0, p_size, "#ff6500"), new Point(32, 0, 0.0, p_size, "#ff6500"), new Point(48, 0, 0.0, p_size, "#ff6500"), new Point(64, 0, 0.0, p_size, "#ff6500")
, new Point(0, 16, 0.0, p_size, "#ff6500"), new Point(16, 16, 0.0, p_size, "#ff6500"), new Point(32, 16, 0.0, p_size, "#ff6500"), new Point(48, 16, 0.0, p_size, "#ff6500"), new Point(64, 16, 0.0, p_size, "#ff6500")
, new Point(0, 32, 0.0, p_size, "#ff6500"), new Point(16, 32, 0.0, p_size, "#ff6500"), new Point(32, 32, 0.0, p_size, "#ff6500"), new Point(48, 32, 0.0, p_size, "#ff6500"), new Point(64, 32, 0.0, p_size, "#ff6500")
, new Point(0, 48, 0.0, p_size, "#ff6500"), new Point(16, 48, 0.0, p_size, "#ff6500"), new Point(32, 48, 0.0, p_size, "#ff6500"), new Point(48, 48, 0.0, p_size, "#ff6500"), new Point(64, 48, 0.0, p_size, "#ff6500")
, new Point(0, 64, 0.0, p_size, "#ff6500"), new Point(16, 64, 0.0, p_size, "#ff6500"), new Point(32, 64, 0.0, p_size, "#ff6500"), new Point(48, 64, 0.0, p_size, "#ff6500"), new Point(64, 64, 0.0, p_size, "#ff6500")
, new Point(86, 86, 0.0, p_size, "#ff6500"), new Point(102, 86, 0.0, p_size, "#ff6500"), new Point(118, 86, 0.0, p_size, "#ff6500"), new Point(134, 86, 0.0, p_size, "#ff6500"), new Point(150, 86, 0.0, p_size, "#ff6500")
, new Point(86, 102, 0.0, p_size, "#ff6500"), new Point(102, 102, 0.0, p_size, "#ff6500"), new Point(118, 102, 0.0, p_size, "#ff6500"), new Point(134, 102, 0.0, p_size, "#ff6500"), new Point(150, 102, 0.0, p_size, "#ff6500")
, new Point(86, 118, 0.0, p_size, "#ff6500"), new Point(102, 118, 0.0, p_size, "#ff6500"), new Point(118, 118, 0.0, p_size, "#ff6500"), new Point(134, 118, 0.0, p_size, "#ff6500"), new Point(150, 118, 0.0, p_size, "#ff6500")
, new Point(86, 134, 0.0, p_size, "#ff6500"), new Point(102, 134, 0.0, p_size, "#ff6500"), new Point(118, 134, 0.0, p_size, "#ff6500"), new Point(134, 134, 0.0, p_size, "#ff6500"), new Point(150, 134, 0.0, p_size, "#ff6500")
, new Point(86, 150, 0.0, p_size, "#ff6500"), new Point(102, 150, 0.0, p_size, "#ff6500"), new Point(118, 150, 0.0, p_size, "#ff6500"), new Point(134, 150, 0.0, p_size, "#ff6500"), new Point(150, 150, 0.0, p_size, "#ff6500")
, new Point(0, 86, 0.0, p_size, "#5c5c5c"), new Point(16, 86, 0.0, p_size, "#5c5c5c"), new Point(32, 86, 0.0, p_size, "#5c5c5c"), new Point(48, 86, 0.0, p_size, "#5c5c5c"), new Point(64, 86, 0.0, p_size, "#5c5c5c")
, new Point(0, 102, 0.0, p_size, "#5c5c5c"), new Point(16, 102, 0.0, p_size, "#5c5c5c"), new Point(32, 102, 0.0, p_size, "#5c5c5c"), new Point(48, 102, 0.0, p_size, "#5c5c5c"), new Point(64, 102, 0.0, p_size, "#5c5c5c")
, new Point(0, 118, 0.0, p_size, "#5c5c5c"), new Point(16, 118, 0.0, p_size, "#5c5c5c"), new Point(32, 118, 0.0, p_size, "#5c5c5c"), new Point(48, 118, 0.0, p_size, "#5c5c5c"), new Point(64, 118, 0.0, p_size, "#5c5c5c")
, new Point(0, 130, 0.0, p_size, "#5c5c5c"), new Point(16, 134, 0.0, p_size, "#5c5c5c"), new Point(32, 134, 0.0, p_size, "#5c5c5c"), new Point(48, 134, 0.0, p_size, "#5c5c5c"), new Point(64, 134, 0.0, p_size, "#5c5c5c")
, new Point(3, 140, 0.0, p_size, "#5c5c5c"), new Point(20, 150, 0.0, p_size, "#5c5c5c"), new Point(32, 150, 0.0, p_size, "#5c5c5c"), new Point(48, 150, 0.0, p_size, "#5c5c5c"), new Point(64, 150, 0.0, p_size, "#5c5c5c"), new Point(10, 147, 0.0, p_size, "#5c5c5c")
, new Point(86, 0, 0.0, p_size, "#5c5c5c"), new Point(102, 0, 0.0, p_size, "#5c5c5c"), new Point(118, 0, 0.0, p_size, "#5c5c5c"), new Point(130, 0, 0.0, p_size, "#5c5c5c"), new Point(140, 3, 0.0, p_size, "#5c5c5c"), new Point(146, 10, 0.0, p_size, "#5c5c5c")
, new Point(86, 16, 0.0, p_size, "#5c5c5c"), new Point(102, 16, 0.0, p_size, "#5c5c5c"), new Point(118, 16, 0.0, p_size, "#5c5c5c"), new Point(134, 16, 0.0, p_size, "#5c5c5c"), new Point(150, 20, 0.0, p_size, "#5c5c5c")
, new Point(86, 32, 0.0, p_size, "#5c5c5c"), new Point(102, 32, 0.0, p_size, "#5c5c5c"), new Point(118, 32, 0.0, p_size, "#5c5c5c"), new Point(134, 32, 0.0, p_size, "#5c5c5c"), new Point(150, 32, 0.0, p_size, "#5c5c5c")
, new Point(86, 48, 0.0, p_size, "#5c5c5c"), new Point(102, 48, 0.0, p_size, "#5c5c5c"), new Point(118, 48, 0.0, p_size, "#5c5c5c"), new Point(134, 48, 0.0, p_size, "#5c5c5c"), new Point(150, 48, 0.0, p_size, "#5c5c5c")
, new Point(86, 64, 0.0, p_size, "#5c5c5c"), new Point(102, 64, 0.0, p_size, "#5c5c5c"), new Point(118, 64, 0.0, p_size, "#5c5c5c"), new Point(134, 64, 0.0, p_size, "#5c5c5c"), new Point(150, 64, 0.0, p_size, "#5c5c5c")
, new Point(70,80, 0.0, 6, "#5c5c5c"), new Point(80,70, 0.0, 6, "#5c5c5c"), new Point(75,75, 0.0, 4, "#5c5c5c")

];

		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;

			g[i].curPos.x = (850) + g[i].curPos.x;
			g[i].curPos.y = (10) + g[i].curPos.y;
			
			g[i].originalPos.x = (850) + g[i].originalPos.x;
			g[i].originalPos.y = (10) + 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, e.pageY);
	};
	
	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 < 50) {
					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();
});

