function preload() {
  if (!document.images) return;
  var wl = new Array();
  var arguments = preload.arguments;
  for (var i = 0; i < arguments.length; i++) {
    wl[i] = new Image();
    wl[i].src = arguments[i];
  }
}


function myalert(title, msg) {
	$("#dialog").attr("title", title);
	$("#dialog").html(msg);
	
	
	$("#dialog").dialog({
		modal: true,
		resizable: true,
		beforeclose: function() {
			$("#dialog").dialog('destroy');
		},
		buttons: {
				OK: function() {
					$(this).dialog('close');
				}
		}
		
	});
}


function alertContattaImmobile(title, id) {
	$("#dialog").attr("title", title);
	$("#dialog").html("<img src='img/loading_nero.gif' align='absmiddle' />");
	
	$("#dialog").dialog({
		dialogClass: 'dialog',
		modal: true,
		resizable: true,
		width: 450,
		beforeclose: function() {
			$("#dialog").dialog('destroy');
		},
		buttons: {
				Invia: function() {
					send();
				},
				
				Chiudi: function() {
					$(this).dialog('close');
				}
		}
		
	});
	
	$("#dialog").load("contatta.php", {id: id} );

	
}

function alertContattaUtente(title, id) {
	$("#dialog").attr("title", title);
	$("#dialog").html("<img src='img/loading_nero.gif' align='absmiddle' />");
	
	
	
	
	$("#dialog").dialog({
		dialogClass: 'dialog',
		modal: true,
		resizable: true,
		width: 450,
		beforeclose: function() {
			$("#dialog").dialog('destroy');
		},
		buttons: {
				Invia: function() {
					sendUtente();
				},
				
				Chiudi: function() {
					$(this).dialog('close');
				}
		}
		
	});
	
	$("#dialog").load("contattaUtente.php", {id: id} );
	
}


function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};





function get_estensione(path) {
    posizione_punto=path.lastIndexOf(".");
	lunghezza_stringa=path.length;
	estensione=path.substring(posizione_punto+1,lunghezza_stringa);
	return estensione;
}

function delImg(id) {
	
	var conferma = confirm("Sei sicuro di voler eliminare questa immagine?");
	
	if (conferma) {
		$.post("gestioneImmagini.php", { act: 'del', id: id }, function(data) {
			$("#dialog").find("p").html("<img src='img/loading_nerogrande.gif /><br />Attendere..")
				.css("text-align", "center");
				
			$("#dialog").dialog({
				title: "Eliminazione immagine in corso",
				open: function () { $('.ui-dialog-titlebar-close').css('display','none'); },
				modal:true,
				resizable: false,
				draggable:false				
			});
			
			if (data == 0) {
				$("#dialog").close('destroy');
				
				myalert("Errore", "Si è verificato un errore nel tentativo di eliminare l'immagine");
				return false;
			}
			else {
				
				setTimeout(function() {
				document.location.href = 'gestioneImmagini.php';
				}, 800);
			}
		});
	}
}

function delImmobile(id) {
	
	var conferma = confirm("Sei sicuro di voler eliminare questo immobile?");
	
	if (conferma) {
		$.post("pannello.php", { a: 'del', id: id}, function(data) {
			if (data == 0) {
				myalert("Errore", "Si è verificato un errore nell'eliminare l'immobile, contattare l'amministratore se il problema persiste");
			}
			else if (data == 1) {
				$(".listaAnnunci tr[rowid="+id+"]").fadeOut("slow", function() { document.location.href = 'pannello.php'; });
			}
		});
	}
}

function ricaricaCaptcha(path) {
	$("#captchaImage").attr("src", path+"?"+randomString());
	$("#captcha").val("");
	$("#capChck").val("0");
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 10;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function showNumber(id) {
	$.post("view.php", { idUtente: id, act: 'tel'}, function(data) {
		if (data == 1) {
			$(".telefono").show();
			$(".telLink").hide();
			return false;
		}
		else {
			myalert("Errore", "Si è verificato un errore");
			return false;
		}
	});
}

function showCell(id) {
	$.post("view.php", { idUtente: id, act: 'cell'}, function(data) {
		if (data == 1) {
			$(".cellulare").show();
			$(".cellLink").hide();
			return false;
		}
		else {
			myalert("Errore", "Si è verificato un errore");
			return false;
		}
	});
}


$(document).ready(function() {
	
	
	
	
	$("#showSlide").click( function() {
		$(".slideShow").lightBox();
	});
	
	$("#search").submit(function() {
		if ($("#regione option:selected").val() == '') {
			$("#regione").addClass("borderError");
			myalert("Errore", "Selezionare una regione");
			return false;
		}
	});
	
	
	
	var reg_sel = $("#reg_sel");
	var prov_sel = $("#prov_sel");
	var com_sel = $("#com_sel");
	var zona_sel = $("#zona_sel");
	
		
	
	if (typeof(reg_sel.val()) != 'undefined' && reg_sel.val().length > 0) {
		
	$("#regione option[value="+reg_sel.val()+"]").attr("selected", "selected");
	
	loadProv(reg_sel.val(), function() {
		
		$("#prov option[value="+prov_sel.val()+"]").attr("selected", "selected");				
	});
	}
	
	if (typeof(prov_sel.val()) != 'undefined' && prov_sel.val().length > 0) {
	
	loadCom(prov_sel.val(), function() {					
		$("#comune option[value="+com_sel.val()+"]").attr("selected", "selected");			
	});
	
	}
	
	
	if (com_sel.val() == 'Roma' && typeof(com_sel.val()) != 'undefined') {
	loadZone(com_sel.val(), function() {
		$("#zona option[value="+zona_sel.val()+"]").attr("selected", "selected");
		
	
	});
	
	}
	
	

	if ($("#uploadImg").length > 0) {
		
		$("#uploadImg").submit( function() {
			var value = $("#image").val();
			
			if (get_estensione(value).toLowerCase() != 'jpg' && get_estensione(value).toLowerCase() != 'png' && get_estensione(value).toLowerCase() != 'gif' && get_estensione(value).toLowerCase() != 'bmp') {
				myalert("Errore", "Il file inserito non è valido");
				$("#image").val("");
				return false;
			}
			
			
			$("#dialog").find("p").html("<img src='img/loading_nerogrande.gif /><br />Attendere..")
				.css("text-align", "center");
				
			$("#dialog").dialog({
				title: "Upload immagine in corso",
				open: function () { $('.ui-dialog-titlebar-close').css('display','none'); },
				modal:true,
				resizable: false,
				draggable:false				
			});
		});
	}
	
	
	if ($(".listaAnnunci").length > 0 && $(".listaAnnunci tr").size() > 2) {
		$(".listaAnnunci").tablesorter({
			headers: { 0: { sorter: false }, 9: { sorter: false } }
			
		});
	}
	$("#fp").submit( function() {
		if ($("#email").val() == '' || typeof($("#email").val()) == 'undefined') {
			myalert("Errore", "Specificare un indirizzo email");
			return false;
		}
	});
	
	$("#residenziale").click(function() {
		$(".tipologiaRes").removeClass("hidden")
			.find("select:first")
				.removeAttr("disabled");
		
		$(".tipologiaCom").addClass("hidden")
			.find("select:first")
				.attr("disabled", "disabled");
	});
	
	$("#commerciale").click(function() {
		$(".tipologiaRes").addClass("hidden")
			.find("select:first")
				.attr("disabled");
		
		$(".tipologiaCom").removeClass("hidden")
			.find("select:first")
				.removeAttr("disabled");
		
	});
	
	$("#turistico").click(function() {
		$(".tipologiaRes").removeClass("hidden")
			.find("select:first")
				.removeAttr("disabled");
				
		$(".tipologiaCom").addClass("hidden")
			.find("select:first")
				.attr("disabled", "disabled");
	});
		
	$("#order").change(function() {
		$("#search").submit();
	});
	

	
	
	if ($("#carousel li").size() > 3) {
		$('#carousel').jcarousel({
        auto: 4,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    	});
    	
    	
	}
	else if ($("#carousel li").size() == 0) {
		
	}
	else {
		$("#carousel").jcarousel({});
	}
	
	if ($("#carousel2 li").size() > 3) {
		
		$('#carousel2').jcarousel({
        auto: 4,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
   
	}
	else if ($("#carousel2 li").size() == 0) {
		
	}
	else {
		$("#carousel2").jcarousel({});
	}
	
	$("input[value=affitto]").click(function() {
		$(".prezzoVen").addClass("hidden");
		$(".prezzoVen > select").attr("disabled", "disabled");
		$(".prezzoAff").removeClass("hidden");
		$(".prezzoAff > select").removeAttr("disabled");
	});
	
	$("input[value=vendita]").click(function() {
		$(".prezzoVen").removeClass("hidden");
		$(".prezzoAff").addClass("hidden");
		$(".prezzoVen > select").removeAttr("disabled");
		$(".prezzoAff > select").attr("disabled", "disabled");
	});
	
	
});
