﻿/// <reference path="~/Scripts/jquery-1.4.4-vsdoc.js"/>
$(document).ready(function () {
	// Cache location of the graph origin 
	var Origin = { X: (window.innerWidth / 2), Y: (window.innerHeight / 2) };

	var Screen = { Width: window.innerWidth, Height: window.innerHeight };

	// Create canvas
	var canvas = document.createElement('canvas'),
		context = canvas.getContext('2d');
	// Bind to container
	var container = $("#Container");
	container.append(canvas);
	// Initialise canvas so it's full screen
	canvas.width = Screen.Width;
	canvas.height = Screen.Height;

	// Allocate array of particles
	var particles = [];
	// Total particle counter
	var particleCount = 0;
	// Maximum particles
	var maxParticles = 200;

	var randomVelocity = function () {
		var velocity = Math.random() * 3;

		if (Math.random() > 0.5) {
			velocity *= -1;
		}

		return velocity;
	};

	var loop = function () {
		// If we haven't capped out yet, create a new particle and render it at the origin
		if (particleCount < maxParticles) {
			var p = new Particle(Origin.X, Origin.Y);
			particles[particles.length] = p;
			p.render(context);

			p.velX = randomVelocity();
			p.velY = randomVelocity();

			particleCount++;
		}

		// Blank out the origin
		context.fillStyle = "rgb(0,0,0)";
		context.fillRect(0, 0, Screen.Width, Screen.Height);

		for (var i = 0; i < particleCount; ++i) {
			particles[i].update();
			particles[i].render(context);
		}
	}

	setInterval(loop, 1000 / 60);
});
