//<![CDATA[
var _pathURL = "/http_request/";

var addthis_config = {
	services_exclude:"more",services_compact:"print,email,favorites,blogger,aim,delicious,digg,facebook,google,googletranslate,live,myspace,segnalo,stumbleupon,twitter,wordpress,yahoomail",
	ui_delay:200,ui_language:"it",ui_offset_top:0,ui_offset_left:0,ui_cobrand:"Attico.it",data_track_linkback:true
};

/* bindo gli eventi change per le select della ricerca */
function eventiRicerca( isHome ) {
	
	var _divErrori = $("#errore"), _divLoader = $("#loader");
	var idCateg = new Array(6),	idContratto = new Array(3);
	
	idContratto[0] = $("#contratto");
	idContratto[1] = $("#linkvendita");
	idContratto[2] = $("#linkaffitto");
	
	$("#ul_contratto li,#ul_contrattoVA3 li").click( function() {

		_divErrori.hide();
		var stringa = "";
		var stringa2 = "";

		if( isHome == "1" ) {
			idContratto[1].removeClass().addClass("linkvendita");
			idContratto[2].removeClass().addClass("linkaffitto");
					
			$(this).removeClass().addClass( $(this).attr("id") + "_hover" );
			
			( $(this).attr("id") == "linkvendita" ) ? idContratto[0].val("V") : idContratto[0].val("A")
			
			stringa = "-home";
			stringa2 = "_home";
		} else {
			idContratto[1].removeClass().addClass("ElencoV3");
			idContratto[2].removeClass().addClass("ElencoA3");
			
			if( $(this).hasClass("ElencoV3") )
				$(this).removeClass().addClass("ElencoV3_hover");
			else
				$(this).removeClass().addClass("ElencoA3_hover");
			
			( $(this).attr("id") == "linkvendita" ) ? idContratto[0].val("V") : idContratto[0].val("A")
		}
		
		var statoSlider = $("#slider-prezzo" + stringa).is("disabled");
		
		$("#slider-prezzo" + stringa).slider("destroy");
		if( idContratto[0].val() == "A" ) {
			$("#slider-prezzo" + stringa).slider({
				orientation: "horizontal",
				range: true,
				step: 200,
				min: 0,
				max: 5000,
				values: [0, 5000],
				slide: function(event, ui) {
					$("#slider_prezzo_min" + stringa2).css("font-weight","bold").html( ui.values[0] + "&euro;" );						
					( ui.values[1] == 5000 ) ? $("#slider_prezzo_max" + stringa2).css("font-weight","bold").html("5000+&euro;") : $("#slider_prezzo_max" + stringa2).css("font-weight","bold").html( ui.values[1] + "&euro;" )
				},
				stop: function(event, ui) {
					$("#prezzo_min").val( ui.values[0] );
					$("#prezzo_max").val( ui.values[1] );
					$("#slider_prezzo_min" + stringa2).css("font-weight","normal");
					$("#slider_prezzo_max" + stringa2).css("font-weight","normal");
				}				
			});
			$("#slider_prezzo_min" + stringa2).html("0&euro;");
			$("#slider_prezzo_max" + stringa2).html("5000+&euro;");
		} else {
			$("#slider-prezzo" + stringa).slider({
				orientation: "horizontal",
				range: true,
				step: 20000,
				min: 0,
				max: 500000,
				values: [0, 500000],
				slide: function(event, ui) {
					$("#slider_prezzo_min" + stringa2).css("font-weight","bold").html( ui.values[0] + "&euro;" );
					( ui.values[1] == 500000 ) ? $("#slider_prezzo_max" + stringa2).css("font-weight","bold").html("500000+&euro;") : $("#slider_prezzo_max" + stringa2).css("font-weight","bold").html( ui.values[1] + "&euro;" )
				},
				stop: function(event, ui) {
					$("#prezzo_min").val( ui.values[0] );
					$("#prezzo_max").val( ui.values[1] );
					$("#slider_prezzo_min" + stringa2).css("font-weight","normal");
					$("#slider_prezzo_max" + stringa2).css("font-weight","normal");
				}
			});
			$("#slider_prezzo_min" + stringa2).html( "0&euro;" );
			$("#slider_prezzo_max" + stringa2).html( "500000+&euro;" );
		}
		if( statoSlider == true )
			$("#slider-prezzo" + stringa).slider("disable");
		
		return(false);
	});
	
	$("#regioni").change(function() {
		_divErrori.hide();
		$("#divTavole").hide();
		$("#linkTavole").hide();
		$("#comuni").attr("disabled",true).html("<option value=\"0\">Comune</option>");
		$("#tavole").attr("disabled",true).html("<option value=\"0\">Tavola</option>");
		$("#divTipologie select").attr("disabled",true);
		$("#slider-prezzo-home").slider("disable");
		$("#slider-mq-home").slider("disable");
		if( $(this).val() > 0 )
			$("#province").removeAttr("disabled").load("/http_request/dropdown.asp", { identity:1, valore:$(this).val() } );
		else
			$("#province").attr("disabled",true).html("<option value=\"0\">Provincia</option>");
		
		if( document.location.href == "http://www.attico.it/" || document.location.href == "http://www.attico.it/default.asp" ) {
			var txtRegione = $("#regioni option:selected").text();		
			if( $(this).val() != 5 )
				txtRegione = txtRegione.replace(" ","_").replace("'","");			
			else
				txtRegione = txtRegione.substring(0, txtRegione.indexOf(" "));
			
			CambioRegione( $(this).val(), txtRegione );		
		}
	});
	$("#province").change(function(event,origine) {
		_divErrori.hide();
		$("#divTavole").hide();
		$("#linkTavole").hide();
		$("#tavole").attr("disabled",true).html("<option value=\"0\">Tavola</option>");
		$("#divTipologie select").attr("disabled",true);
		$("#slider-prezzo-home").slider("disable");
		$("#slider-mq-home").slider("disable");
		if( $(this).val() > 0 ) {
			$("#comuni").removeAttr("disabled").load("/http_request/dropdown.asp", { identity:2, valore:$(this).val() } );
		} else
			$("#comuni").attr("disabled",true).html("<option value=\"0\">Comune</option>");

		if( origine != "province" && ( document.location.href == "http://www.attico.it/" || document.location.href == "http://www.attico.it/default.asp" ) )
			CambioProvincia( $("#province option:selected").text() );
	});
	$("#comuni").change(function() {
		_divErrori.hide();
		
		if( $(this).val() > 0 ) {
			$("#divTavole_Elenco").removeAttr("disabled").load( 
				"/http_request/dropdown.asp", { identity:3, valore:$(this).val() }, 
				function( responseText, textStatus, XMLHttpRequest ) { /* mostro il div delle tavole solo se ci sono */
					( responseText == "" ) ? $("#linkTavole").hide() : $("#linkTavole").show();
				} 
			);
			$("#divTipologie select").removeAttr("disabled");
			$("#slider-prezzo-home").slider("enable");
			$("#slider-mq-home").slider("enable");
		}
		
		if( document.location.href == "http://www.attico.it/" || document.location.href == "http://www.attico.it/default.asp" ) {
			var comuneOK = false;
			var targa = "";
					
			switch( parseInt( $(this).val() ) ) {
				case 46: comuneOK=true; targa="AL"; break;
				case 7067: comuneOK=true; targa="TO"; break;
				case 4129: comuneOK=true; targa="MI"; break;
				case 1342: comuneOK=true; targa="BO"; break;
				case 6482: comuneOK=true; targa="SV"; break;
				case 3282: comuneOK=true; targa="GE"; break;
				case 3435: comuneOK=true; targa="IM"; break;
				case 3106: comuneOK=true; targa="FI"; break;
				case 5158: comuneOK=true; targa="PO"; break;
				case 4917: comuneOK=true; targa="PD"; break;
				case 5929: comuneOK=true; targa="RM"; break;
				case 4425: comuneOK=true; targa="NA"; break;
			}
			
			if( comuneOK ) {
				var txtComune = $("#comuni option:selected").text() + " Citt&agrave;";
				CambioComune( $(this).val(), targa, txtComune );
			} else {
				if( $("#spazioMappa").data("pos") == "comuni" ) {
					var txtRegione = $("#regioni option:selected").text();		
					if( $("#regioni").val() != 5 )
						txtRegione = txtRegione.replace(" ","_").replace("'","");			
					else
						txtRegione = txtRegione.substring(0, txtRegione.indexOf(" "));
						
					CambioRegione( $("#regioni").val(), txtRegione );	
				}			
			}
		}
	});
	$("#linkTavole a").click( function() {
		$("#divTavole").show();
		$("#divTavole *").removeAttr("disabled");
		
		$("#divTavole_Elenco a#all_tavole").click( function() {		
			if( $(this).html() == "Tutte le tavole" ) {
				$(this).html("Nessuna tavola");
				$("#divTavole_Elenco li input:checkbox").attr("checked",true);	
			} else {
				$(this).html("Tutte le tavole");
				$("#divTavole_Elenco li input:checkbox").attr("checked",false);
			}
			return(false);
		});

		return(false);
	});
	
	$("#divTavola_Link").click( function() {
		var numTavole = $("#divTavole_Elenco li input:checkbox:checked").length;
		$("#numTavole_sel").empty();
		
		if( numTavole > 0 ) {
			if( numTavole == 1 )
				$("#numTavole_sel").html( numTavole + " tavola" );
			else
				$("#numTavole_sel").html( numTavole + " tavole" );
		}
		$("#divTavole").hide();
		return(false);
	 });
	
	$("#categoria").change( function () {
		_divErrori.hide();
		$("#tipologia").load("/http_request/dropdown.asp", { identity:6, valore:$(this).val() } );
	});

	/* se sono in HomePage entro nel ciclo */
	if( isHome == "1" ) {
		
		/* disabilito tutto tranne la regione se sono in home */
		RicercaHome_Default();
		
		idContratto[0].val("V");

		idCateg[0] = $("#categoria");
		idCateg[1] = $("#categoria1");
		idCateg[2] = $("#categoria2");
		idCateg[3] = $("#categoria3");
		idCateg[4] = $("#categoria4");
		idCateg[5] = $("#categoria5");
		
		/* salvo la classe iniziale dei link nella ricerca */
		idCateg[0].val("1");
		idCateg[1].data("cssOrig", "linkmr1");
		idCateg[2].data("cssOrig", "linkmr2");
		idCateg[3].data("cssOrig", "linkmr3");
		idCateg[4].data("cssOrig", "linkmr4");
		idCateg[5].data("cssOrig", "linkmr6");
		
		/* carico le tipologie residenziali */
		$("#tipologie").load("/http_request/dropdown.asp", { identity:6, valore:1 } ).change( function(){ _divErrori.hide(); $("#tabdetOpzionali").show(); });
		
		$("#divTavole").hide();
		
		$("#ul_categorie li").click( function() {			
			_divErrori.hide();
			
			RicercaHome_Default();
			
			/* resetto le classi di hover ed imposto l'immagine di on nel bottone cliccato */	
			idCateg[1].removeClass().addClass( "linkmr1" );
			idCateg[2].removeClass().addClass( "linkmr2" );
			idCateg[3].removeClass().addClass( "linkmr3" );
			idCateg[4].removeClass().addClass( "linkmr4" );
			idCateg[5].removeClass().addClass( "linkmr6");
			
			$(this).removeClass().addClass( $(this).data("cssOrig") + "_hover");		
			
			var idCategoria = $(this).attr("id").replace("categoria","");
			
			idCateg[0].val( idCategoria );
						
			if( idCategoria != "5" ) { /* se non clicco nuove costruzioni carico le tipologie */
				if( $("#comuni").attr("disabled") == false && $("#comuni").val() > 0 ) {
					$("#divTipologie select").removeAttr("disabled");
					$("#slider-prezzo-home").slider("enable");
					$("#slider-mq-home").slider("enable");
				}
				
				$("#tipologie").load("/http_request/dropdown.asp", { identity:6, valore:idCategoria } );
			} else { /* nuove costruzioni - nascondo tutte le select tranne regione/provincia/comune/tavola */				
				$("#divTipologie select").attr("disabled",true);
				$("#slider-prezzo-home").slider('disable' );
				$("#slider-mq-home").slider('disable');
			}
			
			return(false);
		});
	} 
	/* elementi risultati ricerca sx */		
	$("#OptionalParameters a").click( function() {
	   var _divErrori = $("#errore");
		var linkDiv = $(this).attr("id"), optInput = linkDiv.replace("link_","");
		
		_divErrori.hide();
		if( $("#" + optInput) )
			$("#" + optInput).slideDown("slow", function() {
				$("#img_" + optInput).attr("src","http://images.attico.it/ico_ricopen.gif");
				$("#" + optInput).attr("id",optInput + "_Up"); 
			});
			
		if( $("#" + optInput + "_Up") ) {				
			$("#" + optInput + "_Up").hide("slow");
			$("#img_" + optInput).attr("src","http://images.attico.it/ico_ricoff.gif");
			$("#" + optInput + "_Up").attr("id", optInput);
		}
		return(false);			
	});
	$("#OptionalParameters input:radio").click( function() {
		var nameOpt = $(this).attr("name");			
		$("#txt_opt_" + nameOpt).html( $(this).attr("alt") );
	});
	
}


function RicercaHome_Default() {
	
	if( $("#province").attr("disabled") == false && $("#regioni").val() == "0" )
		$("#province").attr("disabled",true);
	
	if( $("#comuni").attr("disabled") == false && $("#province").attr("disabled") == true )
		$("#comuni").attr("disabled",true);
	
	if( $("#tavole").attr("disabled") == false && $("#comuni").attr("disabled") == true )
		$("#tavole").attr("disabled",true);
	
	if( ($("#tipologie").attr("disabled") == false || $("#divTipologie").attr("disabled") == false) && ( $("#comuni").attr("disabled") == true ) ) {
		$("#divTipologie select").attr("disabled",true);
		$("#slider-prezzo-home").slider("disable");
		$("#slider-mq-home").slider("disable");		
	}
}


function AttivaTabRivista() {
	var arr = new Array(4);
	arr[0] = $("#linkRiv1");
	arr[1] = $("#linkRiv2");
	arr[2] = $("#linkRiv3");
	arr[3] = $("#linkRiv4");	

	arr[0].click( function() {
		$("#linkRiv2,#linkRiv3,#linkRiv4").removeClass();
		$(this).removeClass().addClass("linkRiv1_hover");
		
		$("#relRiv2,#relRiv3,#relRiv4").hide();					
		$("#relRiv1").show();
		return(false);
	});
	arr[1].click( function() {		
		$("#linkRiv1,#linkRiv3,#linkRiv4").removeClass();
		$(this).removeClass().addClass("linkRiv2_hover");
		
		$("#relRiv1,#relRiv3,#relRiv4").hide();					
		$("#relRiv2").show();
		return(false);
	});
	arr[2].click( function() {
		$("#linkRiv1,#linkRiv2,#linkRiv4").removeClass();
		$(this).removeClass().addClass("linkRiv3_hover");
		
		$("#relRiv1,#relRiv2,#relRiv4").hide();
		$("#relRiv3").show();
		return(false);
	});
	arr[3].click( function() {
		$("#linkRiv1,#linkRiv2,#linkRiv3").removeClass();
		$(this).removeClass().addClass("linkRiv4_hover");
		
		$("#relRiv1,#relRiv2,#relRiv3").hide();
		$("#relRiv4").show();
		return(false);
	});
}


var popup = 0;
function OpenPopUp( Url, width, height ) {
	if (popup) { if(!popup.closed) popup.close() }
	var top = 40, left = (screen.width/2)-(width/2);
	popup=window.open(Url, 'popup' , 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height + ', top =' + top + ', left=' + left + ''); 
	popup.focus(); 
}

/* funzione per l'invio di un form tramite Ajax 
Es: Form_Submit( $(this), "richiesta_specifica" );
$(this) = è l'oggetto che viene cliccato per effettuare l'invio del form
*/
function Form_Submit( obj, nomeForm ) {
	var new_href = "",
		formSerialized = $("#" + nomeForm.replace("_ext","") ).serialize(),
		risposta = "";
	
	risposta = ChiamataSync("prc_" + nomeForm + ".asp","post",formSerialized + "&identity=" + nomeForm,"Loading");
	
	if( risposta == "1" && nomeForm == "ricerca_home_ext" ) {
		nomeForm = "ricerca_home";
		top.location.href = "/risultati.asp?identity=" + nomeForm + "&" + formSerialized;
	}
	
	if( risposta == "1" && (nomeForm == "ricerca_home" || nomeForm == "ricerca_risultati") )		
		document.location.href = "/risultati.asp?identity=" + nomeForm + "&" + formSerialized;
	
	if( risposta == "0" )
		obj.fadeIn();
	else {
		obj.replaceWith( $("#Loading") );
		if( nomeForm == "richiesta_specifica" || nomeForm == "richiesta_specifica_lista" ) {
			if( formSerialized.indexOf("send=ok") == -1 ) {
				document.location.href = location.href + "&send=ok";
			} else {
				location.href.replace("&send=ok","");
				document.location.href = new_href;
			}
		}
	}
}


/* Es.: ChiamataAsync( "interroga.asp", "post", valori, "div_login" ); 
pagina = nome della pagina a cui fare la richiesta ajax
metodo = "post" o "get"
parametri = può essere un form serializzato oppure una stringa di valori in stile querystring.
oggettoHtml = id dell'elemento html nel quale si vuol far apparire il valore di ritorno.

effettua la chiamata ajax in modo asincrono!
*/
function ChiamataAsync( pagina, metodo, parametri, oggettoHtml ) {
	oggettoHtml = "#" + oggettoHtml;
	
	$.ajax({
		url: _pathURL + pagina,
		global: true,
		type: metodo,
		async: true,
		data: parametri,
		dataType: "html",		
		success: function( risultato ) {
			var boolErrore = risultato.substring(0,1);
			risultato = risultato.substring(1);
			
			if( boolErrore == "1" )
				$( oggettoHtml ).html( risultato );
			else
				MostraErrore( oggettoHtml, risultato );
		}
	});
}



/* Es.: var ritorno = ChiamataSync( "interroga.asp", "post", valori, "div_login" ); 
stessa cosa di ChiamataAsync, solo che ritorna il primo carattere di risposta alla richiesta ed è una chiamata sincrona!
*/
function ChiamataSync( pagina, metodo, parametri, oggettoHtml ) {
	oggettoHtml = "#" + oggettoHtml;
	
	var risposta = $.ajax({
		url: _pathURL + pagina,
		global: true,
		type: metodo,
		async: false,
		data: parametri,
		dataType: "html",		
		success: function( risultato ) {
			var boolErrore = risultato.substring(0,1);
			risultato = risultato.substring(1);
			
			if( boolErrore == "1" )
				$( oggettoHtml ).html( risultato );
			else
				MostraErrore( oggettoHtml, risultato );
		}
	}).responseText;
	
	return risposta.substring(0,1);
}



/* Es.: MostraErrore( "#div_login", "errore" );
oggetto = id dell'elemento html dove verrà visualizzato il messaggio
testo = messaggio da mostrare
*/
function MostraErrore( oggetto, testo ) { $( oggetto ).after( $("#errore").fadeIn().html( testo ) ); }

function CreaCaptcha( key ) { Recaptcha.create( key,"recaptcha_div", {theme: "white"}); }

function vedi_annuncio( qst ) { window.location.href="/annuncio.asp?IDImmobile=" + qst;  }

function ValidateCaptcha( challenge, response ) {
	var risposta = ChiamataSync("re-captcha.asp","post","c=" + challenge + "&r=" + response,"Loading");	
	Recaptcha.reload();
	if( risposta == "0" )
		return(false);
	else
		return(true);
}

jQuery(function($) {
	
	var _divErrori = $("#errore"), _divLoader = $("#loader");
	
	$().ajaxStart( function() { $("#Loading").html( _divLoader.show() ); }).ajaxStop( function() { _divLoader.hide(); }).ajaxSuccess( function(e, r, s) { _divLoader.hide();}).ajaxError( function(e, r, s) { MostraErrore("#wrapper","Errore " + r.responseText + " nella richiesta di " + s.url + ", riprovare.\nSe persiste contattare l'amministratore del sito!" ); });		
	
	$("#err-msg-close").live("click",function(){ _divErrori.hide(); });
	
	Inizializer();
});
//]]>