/**
 *	Inizializzazione componenti jQuery
 */
jQuery.noConflict(); /* Metto jQuery in compatibilità per evitare problemi con altre librerie JS eventualmente aggiunte */
jQuery(function(){

	/* Toltip agenda */
	jQuery('.tipancor').each(function(){
		jQuery(this).wrap('<div></div>');
		jQuery(this).bt({
		  positions: ['right','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: 3,
			shadowBlur: 8,
			shadowColor: 'rgba(0,0,0,0.9)',
			shadowOverlap: false,
			noShadowOpts: {strokeStyle: '#3F3B0E', strokeWidth: 1}
		});
	});
	
	/* 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{
		if (jQuery('.js_menu').length>0) {
			jQuery('.js_menu').hide();
			jQuery('.js_menu_link a').click(function(){ 
				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('.js_menu').each(function(){
														 if(jQuery(this).css('display')=='block')
														 {
															jQuery(this).slideUp();
														 }
														 });
						jQuery(this).parents('.js_menu_link').next('.js_menu').slideDown();
					}
					else
					{
						jQuery(this).parents('.js_menu_link').next('.js_menu').slideUp();
					}
				}
				return false;
			});

		}
	}catch(e){}							
	/* 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 */
});
