$(document).ready(function() {
 	$('#menu_prodotti').accordionmenu({
		speed: 500,
		openedCategoryClass: 'categoriaAperta',
		selectedCategoryClass: 'categoriaSelezionata'
	});  
	//rollover per i prodotti costituiti da 2 link separati
	$('#menu_prodotti ul a').mouseover(function(){
		$(this).parents('li:first').addClass('over');
	}).mouseout(function(){
	   $(this).parents('li:first').removeClass('over'); 
	});
	
	//gestione rollover menu fratelli
	$('#submenu_fratelli li').mouseover(function(){
		$(this).addClass('over');
	}).mouseleave(function(){
		$(this).removeClass('over');
	})
	
	
	//creo il livello in cui mostrare gallery o testi
	$('body').append('<div class="overlay"></div>');                                
	$('.overlay').append('<div class="btn_close">close</div>');  
	
	//gestione fotogallery nella sezione fratelli                                                                   
	$('.overlay').prepend($('.gallery').html());
	$('.gallery').html('<div class="content_module_rowgallery_open">photogallery</div>');
	$('.content_module_rowgallery_open').click(function(){ 
		$('#video_embedded').css('visibility','hidden');
		$('.overlay').fadeIn();
	});
	$('.overlay .content_module_rowgallery').rowgallery({
		thumbs_to_move: 1
	}) 
	
	//gestione testo aggiuntivo nelle schede prodotto   
	if($(".class-product").hasClass('eng-GB')){
 	 $('#scheda_prodotto .long_description').prepend('<div class="more_info">more info...</div>');
	}else{
 	 $('#scheda_prodotto .long_description').prepend('<div class="more_info">per saperne di pi&ugrave;...</div>');	
	}
	$('#scheda_prodotto .content').append('<div class="btn_close">close</div>');
	//$('#scheda_prodotto .content').css('top','5000px')
	$('#scheda_prodotto .content .design').jScrollPane({
		scrollbarWidth: 5
	});
	$('#scheda_prodotto .content').hide();                      
	$('#scheda_prodotto .content').css('top','-470px')
	$('#scheda_prodotto .more_info').click(function(){                     
		$('#scheda_prodotto .content').fadeIn();
	});
	
	$('.btn_close').click(function(){
		$('#video_embedded').css('visibility','visible');
		$(this).parent().hide();
	});
	
	
	//gestione striscia nelle pagine dell'animazione
	//1) sposto la striscia a fine pagina 
	$('.body-mainmenuitem144').append('<div class="band-trasp"></div>');
	$('.body-mainmenuitem144 .band-trasp:first').remove();
	//2) inserisco il menu prodotti nella fascia
	$('#menu_portate_il_cuore').appendTo('.band-trasp');
	$('#menu_portate_il_cuore').show();
	//3) se sono in una pagina delle animazioni nascondo la fascia
   	var band_is_moving = false;
	$('.band-trasp .internal').parents('.band-trasp').each(function(){
	  	$(this).css('height','40px');
		$(this).css('margin-top','175px');
		//$('.internal', this).prepend('<p class="open">Portate il cuore...</p>')
		//$('h1', this).hide();
		$(this).mouseenter(function(){
		   	//console.log('apri');
			if (band_is_moving) return;
			band_is_moving = true;
			$(this).animate({
				height: '155px',
				marginTop: '60px'
			},{complete:function(){
				band_is_moving = false;
				//$(this).find('h1').show();
				//$(this).find('.open').hide();
			}
			});
		}).mouseleave(function(){
			//console.log('chiudi');             
			if (band_is_moving) return;
			band_is_moving = true;
			$(this).animate({
				height: '40px',
				marginTop: '175px'
			},{complete:function(){
			    band_is_moving = false;
				// $(this).find('h1').hide();
				// $(this).find('.open').show(); 
			}
			})               
		});  
	})
	
	//4) aggiungo le azioni                                  
	$('#menu_portate_il_cuore .docg').click(function(){
		if ($(this).hasClass('selected')) return;
	   	$(this).find('.content').animate({
			top: "-100px"
		});
	}).mouseleave(function(){
		if ($(this).hasClass('selected')) return;
		$(this).find('.content').animate({
			top: "0"
		});
	});
	
// =======================
// = 	gestione possedere =
// =======================
	
	$('#regioni_list select option:first').attr('selected','selected');
	$('#provincie_list select option:first').attr('selected','selected');
	$('#regioni_list select').change(function(){
		$('#provincie_list label').hide();
		$('#provincie_list select').hide();
		var ajax_url = '/layout/set/ajax/content/view/full/' + $(this).val() + '/(ajax)/1'; 
		$.get(ajax_url, function(data){ 
			var re = /[\t\n\r]/g;
			var my_script = data.replace(re, '');
			eval(my_script); 
			if (jQuery.browser.flash) {
				$('#trade_list').jScrollPane({
					scrollbarWidth: 5
				})
			}else{
				$('#trade_list').append('<div id="two_finger_scrolling"></div>');
			}
		});
	});
	$('#provincie_list select').change(function(){ 
		if($(this).val=='') return;
		var ajax_url = '/layout/set/ajax/content/view/full/' + $(this).val() + '/(ajax)/1'; 
		$.get(ajax_url, function(data){
			var re = /[\t\n\r]/g;
			var my_script = data.replace(re, '');
			//console.log(my_script);
			eval(my_script);
			if (jQuery.browser.flash) {
				$('#trade_list').jScrollPane({
					scrollbarWidth: 5
				}) 
			}else{
				$('#trade_list').append('<div id="two_finger_scrolling"></div>');
			}
		});
	});
    if (jQuery.browser.flash) {
		$('#trade_list').jScrollPane({
			scrollbarWidth: 5
		});
	}else{
		$('#trade_list').append('<div id="two_finger_scrolling"></div>');
	} 
	
// ====================
// = 	gestione sapere =
// ==================== 
	if (jQuery.browser.flash) {
		$('#news_list').jScrollPane({
			scrollbarWidth: 5
		});         
		$('#article_body').jScrollPane({
			scrollbarWidth: 5
		});
	}else{
		$('#news_list').append('<div id="two_finger_scrolling"></div>');
		$('#article_body').append('<div id="two_finger_scrolling"></div>');
	}                              
	
// =====================================
// = visualizzazione ultime news in hp =
// =====================================
	$('#latest_news').rowgallery({
		thumbs_to_move: 1
	}); 
	
	
	// =============================
	// = nasconde il banner eventi =
	// =============================                 
	$('#banner_eventi_btn_chiudi').click(function(){
		$('#banner_eventi').fadeOut();
	})
});  

//funzioni per la gestione dell'homepage
function showHomepageContents(){
   $('#post_animazione_hp').fadeIn();
} 
function hideAnimazioneHp(){
	$('#swf_hp').css('width','0');                          
	$('#latest_news').fadeIn();
}

//apertura dei link in blank
$('[class~=_blank]').attr('target','_blank');

