var $ = jQuery.noConflict();

$(document).ready(function() {

/* NEWSLETTER */ 
		
	// Registra indirizzo email
	$("button[name='registra']").click(function () {
	  $("#resp_newsletter").html('<p class="messaggio" id="resp_newsletter"><img src="./img/ajax-load.gif" alt="" style="border:none;" /></p>');	
	  var add = $("input[name='indirizzo']").attr("value");
	  $("input[name='indirizzo']").attr("value","");

	  $.ajax({
		type: "POST",
		url: "./newsletter/registra.php",
		data: "add="+add,
		success: function(html){
		  $("#resp_newsletter").replaceWith(html);
		}
		,error:function(err){
			$("#resp_newsletter").replaceWith('<p class="messaggio" id="resp_newsletter"><strong>Errore nella connessione. Riprovare.</strong></p>');
			$("input[name='indirizzo']").attr("value",add);
		}
	  });
    });
	
	// Cancella indirizzo email
	$("button[name='cancella']").click(function () {
	  $("#resp_newsletter").html('<p class="messaggio" id="resp_newsletter"><img src="./img/ajax-load.gif" alt="" style="border:none;" /></p>');	
	  var add = $("input[name='indirizzo']").attr("value");
	  $("input[name='indirizzo']").attr("value","");

	  $.ajax({
		type: "POST",
		url: "./newsletter/cancella.php",
		data: "add="+add,
		success: function(html){
		  $("#resp_newsletter").replaceWith(html);
		}
		,error:function(err){
			$("#resp_newsletter").replaceWith('<p class="messaggio" id="resp_newsletter"><strong>Errore nella connessione. Riprovare.</strong></p>');
			$("input[name='indirizzo']").attr("value",add);
		}
	  });
    });

/* SLIDER */
	
	$('.slide').cycle({
		fx: 'fade'
	});
	
/* FOOTER */

	$("#credits").click(function () {
		//$(".window").css("display","block");
		$(".window").slideUp("fast");
		$(".window").slideDown("fast");
		$("#footer").css("height","63px");
		$("#footer ul").css("padding-top","5px");
	});
	
	//Button
	$(".chiudi").click(function () {
		//$(".window").css("display","none");
		$(".window").slideUp("fast");
		$("#footer ul").css("padding-top","25px");
		$("#footer").css("height","45px");
	});
	
	$("#admin").click(function () {
		//var url = "http://192.168.0.2/lega_nord/admin/";
		var url = "http://www.leganordumbria.it/admin/";
		$(location).attr('href',url);
	});
	

/* FOTO */
	// Al click su una foto restituisce la successiva con il nuovo commento
	$("img[name='foto_av']").live('click', function() {
		$(".commento").replaceWith('<div class="commento">---</div>');
		var w = $(".visualizzatore img").width();
		var h = $(".visualizzatore img").height();
		var nw = (w / 2) - 16;
		var nh = (h / 2) - 16;
		
		var id = $(this).attr('id'); 
		var dir = $(this).attr('name');
		var id_album = $("div[id^='idalbum']").attr('id');
		
		if(nw > 100)//Se ancora c'è il load non inserisce un ulteriore padding. 100 è un numero fittizio.
			$(".visualizzatore img").replaceWith('<img src="./img/ajax-load.gif" style="padding:'+nh+'px '+nw+'px '+nh+'px '+nw+'px;" />');
			
		$.ajax({
			type: "GET",
			url: "./get_photo.php",
			data: "foto="+id+"&dir="+dir+"&album="+id_album,
			success: function(html){
				$('.visualizzatore').replaceWith(html);
			}
			,error:function(err){}
		});
	});
	
	// Al click su indietro/avanti restituisce la precedente / successiva con il nuovo commento
	$("li[id^='foto']").click(function() {
		$(".commento").replaceWith('<div class="commento">---</div>');
		var w = $(".visualizzatore img").width();
		var h = $(".visualizzatore img").height();
		var nw = (w / 2) - 16;
		var nh = (h / 2) - 16;
		
		var id = $(".visualizzatore img").attr('id');
		var dir = $(this).attr('id');
		var id_album = $("div[id^='idalbum']").attr('id');
		
		if(nw > 100)//Se ancora c'è il load non inserisce un ulteriore padding. 100 è un numero fittizio.
			$(".visualizzatore img").replaceWith('<img src="./img/ajax-load.gif" style="padding:'+nh+'px '+nw+'px '+nh+'px '+nw+'px;" />');

		$.ajax({
			type: "GET",
			url: "./get_photo.php",
			data: "foto="+id+"&dir="+dir+"&album="+id_album,
			success: function(html){
				$('.visualizzatore').replaceWith(html);
			}
			,error:function(err){}
		});
	});

});
