
// Gestione form ricerca
$(document).ready(function() {
	if($("#text_ricerca").val() == ''){
		$("#text_ricerca").val('Ricerca veloce');
	}
	$("#text_ricerca").click(function(){
		if($("#text_ricerca").val() == 'Ricerca veloce'){
			$("#text_ricerca").val('');
		}
	});
	$("#text_ricerca").blur(function(){
		if($("#text_ricerca").val() == ''){
			$("#text_ricerca").val('Ricerca veloce');
		}
	});
});


// Form filtro (ricerca)
function select_filtro_tip(){
	var val_form = parseInt($("#filtro_tipologia").val());
	if(val_form > 0){
		$("#filtro_modello").attr('disabled', 'disabled');
	}else if(val_form == 0){$("#filtro_modello").removeAttr('disabled');}
}
function select_filtro_mod(){
	var val_form = parseInt($("#filtro_modello").val());
	if(val_form > 0){
		$("#filtro_tipologia").attr('disabled', 'disabled');
	}else if(val_form == 0){$("#filtro_tipologia").removeAttr('disabled');}
}



$(document).ready(function() {
	  
	// Gestione blank
	$("a[rel='blank']").attr("target", "_blank");
	
	
	// Apertura chiusura menu principale
	$("a.toggle_menu").click(function () {
		$("ul.navigazione_principale").slideToggle();
		return false;
	})
	
	
	// Gestione funzioni per form
	if ( !$("label.prevalue input").val()) {
		$("label.prevalue input").val('');
	} else {
		$("label.prevalue span.prevalue").hide();
	}
		
	$("label.prevalue input").focus(function () {
		$(this).prev("span.prevalue").hide();
	});
	
	// Popup dati fiscali
	$("a[href='_dati_fiscali.htm']").click(function () {
		
		$("body").append('<div class="overlay_pagina" id="overlay_dati" style="display: none;"><div id="dati_fiscali"></div><a id="close" href="#">X chiudi</a></div>');
		
		$("#dati_fiscali").load("_dati_fiscali.htm", function () {
			$(".overlay_pagina").fadeIn(500);
			
			$("a#close").bind('click', function () {
				$(".overlay_pagina").fadeOut(500, function () {
					$(this).remove();										   
				});
				return false;
			})
		});
		return false;
	});
	
	// Apri chiudi dettaglio
	if ($(".dettaglio").length > 0) {
		$('<a class="openclose chiudi" href="#">chiudi</a>').appendTo(".dettaglio");
		
		$("a.openclose").live("click", function() {
			var top = $(".dettaglio").hasClass("closed") ? "50%" : -($(".dettaglio").height()/2)+10+"px";
			var text = $(".dettaglio").hasClass("closed") ? "chiudi" : "apri";
			
			$(".dettaglio").animate({
				top: top
			}, 600, function () {
				$(this).toggleClass("closed");
				$("a.openclose").text(text);
			});
			
			return false;
		});
	}	
});


/*======================*/
/* PocketPopup 
/* 
/* v.0.1.1 
/* 17/02/2011
/*======================*/
function PocketPopup(PopId, PopWidth, PopHeight, PopContent, PopAjaxUrl, PopCallback) {
	
	PocketPopup.chiudi = function () {
		$(".overlay, .popup_overlay").remove();
		// Lancio evento chiusura per essere eventualmente utilizzato dalle pagine chiamate
		$(document).trigger("PocketPopupClosed");
		return false;
	}
	
	// Apri
	$("body").append('<div class="overlay"></div><div id="'+PopId+'" class="popup_overlay" style="display: none;"><a class="close" href="#">X</a><div class="popup_content"></div></div>');
	var wIn = (PopWidth) ? PopWidth : 500+'px';
	var hIn = (PopHeight) ? PopHeight : 'auto';
	$(".popup_content").css({
		width: wIn,
		height: hIn
	});
	$(".overlay").css({height : $(document).height()+"px", width:  $(window).width()+"px",opacity: 0.8});
	
	// Chiudi
	$("a.close, .overlay").click(function () {return PocketPopup.chiudi();});
	$(document).keypress(function(e) {if (e.keyCode == 27) {PocketPopup.chiudi(); $(document).unbind();}});
	
	// Dimensioni e posizione
	$(document).bind('set_dimensions', function () {
		var wOut = $(".popup_overlay").outerWidth();
		var hOut = $(".popup_overlay").outerHeight();
		
		/*if (wOut > $(window).width()) {
			wOut = $(window).width();
		}
		if (hOut > $(window).height()) {
			hOut = $(window).height();
			alert($(".popup_overlay").css("padding-left"));
			hIn = hOut - $(".popup_overlay").css("padding-left");
			alert(hOut);
			alert(hIn);
		}*/
		
		$(".popup_overlay").css ({
			marginLeft: -wOut/2,
			marginTop: -hOut/2
		});
		$(document).unbind("set_dimensions");
	});
	
	// Contenuto
	if (PopContent) {
		$(".popup_content").html(PopContent);
		$(".popup_overlay").fadeIn(400);
		$(document).trigger("set_dimensions");
	} 
	else if ((PopAjaxUrl)) {
		$.ajax({
			url: PopAjaxUrl,
			beforeSend: function () {
				$(".popup_content").append('<br /><br /><img id="loading" src="/include/loading_2.gif" alt="Loading" />');
			},
			success: function (result) {
				$(".popup_content").html(result);
				$(".popup_overlay").fadeIn(400);
				$(document).trigger("set_dimensions");	
			},
			error: function (a,b) {
				$(".popup_content").html('<p style="text-align: center"><img src="/immagini_layout/warning.png" alt="Warning" /><br />Si &egrave; verificato un errore!</p>');
				$(".popup_overlay").fadeIn(400);
				$(document).trigger("set_dimensions");
				//setTimeout('window.location.reload();', 2000);
			}
		});
	} 
	
	
	// Callback
	eval(PopCallback);
	
}

