// BOX DI GOOGLE
var google_ad_client = "pub-3604468008153216";
/* 160x600, creato 04/09/10 */
var google_ad_slot = "4229231080";
var google_ad_width = 160;
var google_ad_height = 600;

// Variabili globali
var xhr = '';
var xCookie = new Array();
var flagErrore = 0;
var winLeft = 0;
var winTop = 0;
var xFlagScudo = 0;
var xDivOverOLD = '';
/* riposizionamento overlay in caso di ridimensionamneto finestra */
$(window).resize(function(){
	if(xFlagScudo==1) {
		posizionaWin(xDivOverOLD); 
	}
});
/*$(window).scroll(function(){
	if(xFlagScudo==1) {
		posizionaWin(xDivOverOLD); 
	}
});*/

// POPUP PRIVACY
function normePrivacy() {
	window.open('ext/pop_privacy.html','popPrivacy','width=500,height=600');
}

function XINIZIALIZZA(pag) {
	
	// ARRESTO PROPAGAZIONE EVENTO CLICK SE TAGGATI
	$(".no-click").click(function(event){
	  event.stopPropagation();
	}); 
	
	// MAILTO OFFUSCATO
	jQuery.fn.mailto = function() {
		return this.each(function(){
			var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
			$(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
		});
	};
	
	// MENU
	if($('.m1-cx-nero')) {
		$('.m1-cx-nero').hover(
			function(){
				 $(this).prev().removeClass('m1-sx-nero').addClass('m1-sx-grigio'); 
				 $(this).removeClass('m1-cx-nero').addClass('m1-cx-grigio'); 
				 $(this).next().removeClass('m1-dx-nero').addClass('m1-dx-grigio'); 
			}, // in
			function(){
				 $(this).prev().removeClass('m1-sx-grigio').addClass('m1-sx-nero'); 
				 $(this).removeClass('m1-cx-grigio').addClass('m1-cx-nero'); 
				 $(this).next().removeClass('m1-dx-grigio').addClass('m1-dx-nero'); 
			} // out
		);
		$(function() {
			$('#nav').droppy( { speedIN:500,speedOUT:200 } );
		});
	}
	
	// INIZIALIZZAZIONI
	if($('.bottoni-DIV')) {
		$('.bottoni-DIV').hover(
			function(){
				//alert(this.id);
				 $("#"+this.id).removeClass('bottoni-OUT').addClass('bottoni-OVER'); 
			}, // in
			function(){
				 $("#"+this.id).removeClass('bottoni-OVER').addClass('bottoni-OUT'); 
			} // out
		);
	}
	
	// LOGIN
	xBoxUtente();

}

// STAMPA CONTENUTO DI UN BOX
function xStampa(xDiv,xNascondi) {
	$(xDiv).printElement({ overrideElementCSS:['xstandard.css', { href:'css/xprint.css',media:'print'}]	});
	//$(xDiv).printElement();
}

// GESTIONE BOX LOGIN E COOKIE
function xBoxUtente() {
	if($.cookie("cogein")) {
		xCookie = $.cookie("cogein").split("#");
		var HTML = '<div class="P1-utente-sx"></div><div class="P1-utente-cx">';
		HTML += xCookie[2]+'<br />'+xCookie[0]+' '+xCookie[1]+' - <a href="javascript:xLogout();" tabindex="1" title="effettua nuovamente l\'accesso">Esci</a>';
		HTML += '</div><div class="P1-utente-dx"></div>';
		$("#x-login").html(HTML);
		$("#x-login-box").html("");
	} else {
		$("#x-login").html('<img id="btLogin" src="images/btLoginOut.gif" width="126" height="41" alt="login" />');
		$('#btLogin').hover(
			function(){
				 $(this).attr("src", "images/btLoginOver.gif"); 
			}, // in
			function(){
				 $(this).attr("src", "images/btLoginOut.gif"); 
			} // out
		);
		var HTML = 
		HTML += '<div class="P1-top"></div><div class="P1-cx">';
		HTML += 'Inserisci i dati per accedere alla tua area personale.<br />Se sei un nuovo utente richiedi la <a href="registrazione_nuovo_utente.php" title="registrati per accedere all\'area riservata" tabindex="1000">registrazione</a> oppure contatta direttamente il nostro staff allo <strong>055-340134</strong>.<br />Nel caso invece che tu abbia già effettuato la registrazione ma non ti ricordi i dati per l\'accesso effettua la <a href="recupero_dati_login.php" title="recupera i dati della tua registrazione">procedura di recupero</a>.';
		HTML += '<div id="F1-risposta"></div>';
		HTML += '<form action="" method="post" id="login-form" class="F1-form">';
		HTML += '<div class="F1-txt">Utente:</div>';
		HTML += '<div class="F1-val"><input tabindex="3" class="F1-fld" name="utente" id="utente" type="text" /></div>';
		HTML += '<div class="F1-txt">Password:</div>';
		HTML += '<div class="F1-val"><input tabindex="4" class="F1-fld" name="password" id="password" type="password" /></div>';
		HTML += '<div class="F1-row"><label><input id="okCookie" name="okCookie" type="checkbox" value="ok" />&nbsp;La prossima volta accedi automaticamente</label></div>';
		HTML += '<div class="F1-but"><div id="login-loading">&nbsp;</div>'+XBOT_LOGIN+XBOT_LOGAN;
		HTML += '</div></form></div><div class="P1-bot"></div>';
		$('#btLogin').click(function() { apriOverlay("#x-login-box"); $('#utente').focus(); });
		$("#x-login-box").html(HTML);
		//$('#bt-login').click(function() { xInviaFormLogin(); });
		$('#bt-login').click(function() { xInviaFormLogin(); });
		$('#bt-login').hover(
			function(){ $("#"+this.id).removeClass('bottoni-OUT').addClass('bottoni-OVER'); }, // in
			function(){ $("#"+this.id).removeClass('bottoni-OVER').addClass('bottoni-OUT');  } // out
		);
		$('#bt-chiudi-login').click(function() { chiudiOverlay(); });
		$('#bt-chiudi-login').hover(
			function(){ $("#"+this.id).removeClass('bottoni-OUT').addClass('bottoni-OVER'); }, // in
			function(){ $("#"+this.id).removeClass('bottoni-OVER').addClass('bottoni-OUT');  } // out
		);
	}
}

// LOGOUT
function xLogout() {
	$.cookie("cogein", "");
	xApriPag("index.php");
}

// GESTIONE FORM LOGIN
function xInviaFormLogin() {
	// validazione dati
	flagErrore=0;
	$("#F1-risposta").html("");
	if($("#utente").val().length<3) {
		xErrore("#utente");
		$("#F1-risposta").append("<strong>inserisci il nome utente.</strong><br />");
	}
	if($("#password").val().length==0 ) {
		xErrore("#password");
		$("#F1-risposta").append("<strong>inserisci la password</strong><br />");
	} else if($("#password").val().length<6 || $("#password").val().length>12 ) {
		xErrore("#password");
		$("#F1-risposta").append("<strong>password non valida</strong><br />");
	}
	var durataCookie = 0;
	if($('input[name=okCookie]').is(':checked')) {
		durataCookie = 30;
	}
	if(flagErrore==0) {
		$("#login-loading").html('<div class="F1-loading x-M-left-10"></div>');
		var xform = $("#login-form").serializeArray();
		xhr = $.ajax({  
		  type: "POST",  
		  url: "ext/xlogin.php",  
		  data: xform,
		  cache: false,
		  success:	function(data) {
			var risposta = new Array();
			risposta = data.split("#");
			if(risposta.length==1) {
				$("#F1-risposta").html(data);
				$("#login-loading").html("");
			} else {
				$("#F1-risposta").html("OK, utente riconosciuto");
				risposta.pop(0);
				$.cookie("cogein", data, { expires: durataCookie });
				window.location = "area_riservata.php";
			}
		  }
		});
	}
}

// ERRORI FORM
function xErrore(xOgg) {
	flagErrore = 1;
	$(xOgg).removeClass("F-norm"); 
	$(xOgg).addClass("F-erro"); 
	$(xOgg).focus(function(data) {
		if($(xOgg).hasClass("F-erro")) { 
			$(xOgg).removeClass("F-erro"); 
			$(xOgg).addClass("F-norm");
			$(xOgg).val("");
		}
	});
}

// RESET FORM
function xResettaForm(xForm) {
	if(xhr){ xhr.abort(); }
	$(xForm)[0].reset();
	$(":input", xForm).each(function(index) {
		if($(this).hasClass("F-erro")) { $(this).removeClass("F-erro"); $(this).addClass("F-norm"); }
  	});
	if($("#login-loading")) { $("#login-loading").html(""); }
	if($("#loading")) {	$("#loading").html(""); }
	if($(".F2-cont-erro")) { $(".F2-cont-erro").html(""); }
	if($("#x-status")) { $("#x-status").html(""); }
	if($("#stato-email")) {	$("#stato-email").attr("src","images/contatti_1.gif"); }
	flagErrore = 0;
}

// AUTOCOMPLETE
function lookup(oggetto,cont,pag,xfield) {
	var inputString = oggetto.value;
	if(inputString.length == 0) {
		$('#suggestions_'+cont).hide();
	} else {
		if(xhr){ xhr.abort(); }
		xhr = $.ajax({  
		  type: "POST",  
		  url: pag,
		  data: {queryString: ""+inputString+"", num: ""+cont+""},
		  cache: false,
		  beforeSend: function() {
			  	var position = $("#"+oggetto.id).offset();
			  	$('#suggestions_'+cont).css({ left:position.left, top:position.top+20 });
				$('#suggestions_'+cont).slideDown('fast');
				$('#suggestions_'+cont).addClass('suggestions-loading');
		  },
		  success: function(data) {
				$('#suggestions_'+cont).removeClass('suggestions-loading');
				if(data.length > 0) {
					$('#autoSuggestionsList_'+cont).html('<ul class="suggestionsList">'+data+'</ul>');
				} else {
					$('#'+xfield).val('');
					$('#autoSuggestionsList_'+cont).html('nessun dato con queste iniziali');
				}
		  }
		});
	}
}

// HTML PER PLAYER FLASH
function getFlashHTML(xMovie,xWidth,xHeight,xTitolo) {
	var xSWF = '<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+xWidth+'" height="'+xHeight+'"';
	if(String(xTitolo)!=='undefined') { xSWF += 'title="'+xTitolo+'"'; }
	xSWF += '><param name="movie" value="'+xMovie+'" />';
	xSWF += '<param name="quality" value="high" />';
	xSWF += '<param name="wmode" value="opaque" />';
	xSWF += '<param name="swfversion" value="6.0.65.0" />';
	xSWF += '<param name="expressinstall" value="swf/expressInstall.swf" />';
	xSWF += '<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->';
	xSWF += '<!--[if !IE]>-->';
	xSWF += '<object type="application/x-shockwave-flash" data="'+xMovie+'" width="'+xWidth+'" height="'+xHeight+'">';
	xSWF += '<!--<![endif]-->';
	xSWF += '<param name="quality" value="high" />';
	xSWF += '<param name="wmode" value="opaque" />';
	xSWF += '<param name="swfversion" value="6.0.65.0" />';
	xSWF += '<param name="expressinstall" value="swf/expressInstall.swf" />';
	xSWF += '<div class="no-flash">Il contenuto di questa pagine richiede l\'utilizzo di una versione aggiornata di Adobe Flash Player.';
	xSWF += ' Clicca sull\'icona per scaricarlo.<br /><a href="http://www.adobe.com/go/getflashplayer">';
	xSWF += '<img src="swf/get_adobe_flash_player.png" alt="Get Adobe Flash player" width="158" height="39" /></a></div>';
	xSWF += '<!--[if !IE]>-->';
	xSWF += '</object>';
	xSWF += '<!--<![endif]-->';
	xSWF += '</object>';
	return xSWF;
}

function xApriPag(xUrl) {
	document.location=xUrl;
}

// POSIZIONAMENTO FINESTRA
function posizionaWin(win){
	//winLeft = ($(document).width()/2) - ($(win).width()/2);
	//winTop = ($(document).height()/2) - ($(win).height()/2) + $(document).scrollTop();
	winLeft = ($(window).width()/2) - ($(win).width()/2);
	winTop = ($(window).height()/2) - ($(win).height()/2);
	if(winTop < 10) {
		winTop = 10; 
	}
	if(winLeft < 10) {
		winLeft = 10; 
	}
	$(win).css({ top:winTop, left:winLeft })
}

// OVERLAY
function apriOverlay(win,xNascondi){
	if(xNascondi) {
		var aNas = xNascondi.split(",");
		for (var x=0;x<aNas.length;x++) {
			$("#"+aNas[x]).css("z-index", 1000+Number(x));
		}
	}
//alert(win);
	posizionaWin(win);
	$("#x-scudo").css({ height:$(document).height(), width:$(document).width() }).fadeTo('slow',0.8); 
	//$("#x-scudo").fadeTo('slow',0.9); 
	$(win).fadeIn('fast'); 
	xDivOverOLD = win;
	xFlagScudo = 1;
}
// CHIUSURA OVERLAY
function chiudiOverlay(){
	$(xDivOverOLD).slideUp('fast');
	$("#x-scudo").fadeOut('fast');
	xDivOverOLD = '';
	xFlagScudo = 0;
}


