/*
 * JavaScript Components Beta
 * http://uncms.com/
 *
 * Copyright 2010, Tuesta Group
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.tuestagroup/license
 *
 * Date: Sat Feb 13 22:33:48 2010 -0500
 */
(function($){
	$.fn.carousel = function(options){
		var defaults = { 
			activar: 'visualizadorActivado',
			cssGaleriaImagenes: '.componente-galeria-imagenes',
			cssDescripcionLarga: '.descripcion-larga',
			cssGaleriaImagenes: '.componente-galeria-imagenes',
			idPaginadorVisor: 'paginadorVisor',
			idPaginador: 'paginador'
		};
		var componente = {
			cssPaginaFicha: '.pagina-ficha',	
			cssFicha: '.ficha',
			cssDescripcionBreve: '.descripcion-breve',
			idDetalle: 'detalle-imagen',
			cssDivImagen: 'imagen',
			paginaFicha:'',
			ficha: '',
			paginador:'',
			imagenes: '',
			carrusel: '',
			activa: 0,
			detalle: '',
			seccionAbout: 'about',
			seccionContact: 'contact',
			seccionNews: 'news',
			seccion: ''
		};
		var options = $.extend(defaults, options);  
		return this.each(function(){
			componente.paginaFicha = $(this);
			componente.seccion = window.location.href;
			var isSeccionActivar = true;
			if(componente.seccion.indexOf(componente.seccionAbout) != -1) isSeccionActivar = false;
			if(isSeccionActivar) initialize();
		});
		function initialize(){
			config();
			marcarComponente();
			crearDetalle();
			configDetalle();
			cargarImagenesDetalle();
			crearCarrusel();
			ocultarTodas();
			crearPaginador();
			eventosPaginador();
			eventosImagenes();
			setTimeout(function(){
				$('#detalle-imagen .loading').hide();
				$(componente.carrusel[0]).fadeIn('slow');
				var enlaces = $('a', componente.paginador);
				$(enlaces[0]).addClass('activo');				
			},100)
			return;
		};
		function config(){
			componente.ficha = $(componente.cssFicha, componente.paginaFicha);	
			componente.imagenes = $(options.cssGaleriaImagenes + ' a', componente.paginaFicha);
			return;
		};
		function configDetalle(){
			componente.detalle = $('#' + componente.idDetalle, componente.paginaFicha);	
			return;
		};
		function marcarComponente(){
			componente.paginaFicha.addClass(options.activar);
			return;
		};
		function crearCarrusel(){
			componente.carrusel = $('div.imagen', componente.detalle);
			return;
		};
		function plantillaDetalle(){
			var html = '<div id="' + componente.idDetalle + '">\n';
			html += '<div class="loading"><\/div>\n';
			html += '<\/div>\n'; 
			return html;
		};
		function crearDetalle(){
			componente.paginaFicha.append(plantillaDetalle());
			return;
		};
		function plantillaImagenDetalle(enlace, indice){
			var ruta = enlace.attr('href');
			var html = '<div class="imagen">\n';
			html += '<p><a href="' + ruta + '" rel="' + indice + '"><img src="' + ruta + '" /><\/a><\/p>\n';
			html += '<\/div>\n'; 
			return html;				
		};
		function cargarImagenesDetalle(){
			$(componente.imagenes).each(function(indice){
				var enlace = $(this);
				componente.detalle.append(plantillaImagenDetalle(enlace, indice));
			});
			return;
		};
		function plantillaPaginador(){
			var html = '<div id="' + options.idPaginador + '"><ul>\n';
			$(componente.imagenes).each(function(indice){
				var imagen = $(this);
				var numeracion = indice + 1;
				html += '<li><a href="' + imagen.attr('href') + '" rel="' + indice + '">' + numeracion + '<\/a><\/li>';
			});
			html += '<\/ul><\/div>\n';
			return html;
		};
		function cambiar(actual, siguiente){
			if(isMapaVisible) return;
			var imagenAOcultar = 	componente.carrusel[actual];
			var imagenAMostrar = 	componente.carrusel[siguiente];
			$(imagenAOcultar).fadeOut(600);
			$(imagenAMostrar).fadeIn(1200);
			return;
		};		
		function carruselAutomatico(){
			componente.carrusel.each(function(indice){
				if(indice > 0) $(this).hide();
			});
			var numeroImagenes = componente.carrusel.size();
			var actual = 0;
			setInterval(function(){
				var siguiente = actual + 1;
				if(siguiente > numeroImagenes -1) siguiente = 0;
				cambiar(actual, siguiente);
				actual = siguiente;
			}, 6000);	
			return;
		};	
		function crearPaginador(){
			$('#facebookSharer', componente.ficha).before(plantillaPaginador());
			componente.paginador = $('#' + options.idPaginador, componente.paginaFicha);
			if(componente.seccion.indexOf(componente.seccionContact) > 0){
				componente.paginador.hide();
				$('#facebookSharer').hide();
				carruselAutomatico();
			};
			return
		};
		function ocultarTodas(){
			$(componente.carrusel).each(function(){
				$(this).hide();
			});
			return;
		};
		function ocultarActiva(){
			$(componente.carrusel[componente.activa]).hide();
			return;
		};
		function mostrarSeleccionada(numero){
			$(componente.carrusel[numero]).fadeIn('slow');
			return;
		};	
		function desmarcarTodasPaginador(){
			$('a', componente.paginador).each(function(){
				$(this).removeClass('activo');
			});
			return;
		};
		function marcarPaginador(numero){
			var enlaces = $('a', componente.paginador);
			$(enlaces[numero]).addClass('activo');
			return;
		};		
		function eventosPaginador(){
			$('#' + options.idPaginador + ' a', componente.paginaFicha).each(function(indice){
				$(this).bind('click', function(evento){
					var enlace = $(this);
					var numero = enlace.attr('rel');
					desmarcarTodasPaginador();
					marcarPaginador(numero);
					ocultarActiva();
					$('#detalle-imagen .loading').hide();
					mostrarSeleccionada(numero);
					componente.activa = numero;
					enlace.blur();
					return false;
				})
			});
			return
		};		
		function eventosImagenes(){
			$('#detalle-imagen div.imagen a', componente.paginaFicha).bind('click', function(evento){
				var enlace = $(this);
				var numero = enlace.attr('rel');
				desmarcarTodasPaginador();
				numero ++;
				if(numero >= $(componente.carrusel).size()) numero = 0;
				marcarPaginador(numero);
				ocultarActiva();
				mostrarSeleccionada(numero);
				componente.activa = numero;
				enlace.blur();
				return false;
			});
		};
	};
})(jQuery);
