// GLOBALI

// struttura organizzativa
var APPEAR_TIME = 300;
var hot_spot_timein = null;

//FUNZIONI
// struttura organizzativa
var showPup_thumbnailCache = [];
function onResizeShowPup() {
	jQuery(window).unbind('resize',onResizeShowPup);
	if (jQuery('.map_container').length>0) {
		jQuery('.tree_tip,.tree_tip_cover,.tree_tip_cover_link,.tree_tip_cover_clip').hide().css({
			top:	'-10000px'							
		});
		jQuery('.tree_tip_out').hide().css({
			width:	'0px',
			height:	'0px'
		});
		var imgmap = jQuery('.map_position').find('img:eq(0)');
		var daddy = jQuery('.map_container');
		var dadleft = daddy.offset().left-0;
		daddy.css('paddingLeft',Math.floor( (jQuery(window).width() - imgmap.attr('width'))/2 -dadleft));
	}
}
function showPup(hot_spot_this) {
  //try{
	jQuery('.tree_tip__link').attr('href', '../../../foto_small.gif');
  
	var baseaddr = ''+location.href;
	baseaddr = baseaddr.split('/');
	baseaddr = 'http://'+baseaddr[2]+'/';

	var href = ''+hot_spot_this.attr('href');
		//href = href.replace('www.gruppohera.it','hera2.od.loc');	// per prove su sviluppo			############# ATTENZIONE !!!  #####################
	var text = ''+hot_spot_this.attr('alt');	//alert(href+"\n"+text);
	var title = '';
	var subtitle = '';
	if (text.indexOf(',') >= 0) {
		text = text.split(',');
		title = jQuery.trim(text[1]);
		subtitle = jQuery.trim(text[0]);
	}
	else {
		title = jQuery.trim(text);
	}

	
		jQuery('.tree_tip__title').html(title);
		jQuery('.tree_tip__subtitle').html(subtitle);
		jQuery('.tree_tip__link').hide();
		jQuery('.tree_tip__link').attr('href', href);
		
		var coords = hot_spot_this.attr('coords');
////	  try{
		if (coords!='undefined') {
			coords = coords.split(',');
			//alert(coords); // xa, ya, xb, yb
		}
////	  }catch(e){alert('errore coords: '+e+"\nhot_spot: "+hot_spot_this.nodeName);}

		var imgmap = jQuery('.map_position').find('img:eq(0)');
		//alert(jQuery(window).width() +"\n"+ imgmap.attr('width') +"\n" + Math.floor((jQuery(window).width() - imgmap.attr('width'))/2));
		var daddy = jQuery('.map_container');
		var dadleft = daddy.offset().left-0;
		daddy.css('paddingLeft',Math.floor( (jQuery(window).width() - imgmap.attr('width'))/2 -dadleft));
		hot_spot_this = jQuery('.map_position');


		var hot_spot_this__width  = Math.round((coords[2]-0) - (coords[0]-0));
		var hot_spot_this__height = Math.round((coords[3]-0) - (coords[1]-0));
		var hot_spot_this__position_left = Math.round((hot_spot_this.offset().left-0) + (coords[0]-0) );
		var hot_spot_this__position_top  = Math.round((hot_spot_this.offset().top-0)  + (coords[1]-0) );
		//alert(hot_spot_this__width+","+hot_spot_this__height+"\n"+hot_spot_this__position_left+","+hot_spot_this__position_top);		
		
		var pos = 'sx';
		if (hot_spot_this__position_left < 250) {
			pos = 'dx';
		}
		if (hot_spot_this__position_top -jQuery(window).scrollTop() < 50) {
			pos = 'dw';
		}
		if (jQuery(window).height()-0 - hot_spot_this__position_top +jQuery(window).scrollTop() < 50) {
			pos = 'up';
		}
		if (jQuery(window).width()-0 - hot_spot_this__position_left < 200) {
			pos = 'sx';
		}
		//alert(pos+"\n"+hot_spot_this__position_left+","+hot_spot_this__position_top+"\n"+(jQuery(window).height()-0 - hot_spot_this__position_top));

		if (pos == 'sx') {
			jQuery('.tree_tip').css({
									width:	'223px',
									height:	'85px'
								});
			jQuery('.tree_tip_shadow,.tree_tip_cloud').css({
									width:	'221px',
									height:	'83px'
								});
			jQuery('.tree_tip_shadow').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_nero_sx.gif)'
									});
			jQuery('.tree_tip_cloud').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_bianco_sx.gif)'
									});
			jQuery('.tree_tip_padding').css({
											padding:	'6px 20px 7px 7px'
										});
			jQuery('.tree_tip,.tree_tip_cover').css({
															top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -64)+'px',
															left:	(Math.round(hot_spot_this__position_left) -211)+'px',
															width:	(223 + Math.round(hot_spot_this__width))+'px'
														});
			jQuery('.tree_tip_cover_link').css({
														top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -62 +65)+'px',
														left:	(Math.round(hot_spot_this__position_left) -95)+'px'
													});
			jQuery('.tree_tip_cover_clip').css({
														top:	(Math.round(hot_spot_this__position_top - (85-hot_spot_this__height)))+'px',
														left:	(Math.round(hot_spot_this__position_left))+'px',
														width:	(Math.round(hot_spot_this__width))+'px',
														height:	(Math.round(85-hot_spot_this__height))+'px'
													});
			jQuery('.tree_tip_out').css({
												width:	(Math.round(jQuery(window).width()-0))+'px',
												height:	(Math.round(jQuery(window).height()-0))+'px'
											});
		}
		else if (pos == 'dx') {
			jQuery('.tree_tip').css({
									width:	'223px',
									height:	'85px'
								});
			jQuery('.tree_tip_shadow,.tree_tip_cloud').css({
									width:	'221px',
									height:	'83px'
								});
			jQuery('.tree_tip_shadow').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_nero_dx.gif)'
									});
			jQuery('.tree_tip_cloud').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_bianco_dx.gif)'
									});
			jQuery('.tree_tip_padding').css({
											padding:	'6px 7px 7px 18px'
										});
			jQuery('.tree_tip').css({ //background:'red',
												top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -64)+'px',
												left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width) -11)+'px',
												width:	(223)+'px',
												height:	(85)+'px'
											});
			jQuery('.tree_tip_cover').css({ //background:'yellow', opacity:'0.5',
													top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -64)+'px',
													left:	(Math.round(hot_spot_this__position_left) )+'px',
													width:	(223 +Math.round(hot_spot_this__width) -10)+'px',
												height:	(85)+'px'
												});
			jQuery('.tree_tip_cover_link').css({
														top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) +3)+'px',
														left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width) +117)+'px'
													});
			jQuery('.tree_tip_cover_clip').css({
														top:	(Math.round(hot_spot_this__position_top - (85-hot_spot_this__height)))+'px',
														left:	(Math.round(hot_spot_this__position_left))+'px',
														width:	(Math.round(hot_spot_this__width))+'px',
														height:	(Math.round(85-hot_spot_this__height))+'px'
													});
			jQuery('.tree_tip_out').css({
												width:	(Math.round(jQuery(window).width()-0))+'px',
												height:	(Math.round(jQuery(window).height()-0))+'px'
											});
		}
		else if (pos == 'up') {
			jQuery('.tree_tip').css({
									width:	'211px',
									height:	'96px'
								});
			jQuery('.tree_tip_shadow,.tree_tip_cloud').css({
									width:	'209px',
									height:	'94px'
								});
			jQuery('.tree_tip_shadow').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_nero_up.gif)'
									});
			jQuery('.tree_tip_cloud').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_bianco_up.gif)'
									});
			jQuery('.tree_tip_padding').css({
											padding:	'6px 7px 20px 7px'
										});
			jQuery('.tree_tip').css({ //background:'red',
												top:	(Math.round(hot_spot_this__position_top) -89)+'px',
												left:	(Math.round(hot_spot_this__position_left) )+'px',
												width:	(211)+'px',
												height:	(96)+'px'
											});
			jQuery('.tree_tip_cover').css({ //background:'yellow', opacity:'0.5',
													top:	(Math.round(hot_spot_this__position_top) -89)+'px',
													left:	(Math.round(hot_spot_this__position_left) )+'px',
													width:	(211)+'px',
													height:	(96 + Math.round(hot_spot_this__height) -5)+'px'
												});
			//alert('x '+navigator.appVersion);
			//alert(jQuery.browser.mozilla && !jQuery.browser.chrome());
			if (jQuery.browser.mozilla || jQuery.browser.chrome()) {
				jQuery('.tree_tip_cover_link').css({
														top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -44)+'px',
														left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width) +39)+'px'
													});
			}
			else {
				jQuery('.tree_tip_cover_link').css({
														top:	(Math.round(hot_spot_this__height/2 + hot_spot_this__position_top) -44)+'px',
														left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width) +9)+'px'
													});
			}
			jQuery('.tree_tip_cover_clip').css({
														top:	(Math.round(hot_spot_this__position_top))+'px',
														left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width))+'px',
														width:	(Math.round(211 - hot_spot_this__width))+'px',
														height:	(Math.round(hot_spot_this__height))+'px'
													});
			jQuery('.tree_tip_out').css({
												width:	(Math.round(jQuery(window).width()-0))+'px',
												height:	(Math.round(jQuery(window).height()-0))+'px'
											});
		}
		else if (pos == 'dw') {
			jQuery('.tree_tip').css({
									width:	'211px',
									height:	'96px'
								});
			jQuery('.tree_tip_shadow,.tree_tip_cloud').css({
									width:	'209px',
									height:	'94px'
								});
			jQuery('.tree_tip_shadow').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_nero_dw.gif)'
									});
			jQuery('.tree_tip_cloud').css({
										backgroundImage:	'url('+baseaddr+'immagini/tooltip_bianco_dw.gif)'
									});
			jQuery('.tree_tip_padding').css({
											padding:	'17px 7px 7px 7px'
										});
			jQuery('.tree_tip').css({ //background:'red',
												top:	(Math.round(hot_spot_this__position_top + hot_spot_this__height) -5)+'px',
												left:	(Math.round(hot_spot_this__position_left) )+'px',
												width:	(211)+'px',
												height:	(96)+'px'
											});
			jQuery('.tree_tip_cover').css({ //background:'yellow', opacity:'0.5',
													top:	(Math.round(hot_spot_this__position_top))+'px',
													left:	(Math.round(hot_spot_this__position_left) )+'px',
													width:	(211)+'px',
													height:	(96 + Math.round(hot_spot_this__height) -5)+'px'
												});
			jQuery('.tree_tip_cover_link').css({
														top:	(Math.round(hot_spot_this__position_top) +110)+'px',
														left:	(Math.round(hot_spot_this__position_left) +115)+'px'
													});
			jQuery('.tree_tip_cover_clip').css({
														top:	(Math.round(hot_spot_this__position_top))+'px',
														left:	(Math.round(hot_spot_this__position_left + hot_spot_this__width))+'px',
														width:	(Math.round(211 - hot_spot_this__width))+'px',
														height:	(Math.round(hot_spot_this__height))+'px'
													});
			jQuery('.tree_tip_out').css({
												width:	(Math.round(jQuery(window).width()-0))+'px',
												height:	(Math.round(jQuery(window).height()-0))+'px'
											});
		}
		
		
		
			
	
	jQuery('.tree_tip__image').attr('src', baseaddr+'immagini/foto_small.gif');
	
	var imgsrc = href;
	imgsrc = imgsrc.replace('.html','-20x.html');
	if ((showPup_thumbnailCache[imgsrc]+'') != 'undefined') {//alert('load cache: '+imgsrc);
		jQuery('.tree_tip__image').attr('src', showPup_thumbnailCache[imgsrc]);
		jQuery('.tree_tip,.tree_tip_cover,.tree_tip_cover_link,.tree_tip_cover_clip,.tree_tip_out').show();
		jQuery('.tree_tip__link').show(); // added to fix link disappear
		setTimeout(function(){
			jQuery(window).bind('resize',onResizeShowPup);
		},500);
		/*if (href == '#') {
				jQuery('.tree_tip_cover_link').hide();
			}*/
	}
	else {//alert('load NON cached '+imgsrc);
		jQuery('.ajax_loadplace').load( imgsrc, '', function(responseText, textStatus, XMLHttpRequest) {
			responseText = jQuery.trim(responseText);
			var splitted = responseText.split('#');
			responseText = splitted[0]; // url immagine			
			if (responseText.indexOf('DOCTYPE')>=0) {
				responseText = baseaddr+'immagini/foto_small.gif';
			}
			else if (responseText.indexOf('.gif')<0 && responseText.indexOf('.jpg')<0 && responseText.indexOf('.jpeg')<0 && responseText.indexOf('.png')<0) {
				responseText = baseaddr+'immagini/foto_small.gif';
			}
			showPup_thumbnailCache[imgsrc] = responseText;
			jQuery('.tree_tip__image').attr('src', responseText);
			jQuery('.tree_tip,.tree_tip_cover,.tree_tip_cover_clip,.tree_tip_out').show();
			if (splitted[1] != 1) {
				jQuery('.tree_tip__link').show();
				jQuery('.tree_tip_cover_link').show();
			}
			else{
				jQuery('.tree_tip__link').hide();
				jQuery('.tree_tip_cover_link').hide();
			}
			setTimeout(function(){
				jQuery(window).bind('resize',onResizeShowPup);
			},500);
			/*if (href == '#') {
				jQuery('.tree_tip_cover_link').hide();
			}*/
		});
	}
	
	
  //}catch(e){alert(e);}
}
	
// JQUERY
jQuery.noConflict();
jQuery.browser.chrome = function() {
	var appver = navigator.appVersion+'';
	if (appver.indexOf('Chrome')>=0) {
		return true;
	}
	return false;
}
jQuery(document).ready(function(){
	// landing
	var landing = jQuery('.contenitore_landing');
	if (landing.length > 0) {
		var tab_css = [
						{
							over:{
								backgroundColor:		'#90B5C8'
							},
							out:{
								backgroundColor:		'transparent'
							}
						},
						{
							over:{
								backgroundColor:		'#E9BF37',
								backgroundImage:		'url(immagini/cliente_sei.gif)',
								backgroundPosition:		'top right',
								backgroundRepeat:       'no-repeat'
							},
							out:{
								backgroundColor:		'transparent',
								backgroundImage:		'none'
							}
						},
						{
							over:{
								backgroundColor:		'#ECBDD1'
							},
							out:{
								backgroundColor:		'transparent'
							}
						}
					];             //sostituibili con classi, con piccole modifiche al codice 
		jQuery('.menu2 .top').each(function(tab_idx){
			jQuery(this).mouseover(function(){
				jQuery(this).find('.top_link').css(tab_css[tab_idx]['over']);
			});
			jQuery(this).mouseout(function(){
				jQuery(this).find('.top_link').css(tab_css[tab_idx]['out']);
			});
		});

	}
	// landing[end]

	// language selector [begin]
	var menulang = jQuery('.menu_language');
	if (menulang.length > 0) {
		var selected = menulang.find('.lang_selected');	//alert(selected.attr('href'));
		var otheropt = menulang.find('.lang_otheropt');	//alert(otheropt.attr('href'));

		menulang.find('.level1').remove();
		menulang.append('<select id="languagesel"><option value="'+selected.closest('a').attr('href')+'" selected="selected">'+selected.text()+'</option><option value="'+otheropt.closest('a').attr('href')+'">'+otheropt.text()+'</option></select>');
		var languagesel = jQuery('#languagesel');
		languagesel.selectbox({ debug: true });
		jQuery('#languagesel_container li').each(function(){
			jQuery(this).click(function(){
				location.href= (jQuery(this).attr('id')+'').replace('languagesel_input_','');
			});
		});
		if (! jQuery.browser.msie) {
			jQuery('.menu_container').css('marginTop','1px');
		}
	}
	// language selector [end]
	
	// aclgen_autocomp - riposizionamento per fix su IE6
	var aclgen_autocomp = jQuery('#aclgen_autocomp');
	if (aclgen_autocomp.length > 0) {
		jQuery('body').append(aclgen_autocomp);
		jQuery(window).resize(function(){
			jQuery('#aclgen_autocomp').hide();
		});
		jQuery('body').click(function(){
			jQuery('#aclgen_autocomp').hide();
		});
	}
	// aclgen_autocomp - riposizionamento per fix su IE6 [fine]

	// tabelle hover di riga
	jQuery('.tabella_download tbody').find('tr').each(function(){
		jQuery(this).hover(
			function(){
				jQuery(this).css({
								 	background:		'#EEE'
								 });
			},
			function(){
				jQuery(this).css({
								 	background:		'#FFF'
								 });
			}
		);
	});
	// tabelle hover di riga [end]

	// trasparenza tag gruppo
	if(jQuery('.tab_button').length >0) {
		jQuery('.tab_button').each(function(){
			jQuery(this).prev('div').css({opacity:'0.85',filter:'alpha(opacity=85)'});
		});
	}
	// trasparenza tag gruppo [end]

	// social netvorcs
	var socialnetworks = jQuery('.social_networks');
	if (socialnetworks.length > 0) {
		socialnetworks.show();
		jQuery('.social_networks_bar').slideUp();
		socialnetworks.click(function(){
			jQuery('.social_networks_bar').slideToggle();
		});		
		jQuery('.social_networks_list').find('a').each(function(){
			jQuery(this).click(function(){
				window.open(jQuery(this).attr('href'), "social_network_add_link");
				return false;
			});
		});
	}
	// social netvorcs [end]
	
	// struttura organizzativa - popup

	// ###################################   era COMMENTATO FINO AL RIENTRO
	// RIGA 34 sullo split relativo all'alt (ora e' virgola)
	var map_so = jQuery('#map_so');
	if (map_so.length > 0) {
		jQuery('.tree_tip_shadow').css({
											opacity:	0.4,
											filter:		'alpha(opacity=40)'
										});
		jQuery('.tree_tip_out,.tree_tip_cover,.tree_tip_cover_clip').css({
											opacity:	0.01,
											filter:		'alpha(opacity=1)'
										});
		var imgmap = jQuery('.testocenter img:eq(0)');
		var daddy = jQuery('map:eq(0)').closest('div');
		daddy.removeClass('testocenter');
		daddy.addClass('map_container');
		dadleft = daddy.offset().left-0;
		daddy.css('paddingLeft',Math.floor( ((jQuery(window).width()-0)-(imgmap.attr('width')-0))/2 -dadleft));
		daddy.wrapInner('<div class="map_position" style="width:'+imgmap.attr('width')+'px;height:'+imgmap.attr('height')+'px;"></div>');				
	
		map_so.find('area').each(function(){
			jQuery(this).mouseover(function(){
				clearTimeout(hot_spot_timein);
				var ths = jQuery(this);
				hot_spot_timein = setTimeout(function(){
					showPup(ths);
				}, APPEAR_TIME);
			});					
			jQuery('.tree_tip_out,.tree_tip_cover_clip').mouseover(function(){
				clearTimeout(hot_spot_timein);
				jQuery(window).unbind('resize',onResizeShowPup);
				jQuery('.tree_tip,.tree_tip_cover,.tree_tip_cover_link,.tree_tip_cover_clip').hide().css({
					top:	'-10000px'							
				});
				jQuery('.tree_tip_out').hide().css({
					width:	'0px',
					height:	'0px'
				});
			});			
		});
	}

	// struttura organizzativa - popup [end]
});
