/*****************************************************
*
*	JavaScript functions for 3rdStreetWorks:EasyReader
*
*
*****************************************************/

var readSpeed = THOUSAND;
var typeSize = 22;
var errMargin = 4;
var numChars = 9;
var timerID = null;
var txt = null;
var pos = 0;
var toggleAB = false;
const THOUSAND = 1000;
const HUNDRED = 100;

function initReader(){
	setReaderVals();
	displayMode('#easyReader');
}

function stopReader(){
	if(timerID !=null){	clearTimeout(timerID);}
}

function toggle(){
	toggleAB =!toggleAB;
	return toggleAB? 'A':'B';
}

function startReader(){
	pos = 0;
	setReaderVals();
	easyRead();
}

function easyRead(){
	var charCount = 0;
	var k,i,j,s;
	var wordArray = createWordArray();
	
	j = i = pos;
	s = '';
	
	// sanity check
	if(i>=(wordArray.length-1)){
		if(wordArray[i]!=undefined){
			$('#easyReaderTxtHolderA').text(wordArray[i]);
		}
		return;
	}
	
	// establish the last array indicie (j) which gives us a suitable length (based on numChars) for string s .
	while(charCount < numChars){
		if( (wordArray[j].length + charCount) <= (numChars + errMargin)){
			charCount += wordArray[j++].length;
		}
	}
	
	// backTrack if necessary.
	if(charCount > numChars){
		k = (j-1);
		
		//alert('wordArray[k]: '+wordArray[k]);
		
		if( k != i ){
			j = k;
		}
	}
	
	// use the array slice wordArray[k-j] to create a suitable length (based on numChars)  for string s .
	for(; i<=j; i++){
		s += wordArray[i] + ' ';
	}
	
	$('#easyReaderTxtHolderA').text(s);
	//fadeUpTxtSegment(s);
	
	if(i <= (wordArray.length-1)){
		pos = i;
		timerID = setTimeout("easyRead()", readSpeed);
	}
	
}

function fadeUpTxtSegment(s){
	var aORb = toggle();
	var altAorB = (aORb=='A') ? 'B' : 'A';
	var activeDiv = '#easyReaderTxtHolder'+aORb;
	var altDiv = '#easyReaderTxtHolder'+altAorB;
	
	$(activeDiv).css({opacity:100});
	$(altDiv).css({opacity:0});
	$(altDiv).text(s);
	$(activeDiv).fadeOut(readSpeed/3);
	$(altDiv).fadeIn(readSpeed/3);
	
}


function setReaderVals(){
	stopReader();
	readSpeed = ($('#readSpeed').val()*HUNDRED);
	typeSize = $('#txtSize').val();
	$('#easyReaderTxtHolder').css('font-size',typeSize);
	numChars = $('#numChars').val();
	txt = $('#easyReaderTextArea').val();
	// remove all carriage returns.
	txt = txt.replace(/^\s+|\s+$/g, ' ');
}

function createWordArray(){
	var wordArray = txt.split(' ');
	var returnArray = Array();
	
	for(i=0; i<wordArray.length; i++){
		if(wordArray[i].length>=(numChars+errMargin)){
			returnArray = hyphenateWord(word,returnArray);
		} else {
			returnArray.push(wordArray[i]);
		}
	}
	
	//alert('wordArray.length: '+wordArray.length+' vs returnArray.length: '+returnArray.length);

	return returnArray;
}

function hyphenateWord(word, wordArray){
	if(word.length>(numChars+errMargin)){
		// split the word with a hyphen.
		wordArray.push(word.substr(0,numChars)+'-');
		hyphenateWord(word.substr(numChars,(word.length-1)),wordArray);
	} else {
		wordArray.push(word);
	}
	return wordArray;
}

function wrapText(){
	txt.replace(/(.{1,#{numChars}})( +|$\n?)|(.{1,#{numChars}})/,"\\1\\3\n");
}

