LoginPanel = false;
OffsetMovingPanel = 200;
function Login(){
	document.getElementById('login').style.width = document.getElementById('menu').offsetWidth+'px';
	if(!LoginPanel){
		$('#login').animate({
		top: "+="+OffsetMovingPanel+"px",
		duration: 1000,
		easing: 'easeOutExpo'
		});
		LoginPanel = true;
		document.getElementById('login_bar-button').innerHTML = 'Chiudi';
		document.getElementById('login_bar-button').style.backgroundPosition = 'right top';
	} else {
		$('#login').animate({
		top: "-="+OffsetMovingPanel+"px",
		duration: 1000,
		easing: 'easeOutExpo'
		});
		LoginPanel = false;
		document.getElementById('login_bar-button').innerHTML = 'Accedi';
		document.getElementById('login_bar-button').style.backgroundPosition = 'right bottom';
	}
}

function Invia(){
	var email = document.getElementById("email").value;
	var nome = document.getElementById("nome").value;
	var tel = document.getElementById("tel").value;
	var msg = document.getElementById("msg").value;
	document.getElementById("email").style.background = "none";
	document.getElementById("nome").style.background = "none";
	document.getElementById("tel").style.background = "none";
	document.getElementById("msg").style.background = "none";
	var email_exp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	var nome_exp = /^[a-z A-Z]{3,100}(\'\ì\ò\à\ù\è\é)*$/;
	var tel_exp = /^[0-9- \/]{6,30}$/;
	var validation = true;
	if (nome.search(nome_exp) == -1) {
		validation = false;
    	alert("Inserire un Nome valido!");
		document.getElementById("nome").style.background = "url('/images/invalidForm.gif') repeat";
		document.getElementById("nome").focus();
		return false;
	}
	if (email.search(email_exp) == -1) {
		validation = false;
    	alert("Inserire un\'indirizzo E-mail valido!");
		document.getElementById("email").style.background = "url('/images/invalidForm.gif') repeat";
		document.getElementById("email").focus();
		return false;
	}
	if (tel.search(tel_exp) == -1) {
		validation = false;
    	alert("Inserire un recapito telefonico valido!");
		document.getElementById("tel").style.background = "url('/images/invalidForm.gif') repeat";
		document.getElementById("tel").focus();
		return false;
	}
	if (msg == "Messaggio:" || msg == "") {
		validation = false;
		alert("Scrivi un messaggio!");
		document.getElementById("msg").style.background = "url('/images/invalidForm.gif') repeat";
		return false;
	}
	if (validation == true) {
		return true;
		document.getElementById("form").submit();
	} else {
		return false;
	}
}
selected_menu = 0;
clearTimeOut = 0;
open_menu = 0;
function keep_open(){
	clearTimeOut = 0;
	try{clearTimeout(temp_timeout)}catch(e){}
}
function close_menu(id){
	clearTimeOut = 1;
	temp_timeout = setTimeout("close_menu_final("+id+");", 40);    
}
function close_menu_final(id){
	if(clearTimeOut == 1){
		if($("#main-menu-item-"+id).attr("id") != selected_menu){
			$("#main-menu-item-"+id).removeClass("hover");
		}
		$("#sub-menu-"+id).slideUp({duration: 250});
	}
}

$(document).ready(function(){
	//alert(location.pathname);
	$('#login-content').mouseleave(function(){Login();});
	$("a[href='"+location.pathname+"']").addClass("hover");
	selected_menu = $("a[href='"+location.pathname+"']").attr("id");
	try{document.getElementById(selected_menu).href = 'javascript:void(0);';}catch(e){}
	open_menu = 0;
	$("a[id^='main-menu-item-']").mouseover(function(){
		$(this).addClass("hover");
		sub_menu_id = $(this).attr("id").replace("main-menu-item-", "");
		if(open_menu !== sub_menu_id){
			close_menu(open_menu);
		}
		open_menu = sub_menu_id;
		$("#sub-menu-"+open_menu).slideDown({duration: 250});
		//alert("Open "+$.open_menu);
	});
	$("[id^='sub-menu-']").mouseover(function(){
		keep_open();
	});
	$("[id^='sub-menu-']").mouseout(function(){
		sub_menu_id = $(this).attr("id").replace("sub-menu-", "");
		close_menu(sub_menu_id);
	});
	$("a[id^='main-menu-item-']").mouseout(function(){
		sub_menu_id = $(this).attr("id").replace("main-menu-item-", "");
		close_menu(sub_menu_id);
	});
});

$(document).ready(function(){
    $(".ListContainer").hide();//Chiudo tutte le Liste
	//Stati dei Titoli
    $("h4.titolo").toggle(function(){
        $(this).addClass("active");
        }, function () {
        $(this).removeClass("active");
    });
	//Animazione
    $("h4.titolo").click(function(){
        $(this).next(".ListContainer").slideToggle("slow");
    });
    
});
//Switch tra Articoli e Novità
function NewsSwitch(type){
	switch(type){
		case "UltimiArticoli":
			document.getElementById('news_title').innerHTML = 'Ultimi Articoli';
			document.getElementById('latest_news').style.display = 'block';
			document.getElementById('latest_posts').style.display = 'none';
			document.getElementById('latest_posts_content').style.display = 'block';
			document.getElementById('latest_news_content').style.display = 'none';
		break;
		case "UltimeNovita":
			document.getElementById('news_title').innerHTML = 'Ultime Novità';
			document.getElementById('latest_news').style.display = 'none';
			document.getElementById('latest_posts').style.display = 'block';
			document.getElementById('latest_posts_content').style.display = 'none';
			document.getElementById('latest_news_content').style.display = 'block';
		break;
	}
}
Shadowbox.init({overlayOpacity:0.8});
function Ascolta(url,titolo){
	Shadowbox.open({
		player:     "iframe",
		content:    url,
		title:      titolo,
		height:     200,
		width:      300
	});
}

function AreaRiservataLogin() {
	var user_login = document.getElementById("user_login").value;
	var user_password = document.getElementById("user_password").value;
	if(user_login == "" || user_password == ""){
		alert("Inserisci la tua E-mail e la Password per Accedere!");
		return;
	}	
	var user_remember = document.getElementById("user_remember").checked;
	var login_login = document.getElementById("login-login").innerHTML;
	jQuery.ajax({
		type: "post",url: "/wp-admin/admin-ajax.php",data: {action: 'AreaRiservataLogin', user_login: user_login, user_password: user_password, user_remember: user_remember, _ajax_nonce: '<?php echo $nonce; ?>'},
		beforeSend: function() {//Prima dell'invio della richiesta
			//alert("invio dati:\n"+user_login+"\n"+user_password+"\n"+user_remember);
			document.getElementById("login-login").innerHTML = '<div style="margin:100px auto;width:500px;text-align:center;">Accesso in corso...<br /><img src="/wp-content/themes/cg_tema/images/loading.gif" /></div>';
			}, 
		success: function(html){ //a completamento della richiesta AJAX
			//alert(html);
			var logged = html.substr(0,1);//prendo solo il primo carattere dell'output perché non so perché ma viene aggiunto uno ZERO in fondo
			//alert(logged);
			if(logged == 1){//OK
				//alert("OK");
				window.location = location.pathname+location.search;
			} else if(logged == 2){//non ha accesso all'area riservata
				window.location = '/messaggi/accesso-vietato';
			} else {
				alert("Indirizzo E-mail o Password Errati!");
				document.getElementById("login-login").innerHTML = login_login;
				document.getElementById("user_login").value = user_login;
				document.getElementById("user_password").value = user_password;
				if(user_remember){document.getElementById("user_remember").checked = true;}else{document.getElementById("user_remember").checked = false;}
			}
		}
	});
}
