var tim = null;
function CaricaDataOra() {
	
	if ($("name_giorno") == null){
		clearInterval(tim);
		return;
	}

	var dayName=new Array ("DOM", "LUN", "MAR", "MER", "GIO", "VEN", "SAB");
	var monName=new Array ("GEN", "FEB", "MAR", "APR", "MAG", "GIU", "LUG", "AGO", "SET", "OTT", "NOV", "DIC"); 
	now= new Date;

	$("name_giorno").innerHTML = dayName[now.getDay()];
	$("num_giorno").innerHTML = now.getDate();
	$("mese_giorno").innerHTML = monName[now.getMonth()];
	var ora = now.getHours();
	var minuti = now.getMinutes();
	var secondi = now.getSeconds();

	if (secondi < 15)
		secondi = secondi * (-51) -3;
	else if (secondi < 21)	
		secondi = secondi * (-51);
	else if (secondi < 50)	
		secondi = secondi * (-51) -3;
	else
		secondi = secondi * (-51) -5;
	$("lancetta_secondi").style.background = "url(../images/time_picker_files/clock_minutes.gif) no-repeat scroll "+secondi+"px 0px";

//	var minuti = 59;
	if (minuti < 21)
		minuti = minuti * (-51);
	else if (minuti < 50)	
		minuti = minuti * (-51) -3;
	else
		minuti = minuti * (-51) -5;
	$("lancetta_minuti").style.background = "url(../images/time_picker_files/clock_minutes.gif) no-repeat scroll "+minuti+"px 0px";

	if (ora == 0 || ora == 12)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll 0px 0px";
	else if (ora == 1 || ora == 13)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -30px 0px";
	else if (ora == 2 || ora == 14)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -60px 0px";
	else if (ora == 3 || ora == 15)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -90px 0px";
	else if (ora == 4 || ora == 16)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -120px 0px";
	else if (ora == 5 || ora == 17)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -150px 0px";
    else if (ora == 6 || ora == 18)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -181px 0px";
  	else if (ora == 7 || ora == 19)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -211px 0px";
    else if (ora == 8 || ora == 20)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -241px 0px";
	else if (ora == 9 || ora == 21)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -271px 0px";
	else if (ora == 10 || ora == 22)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -301px 0px";
	else if (ora == 11 || ora == 23)
		$("lancetta_ora").style.background = "url(../images/time_picker_files/clock_hours.gif) no-repeat scroll -332px 0px";
}
/***********************************FUNZIONI ROLLOVER***********************************************/
function NuovaImmagine(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function CambiaImmagine() {
	if (document.images && (FlagImage == true)) {
		for (var i=0; i<CambiaImmagine.arguments.length; i+=2) {
			document[CambiaImmagine.arguments[i]].src = CambiaImmagine.arguments[i+1];
		}
	}
}

var FlagImage = false;
function CaricaImmagini() {
	if (document.images) {
		home_over = NuovaImmagine("images/header/Home_over.png");
		Istituto_over = NuovaImmagine("images/header/Istituto_over.png");
		Ultime_over = NuovaImmagine("images/header/Ultime_over.png");
		Contatti_over = NuovaImmagine("images/header/Contatti_over.png");
		
		FlagImage = true;
		Effect.Appear("HeaderFotoIstituto",{duration:3});
		Element.hide('progress_activity');
	}
	effettiHome();	
}
/***********************************FUNZIONI ROLLOVER***********************************************/

function effettiHome(){
	
	if (tim != null)
		clearInterval(tim);
	tim = setInterval ("CaricaDataOra()", 1000);
	CaricaDataOra();
	initDOMnews();
}

/***********************************FUNZIONI MENU***********************************************/
function GestisciSottoMenu(SottoMenu) {
	MostraSottoMenu(SottoMenu,'SottoMenu_Informazioni');
	MostraSottoMenu(SottoMenu,'SottoMenu_Organizzazione');
	MostraSottoMenu(SottoMenu,'SottoMenu_Piano');
	MostraSottoMenu(SottoMenu,'SottoMenu_Area');
	MostraSottoMenu(SottoMenu,'SottoMenu_Amministratore');
}

function MostraSottoMenu(SottoMenu,SottoMenuView) {
	if (SottoMenuView == SottoMenu){
		if ($(SottoMenuView)){
			if ($(SottoMenu).style.display == "none"){
				Effect.BlindDown(SottoMenu,{duration:1});
			}else{
				Effect.BlindUp(SottoMenu,{duration:1});
			}
		}
	}else{
		if ($(SottoMenuView)){
			if ($(SottoMenuView).style.display != "none"){
				Effect.BlindUp(SottoMenuView,{duration:1});
			}
		}
	}
}
/***********************************FUNZIONI MENU***********************************************/

/***********************************FUNZIONI AJAX***********************************************/
function ProcessResponse(request) {
	var d;
	var resp = request.responseText;

	//Element.update('Page',resp);
	if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(resp))
		obj = eval('(' + resp + ')');
		
	obj.each( 
		function(elem) {	
		
			if (elem.action == 'fine') {
			   	//Aggiornamento sessione automatico
	        } else if (elem.action == 'msg_error') {	
		        MostraMessaggio(elem.html,"Attenzione");
		        return;
			} else if (elem.action == 'AVVISO') {
				elem.descr.each(	        		
					function(descr){	
		        		MostraMessaggio(descr.html,descr.section);
					}
				)					
		        return;
	        }else if (elem.action == 'SEZIONE') {
	        	elem.descr.each(	
					function(descr){
						d = $(descr.section);
						if (d){
							Element.update(d,descr.html);
						}
					}
				)				 			
			}else if (elem.action == 'loadhtml') {        		        	
	        	elem.descr.each(	        		
					function(descr){
						d = $(descr.section);
						if (descr.section == 'txtcodiceweb' || descr.section == 'codicehtml' || descr.section == 'codicetext'){
							var codice = descr.html;
							codice = Base64.decode(codice);
							d.value = codice;			
						}else{
							Element.update(d,descr.html);
						}
					}
				)
				controlliInvioSMS(false);
			}
	    }	    
	);
}
function openSection(page) {
	var params;
	var url = '../index.php';
	if (page == 'HOME')
		params = 'azione=' + page +'&tipo=HOME';
	else
		params = 'azione=' + page +'&tipo=PAGINA';
	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}
function openEvent(event) {
	var url = '../index.php';	
	var params = 'azione=ULTIME&tipo=PAGINA&ID='+event;	
	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}
function Login(azione) {
	var params;
	var url = '../login.php';
	if (azione == '1')
		params = 'azione=' + azione + '&username=' + $('username').value + '&password=' + $('password').value;
	else
		params = 'azione=-1';
	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}
function CambiaImpostazioni(azione) {
	var params;
	var url = '../impostazioni.php';
	if (azione == 'PASSWORD')
		params = 'azione=' + azione + '&nuova_password=' + $F('nuova_password') + '&conferma_password=' + $F('conferma_password');
	else if (azione == 'MAIL')
		params = 'azione=' + azione + '&nuova_mail=' + $F('nuova_mail');
	else if (azione == 'CHANGE_EVENTI')
		params = 'azione=' + azione;
	else
		params = 'azione=-1';
	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}

function CambiaEventi(form, azione){
	var params;
	var url = '../eventi.php';
	if (form == '')
		params = 'azione=' + azione;
	else
		params = Form.serialize(form) + '&azione=' + azione;

	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}

/***********************************FUNZIONI AVVISI***********************************************/
function openAvviso(page) {
	var url = '../index.php';
	var params = 'azione=' + page +'&tipo=AVVISO';
	var myAjax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onSuccess: ProcessResponse});
}
function ShowError(request) {
	alert("Errore AJAX" + request.responseText);
	alert (request);
}
function ApriMappa(){
	var confWin  = $('FinestraWindow');
	messaggio = "<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.it/maps?f=q&amp;hl=it&amp;geocode=&amp;q=Via+S.+Zeno,+2+%E2%80%93+56127+Pisa&amp;sll=41.442726,12.392578&amp;sspn=9.089915,17.62207&amp;ie=UTF8&amp;s=AARTsJq5GAmwCTvDIxYRvSYHVa3QL3QLWA&amp;view=map&amp;ll=43.729181,10.408344&amp;spn=0.00428,0.008605&amp;t=h&amp;z=14&amp;output=embed'></iframe><br />";
	messaggio = "<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.it/maps?f=q&amp;hl=it&amp;geocode=&amp;q=Via+S.+Zeno,+2+%E2%80%93+56127+Pisa&amp;sll=41.442726,12.392578&amp;sspn=9.089915,17.62207&amp;ie=UTF8&amp;s=AARTsJrPD9eRnzIDHKXfymG3sH0ThNrp_A&amp;ll=43.729181,10.408344&amp;spn=0.00428,0.008605&amp;t=h&amp;z=14&amp;output=embed'></iframe><br />";
	
	var html ="<div id='layer'></div><div id='FinestraMsgBox' style='width:435px;'>"+
            "<div class='area'><div class='intestazione'>"+
			"<img src='../images/header/chiudi.gif' onClick='ChiudiMsgBox()' align='right' vspace='3' style='cursor:pointer;'/>"+		
			"Mappa</div>"+
            "<div class='contenitore'><br>" + messaggio + "<div align='center'>"+
            "<br><img src='../images/header/Okbutton.gif' onClick='ChiudiMsgBox()' align='center' style='cursor:pointer;'/>"+
            "</div></div></div>";
	
	 Element.update(confWin, html);	
   
   	var layer= $('layer');
  	var finestra= $('FinestraMsgBox');
         	
    altez =document.body.clientHeight / 5;
    larg = document.body.clientWidth / 2;
    finestra.style.left = larg + "px";
    finestra.style.top = altez + "px";
	
	finestra.setStyle({ opacity: 1.0 }); 
	layer.setStyle({ opacity: 0.5 }); 
	layer.style.display='inline';
    finestra.style.display='inline';   
	
    new Draggable('FinestraMsgBox',{ scroll:window,
                                    	  handle: 'intestazione',
                                    	  starteffect:function(){},
                                    	  endeffect:function(){}
                                  		});         
	
	}

function MostraMessaggio(messaggio,titolo) {
	var confWin  = $('FinestraWindow');
	if (titolo=="")
		titolo = "Attenzione";
            
    var html = "<div id='layer'></div><div id='FinestraMsgBox' style='width:300px;'>"+
            "<div class='area'><div class='intestazione'>"+
			"<img src='../images/header/chiudi.gif' onClick='ChiudiMsgBox()' align='right' vspace='3' style='cursor:pointer;'/>"+		
			titolo+"</div>"+
            "<div class='contenitore'><br>" + messaggio + "<div align='center'>"+
            "<br><img src='../images/header/Okbutton.gif' onClick='ChiudiMsgBox()' align='center' style='cursor:pointer;'/>"+
            "</div></div></div>";

    Element.update(confWin, html);	

	var layer= $('layer');
  	var finestra= $('FinestraMsgBox');
         	
    altez =document.body.clientHeight / 4;
    larg = document.body.clientWidth / 2;
    finestra.style.left = larg + "px";
    finestra.style.top = altez + "px";
	
	finestra.setStyle({ opacity: 1.0 }); 
	layer.setStyle({ opacity: 0.5 }); 
	layer.style.display='inline';
    finestra.style.display='inline';   
	
    new Draggable('FinestraMsgBox',{ scroll:window,
                                    	  handle: 'intestazione',
                                    	  starteffect:function(){},
                                    	  endeffect:function(){}
                                  		});                        		
      	 
}
function ChiudiMsgBox() {
	
	var layer= $('layer');
  	var finestra= $('FinestraMsgBox');	
	layer.style.display='none';
	finestra.style.display='none';
}
/***********************************FUNZIONI AVVIS***********************************************/
