/****************************************************
				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'});
				}});
			}
		});
	}
}
/****************************************************
				CORPO PRINCIPALE
****************************************************/
jQuery.noConflict();
jQuery(document).ready(function(){
	/* socials header [begin] */
	jqr_slided_popup('.jqr_headsocials_open','.jqr_headsocials', 39);
	/* socials header [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] */
	
	// fix PNG per IE6
	/*if (jQuery.browser.msie && jQuery.browser.version=='6.0') {
		try{			
			jQuery(document).pngFix(); 
		}
		catch(e){
			alert(e);
		}
	}*/
	// fix PNG per IE6 [end]
});