/*
 *
 */
document.documentElement.className += ' hasJS';
JQ = jQuery.noConflict();
JQ(document).ready(function() {
	
	JQ(".tBlank").click( function() {
		var url = JQ(this).attr('href');
		window.open(url, "", "width=1000,height=600");
		return false;
    });
    
    JQ(".tBlankAll").click( function() {
		var url = JQ(this).attr('href');
		window.open(url);
		return false;
    });
    
    if (JQ("a[rel=example_group]").length > 0) {
    	JQ("a[rel=example_group]").fancybox({
			'transitionIn'		: 'none',			
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'overlayColor'      : '#000',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Immagine ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});		
	}
    
	//gestione Menu
	/*JQ(".closeMenu").each(function() {
		//JQ(this).hide();
		JQ(this).hide();
		//JQ(this).animate({height: 0}, 10000);
	});*/
	
	// Animazione
	/*var open = false;
	JQ("#tba-UG").click(function(){
		if (!open) {
			JQ(".contBgTool").css("background", "#98000e");
			JQ(".UCTCont").css("background", "url(/skin/website/img/i/bgTool.jpg) no-repeat 0 0");
			
			JQ("a#tba-UG").css({ "background" : "white" , "color" : "black !important" });
			JQ("#menuTool li a").css({ "color" : "white" });
			
			JQ("#menuTool li a").hover(
				function() {
					JQ(this).css({
						  "color"      : "black !important"
						, "background" : "white !important"
					});
				}
				, function() {
				
				}
			);
			
			var nowWrapper = JQ(".wrapper").html();
			JQ(".wrapper").html("");			
			var xhtmlWrapper = '<div id="contShad" class="contShad"><div class="UCT-sh1"/><div class="UCT-sh2"/><div class="UCT-sh3"/><div class="UCT-sh4"/><div class="UCT-sh5"/></div>';
			xhtmlWrapper += '<div style="clear: both; position: relative; width: 990px; height: 230px; margin: 0 auto; background: url(/skin/website/img/i/bgTool.jpg) no-repeat -100px -10px;">';
			xhtmlWrapper += '<iframe height="230" frameborder="0" width="990" scrolling="no" align="middle" allowtransparency="true" marginheight="0" marginwidth="0" name="UCTFrame" id="UCTFrame" src="http://uccmb.intesys.net/UCToolbarContent.html"/>';
			xhtmlWrapper += '</div>';
			xhtmlWrapper += nowWrapper;
			xhtmlWrapper += '<div style="clear: both; height: 20px; width: 100%; ">';
			xhtmlWrapper += '<div class="closeRedLayer" style="margin: 0 auto; width: 72px; height: 20px; background: url(/skin/website/img/i/btnClose.gif) no-repeat 0 0;"></div>';
			xhtmlWrapper += '</div>';
			
			JQ(".wrapper").append(xhtmlWrapper);
			JQ(".wrapper").animate({ "height": "237px"}, 900);		
		}		
		open = true;
		
	});*/
	
	JQ(".closeRedLayer").click(function() {
		JQ(".wrapper").animate({ "height": "0px"}, 900);
		//JQ(this).fadeOut(500);
	})
	
	// Inizialize GoogleMap -> googleMap.js
	if (JQ(".menuTabs").size() > 0) {
		initialize();
	};


	JQ('#q').focus(function() {
		if (JQ(this).val() == JQ(this).attr('title')) {
			JQ(this).val('')
		}
	});
	
	JQ('#q').blur(function() {
		if (JQ(this).val() == '') {
			JQ(this).val(JQ(this).attr('title'));
		}
	});	
	
	// gestione custom SELECT
	/*
	JQ("#immobile").selectbox();	
	JQ("#regione").selectbox();
	JQ("#provincia").selectbox();
	JQ("#comune").selectbox();	
	*/	
	
	// gestione barra STAGE
	if (JQ("#iStage").length > 0) {
		var hStage  = JQ("#iStage").height();
		var cssBt   = { position: "absolute"
					  , display: "none"
					  , top: hStage + "px"
					  , left: "22px"
					  , cursor: "pointer"
					  };
		var cssTxt  = { position: "absolute"
					  , display: "none"
					  , top: hStage + "px"
					  , left: "50px"
					  , fontSize: "9px"
					  };
		var imgOut  = '/skinAdmin/entigo/widgets/ico-stage.gif';
		var imgOver = '/skinAdmin/entigo/widgets/ico-stage2.gif';
		var htmlImg = '<img src="' + imgOut + '" width="20" height="14" alt="apri/chiudi stage" id="btStage" />';
		var htmlTxt = '<p id="btStageTxt">&lt; Barra di stage</p>';

		JQ("#iStage").css("top", "-" + hStage + "px");
		JQ(".iStageDx").after(htmlImg);
		JQ("#btStage").css(cssBt).fadeIn("slow");
		JQ(".iStageDx").after(htmlTxt);
		JQ("#btStageTxt").css(cssTxt).fadeIn(500).fadeOut(2000);

		JQ("#btStage").click(function() {
			if (JQ("#iStage").css("top") == '0px') {
				JQ("#iStage").animate({top: "-" + hStage + "px"}, 500);
				JQ(this).attr("src", imgOut);
			} else {
				JQ("#iStage").animate({top: "0px"}, 300);
				JQ(this).attr("src", imgOver);
			}
		});
	};
	
	 /* TEST SELECT 
	JQ('#regione').change(function() {
		var aaa = JQ('#regione').text();
		var bbb = JQ('#regione option:selected').val();
		alert('select opt regione: ' + aaa);
		alert('selected regione: ' + bbb);
	})
	JQ('#provincia').change(function() {
		var ccc = JQ('#provincia').text();
		var ddd = JQ('#provincia option:selected').val();
		alert('select opt provincia: ' + ccc);
		alert('selected provincia: ' + ddd);
	})
	*/
	
	// gestione popolamento select
	JQ('#regione').change(function() {
		provinciaDynamic();
	})
	if (JQ('#regione option:selected').val()) {
		provinciaDynamic();
	};	
	JQ('#provincia').change(function() {
		comuneDynamic();
	})
	if (JQ('#provincia option:selected').val()) {
		comuneDynamic();
	};
	
	// gestione background voci lista immobili
	/*JQ(".voceLista").each(function() {
		JQ(this).mouseover(function() {
			//JQ(this).css('background-color','#fafafa').fade();
			JQ(this).fadeTo(100, 1);
		});
		JQ(this).mouseout(function() {
			//JQ(this).css('background-color','#ffffff');
			JQ(this).fadeTo(100, 0.8);
		});
	});*/
	
	// gestione TABS dettaglio Immobili
	if (JQ(".menuTabs").length > 0) {	
		// chiudo tutti i tab
		JQ(".datiScheda").each(function (i) {
			JQ(this).hide();
		});
		
		if (JQ("p.txtError").length > 0) {
			// apro il form
			var toOpen = JQ(".datiScheda:last");
			JQ(toOpen).fadeIn('slow');
			var toCurrent = JQ(".menuTabs li:last");
			JQ(toCurrent).addClass("current");
		} else if (JQ(".sendOk").length > 0) {
			// apro il form
			var toOpen = JQ(".datiScheda:last");
			JQ(toOpen).fadeIn('slow');
			var toCurrent = JQ(".menuTabs li:last");
			JQ(toCurrent).addClass("current");
			JQ(".sendOk").each(function() {
				JQ(this).hide();
				JQ(this).fadeIn(2000);
			});
		} else {
			// apro il primo
			var toOpen = JQ(".datiScheda:first");
			JQ(toOpen).fadeIn('slow');
			var toCurrent = JQ(".menuTabs li:first");
			JQ(toCurrent).addClass("current");
		}
		
		// click sul tab
		JQ(".menuTabs a").click(function (i) {
			JQ(".menuTabs li").removeClass("current");
			var parentTag = JQ(this).parent().get(0);
			JQ(parentTag).addClass("current");			
			JQ(".datiScheda").each(function (i) {
				JQ(this).hide();
			});
			var title = JQ(this).attr("class");
			JQ(".datiScheda."+title).each(function (i) {
				JQ(this).fadeIn('slow');
			});
		});
		
		// click su dati->contattaci
		JQ(".foto a.contattaci").click(function (i) {
			JQ(".menuTabs li").removeClass("current");
			var parentTag = JQ(".menuTabs li a.tab4").parent().get(0);
			JQ(parentTag).addClass("current");	
			JQ(".datiScheda").each(function (i) {
				JQ(this).hide();
			});
			//var title = JQ(this).attr("class");
			var title = "tab4";
			JQ(".datiScheda."+title).each(function (i) {
				JQ(this).fadeIn("slow");
			});
		});
		
	};
	
		// lightbox img gallery
	if (JQ(".lightbox").length > 0) {
		JQ(".lightbox").lightbox();		
		//JQ(".lightbox").lightBox({fixedNavigation:true});
	}
	
	// gestione IMG HOVER
	JQ(".mouseOver").each(function() {
		var src = JQ(this).attr('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length);
		JQ(this).mouseover(function() {
			JQ(this).attr('src',src.replace(extension,'-on' + extension));
		});
		JQ(this).mouseout(function() {
			JQ(this).attr('src',src);
		});
	});
	
	// gestione showError form
	JQ(".showError").each(function() {
		var href = JQ(this).attr('href');
		var href = href.replace('#', '');
		JQ(this).click(function() {
			JQ("label[for='" + href + "']").css("background","#ffffcc").css("color","#ff0000");
			return false;
		});
	});
	
	// gestione righe news
	/*JQ(".news li").each(function() {
		alert(JQ(this).children().attr('id'));
		JQ(this).children().css('color', '#000000').css('opacity', '0.7');
		JQ(this).mouseover(function (i) {
			//JQ(this).children().fadeTo("slow", 1);
			JQ(this).children().animate({opacity: "1"}, 250).animate({opacity: "0.7"}, 1000).stop();
		});
	});*/
	
	// gestione messaggio ok
	/*JQ(".sendOk").each(function() {
		var txtColor = JQ(this).css('color');
		JQ(this).hide();
		JQ(this).fadeIn(2000);
	});	*/
	
	// gestione mappa interattiva homepage
	if (JQ("#mappaHome").length > 0) {
		JQ(function() {
			JQ("#mappaHome").maphilight();
		});
		JQ("area").each(function() {
			var regione = JQ(this).attr('alt');
			/*
			JQ(this).mouseover(function() {
				JQ(".regione").text(regione).fadeIn();
			});
			JQ(this).mouseout(function() {				
				JQ(".regione").hide();
			});
			*/
			JQ(this).click(function() {
				if (JQ('#regione option[value=' + regione + ']').text()) {
					var toSelect = JQ('#regione option[value=' + regione + ']').attr('selected', 'selected');
					JQ('#regione').change();					
					JQ('#ricerca').before('<p>Loading data...</p>').fadeOut().submit();
					return false;
				} else {
					var toSelect = JQ('#regione option[value=""]').attr('selected', 'selected');
					JQ('#regione').change();
				};
			});
		});
	};
	
	// gestione mappa interattiva
	if (JQ("map").attr("id") == "mappa") {
		JQ(function() {
			JQ(".mappaIt").maphilight();
			JQ(".regione").hide();
		});
		JQ("area").each(function() {
			var regione = JQ(this).attr('alt');
			JQ(this).mouseover(function() {
				JQ(".regione").text(regione).fadeIn();
			});
			JQ(this).mouseout(function() {				
				JQ(".regione").hide();
			});
			JQ(this).click(function() {
				if (JQ('#regione option[value=' + regione + ']').text()) {
					var toSelect = JQ('#regione option[value=' + regione + ']').attr('selected', 'selected');
					JQ('#regione').change();					
					JQ('#ricerca').before('<p>Loading data...</p>').fadeOut().submit();
					return false;
				} else {
					var toSelect = JQ('#regione option[value=""]').attr('selected', 'selected');
					JQ('#regione').change();
				};
			});
		});
	};
	
	// gestione menu Print/Add/Send
	JQ("a.icoPrint").click(function() {
		window.print();
	});
	JQ("a.icoAdd").click(function() {
		var data = JQ(this).attr("rel");
		var data_array = data.split(" // ");
		createBookmarkLink(data_array[0], data_array[1])
	}); 
    
	if (!JQ("#credito_certificato_in_parte").is(":checked")) {
			JQ("#importo_certificato").parents('.rowForm').hide();
	}
	if (!JQ("#credito_certificato_si").is(":checked")) {
		JQ("#termine_pagamento").parents('.rowForm').hide();
	}
	if (!JQ("#credito_certificato_no").is(":checked")) {
		JQ("#istanza_certificazione_si").parents('.rowForm').hide();
	}
	
	JQ("#credito_certificato_in_parte").click(function() {
		if (JQ("#credito_certificato_in_parte").is(":checked")) {
			JQ("#importo_certificato").parents('.rowForm').show();
			JQ("#termine_pagamento").parents('.rowForm').hide();
			JQ("#istanza_certificazione_si").parents('.rowForm').hide();
		} 
	});
	
	JQ("#credito_certificato_si").click(function() {
		if (JQ("#credito_certificato_si").is(":checked")) {
			JQ("#importo_certificato").parents('.rowForm').hide();
			JQ("#termine_pagamento").parents('.rowForm').show();
			JQ("#istanza_certificazione_si").parents('.rowForm').hide();
		}
	});
	
	JQ("#credito_certificato_no").click(function() {
		if (JQ("#credito_certificato_no").is(":checked")) {
			JQ("#importo_certificato").parents('.rowForm').hide();
			JQ("#termine_pagamento").parents('.rowForm').hide();
			JQ("#istanza_certificazione_si").parents('.rowForm').show();
		}
	});
	
	
	// Form "Cedere i tuoi crediti verso la pubblica amministrazione2
	JQ("#regione_ente_debitore").parents('.rowForm').hide();
	JQ("#provincia_ente_debitore").parents('.rowForm').hide();
	JQ("#comune_ente_debitore").parents('.rowForm').hide();
	
	if (JQ("tipologia_ente_debitore option:selected").val() == undefined) {
		JQ("#enteconvezionato").hide();
	}
	
	if (JQ("#tipologia_ente_debitore option:selected").val() == "regione") {
		JQ("#regione_ente_debitore").parents('.rowForm').show();
		aggiornaEnteDebitore('regione');
	}
	if (JQ("#tipologia_ente_debitore option:selected").val() == "provincia") {
		JQ("#provincia_ente_debitore").parents('.rowForm').show();
		aggiornaEnteDebitore('provincia');
	}
	if (JQ("tipologia_ente_debitore option:selected").val() == "comune") {
		JQ("#comune_ente_debitore").parents('.rowForm').show();
		aggiornaEnteDebitore('comune');
	}
	
	JQ("#tipologia_ente_debitore").change(function() {
		JQ("#regione_ente_debitore").parents('.rowForm').hide();
		JQ("#provincia_ente_debitore").parents('.rowForm').hide();
		JQ("#comune_ente_debitore").parents('.rowForm').hide();		
		if (JQ("#tipologia_ente_debitore option:selected").val() == "regione") {
			JQ("#regione_ente_debitore").parents('.rowForm').show();
				aggiornaEnteDebitore('regione');
		}
		if (JQ("#tipologia_ente_debitore option:selected").val() == "provincia") {
			JQ("#provincia_ente_debitore").parents('.rowForm').show();
			aggiornaEnteDebitore('provincia');
		}
		if (JQ("#tipologia_ente_debitore option:selected").val() == "comune"){
			JQ("#comune_ente_debitore").parents('.rowForm').show();
			aggiornaEnteDebitore('comune');
		}
		if (JQ("tipologia_ente_debitore option:selected").val() == undefined) {
			JQ("#enteconvezionato").hide();
			JQ("#regione_ente_debitore").attr("selectedIndex",0);
			JQ("#provincia_ente_debitore").attr("selectedIndex",0);
			JQ("#comune_ente_debitore").attr("selectedIndex",0);
			JQ("#enteconvezionato").text('');
		}
	});
	
	
	JQ('#regione_ente_debitore').change(function() {
		aggiornaEnteDebitore('regione');
	});
	JQ('#provincia_ente_debitore').change(function() {
		aggiornaEnteDebitore('provincia');
	});
	JQ('#comune_ente_debitore').change(function() {
		aggiornaEnteDebitore('comune');
	});
	
	function aggiornaEnteDebitore(tipologia) {
		if ((JQ("#"+tipologia+"_ente_debitore option:selected").val() == undefined)
			|| (JQ("#"+tipologia+"_ente_debitore option:selected").val() == '')
			) {
			JQ("#enteconvezionato").text('');
			JQ("#enteconvezionato").hide();
			return ;
		}
		JQ("#enteconvezionato").load('/custom/dynamic-ente-convezionato'
				, {
					'tipologia' : tipologia
					,'valore' : JQ("#"+tipologia+"_ente_debitore option:selected").val()
				}
		);
		JQ("#enteconvezionato").show();

	}
	
	// Tipologia 
	
	JQ(".rememberPassword").click(function(){
		JQ("#fRemember").fadeIn();
	});
});


/*
 * funzioni popolamento select
 */
function fCheck(o) {
	//alert('fCheck() su '+o.id);
}

function provinciaDynamic() {
	JQ("#provincia").load('/returnOpt/dynamicOptions'
			, {    
					'fieldID': 'provincia' 
				  , 'tipo': 'Immobili' 
				  , 'default': 'VR' 
				  , 'value': JQ('#provincia option:selected').val() 
				  , 'xxxx': JQ('#regione option:selected').val()
			  }
			, function() {
				JQ("#provincia").change();
				//JQ("#provincia2").selectbox();
			  }
			);
}

function comuneDynamic() {
	JQ('#comune').load('/returnOpt/dynamicOptions'
			, {    
					'fieldID': 'citta' 
				  , 'tipo': 'Immobili' 
				  , 'default': 'L949'
				  , 'value': JQ('#comune option:selected').val()  
				  , 'xxxx': JQ('#provincia option:selected').val() 
			  }
			, function() {
				JQ("#comune").change();
				//JQ("#comune2").selectbox();
			  }
			  );
}

function showHideVociMenu(idVoceMenu) {
	if (document.getElementById(idVoceMenu)) {
		if (document.getElementById(idVoceMenu).style.display == 'none') {
			document.getElementById(idVoceMenu).style.display = 'block';
			JQ('#freccia'+idVoceMenu).addClass("openWithSons");
			return ;
		} 		
		document.getElementById(idVoceMenu).style.display = 'none';
		JQ('#freccia'+idVoceMenu).removeClass("openWithSons");
		return ;
	}	
}


/*
 * Gestione bookmark
 */
/*function bookmarksite(title, url){
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}*/
function createBookmarkLink(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}

