/**
 *	Inizializzazione componenti jQuery
 */

function consoleLog(str)
{
	if ( ! jQuery.browser.msie ) console.log(str);
	else if ( ipOD == '93.63.225.36' ) alert(str)
}

function closePopUp(classe)
{
	jQuery('.'+classe).btOff();
	if ( jQuery.cookie('popup_novita') == null )
	{
		jQuery.cookie('popup_novita', 'true', {
			expires:	30,
			domain: 	'provincia.tn.it',
			path: 		'/'
		});
	}
	return false;
}

function setCookieBoxHome(str)
{
//	setto la scadenza ad un'ora dalla creazione del cookie
	var date = new Date();
	date.setTime(date.getTime() + (60 * 60 * 1000));	// 60 min
//	date.setTime(date.getTime() + (15 * 1 * 1000));		// 15 sec
//	alert(date.getTime())
	jQuery.cookie('boxHome', str, {
		expires:	date,
		domain: 	'provincia.tn.it',
		path: 		'/'
	});
}


jQuery.noConflict(); /* Metto jQuery in compatibilità per evitare problemi con altre librerie JS eventualmente aggiunte */
jQuery(function(){

	// Arpri/chiudi
	jQuery('.dettagli').each(function(i){
	/*	if(i>0) // nascondo tutti tranne il primo
		{
			jQuery(this).parents('div').next('.jq_dettagli').toggle();
			jQuery(this).toggleClass('dettagli_giu');
		} */
		jQuery(this).click(function(){
			jQuery(this).parents('div').next('.jq_dettagli').toggle();
			jQuery(this).toggleClass('dettagli_giu');
		});
	});
	jQuery('.js_img_link').css('display','block');
	jQuery('.dettagli_spec').click(function(){ // Argomenti: per la categoria speciale "primo piano" l'apri/chiudi collassa tutto il blocco nella fascia viola
		jQuery(this).toggleClass('dettagli_giu');
		jQuery('.jq_li_spec').each(function(i){
			if(i==0) // nascondo tutti tranne il primo
			{
				jQuery(this).find('.box_categorizzato_catspec').toggle();
				jQuery(this).find('.js_img_link').toggle();
			}
			else
			{
				jQuery(this).toggle();
			};
		})
	});

	/* Toltip agenda */
	jQuery('.tipancor').each(function(){
		jQuery(this).wrap('<div></div>');
		jQuery(this).bt({
			positions: ['bottom','bottom'],
			trigger: ['mouseover', 'click'],
			contentSelector: "jQuery('#text_'+jQuery(this).attr('id')+':eq(0)').html()",
			closeWhenOthersOpen: true,
			width: 220,
			centerPointX: .9,
			spikeLength: 25,
			spikeGirth: 10,
			padding: 8,
			cornerRadius: 5,
			fill: '#FFFFFF',
			strokeStyle: '#3F3B0E',
			strokeWidth: 1,
			shadow: true,
			shadowOffsetX: 3,
			shadowOffsetY: 8,
			shadowBlur: 8,
			shadowColor: 'rgba(0,0,0,0.9)',
			shadowOverlap: false,
			noShadowOpts: {strokeStyle: '#3F3B0E', strokeWidth: 1}
		});
	});

//	Toltop iniziali
	function agenda_barra_balloon_open()
	{
		jQuery('.agenda_barra').bt({
			positions: 'bottom',
			trigger: 'none',
			contentSelector: "jQuery('.agenda_barra_balloon')",
			clickAnywhereToClose: false,
			closeWhenOthersOpen: false,
			width: 220,
			centerPointX: .9,
			spikeLength: 25,
			spikeGirth: 10,
			padding: 8,
			cornerRadius: 5,
			fill: '#DFE9F0',
			strokeStyle: '#265380',
			strokeWidth: 1,
			shadow: true,
			shadowOffsetX: 5,
			shadowOffsetY: 5,
			shadowBlur: 8,
			shadowColor: 'rgba(38,83,128,0.5)',
			shadowOverlap: false,
			noShadowOpts: {strokeStyle: '#3F3B0E', strokeWidth: 1}
		});
		jQuery('.agenda_barra').btOn();
		jQuery('.agenda_barra_balloon').closest('.bt-wrapper').attr('id','agenda_barra_balloon_box');
		jQuery('#agenda_barra_balloon_box .bt-content').css('padding','8px 0');
		jQuery('#agenda_barra_balloon_box .bt-content').css('width',(jQuery('#agenda_barra_balloon_box .bt-content').width()+16)+'px');
		jQuery('#agenda_barra_balloon_box').hide();
		jQuery('#agenda_barra_balloon_box').fadeIn(function(){clearInterval(agenda_barra_balloon_init);});
	}

	function form_cerca_balloon_open()
	{
		jQuery('.form_cerca').bt({
			positions: 'bottom',
			trigger: 'none',
			contentSelector: "jQuery('.form_cerca_balloon')",
			clickAnywhereToClose: false,
			closeWhenOthersOpen: false,
			width: 220,
			centerPointX: .9,
			spikeLength: 25,
			spikeGirth: 10,
			padding: 8,
			cornerRadius: 5,
			fill: '#DFE9F0',
			strokeStyle: '#265380',
			strokeWidth: 1,
			shadow: true,
			shadowOffsetX: 5,
			shadowOffsetY: 5,
			shadowBlur: 8,
			shadowColor: 'rgba(38,83,128,0.9)',
			shadowOverlap: false,
			noShadowOpts: {strokeStyle: '#3F3B0E', strokeWidth: 1}
		});
		jQuery('.form_cerca').btOn();
		jQuery('.form_cerca_balloon').closest('.bt-wrapper').attr('id','form_cerca_balloon_box');
		jQuery('#form_cerca_balloon_box .bt-content').css('padding','8px 0');
		jQuery('#form_cerca_balloon_box .bt-content').css('width',(jQuery('#form_cerca_balloon_box .bt-content').width()+16)+'px');
		jQuery('#form_cerca_balloon_box').hide();
		jQuery('#form_cerca_balloon_box').fadeIn(function(){clearInterval(form_cerca_balloon_init);});
	}

/*	if ( jQuery.cookie('popup_novita') == null )
	{
		if ( jQuery('.agenda_barra_balloon').length > 0 )
		{
			agenda_barra_balloon_init = setInterval(function(){
				agenda_barra_balloon_open();
			}, 2000);
		}
		if ( jQuery('.form_cerca_balloon').length > 0 )
		{
			form_cerca_balloon_init = setInterval(function(){
				form_cerca_balloon_open();
			}, 3000);
		}
	} */

	/* credits Window - START 
	if(jQuery('a.credits_link[rel]').length>0) {
		jQuery('a.credits_link[rel]').click(function(){
			link_ajax=jQuery(this).attr('rel');
			jQuery('body').append('<div class="window_credits visibility_hidden"><div class="window_credits_padding"><div class="window_credits_bordo"><div class="window_credits_header"><div class="window_credits_tit"><a href="#" class="close_credits"><span class="skip">chiudi</span></a><div class="clear">&nbsp;</div></div></div><div class="clear">&nbsp;</div><div class="credits_ajax"></div></div></div></div>');			
			var link_top = jQuery(this).offset().top-0;
			var link_left = jQuery(this).offset().left-0;

			var win_h=jQuery('body').height()-0;
			var win_w=jQuery('body').width()-0;
			jQuery('.credits_ajax').load(link_ajax, function(response,status,xhr){
				var mgl=0;
				var mgt=0;
				var mg_w=jQuery('.window_credits').width()-0;
				var mg_h=jQuery('.window_credits').height()-0;
				mgl=win_w-mg_w-10;
				if (mgl>link_left) { mgl=link_left;}
				mgt=win_h-mg_h-0;
				if (link_top<mg_h){mgt=link_top;}
				else {mgt=link_top-mg_h;}
				mgt -= 5; / * offset verticale per distanziarlo un po dal link dei crediti * /
				jQuery('.window_credits').css({'top': mgt+'px','left': mgl+'px' });
				jQuery('.window_credits').toggleClass('visibility_hidden');
				if (jQuery('.window_credits').length>0)
				{
				jQuery('.close_credits').click(function(){
					jQuery('.window_credits').remove();
					return false;
				});
				}
			});
			return false;
		});
	}*/
	if(jQuery('a.credits_link[rel]').length>0) {
		jQuery('a.credits_link[rel]').click(function(){
			link_ajax = jQuery(this).attr('rel').replace(/____/g,'/');
			jQuery('body').append('<div class="window_credits visibility_hidden"><div class="window_credits_padding"><div class="window_credits_border"><div class="window_credits_header"><div class="window_credits_tit"><a href="#" class="close_credits"><span class="skip">chiudi</span></a><div class="clear">&nbsp;</div></div></div><div class="clear">&nbsp;</div><div class="credits_ajax"></div><div class="clear">&nbsp;</div></div></div></div>');
			var link_top	= jQuery(this).offset().top-0;
			var link_left	= jQuery(this).offset().left-0;
			var link_width  = jQuery(this).width();
			var link_pad	= jQuery(this).css('padding-left').replace('px','')-0;
			var win_h = jQuery('body').height()-0;
			var win_w = jQuery('body').width()-0;
			jQuery('.credits_ajax').load(link_ajax, function(response,status,xhr){
				n=0;
				waitingForResult = setInterval(function(){
					if(status=='success'){
						clearInterval(waitingForResult);
						var mgl=0;
						var mgt=0;
						var mg_w=jQuery('.window_credits').width()-0;
						var mg_h=jQuery('.window_credits').height()-0;
						/* mgl=win_w-mg_w-10; */
						mgl=Math.floor(link_left+(link_width/2)-(mg_w/2)) +link_pad;
						if ((mgl+mg_w)>win_w){
							mgl=win_w-mg_w-5;
						}
						if (mgl<0){
							mgl=5;
						}
						/*if (mgl>link_left) { mgl=link_left;}*/
						mgt=win_h-mg_h-0;
						if (link_top<mg_h){
							mgt=link_top;
						}
						else {
							mgt=link_top-mg_h;
						}
						jQuery('.window_credits').css({'top': mgt+'px','left': mgl+'px' });
						jQuery('.window_credits').toggleClass('visibility_hidden');
						/* modifica target blank */
						jQuery('a.nuovafinestra').click(function(){
								jQuery(this).attr('target','_blank');
						});
						jQuery('a.nuovafinestra_inline').click(function(){
								jQuery(this).attr('target','_blank');
						});
						/* fine target blank */
						if (jQuery('.window_credits').length>0)
						{
							jQuery('.close_credits').click(function(){
								jQuery('.window_credits').remove();
								return false;
							});
						}
					}
				}, 500);
			});
			return false;
		});
	}

	/* credits Window - END */

	/* Tendine Newsletter START */
		/*  caricamento pagina */
		jQuery('.js_isc_a').each(function(){	// nascondo i dati non compilati
			if(jQuery(this).find('input,select').val() =='')
				jQuery(this).addClass('visibility_hidden_sms');
		});
		jQuery('.js_isc_s').each(function(){ //mostro le tendine opzioni principarli (strutture, volontariato, altro) in base alla scelta del profilo
			var val_ab = null;
			var val = jQuery(this).attr("value"); //value della option selezionata	
			val_ab = jQuery(this).find('option[value="'+val+'"]').attr("class"); // classe della option selezionata		
			jQuery('.a_'+val_ab).removeClass('visibility_hidden_sms'); // rendo visibile la sottotendina corrispondente
		});									
		jQuery('.js_isc_a select .js_isc_altro').each(function(){ //abilito altro, se uno dei sottomenù è su altro
			if(jQuery(this).attr("value") == jQuery(this).parent('select').attr("value"))
				jQuery('.a_js_isc_altro').removeClass('visibility_hidden_sms'); // rendo visibile la sottotendina corrispondente
		});
		/* al change di profilo  o dei sottomenù */
		jQuery('.js_isc_s').change(function(){ //change di profilo
			var val_ab = null;
			var val = jQuery(this).attr("value"); //value della option selezionata
			val_ab = jQuery(this).find('option[value="'+val+'"]').attr("class"); // classe della option selezionata
			
			jQuery('.js_isc_a').each(function(){ // svuoto i cambi aggiuntivi e li nascondo
				jQuery(this).find('input,select').val('');
				if(jQuery(this).hasClass('visibility_hidden_sms')=='false');
					jQuery(this).addClass('visibility_hidden_sms');
			});								  
			jQuery('.a_'+val_ab).removeClass('visibility_hidden_sms'); // rendo visibile la sottotendina corrispondente
		});
		jQuery('.js_isc_a select').change(function(){ //change dei sottomenù
			var val_ab = null;
			var val = jQuery(this).attr("value"); //value della option selezionata
			val_ab = jQuery(this).find('option[value="'+val+'"]').attr("class"); // classe della option selezionata
			if(val_ab =='js_isc_altro')
				jQuery('.a_js_isc_altro').removeClass('visibility_hidden_sms'); // rendo visibile la sottotendina corrispondente
			else
			{
				jQuery('.a_js_isc_altro').find('input').val('');
				jQuery('.a_js_isc_altro').addClass('visibility_hidden_sms'); // rendo visibile la sottotendina corrispondente
			}
		});
		
	/* Tendine Newsletter END */
	
	/* Rotazione News */
	var age_tot = jQuery('li.js_agenda').length;
	var num = null;
	jQuery('li.js_agenda').each(function(i){
		if(i>=1)
			jQuery(this).addClass('jq_rotation_nascosto');
	});
	setInterval( function(){
					if (num==null) {
						num = 0;
					}
					if(num==age_tot)
						num = 0;
					rotazione(num);
					num++;
				}, 10*400 );//}, 10*1000 );

	function rotazione(num) {
		jQuery('li.js_agenda').each(function(i){
			if(i==num) 
				jQuery(this).removeClass('jq_rotation_nascosto');
			else
				jQuery(this).addClass('jq_rotation_nascosto');
		});
	}
	
	/* Rotazione Banner */
	var ban_tot = jQuery('.js_box_banner').length;
	var num2 = null;
	jQuery('.js_box_banner').each(function(i){		
		if(i>=1) {
			jQuery(this).addClass('jq_rotation_nascosto');
		}
	});
	setInterval( function(){
					if (num2==null) {
						num2 = 0;
					}
					if(num2==ban_tot) {
						num2 = 0;
					}
					rotazione2(num2);
					num2++;
				}, 10*400 );//}, 10*1000 );

	function rotazione2(num2) {
		jQuery('.js_box_banner').each(function(i){
			if(i==num2) {
				jQuery(this).removeClass('jq_rotation_nascosto');
			}
			else {
				jQuery(this).addClass('jq_rotation_nascosto');
			}
		});
	}
	/* menù chiusi a soffietto - START */
	try{
	//	tolgo la classe 'js_sf_home' da tutti tranne il primo
		var ginetti = jQuery('.js_menu_link .js_sf_home');
		if (ginetti.length > 0)
		{
			var gino = 0;
			var indxGinetti = 0;
			var locationHref = unescape(location.href.toLowerCase());
			var paramDaFuori = jQuery.trim(locationHref);
			var cancellettoPos = paramDaFuori.lastIndexOf('#');
			if ( cancellettoPos != -1 ) paramDaFuori = paramDaFuori.substring((cancellettoPos+1));
			var interrogativoPos = paramDaFuori.lastIndexOf('?');
			if ( interrogativoPos != -1 ) paramDaFuori = paramDaFuori.substring(0,interrogativoPos);
			if ( paramDaFuori.indexOf('http') != -1 ) paramDaFuori = '';
		//	consoleLog(refTabOpen)
		//	consoleLog(paramDaFuori+" | "+cancellettoPos+" | "+interrogativoPos);
			txtTitle = '';
			cookieBoxHome = jQuery.cookie('boxHome');
			if ( cookieBoxHome != null ) cookieBoxHome = jQuery.trim(cookieBoxHome.toLowerCase());
			spegniQuesto = true;
			breakOthers = false;
			arrTitles = new Array();
			ginetti.each(function() {
				txtTitle = jQuery.trim(jQuery(this).find('a').text().toLowerCase());
				arrTitles.push(txtTitle);
			});
			ginetti.each(function() {
				spegniQuesto = true;
				if ( ! breakOthers )
				{
					txtTitle = jQuery.trim(jQuery(this).find('a').text().toLowerCase());
				//	consoleLog('--- ('+indxGinetti+') '+paramDaFuori+' | '+cookieBoxHome+' | '+refTabOpen+' | '+txtTitle+' ---')
					if ( (','+arrTitles.join(',')+',').indexOf(','+paramDaFuori+',') != -1 )
					{
					//	consoleLog('--- ('+indxGinetti+') parametro esterno ---')
						if ( txtTitle == paramDaFuori )
						{
						//	consoleLog('--- ('+indxGinetti+') da fuori: '+paramDaFuori+' ---')
							spegniQuesto = false;
							breakOthers = true;
						}
					}
					else
					{
					//	consoleLog('--- ('+indxGinetti+') non da fuori ---')
						if ( (','+arrTitles.join(',')+',').indexOf(','+cookieBoxHome+',') != -1 )
						{
						//	consoleLog('--- ('+indxGinetti+') cookie ---')
							if ( txtTitle == cookieBoxHome )
							{
							//	consoleLog('--- ('+indxGinetti+') cookie: '+cookieBoxHome+' ---')
								spegniQuesto = false;
								breakOthers = true;
							}
						}
						else
						{
						//	consoleLog('--- ('+indxGinetti+') no cookie ---')
							if ( (','+arrTitles.join(',')+',').indexOf(','+refTabOpen+',') != -1 )
							{
							//	consoleLog('--- ('+indxGinetti+') da db ---')
								if ( txtTitle == refTabOpen )
								{
								//	consoleLog('--- ('+indxGinetti+') da db: '+refTabOpen+' ---')
									spegniQuesto = false;
									breakOthers = true;
								}
							}
							else
							{
							//	consoleLog('--- ('+indxGinetti+') non da db ---')
								if ( indxGinetti == 0 )
								{
								//	consoleLog('--- ('+indxGinetti+') primo della lista ---')
									spegniQuesto = false;
									breakOthers = true;
								}
							}
						}
					}
				}
				if ( spegniQuesto )
				{
					jQuery(this).removeClass('js_sf_home');
				}
				indxGinetti++;
			});
		}
	//	console.log(jQuery('.js_menu'))
		if (jQuery('.js_menu').length > 0) {
		//	console.log(111)
			jQuery('.js_menu').hide();
			if (jQuery('.js_menu_blocco').length > 1) { // se ho il blocco menu sono in home...
			//	jQuery('.js_menu_blocco').css('display','block');
				jQuery('.div_nav_int_float_cx .js_menu_blocco').css('display','block');
				jQuery('.js_menu_blocco').html( jQuery('.js_sf_home').parents('.js_menu_link').next('.js_menu').html() );
				jQuery('.js_menu_link a').click(function(){
					setCookieBoxHome(jQuery(this).text());
					jQuery('.js_menu_link .js_sf_home').removeClass('js_sf_home');		
					//jQuery('.js_menu_link a.mattivo').removeClass('mattivo');					
					if (jQuery(this).parents('.js_menu_link').next('.js_menu').length > 0)
					{
						jQuery(this).parents('.default').addClass('js_sf_home');
						//jQuery(this).addClass('mattivo');
						jQuery('.js_menu_blocco').html( jQuery(this).parents('.js_menu_link').next('.js_menu').html() );
					}
					return false;
				});
			}
			else { // se non sono in home
				jQuery('.js_menu_link a').click(function(){
					setCookieBoxHome(jQuery(this).text());
					// quelli pre-aperti non hanno la classe js_menu sul secondo div del li. la aggiungo per mantenere la funzionalità
					if ( ! jQuery('.js_menu_link a.mattivo').parents('.js_menu_link').next('div').hasClass('js_menu')) {
						jQuery('.js_menu_link a.mattivo').parents('.js_menu_link').next('div').addClass('js_menu');
					}
					jQuery('.js_menu_link a.mattivo').parents('.js_menu_link').next('.js_menu').slideUp();
					jQuery('.js_menu_link a.mattivo').removeClass('mattivo');					
					if (jQuery(this).parents('.js_menu_link').next('.js_menu').length > 0)
					{
						if(jQuery(this).parents('.js_menu_link').next('.js_menu').css('display') != 'block')
						{
							jQuery(this).addClass('mattivo');
							jQuery(this).parents('.js_menu_link').next('.js_menu').slideDown();
						}
						else
						{							
							jQuery(this).parents('.js_menu_link').next('.js_menu').slideUp();
							jQuery(this).removeClass('mattivo');
						}
					}
					return false;
				});
			}
		}
	}catch(e){
	//	console.log(e.message)
	}
	/* menù chiusi a soffietto - END */

	/* il fix PNG per IE6 */
		if (jQuery.browser.msie && jQuery.browser.version=='6.0') {
			DD_belatedPNG.fix('img');
			/*jQuery("img[src$=png]").pngfix();
			jQuery(".jq_box_selezionato").pngfix({sizingMethod:'scale'}); */
		}
	/*fix PNG per IE6 [end]*/
	

	/* galleria video con pop-up START */
	jQuery(document).ready(function(){
		if (jQuery('.jq-map-imm').length>0) {
			jQuery('area').each(function(idx){
				var corner_where = ['bottomLeft','bottomLeft','bottomLeft','bottomMiddle','bottomMiddle','bottomMiddle','bottomRight','bottomRight','bottomRight'];
				var x_offsets = [-30,-30,-35,-150,-160,-165,-280,-280,-270];
				var y_offsets = [-265,-260,-245,-245,-265,-295,-270,-275,-240];
				var myalt = ''+jQuery(this).attr('alt');
				myalt = '<div style="font-size:0.8em;"><strong>'+myalt.replace(' - ',"</strong><br/>")+'</div>';//+'<br>'+(coordsplit[3]-coordsplit[1]+110)
				jQuery(this).removeAttr('alt')			
				jQuery(this).qtip({
					content:	myalt,
					show:		'mouseover',
					hide:		'mouseout',
					position:	{
									adjust:		{	
													x:			x_offsets[idx],
													y:			y_offsets[idx]
												}
								},
					style:		{
									background:	'#202020',
									border:		{
													width: 1,
													radius: 4,
													color: '#AC8F5A'
												},
									tip:		{	// Now an object instead of a string
													corner:	corner_where[idx],	// We declare our corner within the object using the corner sub-option
													color:	'#AC8F5A',
													size:	{
																x:	24,		// Be careful that the x and y values refer to coordinates on screen, not height or width.
																y:	24		// Depending on which corner your tooltip is at, x and y could mean either height or width!
															}	
												},
									name: 'dark' 	// Inherit from preset style
								},
					api:		{
									onShow:		function() {
													jQuery('.qtip-contentWrapper').css({
														opacity:	0.9,
														filter:		'alpha(opacity=90)'
													});
												}
								}
				});
			});
		}

		// opacita bottone play lista video
		jQuery('.jq_play_button').css({
			'opacity':	0.75,
			'filter':	'alpha(opacity=75)'
		});
	});

	/* galleria video con pop-up END */
});

G_maxHeight_calculated = false;
G_maxHeight = 0;
G_indx = 0;
function getRealMaxHeightItems()
{
/*	consoleLog(allItemSlider.length)
//	var maxHeight = 0;
	allItemSlider.each(function(i) {
		var thisItem = jQuery(this);
		setTimeout(function(){
			if ( ipOD == '93.63.225.36' ) thisItem.css('border','1px solid red');
			consoleLog(i+" : "+thisItem.height())
			if ( thisItem.height() > G_maxHeight )
				G_maxHeight = thisItem.height();
		},1);
	});
//	return maxHeight + 10;
	setTimeout(function(){
		G_maxHeight_calculated = true;
	}, allItemSlider.length*5 ); */

	var allItemSlider = jQuery('#slider1 .panel>div');
	allItemSlider.each(function(i) {
		var thisItem = jQuery(this);
		setTimeout(function()
		{
		//	consoleLog(i+" | "+(allItemSlider.length-1)+" : "+thisItem.height())
			if ( thisItem.height() > G_maxHeight )
				G_maxHeight = thisItem.height();
			if (G_indx==allItemSlider.length-1)
			{
			//	consoleLog('go');
				G_maxHeight_calculated = true;
			}
			G_indx++;
		},1);
	});
//	return G_maxHeight + 10;
}
