	function randHex() {
				var num = Math.round(Math.random() * 255).toString(16);
				if (num.length == 1)
					num = "0"+num;
				return num;
			}
			
			jQuery(function() {
				if (Modernizr.canvas === true)
					jQuery("#canvas-warning").hide();
				
				// Convenience
				$canvas = jQuery("#canvas");
				canvas = $canvas[0];
				context = canvas.getContext("2d");
 				canvas.style.width=$(window).width()*1.10+'px';
 				canvas.style.height=$(window).height()*0.25+'px';
				canvas.style.zIndex=20;
//  				canvas.style.backgroundColor="red";
				
				// Dimensions
				var width = $canvas.width();
				var height = $canvas.height();
				
				// Set actual canvas size to match css
				$canvas.attr("width", width);
				$canvas.attr("height", height);
				
				// Information
				jQuery("#info").html("Size: "+canvas.width+"x"+canvas.height);
				
				// Frame rate
				var frame = 0;
				
				// Snakes
				var n = 190;
				var initialRadius = width/200;
				snakes = new SnakeCollection();
				for (var i=0 ; i<n ; i++) {
					var snake = new Snake(canvas);
					snake.x = 40+width/n - initialRadius + i*1*initialRadius*n/n;
					snake.y = $canvas.height()-$canvas.height()*0.05+Math.floor(Math.random()*20)+7;
					snake.radius = initialRadius;
					snakes.add(snake);
				}
				for (var i=0 ; i<n ; i++) {
					var snake = new Snake(canvas);
					snake.x = 40+width/n - initialRadius + i*1*initialRadius*n/n;
					snake.y = $canvas.height()-$canvas.height()*0.25+Math.floor(Math.random()*50)+5;
					snake.radius = initialRadius;
					snakes.add(snake);
				}		
				for (var i=0 ; i<n ; i++) {
					var snake = new Snake(canvas);
					snake.x = 40+width/n - initialRadius + i*1*initialRadius*n/n;
					snake.y = $canvas.height()-$canvas.height()*0.45+Math.floor(Math.random()*70)+2;
					snake.radius = initialRadius;
					snakes.add(snake);
				}
			
					
				// Frame drawer
				var interval = setInterval(function() {
					snakes.next();
					
					frame++;
				}, 0);
				
				// fps
				var fpsInterval = setInterval(function() {
					jQuery("#fps").html(frame+" fps<br/>"+snakes.snakes.length+" branches running");
					frame = 0;
					if (snakes.snakes.length == 0) {
						clearInterval(interval);
						clearInterval(fpsInterval);
						var delay = 1500;
						jQuery("#info-container").fadeOut(1500, function(){
							jQuery("#info-container").html("Refresh for more delicious trees :)").fadeIn(delay, function() {
								setTimeout(function() {
									jQuery("#info-container").fadeOut(delay);
								}, delay*3)
							});
						});
					}
				}, 1000);
			});
			
			Snake = function(canvas) {
	this.setCanvas(canvas);
	this.x = this.canvasWidth;
	this.y = this.canvasHeight;
	this.radius = 2;
	this.speed = this.canvasWidth/2500;
	this.angle = Math.PI/2;
	this.fillStyle = "#9EFF0B";
	this.shadowColor = "#32CD32";
	this.shadowBlur = 14;
	this.generation = 3;
	this.lifespan = 0;
	this.totalDistance = 180;
	this.distance = 0;
};

Snake.prototype = {
	setCanvas: function(canvas) {
		this.canvas = canvas;
		this.context = canvas.getContext("2d");
		this.$canvas = jQuery(canvas);
		this.canvasWidth = $canvas.width();
		this.canvasHeight = $canvas.height();
	},
	
	next: function() {
		this.draw();
		this.iterate();
		this.randomize();
// 		this.limitSpeed();
// 		this.reset(context);
		this.split();
		this.lifespan++;
		this.die();
	},
	
	draw: function() {
		var context = this.context;
		context.save();
		context.fillStyle = this.fillStyle;
		context.shadowColor = this.shadowColor;
		context.shadowBlur = this.shadowBlur;
		context.beginPath();
		context.moveTo(this.x, this.y);
//  		context.arc(20, 20, 10, 0, Math.PI*2, 0);
		context.arc(this.x, this.y, this.radius, 0, 66*Math.PI, 0);
		context.closePath();
		context.fill();
		context.restore();
	},
	
	iterate: function() {
		var lastX = this.x;
		var lastY = this.y;
		this.x += this.speed * Math.cos(this.angle);
		this.y += this.speed * -Math.sin(this.angle);
		this.radius *= (1.01 - this.generation/120); // minus 0.004 per generation
		var deltaDistance = Math.sqrt(Math.abs(lastX-this.x) + Math.abs(lastY-this.y));
		this.distance += deltaDistance;
		this.totalDistance += deltaDistance;
		if (this.speed > this.radius*55)
			this.speed = this.radius*2;
	},
	
	randomize: function() {
		this.angle += Math.random()/5 - 1/5/2;
	},
	
	reset: function(context) {
		var $canvas = jQuery(context.canvas);
		var margin = 30+this.radius;
		var width = $canvas.width();
		var height = $canvas.height();
		
		if (this.x < -margin || this.x > width+margin || this.y < -margin || this.y > height+margin) {
// 			this.x = width/2;
			this.y = height;
			// New color
			var grey = Math.floor(Math.random()*255).toString(16);
			this.fillStyle = "#" + grey + grey + grey;
			this.shadowColor = this.fillStyle;
		}
	},
	
	split: function() {
		// Calculate split chance
		var splitChance = 0;
		// Trunk
		if (this.generation == 0)
			splitChance = (this.distance-this.canvasHeight/2)/100;
		// Branch
		else if (this.generation < 3)
			splitChance = (this.distance-this.canvasHeight)/2/100;
		
		// Split if we are allowed
		if (Math.random() < splitChance) {
			var n = 2+Math.round(Math.random()*2);
			for (var i=0 ; i<n ; i++) {
				var snake = new Snake(this.canvas);
				snake.x = this.x;
				snake.y = this.y;
				snake.angle = this.angle;
				snake.speed = this.speed;
				snake.radius = this.radius * 0.9;
				snake.generation = this.generation + 1;
				snake.fillStyle = this.fillStyle;
				snake.shadowColor = this.shadowColor;
				snake.shadowBlur = this.shadowBlur;
				snake.totalDistance = this.totalDistance;
				this.collection.add(snake);
			}
			this.collection.remove(this);
		}
	},
	
	die: function() {
		if (this.radius < 0.6) {
			this.collection.remove(this);
// 			console.log(this.distance);
		}
	}
}
SnakeCollection = function() {
	this.canvas = canvas;
// 	window.alert(this.canvas);
	
	this.snakes = [];
}

SnakeCollection.prototype = {
	next: function() {
		n = this.snakes.length;
		for (var s in this.snakes) {
			var snake = this.snakes[s];
			if (this.snakes[s])
				this.snakes[s].next();
		}
	},
	
	add: function(snake) {
		this.snakes.push(snake);
		snake.collection = this;
	},
	
	remove: function(snake) {
		for (var s in this.snakes)
			if (this.snakes[s] === snake)
				this.snakes.splice(s, 1);
	}
}

