function onInit() {
	setIdleEventInterval( 1 );
	
	minAmount = 5;
	maxAmount = 15;
	minWidth 	= 0.1;
	maxWidth 	= 5;
	
	maxLifeTime = 100;
	maxBranches	= 1000;
	
	lastPoint 				= new Point( 0, 0 );
	currentDirection 	= new Point( 0, 0 );
	
	distanceCounter 	= 0;
}

function onOptions() {
	values = Dialog.prompt("Weed Rounded:", [
		{ value: size, description: "Radius", width: 50 },
		{ value: minAmount, description: "Minimal Amount", width: 50 },
		{ value: maxAmount, description: "Maximal Amount", width: 50 },
		{ value: minWidth, description: "Minimal Stroke Width", width: 50 },
		{ value: maxWidth, description: "Maximal Stroke Width", width: 50 },
	]);
	if (values != null) {
		size = values[0];
		minAmount = values[1];
		maxAmount = values[2];
		minWidth = values[3];
		maxWidth = values[4];
	}
}

function onMouseDown( event ) {
	lastPoint = event.point;
	
	branches 					= [];
	
	mainBranch = new Path();
	mainBranch.moveTo( event.point );
	mainBranch.style.stroke.width = 0.25;

}

function onMouseUp( event ) {
	for ( var i in branches ) {
		branches[ i ].finish();
	}
	mainBranch.pointsToCurves();
}

function onMouseDrag( event ) {
	mainBranch.lineTo( event.point );
	
	currentDirection = event.point.subtract( lastPoint );
	currentDirection = currentDirection.normalize().multiply( Math.random() * 10 );
	
	// Spawn new branches
	if( canBranch() && distanceCounter > 30 ) {
		var group = new Group();
		var rotationDirection = ( Math.random() > 0.5 ) ? 1 : -1;
		branches.push( new Branch( event.point, group, currentDirection, rotationDirection, maxLifeTime ) );
		distanceCounter = 0;
	}
	
	// Grow branches
	for( var i in branches ) {
		var retval  = branches[ i ].grow();
		if( retval == false ) {
			branches[ i ].finish();
			branches.splice( i, 1 );
		}
	}
	
	distanceCounter += Math.abs( lastPoint.getDistance( event.point ) );
	
	lastPoint = event.point;
}

// Branch:

function Branch( point, group, direction, rotationDirection, life ) {
	this.point 	= point;
	
	this.vector = direction; // Mouse movement speed dependant branch radius
	
	this.rotate = rotationDirection / 100;
	this.rotationDirection = rotationDirection;
	this.life		= life;
	
	this.path 	= new Path();
	this.path.moveTo( point );
	this.path.style.stroke.width = 0.25; //minWidth + Math.random() * (maxWidth - minWidth);
	group.appendChild( this.path );
	
}

Branch.prototype.grow = function() {
	
	// Animate branch
	this.vector = this.vector.transform( new Matrix().rotate( this.rotate ) );
	this.rotate += this.rotationDirection / 100;// / this.life;
	this.point 	= this.point.add( this.vector );
	this.path.lineTo( this.point );
	
	// Possibly die branch and blossom
	var chanceOfDeath = ( maxLifeTime - this.life ) / maxLifeTime;
	if( chanceOfDeath > Math.random() + 0.525 ) {
		/*
		var pt = this.point;
    for( var i = 0; i < 10; ++i ) {
    	var degree = Math.random() * 360;
      var rad = 7 * Math.random();
      var xPt = pt.x + rad * Math.sin(degree * Math.PI / 180);
      var yPt = pt.y + rad * Math.cos(degree * Math.PI / 180);
      var newSize = rad;//5 * Math.random() ;
      rect = new Rectangle(0, 0, newSize, newSize);
      rect.center = new Point(xPt, yPt);
      activeDocument.createOval(rect);
    }
    */
    return false;
	}
	// If branch is just dead, kill it for good
	if( --this.life <= 0 ) return false;
	
	// Possibly create a new branch from this branch (but only in early years)
	if( canBranch() && Math.random() < 0.1 && this.life > maxLifeTime - 30 ) {
		branchBranch( this );
	}
	
}

Branch.prototype.finish = function() {
	this.path.pointsToCurves( );
}

function branchBranch( branch ) {
	var group = new Group();
	branches.push( new Branch( branch.point, group, branch.vector.multiply( 0.5 ), branch.rotationDirection * -1, branch.life ) );
}

function canBranch() {
	return ( branches.length < maxBranches );
}