var currentState = "NONE"; 
var previousState = "NONE"; 
 
var player = null;
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}
	

function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}
 
 
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
 
	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "current state: " + currentState + 
		"<br>previous state: " + previousState; 
	}
 
	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
		document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon.gif");
	} else if (currentState == "PLAYING") {
		document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-on.gif");
	}
}


// Load the player only the first time it is needed
function createPlayerLoad() {
	var flashvars = {
		type:"sound",
		skin:"http://lusitania-store.com/jwflv/skins/simple.swf",
		autostart:"true"
	}
 
	var params = {
		hidden:"true",
		allowfullscreen:"false", 
		allowscriptaccess:"always"
	}
 
	var attributes = {
		id:"placeholder1",  
		name:"placeholder1",
		style:"position: absolute; left: -4000px;"
	}
 
	swfobject.embedSWF("http://lusitania-store.com/jwflv/mediaplayer/player-licensed.swf", "placeholder1", "120", "20", "9.0.115", false, flashvars, params, attributes);
}	


function getXMLHTTPObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {
			try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {
				try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") }     catch(e) {
					try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") }  catch(e) {
						throw new Error( "This browser does not support XMLHttpRequest or XMLHTTP." );
					};
				};
			};
		};
	}
	return xmlHttp;
}


function stateChanged() { 
	if (xmlHttp.readyState==4) { 
		
		if(xmlHttp.status  == 200 || xmlHttp.status  == 0 ) {
			
		 	if(xmlHttp.responseText == "TIMEOUT") {
				if (alertID != "") {
					//document.getElementById(alertID).innerHTML="Timeout, please try later...";
					document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/erro.png");
				}
				return;
			}
			if (playerID) {
				var objfile = {file:xmlHttp.responseText,type:'sound'};
				player.sendEvent("LOAD",objfile);				
//				document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-on.gif");
			}
		}       
		else 
			alert("Error code " + xmlHttp.status);
		
	}
	
	if (alertID != "") {
		if (xmlHttp.readyState==3) { 
			//document.getElementById(alertID).innerHTML="Processing...";
			document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-wait.gif");
		}
		if (xmlHttp.readyState==2) { 
			//document.getElementById(alertID).innerHTML="Sending request...";
			document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-wait.gif");
		}
		if (xmlHttp.readyState==1) { 
			//document.getElementById(alertID).innerHTML="Synthesizing speech...";
			document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-wait.gif");
		}
	}
}


function Dixi_ReadMe(text_block_id, voz, player_id, alert_id) {
	
	texto = text_block_id;
	playerID  = player_id;
	alertID   = alert_id;

	if (texto.length==0) { 
		if (alertID != "") {
			//document.getElementById(alertID).innerHTML="Nothing to read in document!";
		}
		return;
	}
	
	if (playerID != "") {
		xmlHttp=getXMLHTTPObject();
		if (xmlHttp==null) {
			alert ("Browser does not support AJAX!");
			return;
			var url="";
		}
		
		var ip = "Leitura de Conteudo";
		
        url="http://lusitania-store.com/sintese/pt/dixiServer_webservice.php";
          
        xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange=stateChanged;

        if(encodeURIComponent){
          texto=encodeURIComponent(texto); // string URL encoding
        }
        else{// to be corrected later...
          alert("Your browser is not supported.");
        }

        xmlHttp.send("texto=" + texto +"&voz=" + voz + "&ip=" + ip);
	}
	
	return;	
}


// This function accepts the text to read (id1+id2+additional text), synthesizing it each time the user request it
function readProductAlways(id1, id2,text){
	var d=window.document;
	var txt='';

	// If doesn't exist, create it
	if (!player) {
		createPlayerLoad();					
	}

	// Read players state ( div id=stat )
	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "current state: " + currentState + 
		"<br>previous state: " + previousState; 
	}

	// Detects player state and proceed according
	if ( (currentState=="COMPLETED") || (currentState=="IDLE") || (currentState=="NONE") ) {
		if ( id1 ) {
			txt = d.getElementById(id1).innerHTML;
			txt = txt;
			if ( txt[txt.length-1]!="." ) { // Add a "." between two texts
				txt = txt + ". ";	
			}
		}
		if ( id2 ) {
			txt = txt + d.getElementById(id2).innerHTML;
		}
		txt = txt + text;
//		Dixi_ReadMe(txt, 'vicente', 'placeholder1', 'placeholder2');
		document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon-wait.gif");
		Dixi_ReadMe(txt, 'vicente', 'placeholder1', 'placeholder2');
	} else {
		player.sendEvent('STOP');
		document.getElementById("som").setAttribute("src", "http://lusitania-store.com/sintese/pt/audio-icon.gif");
	}

	return false;
}
