/*
Type Generator by Damian Domes, Matthias Gieselmann, Johannes Hucht
*/

// Headline Stroke Style
var headLetterStyle = {
    strokeColor: 'white',
	strokeWidth: 2,
	strokeCap: 'butt',
	strokeJoin: 'miter',
	fillColor: null
};

// Make Grid for the whole document
segmentLengthX = 1.2*2;
segmentLengthY = 1.68*2;
walkVector = new Point(segmentLengthX, segmentLengthY);

// set up vectors for 8 directions to walk in
var directionsArray = new Array();
directionsArray[1] = new Point(0,-1);
directionsArray[2] = new Point(1,-1);
directionsArray[3] = new Point(1,0);
directionsArray[4] = new Point(1,1);
directionsArray[5] = new Point(0,1);
directionsArray[6] = new Point(-1,1);
directionsArray[7] = new Point(-1,0);
directionsArray[8] = new Point(-1,-1);
	
writeText("ToYS,_TRICKS#&_TOoLS",5,3,0,0,segmentLengthX,segmentLengthY,10,0.03,12,0,headLetterStyle);	

function onFrame(event) {
	if (event.count % 40 == 0) {
		project.activeLayer.selected = false;
		var index_1 = Math.floor(Math.random()*textGroup.children.length);
		var index_2 = Math.floor(Math.random()*textGroup.children[index_1].children.length);
		var index_3 = Math.floor(Math.random()*textGroup.children[index_1].children[index_2].segments.length);
		textGroup.children[index_1].children[index_2].segments[index_3].selected = true;
		var vector = directionsArray[Math.floor(Math.random()*8)]*walkVector;
		textGroup.children[index_1].children[index_2].segments[index_3].point += vector;
	}
}

function writeText(
	text,
	firstLineX,
	firstLineY,
	randomX,
	randomY,
	segmentLengthX,
	segmentLengthY,
	segmentsPerStep,
	deviationRatio,
	LineSpacing,
	wrapText,
	letterStyle
	) {

	// set up 8 walking commands
	// with rescpective possible walking directions
	var commandArray =  new Array();
	commandArray['t'] = new Array(8,1,2);
	commandArray['tr'] = new Array(1,2,3);
	commandArray['r'] = new Array(2,3,4);
	commandArray['br'] = new Array(3,4,5);
	commandArray['b'] = new Array(4,5,6);
	commandArray['bl'] = new Array(5,6,7);
	commandArray['l'] = new Array(6,7,8);
	commandArray['tl'] = new Array(7,8,1);
	
	// Create Group for this text
	textGroup = new Group();
	
	/* Set a couple of variables we will need */
	// Describe the characters
	var characters = new Array();
	characters["C"] = "rh,r,tr,blh,lh,tl,t,t,tr,r,br,bh,bh,bh,rh";
	characters["I"] = "t,t,t,t,bh,bh,bh,bh,rh";
	characters["K"] = "t,t,t,t,bh,bh,bh,tr,tr,tr,blh,blh,br,br,rh";
	characters["k"] = "t,t,t,t,bh,bh,bh,tr,tr,blh,br,b,rh";
	characters["L"] = "r,r,lh,lh,t,t,t,t,bh,bh,bh,bh,rh,rh,rh";
	characters["l"] = "th,th,th,th,b,b,b,b,r,r,rh";
	characters["O"] = "th,br,tr,t,t,tl,bl,b,b,brh,rh,rh";
	characters["o"] = "th,br,r,tr,t,t,tl,l,bl,b,b,brh,rh,rh,rh";
	characters["R"] = "t,t,t,t,r,br,bl,l,rh,br,b,rh";
	characters["S"] = "th,br,r,tr,tl,l,tl,tr,r,br,bh,bh,bh,rh";
	characters["T"] = "rh,t,t,t,t,lh,r,r,bh,bh,bh,bh,rh";
	characters["Y"] = "rh,tl,brh,tr,t,t,t,bh,bh,tl,tl,rh,rh,bh,bh,bh,bh,rh";
	characters["y"] = "rh,t,t,tl,t,rh,rh,b,bl,brh,brh";
	characters["_"] = "rh,rh";
	characters["–"] = "th,th,r,r,bh,brh";
	characters[","] = "th,b,b,th,rh";
	characters["&"] = "trh,rh,rh,bl,l,tl,tr,tr,tl,bl,br,br,br,rh";
	characters["#"] = "#";

	var characterWidth = segmentsPerStep*segmentLengthX*3;
	var characterHeight = segmentsPerStep*segmentLengthY*4;
	var LineHeight = characterHeight+(segmentLengthY*LineSpacing);
	var LineY = characterHeight;
	
	startPoint = new Point(0, LineY);
	walkVector = new Point (segmentLengthX, segmentLengthY);
	path = new Path();
	currentPos = startPoint;
	path.add(currentPos);
	path.style = letterStyle;
	var wordCounter = 0;
	textArray = text.split("");
	
	// The following loop walks through
	// all the characters in our text
	for(var k = 0; k < textArray.length; k++) {
	  
		//start a new group
		letterGroup = new Group();
		
		// start a new path for every letter
		path = new Path();
		currentPos = new Point(currentPos.x, LineY);
		path.add(currentPos);
		path.style = letterStyle;
		letterGroup.appendTop(path);
		
		var character = textArray[k];
		// parse character descriptions
		var characterArray = characters[character].split(",");
	  
		// count words for text wrap
		if (character == "_") {
				wordCounter++;
				if (wrapText > 0 && (wordCounter % wrapText) == 0) {
					lineBreak();
					// reset characterArray so empty space is not 
					// printed on beginning of new line
					var characterArray = new Array(); 
				}
		}
		
		// The following loop walks through
		// all the walk commands in our character
		var last_was_hidden;
		for (z = 0; z < characterArray.length; z++) {
			var walkCommand = characterArray[z];
			// check if walk command ends with h ("hidden")
			var hidden = false;
			if (walkCommand.charAt(walkCommand.length-1) == "h") {
				hidden = true;
				walkCommand = walkCommand.slice(0, -1);
				last_was_hidden = true;
			}
			else if(last_was_hidden == true) {
				path = new Path();
				path.add(currentPos);
				path.style = letterStyle;
				last_was_hidden = false;
			}
			
			// check if we want do do a linebreak
			if (walkCommand == "#") {
				lineBreak(hidden);    
			}
			else {
				for(i=0; i<segmentsPerStep; i++) {
					walk(commandArray[walkCommand],hidden);
				}
			}
			letterGroup.appendTop(path);
		}
	textGroup.appendTop(letterGroup);
	}
	
	// this function handles all the walking,
	// no matter what direction.
	// it is given an array with 3 values
	// which specify the three directions it can
	// walk in
	function walk(directions, hidden) {
		var index = 1;
		var randomNumber = Math.random();
		if (randomNumber < deviationRatio) { index = 0; }
		if (randomNumber > (1-deviationRatio)) { index = 2; }
		var direction = directions[index];
		vector = directionsArray[direction]*walkVector;
		currentPos += vector;
		if (hidden != true) { path.add(currentPos); }
	}
		
	function lineBreak() {
		LineY += LineHeight;
		path = new Path();
		currentPos = new Point(0, LineY);
		path.add(currentPos);
		path.style = letterStyle;
		textGroup.appendTop(path);
		wordCounter = 0;
	}
	
	// position our box
	position = new Point(firstLineX*segmentLengthX,firstLineY*segmentLengthY);
	textGroup.translate(position);
}
