/*
 *  In questa js vengono richiamate tutte le funzioni che hanno bisogno di essere lancite
 *  quando la pagina è disponibile.
 */

$(document).ready(function (){

//--------------------------------------------------------------------------------------------------
// GESTIONE LOG IN SEZIONE RISERVATA
$("#btLogOn").click(function(){
	var id=$("#txtId").val();
	var pw=$("#txtPW").val();
	
	if (id==''){
		alert("Campo Id vuoto. Id field is empty.");
		return;
	}

	if (pw==''){
		alert("Campo Password vuoto. Password field is empty.");
		return;
	}
	
	$.post("./pages/include/login.php", {id:id, pw:pw, rand:Math.random()}, function(data){
			if(data=="ko"){
				alert("Accesso non eseguito. Access not granted.")
			}
			if(data=="ok"){
				window.location.href = "./pages/include/admin.php";
			}
		}); 		
	});	
	
//--------------------------------------------------------------------------------------------------
// GESTIONE LOG OFF
$("#btLogOff").click(function(){
	$.post("/pages/include/logoff.php", {rand:Math.random()}, function(data){
			if(data=="ko"){
				alert("Logoff non eseguito.")
			}
			if(data=="ok"){
				window.location.href = "http://www.boscaropasqualino.it";
			}
		}); 		
	});	

				
//--------------------------------------------------------------------------------------------------
// CAMBIO PAGINA VERSO IL GESTORE NEWSLETTER PHPLIST				
$("#btNewsletter").click(function(){
	window.location.href = "http://www.boscaropasqualino.it/pages/lists/admin";
});

//--------------------------------------------------------------------------------------------------
// GESTIONE VISUALIZZAZIONE BLOCCHI AREA RISERVATA
$("#selOp").change(function(){
	var op=$("#selOp").val();
	//alert(op);
	switch (op){
		case "0":
			$("#blNews").hide();
			$("#blFrmNews").hide();
			$("#blNewsletter").hide();
			$("#blFrmNewsletter").hide();						
			break;
		case "1":
			$("#blNews").show();
			$("#blFrmNews").hide();			
			$("#blNewsletter").hide();
			$("#blFrmNewsletter").hide();						
			break;
		case "2":
			$("#blNews").hide();
			$("#blFrmNews").show();			
			$("#blNewsletter").hide();
			$("#blFrmNewsletter").hide();			
			break;
		case "3":
			$("#blNews").hide();
			$("#blFrmNews").hide();			
			$("#blNewsletter").show();
			$("#blFrmNewsletter").hide();			
			break;
		case "4":
			$("#blNews").hide();
			$("#blFrmNews").hide();			
			$("#blNewsletter").hide();
			$("#blFrmNewsletter").show();			
			break;
	}
	});

//--------------------------------------------------------------------------------------------------
// GESTIONE VISUALIZZAZIONE INFORMAZIONI PER MODIFICA NEWS
$(".btInfos").live("click", function(){
	var def=$(this).attr('id');
	var id=def.substr(3,def.legth);
	$("#tab1"+id).toggle();
	$("#tab2"+id).toggle();
});

//--------------------------------------------------------------------------------------------------
// GESTIONE PULSANTI MODIFICA INFORMAZIONI NEWS
$(".btNewsMod").live("click", function(){
	var def=$(this).attr('id');
	var id=def.substr(1,def.legth);
	$("#S"+id).show();
	$("#A"+id).show();
	$("#M"+id).hide();
	$("#E"+id).hide();
	$("#txtData"+id).removeAttr("readonly");
	$("#titleIt"+id).removeAttr("readonly");
	$("#shortIt"+id).removeAttr("readonly");
	$("#longIt"+id).removeAttr("readonly");
	$("#tagsIt"+id).removeAttr("readonly");
	$("#titleEn"+id).removeAttr("readonly");
	$("#shortEn"+id).removeAttr("readonly");
	$("#longEn"+id).removeAttr("readonly");
	$("#tagsEn"+id).removeAttr("readonly");
});

//--------------------------------------------------------------------------------------------------
// GESTIONE MODIFICA INFORMAZIONI NEWS
$(".btNewsSave").live("click", function(){
	var def=$(this).attr('id');
	var id=def.substr(1,def.legth);
	var info=$("#txtData"+id).val()+";";
	info=info+$("#titleIt"+id).val()+";";
	info=info+$("#shortIt"+id).val()+";";
	info=info+$("#longIt"+id).val()+";";
	info=info+$("#tagsIt"+id).val()+";";
	info=info+$("#titleEn"+id).val()+";";
	info=info+$("#shortEn"+id).val()+";";
	info=info+$("#longEn"+id).val()+";";
	info=info+$("#tagsEn"+id).val()+";";
	
	var ans=confirm("Confermi la modifica?");
	if (ans){
			$.post("newsOps.php", {azione:"modificaNews", id:id, dati:info, rand:Math.random()}, function(data){
				if(data=="ko"){
					alert("Attenzione!", "Errore del database. Contattare l'assistenza.");
				}else{
					window.location.href = "admin.php";
				}
			}); 		
	}else{
		return;
	}
});

//--------------------------------------------------------------------------------------------------
// GESTIONE ANNULLAMENTO MODIFICA INFORMAZIONI NEWS
$(".btNewsCancel").live("click", function(){
	var def=$(this).attr('id');
	var id=def.substr(1,def.legth);
	var ans=confirm("Annullamento delle modifiche?");
	if (ans){
		window.location.href = "admin.php";
		}
});

//--------------------------------------------------------------------------------------------------
// GESTIONE ELIMINAZIONE NEWS
$(".btNewsDel").live("click", function(){
	var def=$(this).attr('id');
	var id=def.substr(1,def.legth);
	var ans=confirm("Confermi eliminazione news?");
	if (ans){
			$.post("newsOps.php", {azione:"eliminaNews", id:id, rand:Math.random()}, function(data){
				if(data=="ko"){
					alert("Attenzione!", "Errore del database. Contattare l'assistenza.");
				}else{
					window.location.href = "admin.php";
				}
			});
	}
});

//--------------------------------------------------------------------------------------------------
// GESTIONE AGGIUNTA NUOVA NEWS
$("#btSaveNewNews").click(function(){
	var info=$("#txtNewData").val()+";";
	info=info+$("#txtNewTitIt").val()+";";
	info=info+$("#txtNewShortIt").val()+";";
	info=info+$("#txtNewLongIt").val()+";";
	info=info+$("#txtTagsIt").val()+";";
	info=info+$("#txtNewTitEn").val()+";";
	info=info+$("#txtNewShortEn").val()+";";
	info=info+$("#txtNewLongEn").val()+";";
	info=info+$("#txtTagsEn").val()+";";
	var ans=confirm("Confermi inserimento news?");
	if (ans){
			$.post("newsOps.php", {azione:"salva", dati:info, rand:Math.random()}, function(data){
				if(data=="ko"){
					alert("Attenzione!", "Errore del database. Contattare l'assistenza.");
				}else{
					//window.location.href = "admin.php";
				}
			});
	}
});

//--------------------------------------------------------------------------------------------------
// FUNIONE DI GESTIONE SCROLLER DELLE NEWS
	$("#news_roller").ScrollNews({
			inizialize  : 'true',
			size		: '90px',
			timeout		:  2000,
			speed		:  'fast',
			direction	: 'up',
			onmouseover : 'stop'
	});

//--------------------------------------------------------------------------------------------------
	// FUNZIONI DEL SISTEMA DI GESTIONE DEI MENU'
	// Funzioni di rollup rolldown voci del menù di navigazione
	$('#nav li').hover(
		function () {
			//mostra sottomenu
			$('ul', this).stop(true, true).delay(50).slideDown(100);
		}, 
		function () {
			//nascondi sottomenu
			$('ul', this).stop(true, true).slideUp(200);		
		}
	);

//--------------------------------------------------------------------------------------------------
	// FUNZIONE DI GESTIONE INFO PER FORM CONTATTO
	$("#mailSubmit").click(function(){
	//Recuperiamo tutte le variabili
		var valid = '';
		var isr = '<br />';
		var name = $("#nome").val();
		var mail = $("#email").val();
		var subject = $("#oggetto").val();
		var messaggio = $("#messaggio").val();
	//Eseguiamo una serie di controlli
		if (name.length<1) {
			valid += '<b>Nome non valido | <i>Name not valid</i></b>'+isr;
		}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<b>Indirizzo non valido | <i>Address not valid</i></b>'+isr;
		}
		if (subject.length<1) {
			valid += '<b>Oggetto non valido | <i>Topic not valid</i></b>'+isr;
		}
		
	//Se i controlli non vengono superati, appare il messaggio di errore.
		if (valid!='') {
			$("#risposta").fadeIn("slow");
			$("#risposta").html(valid);
			$("#risposta").css("color","yellow");
			$("#risposta").css("background-color","#679db7");
		}
		//Se i controlli vengono superati, compare un messaggio di invio in corso
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&messaggio=' + encodeURIComponent(messaggio);
			$("#risposta").css("display", "block");
			$("#risposta").css("background-color","#FFFFA0");
			$("#risposta").html("<p>Invio del messaggio in corso..</p>");
			$("#risposta").fadeIn("slow");
			setTimeout("mailSend('"+datastr+"')",2000);
		}
		return false;
		});
	
//---------------------------------------------------------------------------------------------	
	// FUNZIONE DI GESTIONE ISCRIZIONE/CANCELLAZIONE NEWSLETTER
	$("#nlSubmit").click(function(){
		//Recuperiamo tutte le variabili
		var valid = '';
		var mail = $("#nlMail").val();
	//Eseguiamo una serie di controlli
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<b>Inserire una mail valida.</b><br><i>Enter a valid address.</i>';
		}
	//Se i controlli non vengono superati, appare il messaggio di errore.
		if (valid!='') {
			$("#nl_monitor").fadeIn("slow");
			$("#nl_monitor").html(valid);
			$("#nl_monitor").css("display", "block");			
			$("#nl_monitor").css("color","yellow");
		}
		//Se i controlli vengono superati, compare un messaggio di invio in corso
		else {
			var nldatastr ='nlMail=' + mail;
			//alert(nldatastr);
			$("#nl_monitor").css("display", "block");
			$("#nl_monitor").css("color","#33ff00");			
			$("#nl_monitor").html("<b>Invio in corso</b><br /><i>Sending...</i>");
			$("#nl_monitor").fadeIn("slow");
			setTimeout("nlSend('"+nldatastr+"')",2000);
		}
		return false;
	});	
	
	
	
//---------------------------------------------------------------------------------------------	
// GESTIONE VISUALIZZAZIONE NEWS NELLA PAGINA
// select #flowplanes and make it scrollable. use circular and navigator plugins
$("#flowpanes").scrollable({ circular: true, mousewheel: false }).navigator({

	// select #flowtabs to be used as navigator
	navi: "#flowtabs",

	// select A tags inside the navigator to work as items (not direct children)
	naviItem: 'a',

	// assign "current" class name for the active A tag inside navigator
	activeClass: 'current',

	// make browser's back button work
	history: true
});
	
}); // CHIUSURA EVENTO DOCUMENT.READY
	

// FUNZIONI JAVASCRIPT NON DIPENDENTI DALL'EVENTO DOCUMENT.READY

//Continuazione del modulo invio form contatto.
//Creazione della funzione di invio. Si baserà sul nostro file php "mail.php".
function mailSend(datastr){
	$.ajax({	
		type: "POST",
		url: "pages/include/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#risposta").fadeIn("slow");
		$("#risposta").html(html);
		$("#risposta").css("background-color","#e1ffc0");
		setTimeout('$("#risposta").fadeOut("slow")',2000);
		}
	});
}
	
//Continuazione del modulo iscrizione/revoca newsletter.
//Creazione della funzione di invio. Si baserà sul nostro file php "op_newsletter.php".
function nlSend(nldatastr){
	$.ajax({	
		type: "POST",
		url: "pages/include/op_newsletter.php",
		data: nldatastr,
		cache: false,
		success: function(html){
		$("#nl_monitor").fadeIn("slow");
		$("#nl_monitor").css("display", "block");	
		$("#nl_monitor").css("color","#33ff00");			
		$("#nl_monitor").html("<b>Iscrizione completa, grazie.</b><br /><i>Subscribtion complete, thanks.</i>");
		setTimeout('$("#nl_monitor").fadeOut("slow")',2000);
	}
	});
}
