////////////////////////////////////////////////////////////////////////////////
// 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 values = {
	text: 'Moving the paper up and down while my dad\'s old automatic typewriter was busy typing a letter allowed me to draw with text.',
	size: 10,
	tracking: 100,
	count: 0,
	splitter: 'Letters',
	perfectSpacing: false,
	repeat: true
};
var components = {
	text: { type: 'string',
		label: 'Text',
		multiline: true,
		width: 155,
		height: 80,
		onChange: function(value) {
			refreshText();
		}
	},
	list: { type: 'list', label: 'Next', width: 145 },
	splitter: { type: 'list', label: 'Draw', width: 145,	
		options: ['Words', 'Letters'], 
		onChange: function(value) {
			refreshText();
		}
	},
	perfectSpacing: {
		label: 'Kerning', type: 'checkbox'
	},
	repeat: {
		label: 'Repeat', type: 'checkbox'
	},
	font: { label: 'Font', type: 'font', value: 'Arial Bold' },
	size: { 
		label: 'Size', type: 'number',
		min: 0, max: 1000, steppers: true
	},
	tracking: { 
		label: 'Tracking', type: 'number',
		min: 0, max: 1000, steppers: true
	},
	credits: {
		type: 'text', value: 'Jonathan Puckey 2006 - 2010'
	}
}

refreshText(); 

var palette = new Palette('Text Pencil', components, values);
var count = 0;

tool.eventInterval = 1;
var adjustLeft = true;
var index, listLength;
var max = new Point(0, 0);
function onMouseDown(event) {
	index = components.list.selectedIndex;
	listLength = components.list.options.length;
	lastPoint = event.point;
	textItem = createTextItem();
	// vertical spacing is set to the values.size + tracking. I think this is how Adobe handles vertically oriented text
	// (make it tighter when using letters then when using words)
	if (values.splitter == 'Letters') {
		max.y = values.size * 0.8 + values.size * 0.8 * values.tracking / 1000;
	} else {
		max.y = values.size + values.size * values.tracking / 1000;
	}
	finished = false;
}

function onMouseUp(event) {
	components.list.selectedIndex = index;
	textItem.remove();
	if(finished)
		components.list.selectedIndex = 0;
	// wordsList.get(values.count).selected = true;
}


var lastPoint, finished;
function onMouseDrag(event) {
	if(!finished) {
		var dist = event.point - lastPoint;
		var absDist = dist.abs(); 
		if (absDist.x >= max.x | absDist.y >= max.y) {
			var dir = dist / absDist;
			if (isNaN(dir.x)) dir.x = 1;
			if (isNaN(dir.y)) dir.y = 1;

			if (values.perfectSpacing) {
				lastPoint = textItem.point = lastPoint + new Point(Math.min(absDist.x, max.x), Math.min(absDist.y, max.y)) * dir;
			} else {
				lastPoint = textItem.point = event.point;
			}

			if (max.x <= absDist.x && dist.x < 0) {
				//align the text to the right
				textItem.paragraphStyle.justification = 'right';
				// if it is the first character with right justification,
				// move it to the right by the amount of it's own width
				if (adjustLeft == true)
					offset = [textItem.bounds.width, 0];
				textItem.point += offset;
				adjustLeft = false;
			} else {
				adjustLeft = true;
			}
			max.x = textItem.bounds.width;
			if (index < listLength - 1) {
				index++;
			} else if(values.repeat == true) {
				index = 0;
			} else {
				finished = true;
			}
			if(textItem.content == ' ')
				textItem.remove();
			textItem = createTextItem();
		}
	}
}

function createTextItem() {
	return new PointText([-8000, -8000]) {
		characterStyle: {
			font: values.font,
			fontSize: values.size,
			tracking: values.tracking
		},
		content: components.list.options[index]
	};
}

function refreshText() {
	if (values.splitter == 'Letters') {
		components.list.options = values.text.split('');
	} else {
		components.list.options = values.text.replace(/\r/g, ' ').replace(/ /g, ' ||').split('||');
	}
}