CARRUSELES = {
	inicio : function() {
		$(".alojaCarrusel").each(function(i){
			var ul = $(this).find("ul.carrusel");

			var carruselNumPromoVisible = 0;
			var carruselNumMaxPromo = 0;

			/* Decidimos que imagen se ve */
			var carruselNumMaxPromo = $(ul).find(">li").length;
			var carruselNumPromoVisible = parseInt(Math.random() * carruselNumMaxPromo) + 1;
			$(this).append('<div class="botonera"><span class="anterior" title="Anterior">&lt;</span> <span class="legend">0 / 0</span> <span class="siguiente" title="Siguiente">&gt;</span></div>');

			var anterior = $(this).find('.botonera .anterior');
			var siguiente = $(this).find('.botonera .siguiente');
			var legend = $(this).find('.legend');

			legend.html(carruselNumPromoVisible + ' / ' + carruselNumMaxPromo);

			$(ul).jcarousel({
				start: carruselNumPromoVisible,
				scroll: 1,
				visible: 1,
				initCallback: function(carousel) {
					jQuery('.jcarousel-control a').bind('click', function() {
						carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
						return false;
					});

					jQuery('.jcarousel-scroll select').bind('change', function() {
						carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
						return false;
					});

					siguiente.bind('click', function() {
						if (carruselNumPromoVisible < carruselNumMaxPromo) {
							carruselNumPromoVisible++;
							legend.html(carruselNumPromoVisible + ' / ' + carruselNumMaxPromo);
						}
						carousel.next();
						return false;
					});

					anterior.bind('click', function() {
						if (carruselNumPromoVisible > 1) {
							carruselNumPromoVisible--;
							legend.html(carruselNumPromoVisible + ' / ' + carruselNumMaxPromo);
						}
						carousel.prev();
						return false;
					});
				},
				buttonNextHTML: null,
				buttonPrevHTML: null
				});
		});
	}
}
$(document).ready(CARRUSELES.inicio);
