// FLUENCY WEBAPI
// Javascript interface
// Copyright 2009 Fluency, Amsterdam. All rights reserved
// http://www.fluency.nl

// USAGE: place this script within a DIV-element in the BODY of your webpage!

// NOTE: the script will not work unless your website has been granted access to the Fluency server!

// location of speak.swf
speakSWFLocation = "/speak.swf";

// embed speak.swf
document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ");
document.write("id='speakObj' width='0' height='0' ");
document.write("codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab'> ");
document.write("<param name='movie' value='" + speakSWFLocation + "' /> ");
document.write("<param name='quality' value='high' /> ");
document.write("<param name='bgcolor' value='#FFFFFF' /> ");
document.write("<param name='allowScriptAccess' value='sameDomain' /> ");
document.write("<embed src='" + speakSWFLocation + "' quality='high' bgcolor='#FFFFFF' ");
document.write("width='0' height='0' name='speakObj' align='middle' ");
document.write("play='true' loop='false' quality='high' allowScriptAccess='sameDomain' ");
document.write("type='application/x-shockwave-flash' ");
document.write("pluginspage='http://www.adobe.com/go/getflashplayer'> ");
document.write("</embed>");
document.write("</object>");

// access speak.swf
function fluencyWebAPI() {

	if (navigator.appName.indexOf("Microsoft") !=-1) {
		return window["speakObj"];
	} else {
		return document["speakObj"];
	}
}

// status and error messages
function speakMessage(msg, id) {

	try { document.getElementById(id).innerHTML = msg; } catch(e) {}
}

// API functions

function speakText(str, voice, msgid) {

	if (str == "") {
		speakMessage("Geen tekst!", msgid);
	}
	else
	try {	
		fluencyWebAPI().speakText(escape(str), escape(voice), msgid);
	} catch(e) {
		speakMessage("De spraak-functie is niet beschikbaar!", msgid);
	}
}

function stopSpeaking() {

	try { fluencyWebAPI().stopSpeaking(); } catch(e) {}
}

function getSelectedText() {

	var str = "";
	// preferred method in Firefox
	if (window.getSelection) {
    		str = window.getSelection().toString();
  	}
  	else 
	// older versions?
	if (document.getSelection) {
		str = document.getSelection().toString();
  	}
  	else 
	// Internet Explorer
	if (document.selection) {
    		str = document.selection.createRange().text;
  	}
    	return str;
}

function speakSelection(voice, msgid) {

	str = getSelectedText();
  	if (str == "") {
    		speakMessage("Selecteer eerst met de muis de tekst die je wilt horen!", msgid);
  	}
  	else {
    		speakText(str, voice, msgid);
  	}
}


// speak HTML (strips tags, but does not convert encoded characters!)
function speakHTML(str, voice, msgid) {

  	// replace newlines and carriage returns with spaces
  	str = str.replace(/\n/ig," "); 
  	str = str.replace(/\r/ig," "); 

  	// replace certain end tags, paragraph and break tags with a newline
  	str = str.replace(/<\/h[12345]>/ig, "\n");
  	str = str.replace(/<br>/ig, "\n");
  	str = str.replace(/<p>/ig, "\n");
  	str = str.replace(/<\/p>/ig, "\n");
  	str = str.replace(/<\/td>/ig, "\n");
  	str = str.replace(/<\/div>/ig, "\n");

  	// remove all tags
  	str = str.replace(/(<([^>]+)>)/ig, ""); 
  
  	// speak it!
  	speakText(str, voice, msgid);
}

// speak element
function speakElement(id, voice, msgid) {

	str = document.getElementById(id).innerHTML;
	speakHTML(str, voice, msgid);
}

