
function populateTowns(id_prov, id_town)
{
	if(document.getElementById(id_prov).selectedIndex > 0)
	{
		var province = document.getElementById(id_prov).value;
		$.get("/ajax/towns", { code: province }, function(data) {
				$('#'+id_town).html(data);
			}
		);
		$('#'+id_town).slideDown();
	}
	else
	{
		$('#'+id_town).hide();
	}
}

function sendInviti()
{
	var data = $('#invita_form').serialize();
	
	$.post("/ajax/invitation", data, function(res) {
			if(res == "200")
				$('#invita_form').html("Le email sono state inviate");
			else
				$('#invita_form').html("Si è verificato un errore, ti preghiamo di riprovare fra qualche minuto");
		}
	);
}






function validateInvita()
{
	var email_sender = $('#sender_email').val().replace(/^\s+|\s+$/g, '');
	var name_sender = $('#sender_name').val().replace(/^\s+|\s+$/g, '');
	var email1 = $('#email1').val().replace(/^\s+|\s+$/g, '');
	
	$('#v_name').hide();
	$('#v_email1').hide();
	$('#v_email').hide();
	
	var send = true;
	
	if(name_sender.length < 2)
	{
		document.getElementById('sender_name').className = 'input_error';
		$('#v_name').show();
		send = false;
	}
	if(! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#sender_email').val()))
	{
		document.getElementById('sender_email').className = 'input_error';
		$('#v_email').show();
		send = false;
	}
	if(! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email1').val()))
	{
		document.getElementById('email1').className = 'input_error';
		$('#v_email1').show();
		send = false;
	}
	if(! document.getElementById('privacy').checked)
	{
		alert('Devi accettare le specifiche sulla PRIVACY');
		send = false;
	}
	
	if(send)
		sendInviti();
	else
		location.href='#form';
}

function newsletterError(msg)
{
	$("#newsletter_error").hide();
	$('#newsletter_error').html(msg);
	$("#newsletter_error").slideDown("slow");
}

function sendMessaggio()
{
	var data = $('#message_form').serialize();
	
	$.post("/ajax/send-message", data, function(res) {
			if(res == "200")
				$('#message_form').html("Messaggio inviato");
			else
				$('#message_form').html("Si è verificato un errore, ti preghiamo di riprovare fra qualche minuto");
		}
	);
}

function validateMessaggio()
{
	$('#v_name').hide();
	$('#v_surname').hide();
	$('#v_email').hide();
	$('#v_province').hide();
	$('#v_town').hide();
	var send = true;
	
	var flds = new Array('name', 'surname', 'province_form', 'town_form');
	for(i=0; i<flds.length; i++)
	{
		if(document.getElementById(flds[i]).value.replace(/^\s+|\s+$/g, '').length < 2)
		{
			$('#v_'+flds[i]).show();
			document.getElementById(flds[i]).className = 'input_error';
			send = false;
		}
	}
	if(! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val()))
	{
		document.getElementById('email').className = 'input_error';
		$('#v_email').show();
		send = false;
	}
	
	if(send)
		sendMessaggio();
	else
		alert('Controlla i dati inseriti');
}

function subscribe()
{
	if(! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val()))
	{
		newsletterError("Inserire un indirizzo e-mail valido");
		return;
	}
	if(document.getElementById('province').selectedIndex == 0)
	{
		newsletterError("Selezionare una provincia");
		return;
	}
	if(document.getElementById('towns').selectedIndex == 0)
	{
		newsletterError("Selezionare un comune");
		return;
	}

	opts = { province: document.getElementById('province').value,
				town: document.getElementById('towns').value,
				email: $('#email').val()
	};
	
	$.post("/ajax/subscribe-newsletter", opts, function(data) {;
			if(data == "500")
			{
				newsletterError("Si &egrave; verificato un problema, ripetere la procedura");
			}
			else
			{
				$('#newsletter_form').html("<span class=\"message\">Grazie per esserti registrato, riceverai una email di conferma a breve</span>");
			}
		}
	);
}
					
function menu_hover() {
	
	$("#menu li").mouseover( function () {
		$(this).addClass("menu_li_over");
	
		}
	);
	$("#menu li").mouseout( function () {
		$(this).removeClass("menu_li_over");
	
		}
	);

}

function text_to_image (text_container, text_class, capitalize) {


	if ($(text_container).children("span").is("span")) {
		
	   text_to_convert = $(text_container).find("span").html();

		
		
	} else {
		
	     var text_to_convert = 	$(text_container).html();
	
    	 
	}
	backup_text = document.createElement ("span");
	 backup_text.setAttribute ("class", "original_text");
	 $(text_container).empty().append(backup_text);
	 $(backup_text).html(text_to_convert).hide();
	
	if (capitalize) text_to_convert = text_to_convert.toUpperCase();
	img_path = "/text/" + text_class + "/" + text_to_convert;
	img_elem = document.createElement ("img");
	img_elem.src = img_path;
	$(text_container).append(img_elem);
	
}

function apply_text_images (text_container_selector, text_class, capitalize ) {
	all_titles= $(text_container_selector);
	for (i=0; i<all_titles.length; i++){
		text_to_image (all_titles[i],text_class, capitalize);
	}
	
	
}

function letta_init()
{
	menu_hover();
	apply_text_images("div.post.detail.section div.post div.title h1","main_title", true);
	apply_text_images("#yui-main h1.section_title","main_title", true);
	apply_text_images("#extra h1.section_title","extra_title", true);
	apply_text_images(".agenda h1.section_title","extra_title", true);
	apply_text_images(".submenu h1.section_title","menu_title", true);
	apply_text_images("h5.comments","menu_title", true);	
}

$("body").ready(letta_init);