////////////////////////////////////////////////////////////////////////////////
// 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 j;

var lines;
var angle = (90).toRadians();

var values = {
	threshold: 1,
	lineCount: 10,
	lineAmplification: 1,
	lineGap: 5,
	lineColor: 'white',
	lineStroke: 1,
	strokeDiff: 3,
	speed: true,
	lineSmooth: true
	} ;
	
var items = {
	threshold: {type: 'number', label: 'Distance Threshold', units: 'point'},
	lineCount: {type: 'number', label: 'Lines Quantity', steppers: true},
	lineStroke: {type: 'number', label: 'Int Stroke', steppers: true, units: 'point'},
	strokeDiff: {type: 'number', label: 'Ext Stroke (ratio)', steppers: true},
	lineGap: {type: 'number', label: 'Dist beetween lines'},
	lineColor: {type: 'list', label: 'Color', options:['white', 'black', 'free']},
	speed: {type: 'checkbox', label: 'Speed'},
	lineAmplification: {type: 'number', label: 'Speed Amplification'},
	lineSmooth: {type: 'checkbox', label: 'Smooth Line'}
	} ;
	
var palette = new Palette('Striped Tape', 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 = [];
	var group = new Group();
	for (var j = 0; j < values.lineCount; j++){
		var line = new Path();
		lines[j] = line;		
		lines[j].add(event.point);

		if (values.lineColor == 'white'){
			line.fillColor = 'white';
			line.strokeColor = 'black';
		} else if (values.lineColor == 'black') {
			line.fillColor = 'black';
			line.strokeColor = 'white';
		}
		group.appendTop(line);
	}
}

var dragPoint;
var dragDelta;
function onMouseDrag(event) {	
		dragPoint = event.point;
		dragDelta = event.delta.clone();
		var step = event.delta.clone();
	for (var j = 0; j < values.lineCount; j++) {
		var line = lines[j];
		var top, bottom;
		
		if(values.speed == true){
			var step = event.delta.clone();
			step.length *= ((values.lineCount - j - 1) / values.lineCount)*values.lineAmplification;
		} else {
			step.length = ((values.lineCount - j - 1) * values.lineGap);
		}
		var top = event.middlePoint + step.rotate(angle * -1);
		var bottom = event.middlePoint + step.rotate(angle);
	
		line.add(top);
		line.insert(0, bottom);
		
		if(values.lineSmooth == true){
			line.smooth();
		}
	}
}

function onMouseUp(event) {
	for (var j = 0; j < values.lineCount; j++) {
		var line = lines[j];
		if(values.speed == true) {
			line.add(event.point);
			line.insert(0, event.point);
		} else {
			line.add(dragPoint + dragDelta * 5);
			line.insert(0, dragPoint + dragDelta * 5);
		}
		line.strokeWidth = values.lineStroke;
		lines.first.strokeWidth = values.lineStroke*values.strokeDiff;
	}
}

