/*************************************************************************************************
**  MyVoiceŽ Net Auto
**	versione 0.0.2
**  Beta Version per inserimento automatico contributi vocali
**  Si prega voler inviare commenti e bug reporting all'indirizzo myvauto@rigel.li.it
**************************************************************************************************/
myvautolib=1;

window.onload=addvoices //Parte dopo che la pagina č caricata
document.onload=addmyvobj();  //Parte prima di caricare gli elementi

/* Funzione invocata al caricamento del documento html. Crea l'oggetto myvoice **/
function addmyvobj() {CreaOggettoMyVoice('myvobj','true','true');}

/**  Invocata subito dopo il caricamento della pagina web. 
**   Specifica su quali tag e per quali eventi inserire i contributi vocali  **/
function addvoices() 
   {
   if(!document.getElementsByTagName || !UseMyVoice) return;
   if (hideFloatPanel) restoreFloatValues();
   addvoice("p",new Array("onmouseover"));
   addvoice("a",new Array("onmouseover","onfocus"));
   addvoice("img",new Array("onmouseover","onfocus"));
   addvoice("ul",new Array("onmouseover"));
   addvoice("div",new Array("onmouseover"));
   speakTitle();
   }

function speakTitle() {if (document.title) ParlaTesto(document.title);}

/** Aggiunge un contributo vocale per tutti gli elementi con tagName specificato dal corrispondente parametro
**  e per tutti gli eventi indicati nel vettore 'evs'.     */
function addvoice(tagname, evs)
{
	var l=document.getElementsByTagName(tagname);
	for(var i=0;i<l.length;i++)
	   {for (var j=0; j<evs.length; j++) addVoiceToItem(l[i], evs[j]);
	   }
}

/** Aggiunge un contributo vocale per l'elemento html 'obj' e per l'evento indicato da 'attrname' */
function addVoiceToItem(obj, attrname)
{
	var objelem=null;
	//if you want to handler others events you must add a line like the following lines;
	switch (attrname)
		{
		case "onmouseover": {objelem=obj.onmouseover; break;}
		case "onfocus": 	{objelem=obj.onfocus; break;}
		default: {return;} //esce se non č uno degli eventi precedenti
		}
	
	if (!isVoiceAdded(objelem))
   	 {
	 var s = funToAdd(objelem);
	var ss2 = getVoiceText(obj); if (ss2==null || ss2=='') return;
	var s1 = "ParlaTesto(getVoiceText(this,'"+attrname+"'));"+s;
	
	//if you want to handler others events you must add a line like the following lines;
	 switch (attrname)
		{
		case "onmouseover": {obj.onmouseover=function(evt){eval(s1); if (InternetExplorer) {window.event.cancelBubble=true;} else evt.cancelBubble=true;}; break;}
		case "onfocus": 	{obj.onfocus=function(){eval(s1);}; break;}
		}
   }
}


/** Gives the text to speak on the basis of the element passed and its attributes */
function getVoiceText(obj)
{		
	var text = '';
	switch (obj.tagName.toLowerCase())
	   {
		case "a": 
		    { // If has title attribute give it, elsewhere gives the innText value
			var txt = getAttributeValue(obj,'title');
			if (txt!=null) {text=txt;}
			else {text=innText(obj);}
			break;
			}
		case "img": 
			{ // If has alt attribute, gives its value
			var txt = getAttributeValue(obj,'alt');
			if (txt!=null) {text=txt;}
			break;
			}
		case "p": {text=innText(obj); break;}
		default: {text=innText(obj); break;}
	   }
	   
	var entities = new Array("nbsp","agrave","egrave","igrave","ograve","ugrave")
	var values = new Array(" ", "a\'", "e\'","i\'","o\'","u\'")
	for (var i=0;i<entities.length;i++)
	   {text=replaceAll(text,'&'+entities[i]+';',values[i]);
	   }
	
	return text;
}


// Set the attribute 'name' of the object 'obj' to 'value'
function setAttribute(obj, name, value) {obj.setAttribute(name,value)}


// Gives the value of the attribute 'name' of the 'obj' element. Return null is the attribute is not defined.
function getAttributeValue(obj, name)
   {
   var attr = obj.attributes.getNamedItem(name);
   if (attr==null) {return null;}
   var value=attr.value;
   if (value==null || value=='') {return null;}
   return value;
   }


/** If the event (objEvent is something like elem.onfocus) contains the Myvoice functions invocations */
function isVoiceAdded(objEvent)
   {
	if (objEvent==null) {return false;}
	var funcVect = new Array();
	funcVect[0]='ParlaTesto'; funcVect[1]='ParlaURL'; 
	for (var i=0;i<funcVect.length;i++) {if (objEvent.toString().indexOf(funcVect[i])>-1) return true;}
	return false;									
   }
   

/** Gives the body of the event. It can be use to modify the event.
	Ex: p.onload=function(){alert('aaa'); fun(a1,a2);} => funToAdd(p.onload) = alert('aaa'); fun(a1,a2);
*/
function funToAdd(objEvent)
   {
	if (objEvent==null) {return '';}
	var strEvent = objEvent.toString();
	var firstIndex = strEvent.indexOf('{')+1;
	var lastIndex = getLastIndex(strEvent, '}');
	var s=strEvent.substring(firstIndex,lastIndex);
	return s;
   }
   

/**************************************************************************************************/
/********************************         UTILS         *******************************************/
/**************************************************************************************************/

/** The text inside an html element */
function innText(obj) {return getTxt(obj);}

function getTxt(obj)
{
if (obj.nodeType==3) 
	{//text node
	return obj.nodeValue;
	}
if (obj.nodeType==1) 
	{//element node
	var ss="";
	var childs = obj.childNodes;
	for (var i=0;i<childs.length;i++){ss+=getTxt(childs[i]);}
	return ss;
	}
return "";
}

   
/** The last occurrence of string "c" in string "str" */
function getLastIndex(str, c)
   {
   var lastIndex=-1; var curIndex=-1;
   for (var i=0; i<str.length; i=curIndex+1){
		var curIndex = str.indexOf(c,i);
		if (curIndex>-1) {lastIndex=curIndex;} else {return lastIndex;}
		}
	return lastIndex;
   }


/** Finds the "n" object in the current element */  
function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Replaces text with by in string
function replaceAll(string,text,by) 
{
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replaceAll(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}
