﻿/*
	HI TECH VOICE v1.0
	Servizio di Text-to Speech realizzato da HI TECL LAB srl
	Copyright 2011 - HI TECH LAB srl
*/

var st = '';
var SWFPath = 'htl-tts/jPlayer';
var lang = '';document.getElementsByTagName('html')[0].lang.substring(0,2);

if ($j) {
	if(!window.HTL){HTL = {};}
	HTL.Selector = {};
	HTL.Selector.getSelected = function(){
	var t = '';
	if(window.getSelection){
		t = window.getSelection();
	}else if(document.getSelection){
		t = document.getSelection();
	}else if(document.selection){
		t = document.selection.createRange().text;
	}
	return t;
	}
	HTL.Selector.mouseup = function(){
	var st = HTL.Selector.getSelected();
	if(st!=''){
	if ($j('#status').text() == 'Pronto' && $j.cookie("toggle-disable")=="enabled"){
		post_selected(st);
		}
	}
	}
			
	$j(document).ready(
		function() {

			$j('#status').bind("click", post);
			$j('#text').bind("keypress", function(){
				$j('#status').text("Pronto");  
				$j('#status').bind("click", post);
			});
			
			/* ABILITAZIONE MOUSEENTER*/
			$j(document).bind("mouseup", HTL.Selector.mouseup);
			$j('p,li,h1,h2,h3').bind("mouseenter", function(){
				if ($j('#status').text() == 'Pronto' && $j.cookie("toggle-disable")=="enabled"){
					var st = $j(this).text();
					post_selected(st);
					$j(this).addClass('reading');
					}
				});
			$j('p,li,h1,h2,h3').bind("mouseleave", function(){
					$j(this).removeClass("reading");
			});
			/* END ABILITAZIONE MOUSEENTER*/
			
			
			/* TASTO ABILITAZIONE VOICE  */
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer({solution:"flash, html",swfPath: ''+SWFPath+'',volume: 50});
			$j("#toggle-disable").bind("click", toggleDisable);
			if ($j.cookie("toggle-disable")==null){
				$j.cookie("toggle-disable","disabled");
				$j("#toggle-disable").text("OFF");
			}
			else if ($j.cookie("toggle-disable")=="enabled"){
				$j("#toggle-disable").text("ON");
				$j("#toggle-disable").attr("class","enabled");
			}
			else if ($j.cookie("toggle-disable")=="disabled"){
				$j("#toggle-disable").text("OFF");
				$j("#toggle-disable").attr("class","disabled");
			}
			/* END TASTO ABILITAZIONE VOICE  */
			
			/* TASTI RAPIDI*/
			
			$j(this).keypress(function(e){
				var key = e.which;
				if (key==49){         //  TASTO '1'
					toggleDisable();
				}else if (key==50){   //  TASTO '2'
					$j('.jp-play').trigger("click");
				}else if (key==51){   //  TASTO '3'
					$j('.jp-pause').trigger("click");
				}else if (key==52){   //  TASTO '4'
					$j('.jp-stop').trigger("click");
				}else if (key==48){   //  TASTO '0'
					read_all();
				}
			});
			
			/* END TASTI RAPIDI*/
			
			/* TASTO LEGGI TUTTO */
			
			$j("#read-all").bind("click",read_all);
			
			/* END TASTO LEGGI TUTTO */
			
			/* TASTO INFO */
			$j("#help-tts").hide();
			$j("#info-tts").bind("mouseenter",function(){$j("#help-tts").show();});
			$j("#info-tts").bind("mouseleave",function(){$j("#help-tts").hide();});
			/* TASTO INFO */
			
			lang = document.getElementsByTagName('html')[0].lang.substring(0,2);
		}
	);
}	

function read_all(){
	if ($j('#status').text() == 'Pronto' && $j.cookie("toggle-disable")=="enabled"){
		objtoremove = $j('.to-read').find('script');
		toread = $j('.to-read').text();
		$j.each(objtoremove, function(i,toremove){toread = toread.replace(toremove.text,"");});
		
		txt_to_read = correct_words(document.title+'.'+toread);
		post_selected(txt_to_read);
	}
}

function correct_words(txt){
	txt = txt.toLowerCase();
	txt = txt.replace(/     /g,".");txt = txt.replace(/hi t/g,"ai t");txt = txt.replace(/lab s/g,"lab.s");txt = txt.replace(/google/g,"gugol");txt = txt.replace(/firefox/g,"fairfox");txt = txt.replace(/dotnetnuke/g,"dotnetniuk");txt = txt.replace(/software/g,"sòftuer");txt = txt.replace(/application/g,"applikèscion");txt = txt.replace(/framework/g,"freimuork");txt = txt.replace(/portal /g,"pòrtal ");txt = txt.replace(/style/g,"stail");txt = txt.replace(/agreement/g,"agrìiment");txt = txt.replace(/source/g,"sours");txt = txt.replace(/freeware/g,"frìiuer");txt = txt.replace(/information/g,"informescion");txt = txt.replace(/facebook/g,"feisbuk");txt = txt.replace(/bug /g,"baagh");txt = txt.replace(/bugfix /g,"baaghfix");txt = txt.replace(/tantissime /g,"tantìssime");txt = txt.replace(/javascript/g,"giàvascript");txt = txt.replace(/medica/g,"mèdica");txt = txt.replace(/database/g,"databèis");txt = txt.replace(/management/g,"manàgement");txt = txt.replace(/protesi/g,"pròtesi");txt = txt.replace(/diagnostica/g,"diagnòstica");txt = txt.replace(/recognition/g,"ricoghniscion");txt = txt.replace(/custom /g,"castom ");txt = txt.replace(/made /g,"meid " );txt = txt.replace(/meccanica/g,"meccànica" );txt = txt.replace(/basic /g,"bèsik " );txt = txt.replace(/evoluto /g,"evolùto " );txt = txt.replace(/e-learning/g,"i leerning" );txt = txt.replace(/rendering/g,"rèndering" );txt = txt.replace(/joomla/g,"giùmla" );txt = txt.replace(/security/g,"sekiùriti" );txt = txt.replace(/edition/g,"edìscion" );txt = txt.replace(/cdburnerxp/g,"cd barner xp" );txt = txt.replace(/burning/g,"barning" );txt = txt.replace(/gratuita/g,"gratùita" );txt = txt.replace(/gratuite/g,"gratùite" );txt = txt.replace(/gratuiti/g,"gratùiti" );txt = txt.replace(/gratuito/g,"gratùito" );txt = txt.replace(/ccleaner/g,"c clìaner" );txt = txt.replace(/mouse/g,"maus" );txt = txt.replace(/leggere/g,"lèggere" );txt = txt.replace(/speech/g,"spìicj");txt = txt.replace(/plugin/g,"plag in");txt = txt.replace(/play/g,"pley");txt = txt.replace(/iphone/g,"ai phon");txt = txt.replace(/ipad/g,"ai pad");txt = txt.replace(/naviga /g,"nàviga ");txt = txt.replace(/\beditoria\b/g,"editorìa");txt = txt.replace(/avanguardia/g,"avanguàrdia");txt = txt.replace(/\bfile\b/g,"fail");txt = txt.replace(/icloud/g,"ai claud");txt = txt.replace(/cloud/g,"claud" );txt = txt.replace(/sidebar/g,"sàidbar" );txt = txt.replace(/splashtop/g,"splèsctop" );txt = txt.replace(/installabile/g,"installàbile" );
	return txt;
}

function toggleDisable(){
	if ($j.cookie("toggle-disable")=="enabled"){
		$j.cookie("toggle-disable","disabled");
		$j("#toggle-disable").attr("class","disabled");
		$j("#toggle-disable").text("OFF");
	}
	else{
		$j.cookie("toggle-disable","enabled");
		$j("#toggle-disable").attr("class","enabled");
		$j("#toggle-disable").text("ON");
	}
}
	
function post(){
	if ($j('#status').text()=="Pronto"){
	$j('#status').ajaxStart(
		function(){
			$j(this).text("Caricamento...");
			$j("#toggle-disable").attr("class","loading");
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("clearMedia");
		}
	);
	
	$j('#status').ajaxComplete(
		function(){
			$j(this).text("Pronto");
			$j("#toggle-disable").attr("class","enabled");
		}
	);
	
	$j.ajax({
		type: 'POST',
		url: 'htl-tts/tts.php',
		data: {text: $('#text').val(), language: ''+lang},
		dataType : 'text',
		success: function(filename) {
			pausecomp(1000);
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("setMedia", {mp3: "htl-tts/output/"+filename});
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("load");
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("play");
		}
	});
	}
}

function post_selected(st1){
	if (st1!='' && $j('#status').text()=="Pronto"){
	st = ''+st1;
	st = correct_words(st);
	$j('#status').ajaxStart(
		function(){
			$j(this).text("Caricamento...");
			$j(this).attr("class","loading");
			$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("clearMedia");
		}
	);
	
	$j('#status').ajaxComplete(
		function(){
			$j(this).text("Pronto");
			$j(this).attr("class","ready");
		}
	);
	
	$j.ajax({
		type: 'POST',
		url: 'htl-tts/tts.php',
		data: {text: ''+st+'', language: ''+lang},
		dataType : 'text',
		cache: false,
		success: function(filename) {
				while (filename.search("DASS") != -1){
					filename=filename.substring(4,filename.length);
				}
				pausecomp(1000);
				$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("setMedia", {mp3: "htl-tts/output/"+filename});
				$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("load");
				$j("#jquery_jplayerHtmlGoogleTTS").jPlayer("play");
			}
	});
	
	}
}

function pausecomp(millis){
	var date = new Date();
	var curDate = null;
	
	do{curDate=new Date();}
	while(curDate-date<millis);
}





