/****************************************************
				Funzioni
****************************************************/
function get_wid() {
	var wid = jQuery('.jqr_nav_img').width()-0;//+3;//+4
	if (jQuery.browser.msie) {	//adjust per IE
		//wid += 3;//+4
		//wid -= 3;
	}
	return wid;
}

function jqr_navigate_after(jqr_gallery_nav_i, jumpIdx, idx) {
	GLOBAL_jqr_can_rotate_flag = true;
	var jqr_galthumbs = jQuery('.jqr_galthumbs');
	var img_prec = jqr_galthumbs.find('img:eq('+idx+')'); // prec
	idx += jumpIdx-idx;
	var img_succ = jqr_galthumbs.find('img:eq('+idx+')'); // succ
	var swap = img_prec.attr('src');
	img_prec.attr('src', img_succ.attr('src'));
	img_succ.attr('src', swap);
	jqr_gallery_nav_i.data('idx', idx); // salvo nuovo indice
		
	var step = jQuery('.jqr_preparazione').find('.jqr_prep_foto:eq('+jumpIdx+')').closest('.jqr_prep_step').data('step');
	jQuery('.jqr_step_view').html('['+step+']');
	
}
function jqr_navigate(jumpIdx, duration) {
	if (GLOBAL_jqr_can_rotate_flag) {	// navigate funziona solo quando non � attiva l'animazione
		GLOBAL_jqr_can_rotate_flag = false;
		var jqr_gallery_nav_i = jQuery('.jqr_nav_img_i');
		var idx = jqr_gallery_nav_i.data('idx')-0;
		if (idx != jumpIdx) {	// la navigazione va solo in caso di cambio immagine effettivo
			if (jQuery('.jqr_thumb_z_tpl').css('display')!='none') {	// nascondo eventuali nuvolette
				jQuery('.jqr_thumb_z_tpl').fadeOut(500);
				jQuery('.jqr_thumb_z_cvr2').hide();
				jQuery('.jqr_thumb_z_cvr1').hide();
				jQuery('.jqr_thumb_z_content').attr('src','images/nuvoletta_i.gif');
			}
			
			var wid = get_wid();
			var off_left = - wid * jumpIdx;
			var effectDuration = 1000; // default
			if (duration!='undefined') {
				effectDuration = duration;
			}
			if (effectDuration == 0) {
				jqr_gallery_nav_i.css({marginLeft:	off_left});
				jqr_navigate_after(jqr_gallery_nav_i, jumpIdx, idx);	
			}
			else {
				jqr_gallery_nav_i.animate(
					{
						marginLeft:	off_left
					},
					{
						duration:	effectDuration,
						complete:	function(){
										jqr_navigate_after(jqr_gallery_nav_i, jumpIdx, idx);
									}
					}
				);
			}
		}
		else {
			GLOBAL_jqr_can_rotate_flag = true;
		}
	}
}
function jqr_slided_popup(openerClass, popupClass, maxHeight) {
	var jqr_openerClass = jQuery(openerClass);
	if (jqr_openerClass.length > 0) {
		jqr_openerClass.click(function(){
			var jqr_popupClass = jQuery(popupClass);
			if (jqr_popupClass.css('display')=='none') {
				jqr_popupClass.css({
					top:	jqr_openerClass.position().top+jqr_openerClass.height()+1,
					left:	jqr_openerClass.position().left
				});
				jqr_popupClass.animate({height:maxHeight},{duration:500});
			}
			else {
				jqr_popupClass.animate({height:1},{duration:500,complete:function(){
					jqr_popupClass.css({display:'none'});
				}});
			}
		});
	}
}
function jqr_slidedFromRight_popup(openerClass, popupClass, maxWidth, posOffset) {
	var jqr_openerClass = jQuery(openerClass);
	if (jqr_openerClass.length > 0) {
		jqr_openerClass.click(function(){
			var jqr_popupClass = jQuery(popupClass);
			var myleft = jqr_openerClass.position().left-0;
			if (jqr_popupClass.css('display')=='none') {				
				jqr_popupClass.css({
					top:	jqr_openerClass.position().top-0 +posOffset,
					left:	myleft -1
				});
				jqr_popupClass.animate({width:maxWidth,left:myleft-maxWidth},{duration:500});
			}
			else {
				jqr_popupClass.animate({width:1,left:myleft-1},{duration:500,complete:function(){
					jqr_popupClass.css({display:'none'});
				}});
			}
		});
	}
}

function jqr_slidedFromLeft_popup(openerClass, popupClass, maxWidth, posOffset) {
	var jqr_openerClass = jQuery(openerClass);
	if (jqr_openerClass.length > 0) {
		jqr_openerClass.click(function(){
			var jqr_popupClass = jQuery(popupClass);
			var myleft = jqr_openerClass.position().left-0 + jqr_openerClass.width();
			if (jqr_popupClass.css('display')=='none') {				
				jqr_popupClass.css({
					top:	jqr_openerClass.position().top-0 +posOffset,
					left:	myleft -1
				});
				jqr_popupClass.animate({width:maxWidth},{duration:500});
			}
			else {
				jqr_popupClass.animate({width:1},{duration:500,complete:function(){
					jqr_popupClass.css({display:'none'});
				}});
			}
		});
	}
}
/* scrolling orizzontale -start */
var GLOBAL_timer_scrollbar = null;
var GLOBAL_timer_scrollbar_count = 10;

function isScrollable() {
	if (jQuery('.ricetta_menu_collegato_int').width() < jQuery('.ricetta_menu_collegato_jq').width() -20) { // -20 è un offset per evitare scroll minimi
		return true;
	}
	return false;
}

function animated_list(selector) {
	if (jQuery('.ricetta_simile_img').length > 0) {
		jQuery('.ricetta_simile_img').each(function(){
			jQuery(this).find('img').mouseover(function(){					
					jQuery('.ricetta_descr').hide();
					jQuery('.ricetta_simile_img img').show();
					jQuery(this).hide();
					jQuery(this).closest('a').find('.ricetta_descr').css('display','block');
			});
			jQuery(this).find('.ricetta_descr').mouseout(function(){
					jQuery(this).hide();
					jQuery(this).closest('a').find('img').show();
			});			
		});		
	}

    jQuery('.ricetta_menu_collegato_jq').css('margin-left',0);
    
    jQuery('.barra_cursore').bind('drag',function( event ){
		if (isScrollable()) {
			//-25 serve come offset per il posizionamento del cursore rispetto al mouse
			//var posLeft = Math.round(jQuery('.barra_scroll').position().left -0) +25 -25;
			var offsetMin = 25;		// margine sx
			var offsetMax = jQuery('.barra_scroll').width();	// 960 -35(larghezza cursore) -25*2(margini sx e dx) +25(margine sx)	
			var posLeft = Math.round(jQuery('.barra_scroll_container').position().left -0) +offsetMin -25;    	
			var newOffsetX = event.offsetX - posLeft;
			if (newOffsetX < offsetMin) {
				newOffsetX = offsetMin;
			}
			if (newOffsetX > offsetMax) {
				newOffsetX = offsetMax;
			}    	
			jQuery( this ).css({
				left:	newOffsetX 
			});
			//document.title = posLeft + " - " + newOffsetX;
			position_lista('.ricetta_menu_collegato_int','.ricetta_menu_collegato_jq',offsetMax);
		}
    });
}

function position_lista(container_selector, selector, offsetMax)
{
	if (isScrollable()) {
		var x = jQuery('.barra_cursore').position().left -25;
		//var m = 0 - Math.round(x * 330 / 515); // 330 = 660-996 e 515 = 540-25 (rapporto tra larghezza area scrollabile e larghezza barra di scroll)
		//610 = ricetta_menu_collegato_int
		//992 = ricetta_menu_collegato_jq
		var lista_ext = jQuery('.ricetta_menu_collegato_int');
		var liextw = (lista_ext.css('width').replace('px',''))-0;
		var lista_int = jQuery('.ricetta_menu_collegato_jq');
		var liintw = (lista_int.css('width').replace('px',''))-0;
		
		var m = 0 - Math.round(x * (liintw-liextw) / (offsetMax-25)); // 330 = 660-996 e 515 = 540-25 (rapporto tra larghezza area scrollabile e larghezza barra di scroll)
		jQuery(selector).css('margin-left',m);
	}
}

/* scrolling orizzontale -end */

/* scroll ingredienti - Start */ 
/*
function ingrscrollpage(pag){
	if (jQuery('.ingr_page').length>1){
		if (jQuery('.ingr_page.current').length>0){
			if (pag=='+1'){
				if(jQuery('.ingr_page.current').next('.ingr_page').length>0){
					var nextPage=jQuery('.ingr_page.current').next('.ingr_page');
					jQuery('.ingr_page.current').removeClass('current');
					nextPage.addClass('current');
				}
			}
			else if (pag=='-1'){
				if(jQuery('.ingr_page.current').prev('.ingr_page').length>0){					
					var nextPage=jQuery('.ingr_page.current').prev('.ingr_page');
					jQuery('.ingr_page.current').removeClass('current');
					nextPage.addClass('current');
				}
			}
		}
	}
}
*/

function ingrscrollpage(pag){
	pag=pag-0;
	if (jQuery('.ingr_page .scroll_item').length>4){
		if (pag>0){
			if (jQuery('.ingr_page .scroll_item.next').length>0){
				jQuery('.ingr_page .scroll_item.current:first').removeClass('current').addClass('prev');
				jQuery('.ingr_page .scroll_item.next:first').removeClass('next').addClass('current');
			}
		}
		if (pag<0){
			if (jQuery('.ingr_page .scroll_item.prev').length>0){
				jQuery('.ingr_page .scroll_item.current:last').removeClass('current').addClass('next');
				jQuery('.ingr_page .scroll_item.prev:last').removeClass('prev').addClass('current');
			}
		}
	}
}

/* scroll ingredienti - End */

/****************************************************
				CORPO PRINCIPALE
****************************************************/
jQuery.noConflict();
jQuery(document).ready(function(){

	/* scroll ingredienti - Start */ 	
	if (jQuery('.box_pag_ingr_scroll').length>0){		
		jQuery('.ingr_right_arrow').click(function(){ ingrscrollpage('+1'); });
		jQuery('.ingr_left_arrow').click(function(){ ingrscrollpage('-1'); });
		jQuery('.scroll_item a[rel]').live('click',function(){
			var ajax_url=jQuery(this).attr('rel')
			if (ajax_url!=''){			
				jQuery('.scroll_item a[rel]').removeClass('current_item');
				jQuery(this).addClass('current_item');
				jQuery('#ajax_ingrediente').find('div.current_ingrediente').fadeOut();
				jQuery('#ajax_ingrediente').load(ajax_url,function(response,status,xhr){
						if (status)
						{
							jQuery('#ajax_ingrediente').addClass('hide_child');
							jQuery(this).find('div.current_ingrediente').fadeIn();
							jQuery('#ajax_ingrediente').removeClass('hide_child');
						}
				});	
				return false;
			}
		});
	}
	/* scroll ingredienti - End */
	
	jQuery('a.nuova_finestra').each(function(){
		jQuery(this).attr('target','_blank');
	});

	jQuery('form.quick_form_src_ric').submit(function(){
		var searchtext=jQuery(this).find('input[name=text]:eq(0)').attr('value');
		var url=jQuery(this).attr('action');
		if (searchtext!="")
			var url=url+'/(text)/'+escape(jQuery.trim(searchtext));
		window.location.href=url;
		return false;
	});

	/* scrolling menu correlato - ricette correlate */
	jQuery('.width_ricetta_menu_collegato_jq').each(function(){
		jQuery(this).parents('.ricetta_menu_collegato_jq').width(jQuery(this).width());
	});
	animated_list('.ricetta_menu_collegato_jq');
	
	//ajax menu speciale
	if (jQuery('.titolodescrizione_menuspeciale').length > 0) {
		jQuery('.jq_ajax').each(function(){
			jQuery(this).click(function(){	
				var link_ajax = jQuery(this).attr('rel');
				jQuery('.menu_mese_corpo_right')
					.html(jQuery('#jq_loader').clone().show())
					.load(link_ajax);
				return false;
			});
		});
	}
	/* seleziono direttamente la prima portata inserita nel menu */
	jQuery('.menu_mese_corpo_left ul li:first').addClass('portata_selezionata');
	jQuery('.menu_mese_corpo_left ul li a').click(function(){
		if (jQuery('.menu_mese_corpo_left ul li').hasClass('portata_selezionata')){
			jQuery('.menu_mese_corpo_left ul li').removeClass('portata_selezionata');
		}
		jQuery(this).parents('li').addClass('portata_selezionata');
	});
	jQuery('.menu_mese_corpo_left ul li').mouseover(function(){
		if (!jQuery(this).hasClass('portata_selezionata'))
			jQuery(this).addClass('portata_hover');
	});
	jQuery('.menu_mese_corpo_left ul li').mouseout(function(){
		jQuery(this).removeClass('portata_hover');
	});
	/* home ricette hover per cambiare lo sfondo */
	jQuery('#jq_ric_mese .jq_ant').mouseover(function(){
		jQuery(this).addClass('homericette_portata_hover');
	});
	jQuery('#jq_ric_mese .jq_ant').mouseout(function(){
		jQuery(this).removeClass('homericette_portata_hover');
	});
	
	
	/* plus_minus */
	jQuery('.plus_minus').click(function(){
		jQuery(this).parents('.plus_minus_box').toggleClass('close');
	});

	/* pagina ricerca menu */
	if(jQuery('select.select_cambia_numero_pagine').length>0)
	{
		jQuery('select.select_cambia_numero_pagine').next('input.submit_cambia_numero_pagine').hide();
		jQuery('select.select_cambia_numero_pagine').change(function(){
			jQuery(this).parent('form.form_cambia_numero_pagine').submit();
		});
	}
	if(jQuery('input.std_src_text').length>0)
	{
		jQuery('input.std_src_text').each(function(){
			if(jQuery(this).val()=='')
			{
				jQuery(this).val("Scrivi qui l'ingrediente");
				jQuery(this).css({'color':'#cccccc'});
			}
		});
		jQuery('input.std_src_text').focus(function(){
			jQuery(this).css({'color':'#333333'});
			jQuery(this).val('');
		});
		
		if(jQuery('form.std_src_ric').length>0)
		{
			jQuery('form.std_src_ric').submit(function(){
				jQuery(this).find('input.std_src_text').each(function(){
					if(jQuery(this).val()=="Scrivi qui l'ingrediente")
					{
						jQuery(this).val('');
					}
				});
			});
		}
		
	}

	
	/* segnalazioni sui prodotti [begin]*/
	jQuery('.orario_produzione').addClass('hide');
	jQuery('.famiglia_di_prodotti select').change(function(){
		if(jQuery(this).children('option:selected').text()=='yogurt')
			jQuery('.orario_produzione').removeClass('hide');
		else
			jQuery('.orario_produzione').addClass('hide');
	});
	/* segnalazioni sui prodotti [begin] */

	/* socials header [begin] */
	jqr_slided_popup('.jqr_headsocials_open','.jqr_headsocials', 39);
	/* socials header [end] */
	
	/* socials footer menu speciale [begin] */
	jqr_slided_popup('.jqr_footsocials_menu_open','.jqr_footsocials_menu', 39);
	/* socials footer menu speciale [end] */
	
	/* suggestions header [begin] */
	jqr_slided_popup('.jqr_headsuggestions_open', '.jqr_headsuggestions', 39);	
	/* suggestions header [end] */
	
	/* preparazione ricetta [begin] */
	/* *********************** DEVE ESSERE PRIMA DELLA GALLERY *********************** */
	var jqr_prep = jQuery('.jqr_preparazione');
	if (jqr_prep.length > 0) {
		jqr_prep.find('.jqr_prep_step').each(function(i){
			jQuery(this).data('step',i+1);
		});
		jqr_prep.find('.jqr_prep_foto').each(function(i){
			jQuery(this).data('idx',i);
			jQuery(this).click(function(){
				jqr_navigate(jQuery(this).data('idx'));
			});
		});
	}
	/* preparazione ricetta [end] */
	
	/* gallery nav [begin] */
	GLOBAL_jqr_can_rotate_flag = true;	
	var jqr_gallery_nav = jQuery('.jqr_nav_img');
	if (jqr_gallery_nav.length > 0) {
		if (jQuery.browser.msie && jQuery.browser.version=='6.0') {
				jQuery('.jqr_gallery_box').css({
																			position:		'relative'
																		});
				jQuery('.jqr_nav_img').css({
																			position:		'absolute',
																			top:				'0px',
																			left:				'18px'
																		});
		}
		
		jQuery('.jqr_nav_img_i').data('idx',0);
		jQuery('.jqr_nav_img_i').css({marginLeft:0});
		
		var wid = get_wid();
		var imgs = jqr_gallery_nav.find('.jqr_thumb_z');		
		var numimgs = imgs.length-0;
		
		jqr_navigate(numimgs-1, 0); // salto all'ultima in tempo nullo
		
		jQuery('.jqr_nav_img_i').data('len',numimgs);
		var wid_total = wid * numimgs +(numimgs*3); //+(numimgs*2); // +(numimgs*2)  per IE
		jQuery('.jqr_nav_img_i').css({width: wid_total});

		jQuery('.jqr_nav_sx').click(function(){
			var idx = jQuery('.jqr_nav_img_i').data('idx')-0;			
			idx -= 1;
			if (idx < 0) {
				idx = jQuery('.jqr_nav_img_i').data('len')-1;
			}
			jqr_navigate(idx);
		});
		jQuery('.jqr_nav_dx').click(function(){
			var idx = jQuery('.jqr_nav_img_i').data('idx')-0;			
			idx += 1;
			if (idx > jQuery('.jqr_nav_img_i').data('len')-1) {
				idx = 0;
			}
			jqr_navigate(idx);
		});
	}
	/* gallery nav [end] */
	
	/* zoom thumb [begin] */
	GLOBAL_nuvoletta_timeout = null;
	var jqr_galthumbs = jQuery('.jqr_galthumbs');
	if (jqr_galthumbs.length > 0) {
		jQuery('.jqr_thumb_z_tpl').fadeOut(1);
	
		var imgs = jqr_galthumbs.find('img');
		imgs.each(function(i){
			jQuery(this).data('zoomedContent', jQuery('.jqr_thumb_z_big:eq('+i+')').text());
			jQuery(this).data('zoomedIdx', i);
			var self = jQuery(this);
			jQuery(this).mouseover(function(){
				setTimeout(function(){	// timeout appear
					var ie6_offsetTop = 0;
					var ie6_offsetLeft = 0;
					if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
						ie6_offsetTop = 133;
						ie6_offsetLeft = 18;
					}
					var zoomed = jQuery('.jqr_thumb_z_tpl');
					var zoomimg = zoomed.find('.jqr_thumb_z_content');
					zoomimg.attr('src',self.data('zoomedContent'));	// metto l'immaginetta nella nuvola
					jQuery('.jqr_thumb_z_cvr1').data('idx',self.data('zoomedIdx'));
					jQuery('.jqr_thumb_z_cvr1').click(function(){
						jqr_navigate(jQuery(this).data('idx')-0);
					});
					zoomed.css({
						top:	self.position().top -58 +ie6_offsetTop,
						left:	self.position().left -30 +ie6_offsetLeft
					});
					
					var cover1 = jQuery('.jqr_thumb_z_cvr1');	// copertura sensibile al click
					cover1.css({
						top:	self.position().top -58 +ie6_offsetTop,
						left:	self.position().left -30 +ie6_offsetLeft
					});
					var cover2 = jQuery('.jqr_thumb_z_cvr2');	// copertura sensibile al click
					cover2.css({
						top:	self.position().top -0 +2 +ie6_offsetTop,
						left:	self.position().left -0 +5 +ie6_offsetLeft
					});
					cover1.show();
					cover2.show();
					zoomed.fadeIn(500);
				},150);
			});			
		});
		var covers = jQuery('.jqr_thumb_z_cvr1,.jqr_thumb_z_cvr2');
		covers.each(function() {
			jQuery(this).mouseover(function(){
				clearTimeout(GLOBAL_nuvoletta_timeout);
			});
			jQuery(this).mouseout(function(){
				GLOBAL_nuvoletta_timeout = setTimeout(function(){
					jQuery('.jqr_thumb_z_tpl').fadeOut(500);
					jQuery('.jqr_thumb_z_cvr2').hide();
					jQuery('.jqr_thumb_z_cvr1').hide();
					jQuery('.jqr_thumb_z_content').attr('src','images/nuvoletta_i.gif');
				},250);
			});
		});
	}
	/* zoom thumb [end] */
	
	/* socials footer [begin] */
	jqr_slidedFromRight_popup('.jqr_footsocials_open','.jqr_footsocials', 133, -10);
	/* socials footer [end] */
	
	/* suggestions footer [begin] */
	jqr_slidedFromLeft_popup('.jqr_footsuggestions_open','.jqr_footsuggestions', 133, -10);
	/* suggestions footer [end] */
	
/* scroll gallery - Start */ 	
	if (jQuery('.box_vetrina').length>0){		
		jQuery('.contenitore_scroll_right').click(function(){ ingrscrollvideo('+1'); });
		jQuery('.contenitore_scroll_left').click(function(){ ingrscrollvideo('-1'); });
	}
/* scroll gallery - End */
	
	
});

/* scroll gallery - Start */
function ingrscrollvideo(pag){
	pag=pag-0;
	if (jQuery('.box_vetrina .content-view-galleryline').length>3){
		if (pag>0){
			if (jQuery('.box_vetrina .content-view-galleryline.next').length>0){
				if (jQuery('.box_vetrina .jq_paginazione_video_start').length>0){jQuery('.box_vetrina .jq_paginazione_video_start').text(jQuery('.box_vetrina .jq_paginazione_video_start').text()-0+pag);}
				if (jQuery('.box_vetrina .jq_paginazione_video_end').length>0){jQuery('.box_vetrina .jq_paginazione_video_end').text(jQuery('.box_vetrina .jq_paginazione_video_end').text()-0+pag);}
				jQuery('.box_vetrina .content-view-galleryline.current:first').removeClass('current').addClass('prev');
				jQuery('.box_vetrina .content-view-galleryline.next:first').removeClass('next').addClass('current');
			}
		}
		if (pag<0){
			if (jQuery('.box_vetrina .content-view-galleryline.prev').length>0){
				if (jQuery('.box_vetrina .jq_paginazione_video_start').length>0){jQuery('.box_vetrina .jq_paginazione_video_start').text(jQuery('.box_vetrina .jq_paginazione_video_start').text()-0+pag);}
				if (jQuery('.box_vetrina .jq_paginazione_video_end').length>0){jQuery('.box_vetrina .jq_paginazione_video_end').text(jQuery('.box_vetrina .jq_paginazione_video_end').text()-0+pag);}
				jQuery('.box_vetrina .content-view-galleryline.current:last').removeClass('current').addClass('next');
				jQuery('.box_vetrina .content-view-galleryline.prev:last').removeClass('prev').addClass('current');
			}
		}
	}
}
/* scroll ingredienti - End */
