// How many pixels do we want?
var npixels = 100;
var pixels = Array();

var root, host, control, cursor;

var mx = 320;
var my = 240;

function Pixel() {
	this.x = Math.random() * root.Width;
	this.y = Math.random() * root.Height;
	this.vx = Math.random() * 10.0 - 5.0;
	this.vy = Math.random() * 10.0 - 5.0;
	this.lastupdate = (new Date()).getTime();
	this.line = host.content.createFromXaml('<Line Stroke="Black" StrokeThickness="1.5" StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>');
	root.children.add(this.line);

	this.accel = function(x,y) {
		this.vx += x;
		if (this.vx > 100.0)
			this.vx = 100.0;
		else if (this.vx < -100.0)
			this.vx = -100.0;
		this.vy += y;
		if (this.vy > 100.0)
			this.vy = 100.0;
		else if (this.vy < -100.0)
			this.vy = -100.0;
	}

	this.update = function() {
		var now = (new Date()).getTime();
		var dt = (now - this.lastupdate) / 75.0;
		this.x += this.vx * dt;
		this.y += this.vy * dt;
		this.lastupdate = now;
	}

	this.draw = function() {
		this.line.X1 = this.x;
		this.line.Y1 = this.y;
		this.line.X2 = this.x - this.vx / 4.0;
		this.line.Y2 = this.y - this.vy / 4.0;
	}
}

function swarmPush(sender, args) {
	var pos = args.getPosition(null);
	// Apply force to each pixel outwards from the mouse
	for (var i = 0; i < npixels; i++) {
		// Get the direction
		var dx = pixels[i].x - pos.x;
		var dy = pixels[i].y - pos.y;
		var mag = Math.sqrt(dx*dx + dy*dy);
		dx /= mag;
		dy /= mag;

		// Apply force.
		pixels[i].accel(dx * 10.0, dy * 10.0);
	}
}

// VROOM!
function engine() {
	// Update all pixel objects
	for (var i = 0; i < npixels; i++) {
		// Get distance
		var dx = mx - pixels[i].x;
		var dy = my - pixels[i].y;
		var d = Math.sqrt(dx*dx + dy*dy);

		// Apply acceleration to pixels towards the mouse cursor
		var ax = dx / 50.0;
		var ay = dy / 50.0;

		// Apply a drag proportional to the distance from the cursor
		if (d > 25.0) {
			ax += -pixels[i].vx * (d - 25.0) / 1000.0;
			ay += -pixels[i].vy * (d - 25.0) / 1000.0;
		}

		// And a "chaotic" acceleration inversely proportional
		// to the distance from the cursor
		ax += (Math.random() * 40.0 - 20.0) / d;
		ay += (Math.random() * 40.0 - 20.0) / d;

		pixels[i].accel(ax,ay);
		pixels[i].update();
		pixels[i].draw();
	}

	setTimeout('engine()',20);
}

// Initialize canvas
function reset() {
	control.height = window.innerHeight;
	control.width = window.innerWidth;
	root.Height = window.innerHeight;
	root.Width = window.innerWidth;
}
window.onresize = reset;

//line = plugin.content.CreateFromXaml('<Line/>');

function moveCursor(sender, args) {
	var pos = args.getPosition(null);
	cursor['Canvas.Left'] = pos.x - 5;
	cursor['Canvas.Top'] = pos.y - 5;
	mx = pos.x;
	my = pos.y;
}

function onLoaded(sender) {
	root = sender;
	host = sender.getHost();
	cursor = root.findName('cursor');
	control = document.getElementById('silverlightControl');
	reset();

	// Initialize pixel objects
	for (var x = 0; x < npixels; x++) {
		pixels.push(new Pixel());
	}

	// Gentlemen...
	engine();
}

