var p;
var values = {
	threshold: 50,
	optForm: 'line',
	optStroke: false,
	optColor: true,
	color1: new RGBColor(1, 0.5, 0),
	color2:	new RGBColor(0.7, 0.9, 0),
	strokeMin: 0.1,
	strokeMax: 3,
	pathGap: 3,
	pathWidth: 20,
	strokeStyle: 'round',
	speed: false
};
	
var items = {
	threshold: {type: 'number', label: 'Distance Threshold', units: 'point'},
	pathWidth: {type: 'number', label: 'Path Width', steppers: true, units: 'point'},
	pathGap: {type: 'number', label: 'Gap', steppers: true, units: 'point'},
	optForm: {type: 'list', label: 'Gradient Type', options: ['circle', 'line']},
	optColor: {type: 'checkbox', label: 'Color'},
	color1: {type: 'color', label: 'Start Color'},
	color2: {type: 'color', label: 'End Color'},
	optStroke: {type: 'checkbox', label: 'Stroke'},
	strokeMin: {type: 'number', label: 'Line Stroke Min', steppers: true, units: 'point'},
	strokeMax: {type: 'number', label: 'Line Stroke Max', steppers: true, units: 'point'},
	strokeStyle: {type: 'list', label: 'Line Stroke Style', options: ['round', 'square']},
	speed: {type: 'checkbox', label: 'Speed'}
};
	
var palette = new Palette('Crescendo', items, values);

function onMouseDown(event){
	tool.distanceThreshold = values.threshold;
	p = new Path();
}

function onMouseDrag(event){
	p.add(event.point)
}

function onMouseUp(event){
	p.smooth();
	var group = new Group();
	
	var pathLength = p.length;
	var size = values.pathGap;
	for(var pos = 0 ; pos < pathLength ; pos += size) {
		var point = p.getPoint(pos);
		var normal = p.getNormal(pos);
		var tangent = p.getTangent(pos);

		
		//  gradient calculation for each item
		var color = new RGBColor(
			((values.color2.red - values.color1.red) * pos / pathLength) + values.color1.red,
			((values.color2.green - values.color1.green) * pos / pathLength) + values.color1.green,
			((values.color2.blue - values.color1.blue) * pos / pathLength) + values.color1.blue
		);

		//  strokeWidth calculation for each item
		var stroke = values.strokeMin + ((values.strokeMax - values.strokeMin) / pathLength * pos)
		
		if (values.optForm == 'circle') {
			if (values.speed == false) {
			var path = new Path.Circle(point, values.pathWidth)
			} else {
			var path = new Path.Circle(point, tangent.length)
			}
			if (values.optStroke == true) {
				path.strokeWidth = stroke;
				if (values.optColor == true) {
					path.strokeColor = color;
					path.fillColor = null;
				} else {
					path.strokeColor = 'black';
					path.fillColor = null;
				}
			} else if (values.optColor == true) {
				path.fillColor = color;
				path.strokeColor = null;
			} else {
				path.fillColor = null;
				path.strokeColor = 'black';
				path.strokeWidth = values.strokeMin;
			}
		} else {
			if (values.speed == false)
			normal.length = values.pathWidth;
			
			var path = new Path.Line(point - normal, point + normal){
				fillColor: null
			};
			if(values.optColor == true) {
				path.strokeColor = color;
			} else {
				path.strokeColor = 'black';
			}
			
			if(values.optStroke == true) {
				path.strokeWidth = stroke;
			} else {
				path.strokeWidth = values.strokeMin;
			}
			
			if(values.strokeStyle == 'round') {
				path.strokeCap = 'round'
			} else {
				path.strokeCap = 'square'
			}
		}
		group.appendTop(path);
	}
	p.remove();
}
