var Carrusel = {
	carrusel: function(imagenes) {
		var ul = $('#imagenesCabecera');

		$('#textoImagen').css('opacity', 0.8);

		Carrusel.creaFotos(ul);

		Carrusel.creaNavegacion(ul);

		Carrusel.play();
	},

	creaTexto:function(i) {
		var imagen = imagenes[i];
		var texto = '';
		
		if(imagen.enlace != '') {
			if(imagen.enlace.indexOf("http:") != -1){
				texto += '<div><a target="_blank" href="' + imagen.enlace + '">+ info</a></div>';
			}else{
				texto += '<div><a href="' + imagen.enlace + '">+ info</a></div>';
			}
		}
		
		texto += '<h3>' + imagen.titulo + '</h3>' + imagen.texto;

		if(texto == '<h3></h3>') texto = '';

		return texto;
	},

      creaFotos:function(ul) {
		ul.html('');

		for(var i = 0; i < imagenes.length; i++) {
			var li = $('<li><img src="' + imagenes[i].ruta + '" alt="" /></li>');
			
			if(i == 0) {
				var texto = this.creaTexto(0);
				if(texto == '') {
					$('#textoImagen').hide();
				} else {
					$('#textoImagen').html(texto);
				}
			} else {
				li.hide();
			}

			ul.append(li);
		}

	},

	creaNavegacion:function(ul) {
		var navegacion = $('<div id="navegacion"><ul id="cajitas"></ul></div>');

		var lista = navegacion.find('ul');

		for(var i = 0; i < imagenes.length; i++) {
			var li = $('<li class="imagen"></li>').attr('posicion', i).click(function() {
				Carrusel.stop();
				Carrusel.cambiaVisible($(this), true);
			});

			lista.append(li);
		}

		lista.find('li:first').addClass('activo');

		var play = $('<div id="play" class="activo"></div>').click(function() {
			if($('#progreso div:animated').length != 0) {
				Carrusel.stop();
			} else {
				Carrusel.play();
				Carrusel.avanza();
			}
		});

		navegacion.append(play);

		var fondoNavegacion = $('<div id="fondoNavegacion"></div>').css('opacity', 0.8);

		ul.parent().append(fondoNavegacion);
		ul.parent().append(navegacion);
	},

	avanza: function() {
		var li = $('#cajitas li.activo');
		var siguiente = li.next();
		if(siguiente.length == 0) {
			siguiente = $('#cajitas li:first');
		}

		Carrusel.cambiaVisible(siguiente, false);
	},

	cambiaVisible: function(li, estatico) {
		var ul = $('#imagenesCabecera');

		ul.find('li').fadeOut('slow');//css('display', 'none');
		ul.find('li:eq(' + li.attr('posicion') + ')').fadeIn("slow");

		$('#cajitas li.imagen').removeClass('activo');
		li.addClass('activo');

		var texto = this.creaTexto(li.attr('posicion'));

		if(texto == '') {
			$('#textoImagen').hide();
		} else {
			$('#textoImagen').html(texto).show();
		}

		if(!estatico) {
			$('#progreso').show().find('div').css('width', '0px').animate( { width: '89px' }, 6000, 'linear', Carrusel.avanza );
		}
	},

	stop: function() {
		var animado = $('#progreso div:animated');
		while(animado.length != 0) {
			animado.stop().css('width', 0);
			animado = $('#progreso div:animated');
		}
		$('#play').removeClass('activo');
		$('#progreso').remove();
	},

	play: function() {
		var progreso = $('<div id="progreso"><div style="width: 0"></div></div>');
		$('#navegacion').append(progreso);

		$('#progreso').show().find('div').css('width', '0px').animate( { width: '89px' }, 6000, 'linear', Carrusel.avanza );

		$('#play').addClass('activo');
	}
}