//
// Template for isound TTS (JavaScript)
//
// Copyright 2005, 2006 Audiantis GmbH - www.audiantis.com
//
// Version 1.5 
// Author: 	kkoechy, olietz
// last edit:	28.5.2006 (added lang to say)
// note: Implementation of JavaScript API (isound-api.js) is neccessary for some functions.

var g_isound_tts_text;


// Implemented in isound-api.js
//function sendCommand(cmd)
//{

	/*
	*** DESCRIPTION ***
		Sends the specified command to the iSound Server
		This doesn't effect the browser because
		iSound sends a HTTP-Status-Code 204 (no content)
		
	*** USAGE ***
		e.g.: sendCommand("setvolsound&" + streamID + "&" + fname + "&" + volume);
			or
			: sendCommand("openstream&1.mp3")

	*** PARAMETERS ***
		cmd : String contains one ore more commands for the iSound server
			: REQUIRED

	*** VARIABLES ***
		
		
	*** RETURN VALUE ***
		none
	*/

//}

// *** TTS RELATED ***
function isound_say(streamID, text, lang)
{
	/*
	*** DESCRIPTION ***
		Start a Text-to-Speech sound.
			
	*** USAGE ***
		
	
	*** PARAMETERS ***
		streamID, text, lang (optional)
		
	*** VARIABLES ***
		none
						
	*** RETURN VALUE ***
		none
	*/
	if(!streamID)
		streamID = stream_getSID();

	if(!lang)
	{
	}
	else
	{
		//if(lang==407)
		//	text = '<VOICE REQUIRED="language=407">' + text + '</VOICE>';
		text = '<VOICE REQUIRED="language=' + lang + '">' + text + '</VOICE>';
		//alert(text);
	}
	
	sendCommand("say&" + streamID + "&" + text);
}


function isound_stopTTS(streamID)
{
	/*
	*** DESCRIPTION ***
		Stop the current Text-to-Speech sound.
			
	*** USAGE ***
		
	
	*** PARAMETERS ***
		streamID
		
	*** VARIABLES ***
		none
						
	*** RETURN VALUE ***
		none
	*/
	if(!streamID)
		streamID = stream_getSID();
	sendCommand("stoptts&" + streamID);
}

//mpelny, 06.02.07
function isound_saySelection(lang)
//function isound_saySelection(streamID)
{
	/*
	*** DESCRIPTION ***
		Say selected text with Text-to-Speech.
			
	*** USAGE ***
		
	
	*** PARAMETERS ***
		streamID
		
	*** VARIABLES ***
		none
						
	*** RETURN VALUE ***
		none
	*/
	
	// mpelny, 06.02.07
	/*
	if(!streamID)
		streamID = stream_getSID();
	alert(g_isound_tts_text);
	sendCommand("say&" + streamID + "&" + g_isound_tts_text);
	*/
	if(!streamID)
		streamID = stream_getSID();
	var text = "<VOICE Required='Gender=male; Language=" + lang + "'>" + g_isound_tts_text + "</VOICE>";
	//alert("#" + text + "#");
	sendCommand("say&" + streamID + "&" + text);
	
}


function isound_copySelection() 
{
	/*
	*** DESCRIPTION ***
		Called from HTML-Event onMouseUp in <body>-tag
		Calls isound_copySelectionTimeout() after 50ms.
		setTimeout() needed for Firefox.
		Without setTimeout(), Firefox will not store the selected text.
		IE works fine without setTime()
			
	*** USAGE ***
		e.g.: <div onMouseUp="javascript:isound_copySelection();">
		    or
			: <body onMouseUp="isound_copySelection();">
	
	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		none
						
	*** RETURN VALUE ***
		true
	*/

	setTimeout("isound_copySelectionTimeout()",50);
	
	return true; 
}

function isound_copySelectionTimeout() 
{
	/*
	*** DESCRIPTION ***
		test browser-support for selected text
		store the result in g_isound_tts_text.
		should work with browser that support javascript 1.2:
		Netscape - not tested -
		Internet Explorer 6.0 SP2
		Firefox 1.06 (mac, win)
		Konquerer - not tested -
		Safari 1.3
		Opera 8.02 (win) no double-click to select		
		Called from isound_copySelection()
		
	*** USAGE ***
		setTimeout("isound_copySelectionTimeout()",50);

	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		txt: gets the selected Text after browercheck
						
	*** RETURN VALUE ***
		none
	*/
	
	// window.getSelection for >= NS6, Firefox, Safari
	if (window.getSelection)
	{
		// get the selected text
		//g_isound_tts_text = "" + window.getSelection();
		
		//mpelny, 06.02.07
		g_isound_tts_text = "" + parent.frameContent.window.getSelection();
	}
	// document.getSelection for <= NS6, Opera
	else if (document.getSelection)
	{
		// get the selected text
		//g_isound_tts_text = "" + document.getSelection();
		
		//mpelny, 06.02.07	
		g_isound_tts_text = "" + parent.frameContent.document.getSelection();
	}
	// document.selection for IE
	else if (document.selection)
	{
		// get the selected text
		//g_isound_tts_text = "" + document.selection.createRange().text;

		//mpelny, 06.02.07
		g_isound_tts_text = "" + parent.frameContent.document.selection.createRange().text;
	}
	//store the selected text for later use (isound_sayselection()).
	
	//alert(g_isound_tts_text);
}

function isound_translate(streamID, text, source, destination)
{
	/*
	*** DESCRIPTION ***
		translate text and speak from source language to destination language
		
	*** USAGE ***

	*** PARAMETERS ***
		
	*** VARIABLES ***
						
	*** RETURN VALUE ***
		none
	*/
	if(!streamID)
		streamID = stream_getSID();
	//alert("Translating " + text);
	sendCommand("translate&" + streamID + "&" + text + "&" + source + "&" + destination)
}
