/**
 * andytabs ( http://jquery.andelux.com/andytabs )
 * plugin para mostrar tabs/pestañas
 * 
 * @version	1.0
 *
 * @author	Javier Pérez <javier.perez@andelux.com>
 * 
 */
(function($){
	$.tabtrigger = function(anchor, triggerfunction) {
		if (!window.andigg_trigger_functions) {
			window.andigg_trigger_functions = [];
		}
		if (triggerfunction) {
			// Añadimos función
			if (!window.andigg_trigger_functions[anchor]) {
				window.andigg_trigger_functions[anchor] = [];
			}
			window.andigg_trigger_functions[anchor].push(triggerfunction);
		} else {
			// Ejecutamos triggers del anchor
			if (window.andigg_trigger_functions[anchor] && window.andigg_trigger_functions[anchor].length > 0) {
				for (var i=0; i<window.andigg_trigger_functions[anchor].length; i++) {
					var f = window.andigg_trigger_functions[anchor][i];
					f($(anchor));
				}
			}
		}
	};
	
	$.fn.andytabs = function(option){
		// Param plugin
		var param = jQuery.extend({
			defaut: null
		}, option);
		
		$(this).each(function() {
			// Initialisation
			var $this = this;
			var nbTab = $("> div", this).size();
			
			hideAll();
			catchLinks();
			changeContent(param.defaut);
			
			function getAnchor(url) {
				return (url && url.indexOf('#')!=-1) ? url.substring( url.lastIndexOf('#') ) : null;
			}

			/**
			 * Atrapa los eventos onclick de todos los A, para comprobar 
			 * si tienen un ancla a una pestaña.
			 */
			function catchLinks(){
				$('a', document).click(function(){
					var anchor = getAnchor($(this).attr('href'));
					if (anchor) {
						if ($(".andytabs-content[id="+anchor.substring(1)+"]", $this).length > 0) {
							changeContent(anchor);
							return false;
						}
					}
				});
			}
			
			// Fonctions
			function hideAll(){
				// Masque tous les content
				$(".andytabs-content", $this).hide();
			}
			
			function changeContent(indice, a){
				if (indice == null) {
					// Miramos document.location por si tuviera un ancla a tab
					var hash = document.location.hash;
					if ($(".andytabs-content[id="+hash.substring(1)+"]", $this).length > 0) {
						indice = hash;
					} else {
						// Obtenemos el primer elemento con un ancla
						//indice = $(".andytabs-nav li:first a", $this).attr('href');
						var $as = $(".andytabs-nav li a", $this);
						for (var i=0; i<$as.length; i++) {
							var href = $($as[i]).attr('href');
							//console.log('href ', href);
							var hash = href.lastIndexOf('#')!=-1 ? href.substring( href.lastIndexOf('#') ) : null;
							//console.log('hash ', hash);
							if (hash!=null && $(".andytabs-content[id="+hash.substring(1)+"]", $this).length > 0) {
								indice = hash;
								break;
							}
						}
						//console.log('indice ', indice);
						if (!indice) return;
					}
				}
				
				if (a == undefined) {
					// Encontramos el <a>
					a = $('a[href=' + indice + ']', $this).get(0);
				}
				//console.log(a);
				
				// Lanzamos evento para el anchor
				$.tabtrigger(indice);
				
				// Ocultamos todos los contenedores de contenido
				hideAll();
				
				// Quitamos la clase "active" a todos los <li>
				$(".andytabs-nav li", $this).removeClass("active");
				
				// Añadimos la clase active al li del a
				$(a).parents('li:first').addClass("active");
				
				// Mostramos el contenido
				$(indice).show();
			}

			// Tab click event
			/*
			$(".andytabs-nav li a", $this).click(function(){
				// Obtenemos el href del nodo hijo A
				var content_id = $(this).attr('href');
				//console.log(content_id, this);
				if (content_id && content_id[0] == '#') {
					// Es un ancla, mostramos el contenido
					changeContent(content_id, this);
				}
				return false;
			});
			*/
		});
	}
})(jQuery);
