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

function onMouseDown(event) {
	var hitResult = document.hitTest(event.point);
	if (hitResult) {
		var clickedItem = hitResult.item;


		var bounds = clickedItem.bounds;

		var rightArea = new Path();
		rightArea.add(bounds.center);
		rightArea.add(bounds.bottomRight);
		rightArea.add(bounds.topRight);

		var leftArea = new Path();
		leftArea.add(bounds.center);
		leftArea.add(bounds.bottomLeft);
		leftArea.add(bounds.topLeft);
		
		var topArea = new Path();
		topArea.add(bounds.center);
		topArea.add(bounds.topRight);
		topArea.add(bounds.topLeft);
		
		var bottomArea = new Path();
		bottomArea.add(bounds.center);
		bottomArea.add(bounds.bottomRight);
		bottomArea.add(bounds.bottomLeft);
		
		if (rightArea.hitTest(event.point)){
			// go right
			var scale = values.scale / 100;
			var scaleValue = scale;
			for (var i = 0 ; i < values.RepeatCount ; i = i+1){
var copy = clickedItem.clone();
copy.rotate(values.rotation.toRadians() * (i + 1));
copy.position.x = copy.position.x + clickedItem.bounds.width*(i+1) + ((i+1) * values.distance);
copy.scale(scaleValue);
scaleValue *= scale;

if (i.isEven() && values.flip){
	copy.scale(-1, 1);
}
			}
		} 
		else if (leftArea.hitTest(event.point)) {
			// go left
			var scale = values.scale / 100;
			var scaleValue = scale;
			for (var i = 0 ; i < values.RepeatCount ; i = i+1){
var copy = clickedItem.clone();
copy.rotate(values.rotation.toRadians() * (i + 1));
copy.position.x = copy.position.x - clickedItem.bounds.width*(i+1) - ((i+1) * values.distance);

copy.scale(scaleValue);
scaleValue *= scale;

if (i.isEven() && values.flip){
	copy.scale(-1, 1);
}
			}
		} 
		else if (topArea.hitTest(event.point)) {
			// go up
			var scale = values.scale / 100;
			var scaleValue = scale;
			for (var i = 0 ; i < values.RepeatCount ; i = i+1){
var copy = clickedItem.clone();
copy.rotate(values.rotation.toRadians() * (i + 1));
copy.position.y = copy.position.y + clickedItem.bounds.height*(i+1) + ((i+1) * values.distance);

copy.scale(scaleValue);
scaleValue *= scale;

if (i.isEven() && values.flip){
	copy.scale(1, -1);
}
			}
		} 
		else if (bottomArea.hitTest(event.point)) {
			// go down
			var scale = values.scale / 100;
			var scaleValue = scale;
			for (var i = 0 ; i < values.RepeatCount ; i = i+1){
var copy = clickedItem.clone();
copy.rotate(values.rotation.toRadians() * (i + 1));
			    copy.position.y = copy.position.y - clickedItem.bounds.height*(i+1) - ((i+1) * values.distance);

			 	copy.scale(scaleValue);
scaleValue *= scale;

if (i.isEven() && values.flip){
	copy.scale(1, -1);
	
}
			}
		}
		rightArea.remove();
		leftArea.remove();
		topArea.remove();
		bottomArea.remove();
	}
}

var values = {
	RepeatCount: 1,
	distance: 0,
	rotation: 0,
	flip: true,
	scale: 100
};

var elements = { 
    RepeatCount: {
		type: 'number',
		label: 'Repeat Count',
		steppers: true,
	},
	 
    rotation: {
		type: 'number',
		label: 'Rotate',
		steppers: true,
		units: 'degree',
		range: [-180, 180]
	},

	distance: {
			type: 'number',
			label: 'Distance',
			steppers: true,
			units: 'millimeter',
		},

	scale: {
			type: 'number',
			label: 'Scale',
			steppers: true,
			units: 'percent',
			range: [0, 100]
		},
	
	flip: {
		type: 'checkbox'
	}
};

var palette = new Palette('ClickFlip Palette', elements, values); 

