	
	var tg_intervals = new Array();

	function tg_gallery_unload(){
			
		$(".tg_image_arrow a").unbind('mouseover mouseout click');
		$('.tg_miniature > a').unbind('mouseover mouseout click');
		$(".tg_miniature_cont a.tg_nav").unbind('click');
		$(".tg_miniature_wrapper").unbind('mousemove');
		clearInterval(tg_intervals['slideshow']);
		clearInterval(tg_intervals['mouse_move']);
	}
		
	function tg_gallery(set) {
		
		//variabili da non modificare

		var tg_slideshow_run = 1;
		var tg_thumb_position 	= 	0
		var tg_thumb_single_width_margin = 	0
		var tg_thumb_single_height_margin = 	0
		var tg_thumb_obj = $('.tg_miniature > a');
		var tg_total_image = tg_thumb_obj.size();
		var tg_image_transition_time_real = 0;
		var tg_first_run = true;
		var tg_slideshow_timer = 0;
		var tg_current_image = -1;
		var tg_first_image_preload = false;
		var tg_images = new Array(); 
		var tg_thumb_single_width = 0;
		var tg_thumb_single_height = 0;
		var tg_max_thumb_fit = 0;

		var tg_current_thumb_position_left = 0;
		var tg_mouse_nav_step_increment = 0;
		var tg_thumb_wrapper_width = 0;
		var tg_thumb_wrapper_height = 0;
		var tg_thumb_width_realtime = 0;
		var tg_thumb_height_realtime = 0;
		var tg_scost_base = 0;
		var tg_mouse_global = 0;
		var tg_mouse_global_old = 0;
		var tg_thumb_lownumber = 1;
		var tg_thumb_overwrap = 0;
		var tg_thumb_width_all = 0;	
		var tg_thumb_height_all = 0;	
		
		
		var $tg_preload = $.tg_preload = function( original, settings ){
			if( original.split ) // selector
				original = $(original);
	
			settings = $.extend( {
			}, $tg_preload.defaults, settings );
			var sources = $.map( original, function( source ){
				if( !source ) 
					return; // skip
				if( source.split ) // URL Mode
					return settings.base + source + settings.ext;
				var url = source.src || source.href; // save the original source
				if( typeof settings.placeholder == 'string' && source.src ) // Placeholder Mode, if it's an image, set it.
					source.src = settings.placeholder;
				if( url && settings.find ) // Rollover mode
					url = url.replace( settings.find, settings.replace );
				return url || null; // skip if empty string
			});
	
			var data = {
				loaded:0, // how many were loaded successfully
				failed:0, // how many urls failed
				next:0, // which one's the next image to load (index)
				done:0, // how many urls were tried
				total:sources.length // how many images are being preloaded overall
			};
			
			if( !data.total ) // nothing to preload
				return finish();
			
			var imgs = $(Array(settings.threshold+1).join('<img/>'))
				.load(handler).error(handler).bind('abort',handler).each(fetch);
			
			function handler( e ){
				data.element = this;
				data.found = e.type == 'load';
				data.image = this.src;
				data.index = this.index;
				var orig = data.original = original[this.index];
				data[data.found?'loaded':'failed']++;
				data.done++;
	
				// This will ensure that the images aren't "un-cached" after a while
				if( settings.enforceCache )
					$tg_preload.cache.push( 
						$('<img/>').attr('src',data.image)[0]
					);
	
				if( settings.placeholder && orig.src ) // special case when on placeholder mode
					orig.src = data.found ? data.image : settings.notFound || orig.src;
				if( settings.onComplete )
					settings.onComplete( data );
				if( data.done < data.total ) // let's continue
					fetch( 0, this );
				else{ // we are finished
					if( imgs && imgs.unbind )
						imgs.unbind('load').unbind('error').unbind('abort'); // cleanup
					imgs = null;
					finish();
				}
			};
			function fetch( i, img, retry ){
				// IE problem, can't preload more than 15
				if( img.attachEvent /* msie */ && data.next && data.next % $tg_preload.gap == 0 && !retry ){
					setTimeout(function(){ fetch( i, img, true ); }, 0);
					return false;
				}
				if( data.next == data.total ) return false; // no more to fetch
				img.index = data.next; // save it, we'll need it.
				img.src = sources[data.next++];
				if( settings.onRequest ){
					data.index = img.index;
					data.element = img;
					data.image = img.src;
					data.original = original[data.next-1];
					settings.onRequest( data );
				}
			};
			function finish(){
				if( settings.onFinish )
					settings.onFinish( data );
			};
		};
	
		 // each time we load this amount and it's IE, we must rest for a while, make it lower if you get stack overflow.
		$tg_preload.gap = 14; 
		$tg_preload.cache = [];
		
		$tg_preload.defaults = {
			threshold:2, // how many images to load simultaneously
			base:'', // URL mode: a base url can be specified, it is prepended to all string urls
			ext:'', // URL mode:same as base, but it's appended after the original url.
			replace:'' // Rollover mode: replacement (can be left empty)
		};
	
		$.fn.tg_preload = function( settings ){
			$tg_preload( this, settings );
			return this;
		};
	

		
		
		//jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
		jQuery.easing['jswing'] = jQuery.easing['swing'];
		jQuery.extend( jQuery.easing,
		{
			def: 'easeOutQuad',
			swing: function (x, t, b, c, d) {
				//alert(jQuery.easing.default);
				return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
			},
			easeInQuad: function (x, t, b, c, d) {
				return c*(t/=d)*t + b;
			},
			easeOutQuad: function (x, t, b, c, d) {
				return -c *(t/=d)*(t-2) + b;
			},
			easeInOutQuad: function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return c/2*t*t + b;
				return -c/2 * ((--t)*(t-2) - 1) + b;
			},
			easeInCubic: function (x, t, b, c, d) {
				return c*(t/=d)*t*t + b;
			},
			easeOutCubic: function (x, t, b, c, d) {
				return c*((t=t/d-1)*t*t + 1) + b;
			},
			easeInOutCubic: function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return c/2*t*t*t + b;
				return c/2*((t-=2)*t*t + 2) + b;
			},
			easeInQuart: function (x, t, b, c, d) {
				return c*(t/=d)*t*t*t + b;
			},
			easeOutQuart: function (x, t, b, c, d) {
				return -c * ((t=t/d-1)*t*t*t - 1) + b;
			},
			easeInOutQuart: function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
				return -c/2 * ((t-=2)*t*t*t - 2) + b;
			},
			easeInQuint: function (x, t, b, c, d) {
				return c*(t/=d)*t*t*t*t + b;
			},
			easeOutQuint: function (x, t, b, c, d) {
				return c*((t=t/d-1)*t*t*t*t + 1) + b;
			},
			easeInOutQuint: function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
				return c/2*((t-=2)*t*t*t*t + 2) + b;
			},
			easeInSine: function (x, t, b, c, d) {
				return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
			},
			easeOutSine: function (x, t, b, c, d) {
				return c * Math.sin(t/d * (Math.PI/2)) + b;
			},
			easeInOutSine: function (x, t, b, c, d) {
				return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
			},
			easeInExpo: function (x, t, b, c, d) {
				return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
			},
			easeOutExpo: function (x, t, b, c, d) {
				return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
			},
			easeInOutExpo: function (x, t, b, c, d) {
				if (t==0) return b;
				if (t==d) return b+c;
				if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
				return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
			},
			easeInCirc: function (x, t, b, c, d) {
				return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
			},
			easeOutCirc: function (x, t, b, c, d) {
				return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
			},
			easeInOutCirc: function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
				return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
			},
			easeInElastic: function (x, t, b, c, d) {
				var s=1.70158;var p=0;var a=c;
				if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
				if (a < Math.abs(c)) { a=c; var s=p/4; }
				else var s = p/(2*Math.PI) * Math.asin (c/a);
				return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
			},
			easeOutElastic: function (x, t, b, c, d) {
				var s=1.70158;var p=0;var a=c;
				if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
				if (a < Math.abs(c)) { a=c; var s=p/4; }
				else var s = p/(2*Math.PI) * Math.asin (c/a);
				return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
			},
			easeInOutElastic: function (x, t, b, c, d) {
				var s=1.70158;var p=0;var a=c;
				if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
				if (a < Math.abs(c)) { a=c; var s=p/4; }
				else var s = p/(2*Math.PI) * Math.asin (c/a);
				if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
				return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
			},
			easeInBack: function (x, t, b, c, d, s) {
				if (s == undefined) s = 1.70158;
				return c*(t/=d)*t*((s+1)*t - s) + b;
			},
			easeOutBack: function (x, t, b, c, d, s) {
				if (s == undefined) s = 1.70158;
				return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
			},
			easeInOutBack: function (x, t, b, c, d, s) {
				if (s == undefined) s = 1.70158; 
				if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
				return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
			},
			easeInBounce: function (x, t, b, c, d) {
				return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
			},
			easeOutBounce: function (x, t, b, c, d) {
				if ((t/=d) < (1/2.75)) {
					return c*(7.5625*t*t) + b;
				} else if (t < (2/2.75)) {
					return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
				} else if (t < (2.5/2.75)) {
					return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
				} else {
					return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
				}
			},
			easeInOutBounce: function (x, t, b, c, d) {
				if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
				return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
			}
		});

		
		function tg_next_image_id(id) {
			id = parseInt(id);
			if(set.tg_infinity_loop && id == tg_total_image-1) return  0;
			return id + 1;
		}
		
		
		function tg_prev_image_id(id) {
			id = parseInt(id);
			if(set.tg_infinity_loop && id == 0) return tg_total_image-1;
			return id -1;
		}
	
		function tg_add_image(id) {
					$(".tg_image")
						
						.append(tg_images[id])
						.find("img")
						.last("")
						.wrap("<div class='tg_image_wrap'>")
						.parent()
						.width(tg_images[id].width)
						.height(tg_images[id].height)
						.css({opacity: 0, position:'absolute', left:(set.tg_big_width-tg_images[id].width)/2, top:(set.tg_big_height-tg_images[id].height)/2})
						.attr("id_img",id)
						
						.animate({opacity: 1},{
						
							duration:tg_image_transition_time_real, 
							easing:set.tg_image_transition_animation, 
							queue:false, 
							complete:function(){
									$(this).prev().remove();
							}
						});
		}
		
		function tg_change_image(id) {
			
			if(set.tg_url_rewrite) document.location.replace(document.location.toString().split('#')[0]+"#"+parseInt(id));
			
			//se e' diversa
			if(id != tg_current_image && tg_images[id]) {
				tg_current_image = id;
				
				//efetto navigazone
				if(set.tg_navigation) {

					$(tg_thumb_obj).removeClass("current").removeClass("selected");
					$(tg_thumb_obj).css({opacity: set.tg_nav_opacity_thumb});
					$(tg_thumb_obj).stop().eq(id).css({opacity: 1}).addClass("current selected");
				}
				
				//centro miniature
					
				//decido se centrare i no le miniature
				if(!tg_first_run && !tg_thumb_lownumber && set.tg_nav_center_on_change && set.tg_navigation && (set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical")) tg_nav_center(id);
		
			
				//solo per la prima
				if(tg_first_run) {
					 $(".tg_image").show();
					 if(set.tg_image_animate_first) {
						 tg_image_transition_time_real = set.tg_image_transition_time;
					 } else {
						 tg_image_transition_time_real = 0;
					 }
					
				} else {
					tg_image_transition_time_real = set.tg_image_transition_time;
				}
		
				
				//tipo di fade	
					if(set.tg_transition_type == "fade" || set.tg_transition_type == "single_fade") {
						
						if(set.tg_transition_type == "single_fade") {
							var tg_image_transition_time_old = set.tg_image_transition_time/4;
							var tg_image_transition_time = set.tg_image_transition_time/2;
						} else {
							var tg_image_transition_time_old = set.tg_image_transition_time;
						}
						
						if(!tg_first_run) {
							//fade immagine vecchia
							$(".tg_image .tg_image_wrap")
								.last()
								.animate({opacity: 0},{
							
									duration:tg_image_transition_time_old, 
									easing:set.tg_image_transition_animation, 
									queue:false, 
									complete:function(){
										
										$(this).remove();
										if(set.tg_transition_type == "single_fade") {
											tg_add_image(id);
										}
									}
							 });
								
							if(set.tg_transition_type != "single_fade") {
								tg_add_image(id);
							}
						} else {
							tg_add_image(id);
						}
						
					} else {
						//no fade
						if(set.tg_transition_type == "no_fade") {
							 tg_image_transition_time_real = 0;
						}
						//crossfade
						tg_add_image(id);
					}
				
				
	
					
				$(".tg_image").width($(tg_images[id]).width());
				$(".tg_image").height($(tg_images[id]).height());
				
				if(set.tg_image_hover_link) {
					var hover_link = $(tg_thumb_obj).eq(id).attr("hover_link");
					if(hover_link) {
						$(".tg_image")
							.find("img")
							.last("")
							.wrap("<a rel='tg_gallery' href='"+hover_link+"'></a>")
						
						//lightbox
						if(set.tg_image_hover_lightbox) {
							$('a[rel=tg_gallery]').lightBox();
						}
					}
				}
				//descrizione
				if(set.tg_description_show) {
				

					
					$(".tg_description")
						.show()
						.css({opacity: 0})
						.html($(tg_thumb_obj).eq(id).next().filter("span").html())
						.animate({opacity: 1},{duration:set.tg_image_transition_time_real,queue:false});
				
					$(".tg_description").width($(".tg_description_cont").width()- $(".tg_download a").outerWidth()-50);
				//	$(".tg_description").height($(".tg_description_cont").height());
				
						var link_download = $(tg_thumb_obj).eq(id).attr("original");
						if(link_download) {
							$(".tg_download a")
								.show()
								.attr("href",link_download);
						}
				
				
				}
				
				//download
				
				
				tg_first_run = false;		
			}		
		}	
		
		function tg_nav_center(id_immagine) {
			if(set.tg_navigation_type == "strip") {
			    var thumb_current_width = $(tg_thumb_obj).eq(id_immagine).width();
			    tg_mouse_nav_step_increment = 0;
			    //controllo se devo spostarmi
			    var current_thumb_pos_center =$(tg_thumb_obj).eq(id_immagine).position().left + (thumb_current_width / 2) - (tg_thumb_wrapper_width / 2) - (tg_thumb_single_width_margin / 2);
			    
			    if(current_thumb_pos_center >= tg_thumb_width_all - tg_thumb_wrapper_width -tg_thumb_single_width_margin) {
					    current_thumb_pos_center =tg_thumb_width_all - tg_thumb_wrapper_width - tg_thumb_single_width_margin;
			    } 
			    if(current_thumb_pos_center <= 0) {
					    current_thumb_pos_center = 0;
					    
					    
			    } 
			    $(tg_thumb_obj).parent().stop().animate({left:-(current_thumb_pos_center)},{duration:500, queue:false,easing:set.tg_nav_animation});

			} 
			
			if(set.tg_navigation_type == "vertical") {
			    var thumb_current_height = $(tg_thumb_obj).eq(id_immagine).height();
			    tg_mouse_nav_step_increment = 0;
			    //controllo se devo spostarmi
			    var current_thumb_pos_center =$(tg_thumb_obj).eq(id_immagine).position().top + (thumb_current_height / 2) - (tg_thumb_wrapper_height / 2) - (tg_thumb_single_height_margin / 2);
			    
			    if(current_thumb_pos_center >= tg_thumb_height_all -tg_thumb_wrapper_height - tg_thumb_single_height_margin) {
					    current_thumb_pos_center = tg_thumb_height_all - tg_thumb_wrapper_height - tg_thumb_single_height - tg_thumb_single_height_margin;
			    } 
			    if(current_thumb_pos_center <= 0) {
					    current_thumb_pos_center = 0;
			    } 
			    $(tg_thumb_obj).parent().stop().animate({top:-(current_thumb_pos_center)},{duration:500, queue:false,easing:set.tg_nav_animation});

			} 		
		}	
		
		function tg_nav_prev(id) {
			//tg_mouse_nav_step_increment = 0;
			if(set.tg_nav_click_change_image) {
					 tg_change_image(tg_prev_image_id(tg_current_image));
			} else {
				if(set.tg_navigation_type == "strip") tg_nav_move($(tg_thumb_obj).parent().position().left + (set.tg_nav_translation_step * tg_thumb_single_width),1);
				if(set.tg_navigation_type == "vertical") tg_nav_move($(tg_thumb_obj).parent().position().top + (set.tg_nav_translation_step * tg_thumb_single_height),1);			
			}
		}	
		
		function tg_nav_next(id) {
			//tg_mouse_nav_step_increment = 0;
			if(set.tg_nav_click_change_image) {
					tg_change_image(tg_next_image_id(tg_current_image));
			} else {
				if(set.tg_navigation_type == "strip") tg_nav_move($(tg_thumb_obj).parent().position().left - (set.tg_nav_translation_step * tg_thumb_single_width),1);
				if(set.tg_navigation_type == "vertical") tg_nav_move($(tg_thumb_obj).parent().position().top - (set.tg_nav_translation_step * tg_thumb_single_height),1);
			}
		}	
		
		function tg_nav_move(tg_current_thumb_position_left,type) {
			if(tg_first_image_preload && tg_thumb_overwrap) {
				if(set.tg_navigation_type == "strip") { 		
					if(tg_current_thumb_position_left > 0) tg_current_thumb_position_left = 0;
					if(tg_current_thumb_position_left < -(tg_thumb_width_realtime - tg_thumb_wrapper_width)) {
						tg_current_thumb_position_left = -(tg_thumb_width_realtime - tg_thumb_wrapper_width);
					}
				
					if(type == 1) $(tg_thumb_obj).parent().stop().animate({left:tg_current_thumb_position_left},{duration:500, queue:false,easing:set.tg_nav_animation});
					else $(tg_thumb_obj).parent().css({left:tg_current_thumb_position_left});
				}
				if(set.tg_navigation_type == "vertical") { 		
					if(tg_current_thumb_position_left > 0) tg_current_thumb_position_left = 0;
					if(tg_current_thumb_position_left < -(tg_thumb_height_realtime - tg_thumb_wrapper_height)) {
						tg_current_thumb_position_left = -(tg_thumb_height_realtime - tg_thumb_wrapper_height);
					}
					
					if(type == 1) $(tg_thumb_obj).parent().stop().animate({top:tg_current_thumb_position_left},{duration:500, queue:false,easing:set.tg_nav_animation});
					else $(tg_thumb_obj).parent().css({top:tg_current_thumb_position_left});
	
				}
				
			}
		}
	
		function tg_slideshow() {
			
			$(".tg_play").hide();
			$(".tg_pause").show();
			
			tg_intervals['slideshow'] = setInterval(function(){
					
					if(tg_first_image_preload && tg_slideshow_run) {
							if(tg_mouse_global_old != tg_mouse_global) tg_slideshow_timer = 0;
							else tg_slideshow_timer += 100;
					}
					if(tg_slideshow_timer >= set.tg_slideshow_interval) {
						tg_change_image(tg_next_image_id(tg_current_image));
						tg_slideshow_timer = 0;
					}  
					tg_mouse_global_old = tg_mouse_global;
			}, 100);
		}
	
		function tg_gallery_init() {
			
			//imposto che la prima caricata
			tg_first_image_preload = true;
			
			//aggiungo classe per verticale
			
			$(".tg_image_cont").show();
			$(".tg_contenitore").height(99).hide().slideDown("slow");
			$(".tg_image_cont").width(set.tg_big_width);
			$(".tg_image_cont").height(set.tg_big_height);
			
			if(set.tg_description_show) $(".tg_description_cont").show();
			else $(".tg_description_cont").hide();
			
			if(set.tg_control_show) $(".tg_control_cont").show();
			else $(".tg_control_cont").hide();			
			
			//visualizzo freccie
			if((set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical") && set.tg_nav_arrow) $(".tg_miniature_cont .tg_nav").show();
			
			$(".tg_loading").hide();
			
			//sistemo dimensioni descrizione
			//$(".tg_description_cont").height($(".tg_description").height());
		
			
			//prima immagine
			tg_change_image(set.tg_first_image);
		
			//slideshow
			if(set.tg_slideshow_autostart) {
				tg_slideshow();
			}
			//nav arrow
			if(set.tg_image_arrow) {
				$(".tg_image_arrow").show();
				
				//navigazione
				$(".tg_image_arrow_prev")
					.css("left",0)
					.css("opacity",0)
					.height($(".tg_image").height())
					.width($(".tg_image").width()/3);
	
				
				$(".tg_image_arrow_next")
					.css("right",0)
					.css("opacity",0)
					.height($(".tg_image").height())
					.width($(".tg_image").width()/3);
					
			} else {
				$(".tg_image_arrow").hide();
			}
			
			//nav tastiera
			if(set.tg_keyboard_nav){
				$(document).keypress(function(event) {
					if(event.keyCode == '39') {
						 tg_change_image(tg_next_image_id(tg_current_image));
					}
					if(event.keyCode == '37') {
						 tg_change_image(tg_prev_image_id(tg_current_image));
					}
					
				});
			}
			
			//controllo slideshow
			if(set.tg_control_show) {
				$(".tg_control_slideshow a").bind('click', function(event) {
					event.preventDefault();
					if(tg_first_image_preload) {
						if($(this).hasClass("tg_pause")) {
							$(".tg_play").show();
							$(".tg_pause").hide();
							tg_slideshow_run = 0;
						} else {
							$(".tg_play").hide();
							$(".tg_pause").show();
							tg_slideshow_run = 1;
							if(!tg_intervals['slideshow']) tg_slideshow();
							
						}
					}
				
				});

			}
		
				
			//controllo navigazione testuale
			if(set.tg_control_show) {
				$(".tg_control_nav a").bind('click', function(event) {
					event.preventDefault();
					if(tg_first_image_preload) {
						if(tg_slideshow_run) tg_slideshow_timer = 0;
						if($(this).hasClass("tg_control_prev")) {
							 tg_change_image(tg_prev_image_id(tg_current_image));
						} else {
							 tg_change_image(tg_next_image_id(tg_current_image));
						}
					}
				
				});
			}
			
			
			//animazione freccie
			$(".tg_image_arrow a").bind('mouseover mouseout click', function(event) {
			
				
				if (event.type == 'click') {
					
					event.preventDefault();
					if(tg_first_image_preload) {
						if(tg_slideshow_run) tg_slideshow_timer = 0;
						if($(this).hasClass("tg_image_arrow_prev")) {
							 tg_change_image(tg_prev_image_id(tg_current_image));
						} else {
							 tg_change_image(tg_next_image_id(tg_current_image));
						}
					}
				
				} else if (event.type == 'mouseover') {
					if(set.tg_high_performance_mode) $(this).stop().css({opacity: set.tg_image_arrow_opacity});
					else $(this).stop().animate({opacity: set.tg_image_arrow_opacity},{duration:500});
				
				} else if (event.type == 'mouseout') {			
					if(set.tg_high_performance_mode) $(this).stop().css({opacity: 0});
					else $(this).stop().animate({opacity: 0},{duration:500});
				} 
				
			});
	
	
			//animazione miniature
			$(tg_thumb_obj).bind('mouseover mouseout click', function(event) {
				if (event.type == 'click') {
					event.preventDefault();
					if(tg_slideshow_run) tg_slideshow_timer = 0;
					tg_change_image($(this).attr("id_img"));
					
				} else if (event.type == 'mouseover') {
						
					if(set.tg_high_performance_mode) $(this).stop().css({opacity: 1});
					else $(this).stop().animate({opacity: 1},{duration:250});
				
				} else if (event.type == 'mouseout') {			
					if(set.tg_high_performance_mode) $(this).not(".selected").stop().css({opacity: set.tg_nav_opacity_thumb});	
					else $(this).not(".selected").stop().animate({opacity: set.tg_nav_opacity_thumb},{duration:500});
			
				} 
				
			});
		
			//azioni navigazione barra miniature
			if(set.tg_navigation && (set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical")){
				$(".tg_miniature_cont a.tg_nav").bind('click', function(event) {
					event.preventDefault();
					if(tg_first_image_preload) {
							if(tg_slideshow_run) tg_slideshow_timer = 0;
							if($(this).hasClass("tg_nav_prev")) {
								tg_nav_prev();
			
							} else {
								tg_nav_next();
							}
					}
				 });
			}
			 
			//movimento miniature 
			if(set.tg_navigation && set.tg_nav_mouse_move && (set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical")){
				
				$(".tg_miniature_wrapper").bind('mousemove', function(e) {
					
					tg_mouse_global = e.pageX+e.pageY;
					if(set.tg_navigation_type == "strip") {
						
							var tg_scost = ((e.pageX - tg_scost_base) / set.tg_nav_mouse_move_sensibility) ;
							//tg_scost = Math.round(tg_scost);
					}
					if(set.tg_navigation_type == "vertical")  {
						tg_scost_base = $(".tg_miniature_wrapper").offset().top + (tg_thumb_wrapper_height/2);
						var tg_scost = ((e.pageY - tg_scost_base) / set.tg_nav_mouse_move_sensibility) ;
					}
					tg_mouse_nav_step_increment = tg_scost;

					
					
					
				});
			
				tg_intervals['mouse_move'] = setInterval(function(){
						if(!$(tg_thumb_obj).parent().is(":animated")) {
							if(set.tg_navigation_type == "strip") tg_nav_move($(tg_thumb_obj).parent().position().left - tg_mouse_nav_step_increment,0)
							if(set.tg_navigation_type == "vertical") tg_nav_move($(tg_thumb_obj).parent().position().top - tg_mouse_nav_step_increment,0)
												
							
						}
				}, 16);
			
			}
		
		}
		

		//#######################   TNX GALLERY ###########################################
		//inizio programma (le miniature sono gia' tutte caricate !
		if(set.tg_navigation) $(".tg_miniature_cont").show();
		else $(".tg_miniature_cont").hide();
		
		//eventuale click
		
		if(set.tg_url_rewrite) { 
		    var tg_location = document.location.toString();
		    if (tg_location.match('#')) { 
			    var tg_anchor = tg_location.split('#')[1];
			    //creo azione subito
			    //$(".tg_miniature > a").eq(tg_anchor).click();
			    set.tg_first_image = parseInt(tg_anchor);
		    } 
		}
		 //preload all the image
		$(tg_thumb_obj).tg_preload({
			threshold: set.tg_preload_max_image,
			onComplete:function( data ){
				
				
				tg_images[data.index] = new Image();
				tg_images[data.index].src = data.element.src;
				
			
				//visualizzo immagine
				 $(tg_thumb_obj).eq(data.index)
				 .show()
				 .css("opacity",0)
				 .css("display","block")
				 .css("float","left")
				 .animate({opacity: set.tg_nav_opacity_thumb},{duration:300, easing:"swing", queue:false});
			
				
				if(data.index === 0)  {
					if(set.tg_navigation_type == "all") {
							$(".tg_miniature").wrap("<div class='tg_miniature_wrapper' style='overflow:hidden;position:relative;margin:auto;'>");
							$("#tg_cont").addClass("all");
							$(".tg_miniature").width($(".tg_miniature").width()+($(tg_thumb_obj).eq(0).width() - 10));
					}
						
					if(set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical") {
					
						//vertical
						if(set.tg_navigation_type == "vertical"){
							$("#tg_cont").addClass("vertical");
						
							//tolgo navigazione miniature se verticale
							set.tg_nav_arrow = 0;
						}
						
					
						//aggiungo wrap se serve
						$(".tg_miniature").css({"position":"absolute"},{"left":"0px"}).wrap("<div class='tg_miniature_wrapper' style='overflow:hidden;position:relative;margin:auto;'>");
						
						
						//calcolo dimensione wrapper
						if(set.tg_nav_arrow) {
							$(".tg_miniature_wrapper").width($(".tg_miniature_cont").width()-($(".tg_miniature_cont .tg_nav").width()*2));
							tg_thumb_wrapper_width = $(".tg_miniature_wrapper").width();
									
						} else {
							tg_thumb_wrapper_width = $(".tg_miniature_cont").width();
							if(set.tg_navigation_type == "vertical"){
								$(".tg_miniature_wrapper").height($(".tg_miniature_cont").height());
								tg_thumb_wrapper_height = $(".tg_miniature_cont").height();
							}
						}
					
						//calcolo il numero massimo di miniature
						$(tg_thumb_obj).each(function(){
							if(set.tg_navigation_type == "strip") {
								tg_thumb_width_all = tg_thumb_width_all + $(this).outerWidth();	
								if(tg_thumb_width_all <= tg_thumb_wrapper_width) {
									 tg_max_thumb_fit++;
								}
							}
							if(set.tg_navigation_type == "vertical") {
								tg_thumb_height_all = tg_thumb_height_all + $(this).outerHeight();
								if(tg_thumb_height_all <= tg_thumb_wrapper_height) {
									 tg_max_thumb_fit++;
								}
							}
							
								
						});
						
						//imposto anche gli step
						if(set.tg_nav_translation_step == "auto") set.tg_nav_translation_step = tg_max_thumb_fit-1;
						if(set.tg_nav_translation_step <= 0) set.tg_nav_translation_step = 1;
							
						//overwrite set.tg_nav_translation_step
						if(set.tg_nav_click_change_image)  set.tg_nav_translation_step = 1;
						
						if(set.tg_navigation_type == "strip") {
							//prima miniatura
							tg_thumb_single_width = $(tg_thumb_obj).eq(0).outerWidth();
							tg_thumb_single_width_margin = $(tg_thumb_obj).eq(0).outerWidth() - $(tg_thumb_obj).eq(0).width();
							var tg_mouse_off = $(".tg_miniature_wrapper").offset();
							tg_scost_base = tg_mouse_off.left + (tg_thumb_wrapper_width/2);
						}
						if(set.tg_navigation_type == "vertical") {
							//prima miniatura
							tg_thumb_single_height = $(tg_thumb_obj).eq(0).outerHeight();
							tg_thumb_single_height_margin = $(tg_thumb_obj).eq(0).outerHeight() - $(tg_thumb_obj).eq(0).height();
						}
						
						//se ci non c'e' bisogno della navigazione che scorre togo alcune cse
						if(tg_max_thumb_fit >= tg_total_image) {
							tg_thumb_lownumber = 1;
							set.tg_nav_mouse_move = 0;
							//se il click sulla striscia non fa' cambiare l' immagine, non abilito le freccie
							if(set.tg_nav_arrow) set.tg_nav_click_change_image = 1;
						} else tg_thumb_lownumber = 0;
							
					}
				}
				
				//quando arrivo a quella che mi serve
				if(data.index === set.tg_first_image)  {
					tg_gallery_init();
					
				} 
				
				//posiziono centrato
				if(data.index == set.tg_first_image+1) {
					if(set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical") tg_nav_center(set.tg_first_image);
				}
				
				if(set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical"){
					//ridimensiono altezza se necessario
					if(set.tg_navigation_type == "strip") {
						if($(".tg_miniature_cont").height() < $(tg_thumb_obj).eq(data.index).outerHeight() ) $(".tg_miniature_cont, .tg_miniature_wrapper").height($(tg_thumb_obj).eq(data.index).outerHeight());
				
						//aggiorno dimensione mentre carica x evitare problemi
						tg_thumb_width_realtime = tg_thumb_width_realtime + $(tg_thumb_obj).eq(data.index).outerWidth();
						$(tg_thumb_obj).parent().width(tg_thumb_width_realtime);
			
						
					}
					if(set.tg_navigation_type == "vertical") {
							tg_thumb_height_realtime = tg_thumb_height_realtime + $(tg_thumb_obj).eq(data.index).outerHeight();
							$(tg_thumb_obj).parent().height(tg_thumb_height_realtime);
					}
					//controllo quando carico se ci sono piu' miniature di quelle che entrano
					if(data.index+1 > tg_max_thumb_fit) tg_thumb_overwrap = 1;
			
				}
				if(set.tg_navigation_type == "all") {
					
				}
				//finito di caricare tutto
				if(data.index == tg_total_image-1) {
					if(set.tg_navigation_type == "strip" || set.tg_navigation_type == "vertical") tg_nav_center(set.tg_first_image);
				}
				
				
			}
		
		});

	}
	
