/* 
	plants.js (v 0.1)

	 A simple plant generator.  Have fun!

	Copyright Jeff Rose 2007 -- http://rosejn.net
	This code is distributed under the terms of the GNU General Public License,
	see <http://www.gnu.org/licenses/> for details.
*/

var Garden = Class.create();
Garden.prototype = {
	initialize: function(canvas_id) {
		this.max_plants = 3;
		this.min_plants = 1;
		this.padding = 20;

		this.canvas = document.getElementById(canvas_id);
		if(this.canvas.getContext) {
			this.ctx = this.canvas.getContext('2d');
			this.width = this.canvas.width;
			this.height = this.canvas.height;

			// Plant dimensions based on canvas size
			this.min_height = this.height * 0.1;
			this.max_height = this.height * 0.5;

			this.draw();
		}
	},

	draw: function() {
		this.num_plants = rand_min_max(this.min_plants,
																	this.max_plants);

		this.plants = [];
		for(var i = 0; i < this.num_plants; i++) {
			var x = rand_min_max(this.padding, 
													 this.width - this.padding);
			var y = rand_min_max(this.height / 2, 
													 this.height);
			var height = rand_min_max(this.min_height, 
																this.max_height);
			this.plants.push(new Plant(this.ctx, x, y, height));
		};
	}
}

var Plant = Class.create();

Plant.prototype = {
	initialize: function(ctx, x, y, height) {
		this.ctx = ctx;
		this.x = x;
		this.y = y;
		this.height = height;

		this.detail = 10;

		this.hw = 2; // Half width
		this.dy = 5; // Space between leaves

		this.draw();
	},

	draw: function() {
		var g = this.ctx;
		var stem_points = this.generateStem();

		g.strokeStyle = "rgba(50, 75, 50, 1)";
		g.moveTo(this.x, this.y);
		stem_points.each(function(p) {
			g.lineTo(p.x, p.y);
		});

		g.lineWidth = 0.5;
		g.stroke();

		var len = stem_points.length;
		for(var i = 4; i < len; i++) { 
					this.drawLeaf(stem_points[i], 1 + 0.1 * (len - i), 
																				1 + 0.05 * (len - i)); 
				}
	},

	drawLeaf: function(point, scale_x, scale_y) {
		var g = this.ctx;

		var angle = Math.random() * Math.PI - Math.PI;
		var green  = (110 + Math.random(50)) / 256.0;
		var length = 8.0 + Math.random() * 2.0;
		var width = 1.0 + Math.random() * 2.0;
		var curve_a = (0.2 * length) + Math.random() * 1.0;
		var curve_b = (0.8 * length) + Math.random() * 1.0;

		g.save();

		g.lineStyle = "rgba(0, 0, 0, 0.0)";

		g.fillStyle = "rgba(80, 190, 50, 0.2)";
		g.translate(point.x, point.y);
		g.scale(scale_x, scale_y);
		g.rotate(angle);

		g.moveTo(0.0, 0.0);

		g.bezierCurveTo(curve_a, width, curve_b, width, length, 0.0);
		g.bezierCurveTo(curve_b, -width, curve_a, -width, 0.0, 0.0);

		g.fill();
		g.restore();
	},

	generateStem: function() {
		var points = [];
		var num_points = rand_min_max(this.height / 10, 
																	this.height / 2);

//		var factor = rand_min_max(0, 5);
		for(var i = 0; i < num_points; i++) {
/*
			var x = 4.0 / num_points * i - 2.0;
			var mult = factor * Math.exp((-1/2)*(x*x));
*/
			points.push(new Point(
        //mult * 10 + 
				rand_min_max(this.x - this.hw, this.x + this.hw), 
				this.y - this.dy * i));
		}

		return points;
	}
}

var Point = Class.create();

Point.prototype = {
	initialize: function(x, y) {
		this.x = x;
		this.y = y;
	}
}

function rand_min_max(min, max) {
	return Math.round(Math.random() * (max - min)) + min;
}


