
(function($){
	$.fn.stxTabs = function(ops){
		    var defaults = { 'directorio':'', 'dirMedia':'media/stxTabs16/',
		    				'elementos': [
		    				    { 'titulo':'Enlaces', 'fondo':'tierra.jpg', 'target':'_blank', 'contenido':{'mod':'stxArtic','ops':{"mostrarTitulo":"0", "idAct":"36", "directorio":"modulos/stxArtic/"}},
									'id':'0'},
								{ 'titulo':'Egresados', 'fondo':'horiz.jpg', 'target':'_self', 'contenido':{'mod':'stxArtic','ops':{"mostrarTitulo":"0", "idAct":"35", "directorio":"modulos/stxArtic/"}},
									'id':'1'}]
		   					};
		    var stxOps;
			var elemento = $(this);
			var elemActual = 0;
		    this.each(function(){
		    	stxOps = $.extend(defaults,ops);
				
		    	$(this).html('<div class="stxTabs" style="width:'+stxOps.ancho+'; height:'+stxOps.alto+';">'
		    +'	<ul class="stxTabsCabs"></ul><div class="stxTabsConts"></div>'
		    +'	</div>');	

				//devTabs();
		    	Ini();
			});	
			
		    function Ini(){
				if(stxOps.elementos && stxOps.elementos.length > 0){
					var i;
					for(i=0; i < stxOps.elementos.length; i++){

						elemento.find('.stxTabsCabs').append('<li><a href="#stxTabCont'+stxOps.elementos[i].id+'">'+stxOps.elementos[i].titulo+'</a></li>');
						elemento.find('.stxTabsConts').append('<div id="stxTabCont'+stxOps.elementos[i].id+'" class="stxTabCont" style="background-image: url('+stxOps.dirMedia + stxOps.elementos[i].fondo +'); background-repeat: no-repeat;" id="stxTabCont'+stxOps.elementos[i].id +'" ></div>');

						var js = agregarJS(stxOps.elementos[i].contenido.ops.directorio+stxOps.elementos[i].contenido.mod+'.js');
						var css = agregarCSS(stxOps.elementos[i].contenido.ops.directorio+stxOps.elementos[i].contenido.mod+'.css');
					}
					
					$(window).load(function(){
						var i;
						for(i=0; i < stxOps.elementos.length; i++){
							contenido = stxOps.elementos[i].contenido;
							var stxTabContId = '#stxTabCont'+stxOps.elementos[i].id;
							eval('$(stxTabContId).'+contenido.mod+'(contenido.ops)');
						}
					});

				}
				
				elemento.find(".stxTabCont").hide();
				elemento.find("ul.stxTabsCabs li:first").addClass("stxTabEleAct").show();
				elemento.find(".stxTabCont:first").show();

				elemento.find("ul.stxTabsCabs li").click(function(){
					elemento.find("ul.stxTabsCabs li").removeClass('stxTabEleAct');
					$(this).addClass("stxTabEleAct");
					elemento.find(".stxTabCont").hide();
					
					var activeTab = $(this).find("a").attr("href");
					elemento.find(activeTab).fadeIn();
				});
		    	
		    }
		    
			//Devuelve los slides a mostrar desde el servidor.
			function devTabs(){
				$.post(	
					stxOpciones.directorio + 'stxTabs.php',
					{"func":"devTabs", "id":stxOpciones.id},
					function(elementos){
						if(elementos){
							stxOpciones.elementos = $.extend(stxOpciones.elementos,eval(elementos));
							Ini();
						}
						else
							alert('Ha ocurrido un error al validar los datos en el servidor o el mismo no esta disponible.');
					}
				);
			}	
			this.Finalizar = function(){
				elemento.find('.stxTabs').remove();
			};
	};
})(jQuery);

