////////////////////////////////////////////////////////////////////////////////
// Note from the Scriptographer.org Team
//
// In Scriptographer 2.9, we switched to a top-down coordinate system and
// degrees for angle units as an easier alternative to radians.
// 
// For backward compatibility we offer the possibility to still use the old
// bottom-up coordinate system and radians for angle units, by setting the two
// values bellow. Read more about this transition on our website:
// http://scriptographer.org/news/version-2.9.064-arrived/

script.coordinateSystem = 'bottom-up';
script.angleUnits = 'radians';


var lastDelta, currentDelta, diffDelta, lastDiffDelta = 0;
var lines, path, j;
var angle = (90).toRadians();

var values = {
	threshold: 100,
	lineCount: 10,
	pathStroke: 1,
	strokeExt: 5,
	angleSensibility: 5,
	pathAmplification: 1,
	curveAmplification: 5,
	lineColor: 'white',
	baseLine: true,
	lineSmooth: true
	} ;
	
var items = {
	threshold: {type: 'number', label: 'Distance Threshold', units: 'point'},
	lineCount: {type: 'number', label: 'Lines Quantity'},
	pathStroke: {type: 'number', label: 'Int Stroke', range: [1,100], steppers: true, units: 'point'},
	strokeExt: {type: 'number', label: 'Ext Stroke (ratio)'},
	angleSensibility: {type: 'number', label: 'Angle Sensibility'},
	pathAmplification: {type: 'number', label: 'Amplification'},
	curveAmplification: {type: 'number', label: 'Curve Amplification'},
	lineColor: {type: 'list', label: 'Color', options:['white', 'black', 'free']},
	baseLine: {type: 'checkbox', label: 'Base Line'}, 
	lineSmooth: {type: 'checkbox', label: 'Smooth Line'}
	} ;
		
var palette = new Palette('Curve Amplifier', items, values);

// --------------------------

function onMouseDown(event) {
	with(document.layers[0]){
    if(locked || hidden){
      Dialog.alert("please unlock and show the first layer");
      return; }
	}

	tool.distanceThreshold = values.threshold;
	
	lines = [];
	
	for (var j = 0; j < values.lineCount; j++){
		lines[j] = new Path();
		lines[j].add(event.point);
	}
}


function onMouseDrag(event) {
	if(lastDelta) {
		var group = new(Group);

		for (var j = 0; j < values.lineCount; j++) {
			 var line = lines[j];

			var instantSpeed = event.delta.clone();
			var step = event.delta.clone();
			step.length *= ((values.lineCount - j - 1) / (values.lineCount * 5));
			
			if(values.baseLine == false){
			instantSpeed.length = step.length;
			}
		
			currentDelta = event.delta;
			diffDelta = currentDelta.getDirectedAngle(lastDelta).toDegrees();

			diffDelta = 0.95 * lastDiffDelta + 0.05 * diffDelta; // ratio between old and new deltas
		
			if(diffDelta < -values.angleSensibility) {
				var top = event.middlePoint + instantSpeed.rotate(angle * -1) / 10 * values.pathAmplification;
				var bottom = event.middlePoint + step.rotate(angle) * Math.abs(diffDelta) / 10 * values.pathAmplification* values.curveAmplification;
			} else if(diffDelta > values.angleSensibility) {
				var top = event.middlePoint + step.rotate(angle * -1) * Math.abs(diffDelta) / 10 * values.pathAmplification* values.curveAmplification;
				var bottom = event.middlePoint + instantSpeed.rotate(angle) / 10 * values.pathAmplification;
			} else {
				var top = event.middlePoint + instantSpeed.rotate(angle * -1) / 10 * values.pathAmplification;
				var bottom = event.middlePoint + instantSpeed.rotate(angle) / 10 * values.pathAmplification;
			}

		line.add(top);
		line.insert(0, bottom);
		
		if (values.lineColor == 'white'){
		line.fillColor = 'white';
		line.strokeColor = 'black';
		} else if (values.lineColor == 'black') {
		line.fillColor = 'black';
		line.strokeColor = 'white';
		}
		
		line.strokeWidth = values.pathStroke;
		lines.first.strokeWidth = values.pathStroke*values.strokeExt;
		
		if(values.lineSmooth == true){
			line.smooth();
		}
	
		group.appendTop(line);

		lastDiffDelta = diffDelta;
		}
	}
	lastDelta = event.delta;
}


