$.fn.galeria = function() {
	this.each(function() {
		$('*', this).css({position:'relative', display:'block'});
		$('div.imagen', this).width(450).height(337).css({overflow:'hidden', position:'relative'});

		$('div.miniaturas', this).height(80);
		if ( $('div.rect_miniaturas a', this).length * 90 < 560 ) {
			$('div.rect_miniaturas', this).width(560).height(90).css({position:'absolute', left:'0px', overflow:'hidden'}).html('<div class="cuadro">'+$('div.miniaturas div.rect_miniaturas', this).html()+'</div>');
			$('div.cuadro', this).width($('div.cuadro a', this).length * 90).css({height:'80px', position:'absolute', marginLeft:Math.round((560 - ($('div.cuadro a', this).length * 90)) / 2)+'px'});
			
			if ($.browser.msie && $.browser.version < 7) $('div.cuadro', this).width($('div.cuadro', this).width()+14);
			$('div.miniaturas a', this).each(function() {
				$(this).bind('click', function(e) {
					e.preventDefault();
					$(this).parent().parent().parent().parent().find('div.imagen').append('<img src="'+$(this)[0].href+'" alt="" style="display:none; position:absolute; top:5px; left:5px" onload="$(this).fadeIn(500, function() { $(this).parent().find(\'img:first\').remove(); });"/>').end();
					$(this).parent().find('a').removeClass('activa').end();
					$(this).addClass('activa').addClass('visitada');
					pageTracker._trackPageview($(this)[0].href);
					return false;
				
				});
			});
		} else {
			$('div.rect_miniaturas', this).width(545).height(90).css({position:'absolute', left:'80px', overflow:'hidden'}).html('<div class="cuadro">'+$('div.miniaturas div.rect_miniaturas', this).html()+'</div>').parent().prepend('<div class="boton boton_anterior"></div><div class="boton boton_siguiente"></div>');

			$('div.cuadro', this).css({width:($('div.cuadro a', this).length * 90)+'px', height:'80px', position:'absolute'});
			if ($.browser.msie && $.browser.version < 7) $('div.cuadro', this).width($('div.cuadro', this).width()+14);
			$('div.boton', this).css({width:'33px',height:'33px',position:'absolute',left:'0px', top:'40px',cursor:'pointer'});
			$('div.boton_anterior', this).css({background:'url(/img/flecha-anterior.gif) no-repeat'});
			$('div.boton_siguiente', this).css({background:'url(/img/flecha-siguiente.gif) no-repeat',left:'667px'});

			$('div.cuadro').css('left', '0px');

			if($('div.cuadro', this).width() > $('div.rect_miniaturas', this).width()) {
				// Centrar la imagen activa
				var width_rect = $('div.rect_miniaturas', this).width();
				var width_cuadro = $('div.cuadro', this).width();
				var left_cuadro = $('div.cuadro', this)[0].offsetLeft;
				var left_activa = $('a.activa', this)[0].offsetLeft;
				if (left_activa > width_rect - 95) {
					var destino = left_activa-95;
					if (destino > width_cuadro - width_rect + 90) destino = width_cuadro - width_rect;
					if (destino < 0) destino = 0;
					$('div.cuadro').css('left', '-'+Math.abs(destino)+'px');
				}

				// Dotar de funcionalidad a los botones
				$('div.boton_anterior', this).bind('mouseover', function() {
					if($(this).parent().find('div.cuadro')[0].offsetLeft < 0) {
						$(this).css('background', 'url(/img/flecha-anterior-hover.gif) no-repeat');
					} else {
						$(this).css('background', 'url(/img/flecha-anterior-off.gif) no-repeat');
					}
				}).bind('mouseout', function() {
					$(this).css('background', 'url(/img/flecha-anterior.gif) no-repeat');
				}).bind('click', function() {
					var destino = $(this).parent().find('div.cuadro')[0].offsetLeft + 90;
					if(destino > 0) {
						destino = 0;
						$(this).css('background', 'url(/img/flecha-anterior-off.gif) no-repeat');
					}
					destino = Math.round(destino / 90) * 90;
					$(this).parent().find('div.cuadro').stop().animate({left:destino+'px'}, 300);
				});
				$('div.boton_siguiente', this).bind('mouseover', function() {
					if($(this).parent().find('div.cuadro')[0].offsetLeft > $(this).parent().find('div.rect_miniaturas').width() - $(this).parent().find('div.cuadro').width()) {
						$(this).css('background', 'url(/img/flecha-siguiente-hover.gif) no-repeat');
					} else {
						$(this).css('background', 'url(/img/flecha-siguiente-off.gif) no-repeat');
					}
				}).bind('mouseout', function() {
					$(this).css('background', 'url(/img/flecha-siguiente.gif) no-repeat');
				}).bind('click', function() {
					var destino = $(this).parent().find('div.cuadro')[0].offsetLeft - 90;
					destino = Math.round(destino / 90) * 90;
					if(destino < $(this).parent().find('div.rect_miniaturas').width() - $(this).parent().find('div.cuadro').width()) {
						destino = $(this).parent().find('div.rect_miniaturas').width() - $(this).parent().find('div.cuadro').width();
						$(this).css('background', 'url(/img/flecha-siguiente-off.gif) no-repeat');
					}
					$(this).parent().find('div.cuadro').stop().animate({left:destino+'px'}, 300);
				});
			}

			$('div.miniaturas a', this).each(function() {
				$(this).bind('click', function(e) {
					e.preventDefault();
					
					
				
					var pos = this.offsetLeft + $(this).parent()[0].offsetLeft;
					if (pos < 80) $(this).parent().parent().parent().find('div.boton_anterior').click();
					else if (pos > 100) $(this).parent().parent().parent().find('div.boton_siguiente').click();
				
					$(this).parent().parent().parent().parent().find('div.imagen').append('<img src="'+$(this)[0].href+'" alt="" style="display:none; position:absolute; top:5px; left:5px" onload="$(this).fadeIn(500, function() { $(this).parent().find(\'img:first\').remove(); });"/>').end();
					$(this).parent().find('a').removeClass('activa').end();
					$(this).addClass('activa').addClass('visitada');
					pageTracker._trackPageview($(this)[0].href);
					return false;
				
				});
			});
		}
	});
}
