////////////////////////////////////////////////////////////////////////////////
// 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 values = {
	threshold: 20,
	lineCount: 20,
	lineOffset: 10,
	lineColor: 'white',
	lineStroke: 1,
	strokeDiff: 5
	} ;
	
var items = {
	threshold: {type: 'number', label: 'Distance Threshold', units: 'point'},
	lineCount: {type: 'number', label: 'Lines Quantity'},
	lineStroke: {type: 'number', label: 'Int Stroke', range: [1,100], steppers: true, units: 'point'},
	strokeDiff: {type: 'number', label: 'Ext Stroke (ratio)'},
	lineOffset: {type: 'number', label: 'Angle Sensibility', units: 'point'},
	lineColor: {type: 'list', label: 'Color', options:['white', 'black', 'free']},
	} ;
	
var palette = new Palette('Striped Wave', 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) {
    var step = event.delta;
    step.angle += (90).toRadians();

    var ThroughPoint;
	
	var group = new Group();

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

    	if(event.count.isEven()) {

        	ThroughPoint = event.middlePoint + step / 5;
		} else {
        	ThroughPoint = event.middlePoint - step / 5;
   		}
    line.curveThrough(ThroughPoint + (values.lineOffset * j), event.point + (values.lineOffset * j), 0.5);
	
	group.appendTop(line);
		
	line.fillColor = null;
	if (values.lineColor == 'white'){
	line.strokeColor = 'black';
	} else if (values.lineColor == 'black') {
	line.strokeColor = 'white';
	}
	line.strokeWidth = values.lineStroke;
	}
		
}

function onMouseUp(event) {
	lines.last.add(event.point);
	lines.last.join(lines.first);
	lines.last.closed = true;
	
	if (values.lineColor == 'white'){
	lines.last.fillColor = 'white';
	} else if (values.lineColor == 'black') {
	lines.last.fillColor = 'black';
	} else if (values.lineColor == 'free') {
	lines.last.fillColor = document.currentStyle.fillColor;
	}	
	lines.last.strokeWidth = values.lineStroke * values.strokeDiff;
	
	lines.last.moveBelow(lines[1]);	
}
