//
//
//    Speaking contols for the Ed Bx Website
//
//

var MIN_CHUNK_SIZE = 400;    
var MAX_CHUNKS = 10;

var USE_READID = false;
var myDates = new Array("1853","1990","1970","1932","1998","2003");
var sayasDateStart = '<say-as interpret-as="Date:Y">';
var sayasDateEnd = '</say-as>';

function read()
{
		// look through the page and find all entities with an ID that starts with 'readme'
		// remember, all entities on a web page must have different IDs.		
		
		// txt contains all the innerHTML's concatenated
		var txt = "";
		var parsed = "";
		toread = document.getElementsByTagName("h2");
		for(var i=0;i<toread.length;i++)
		{
			parsed += toread[i].innerHTML.replace( new RegExp( "<.*>", "gi" ), "" );
			
			parsed = parsed.replace(/^\s+|\s+$/g, '') ;   // trim
			
			// add any missing full stops
			if (parsed.length > 0)
			{
				if (parsed.substring(parsed.length,parsed.length-1) != ".") 
					parsed += ".  	";
				txt += parsed;
			}
		}
		toread = document.getElementsByTagName("p");
		for(var i=0;i<toread.length;i++)
		{
			if (USE_READID)
			{
		    if (toread[i].id.substring(0,6) == "readme")
			    txt += toread[i].innerHTML.replace( new RegExp( "<.*>", "gi" ), "" );
			}
			else
				txt += toread[i].innerHTML.replace( new RegExp( "<.*>", "gi" ), "" );
		}
		
		// loop through the text, saying each sentence in turn
		var sentence = "";
		var dotIndex = txt.indexOf('.');

		while (dotIndex > -1)
		{
			sentence = txt.substring(0,dotIndex+1);
			sentence = sentence.replace(/^\s+|\s+$/g, '') ;   // trim
			
			if (sentence.length > 0)
			{
				thisText = pronounceDates(sentence);
				//alert (thisText);
				sayText (thisText,5,1,2);
			}

			txt = txt.slice(sentence.length);
			txt = txt.replace(/^\s+|\s+$/g, '') ;   // trim
			dotIndex = txt.indexOf('.');
		}
		
//		toread = document.getElementsByTagName("p");

//		for(var i=0;i<toread.length;i++)
//		{
//		    if (toread[i].id.substring(0,6) == "readme")
//			    txt += toread[i].innerHTML;		
//		}
//		toread = document.getElementsByTagName("h2");

//		for(var i=0;i<toread.length;i++)
//		{
//		    if (toread[i].id.substring(0,6) == "readme")
//			    txt += toread[i].value;		
//		}
		
		
		// set the speech rate controls
//		var srate = "<prosody rate='85%'>";
//		var end_srate = "</prosody>";

		// Clean-up basic text
//		txt = txt.replace(/&nbsp;/gi,"");	// remove any non-breakable spaces
//		txt = removeUnicodeChars(txt);      //  keep to a limited character set to avoid confusing the TTS
//		txt = removeWhiteSpace(txt);        //  remove extra spaces between words
//		txt = removeIBlocks(txt);           //  don't read anything in italics
//		txt = removeNL(txt)                 //  remove control characters - final check
	
		// change HTML to punctuation
		//  "<a.*?>(.*?)</a>"  // regex for a tag
		
//		txt = txt.replace(/&gt;/,"");
//		txt = txt.replace( new RegExp( "<p>", "gi" ), "" );     // remove opening paragraphs
//		txt = txt.replace( new RegExp( "</p>", "gi" ), "." );   // replace closing paragraphs with a fullstop
//		txt = txt.replace( new RegExp( "<br>", "gi" ), "," );   // replace breaks with a comma
		
//        var tlen = txt.length;

        // break into chunks of less than 600 characters (for the synthesis engine)
        // look for the first full stop after n00 characters and call this a chunk
//        var chunk = "";
//        var have_chunk = false;
//        var i = 0;
//        var chunki = 0;
//        var chunk_count = 0;
		
//		alert (txt);

//        while ((i < tlen) && (chunk_count < MAX_CHUNKS))
//        {
//                chunk = "";
//                have_chunk = false;
//                chunki = 0;

//                while (!have_chunk)
//                {
//                        chr = txt.substring(i,i+1);
//                        chunk = chunk + chr;
//                        i+= 1;
//                        chunki += 1;
//                        if (((chunki > MIN_CHUNK_SIZE) && (chr == ".")) || (i >= tlen))
//                        {
//                                have_chunk = true;
//                                chunk_count += 1;
//                                if (chunk.length > 1)
//                                {
//                                    sayText(srate + chunk + end_srate,5,1,2);
//                                }
//                                else break;
//                        }
//                }
//        }
}
function stopReading()
{
	stopSpeech();
}

function pronounceDates(txt)
{
	// look through string and find data instances
	// wrap any known dates with say-as tags
	
	for (var i=0;i<myDates.length;i++)
	{
		pos = txt.indexOf(myDates[i]);
		if (pos > -1)
		{
			txtLeft = txt.substring(0,pos);
			txtRight = txt.substring(pos + myDates[i].length, txt.length);
			txt = txtLeft + sayasDateStart + myDates[i] + sayasDateEnd + txtRight;
		}
	}
	return txt;
}

function removeHTMLblock(aSourceString, aTagName){
	regexp= new RegExp ("<" + aTagName + "[^.]\.*\/*" + aTagName + ">", "gi");
	vStrippedHTML = aSourceString.replace(regexp,"");
	return vStrippedHTML;
}
function removeIBlocks(txt)
{
	var pos = 0;

	while (pos>-1)
	{
		pos = txt.indexOf("<i>");
		pos1 = txt.indexOf("</i>");
		if ((pos > -1) && (pos1 > -1))
		{
			txt = txt.substring (0,pos) + txt.substring (pos1+4,txt.length);
			pos = txt.indexOf("<i>");
		}
	}
	return txt;
}
//function removeNL(s){ 
  // removes control characters
//  return s.replace(/[\n\r\t]/g,); 
//}
function removeNL(s) {
 
  //  Remove NewLine, CarriageReturn and Tab characters from a String
  r = "";
  for (i=0; i < s.length; i++) {
    if (s.charAt(i) != '\n' &&
        s.charAt(i) != '\r' &&
        s.charAt(i) != '\t') {
      r += s.charAt(i);
      }
    }
  return r;
  }
function removeUnicodeChars(s)
{
	var r = "";
	for (i=0; i < s.length; i++) 
	{
	  var c = s.charCodeAt(i);
	  if ((c > 31) && (c < 123))
	  {
		  if ((c=32) || (c=33)|| (c=44) || (c=46) || ((c>47) && (c<60)) || (c=63) || ((c>64) && (c<90)) || ((c>96) && (c<123)))
		  	r += s.charAt(i);
	  }
	}
	return r;
}
function removeWhiteSpace (str) {
  return str.replace(/\s+/g,' ');
}
