/****************************************************************************************/
/*		Equivalente para Js del str_replace() de PHP								 	*/
/****************************************************************************************/
/////////////////////////////////////////////////////////////////////////////////
//Recojedor de Fieldsets
function str_replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}
//
/////////////////////////////////////////////////////////////////////////////////

/****************************************************************************************/
/*		Funcion para detectar tiempo transucrrido entre KeyUp y KeyUp si pasa 300*3ms 	*/
/*		Entonces manda el load y cambia los valores en la BBDD y graficamente			*/
/*		Sino, resetea el contador a 3													*/
/****************************************************************************************/
//Valores Generales
var update; //Intancia para tiempo
var time = 3;	//Tiempo Predefinidio 3s
var set_time = 300;
/////////////////////////////////////////////////////////////////////////////////
//Temporizador para deteccion de tecleo
function temporizador(element, nameFunction){
	//El tiempo es 0, entonces actualizo en la bbdd y graficamente
	if(time == 0){
		eval(nameFunction+"(element)");
	//No, entonces kito 1s a time y welvo a empezar al cabo de 300ms
	}else{
		//Kito 1s
		time--;
		//Guardamos en la intancia Update el setTimeou
		update = setTimeout(function(){
			temporizador(element, nameFunction);			
		}, set_time);
	}

}
//Limpia el time Out
function resetTemporizador(){
	//Borramos el setTimeOut de la intancia update
	clearTimeout(update);
	
} 

//
/////////////////////////////////////////////////////////////////////////////////



/****************************************************************************************/
/*		Se van a traspasar todas las funciones.... aqui ;)							 	*/
/****************************************************************************************/
//Valores Generales

/////////////////////////////////////////////////////////////////////////////////
//Control con checbox

if(jQuery) (function($) {
				 
	//Necesitan Objeto HTML
	$.extend($.fn, {
		wait : function(time, type) {
			time = time || 1000;
			type = type || "fx";
			return this.queue(type, function() {
				var self = this;
				setTimeout(function() {
					$(self).dequeue();
				}, time);
			});
		},
		cufon : function(font){		
			var element = this;
			$.getScript("js/cufon/cufon.js", function(){
				$.getScript("js/cufon/"+font+".js", function(){
						Cufon.replace(element.selector);
						Cufon.now();
				});
			});
		},
		recojer : function(){
			selector = $(this).selector;
			
			this.click(function(){
				var clicker = $(this);
				
				$(selector).each(function(){
					if($(this).hasClass('group') && clicker.hasClass('up')){
						$(this).removeClass('down').addClass('up');	
						$(this).parents('fieldset').find('ul:first').slideUp();	
					}
				});			
				if($(this).hasClass('up')){
					$(this).removeClass('up').addClass('down');	
					$(this).parents('fieldset').find('ul:first').slideDown();
					
				}else{
					if(!$(this).hasClass('group')){
						$(this).removeClass('down').addClass('up');	
						$(this).parents('fieldset').find('ul:first').slideUp();
					}
				}		
			});
		},
		new_animate_tabs :  function(){
			var tabs = this;
			
			if(tabs.size() > 0){
				$('.tab_content').css('position','absolute');
				$('.tab_content').css('width','100%');
				$('.tabs_container').css('height',$('.tab_content:first').height());
			}
			
			tabs.find('.tabs a').click(function(){						
				tabs.find('.tabs li').each(function(){					
					$(this).removeClass('active');								
				});
				
				tabs.find('.tab_content').each(function(){									
					$(this).fadeOut('slow');								
				});
				
				var id = $(this).attr('id').replace("tab_", "");
				tabs.find('#tab_content_'+id).fadeIn('slow');
				$(this).parent().addClass('active');
				
				$('.tabs_container').animate({
					'height' : tabs.find('#tab_content_'+id).height()
				},800);
				
				//alert(id);
			});
		},
		new_tabs :  function(){
			var tabs = this;
			
			tabs.find('.tabs a').click(function(){						
				$(this).parents('.tabs').find('li').each(function(){					
					$(this).removeClass('active');								
				});
				
				$(this).parents('.form_tabs').find('.tab_content').each(function(){									
					$(this).hide();								
				});
				
				var id = $(this).attr('id').replace("tab_", "");
				$(this).parents('.form_tabs').find('#tab_content_'+id).show();
				$(this).parent().addClass('active');
				
				//alert(id);
			});
		}

	});

	//Sin Objeto HTML
	$.extend($, {
		//Variables globales			 
		update : null,
		
		getCss :  function( src ) {
			$('head').append('<link type="text/css" rel="stylesheet" href="'+src+'">');
		},
		
		temporizador : function ( option ){
			defaults = {
				times: 3,
				time: 300,
				funcion: null,
			}
			option = jQuery.extend( defaults , option );
			
			clearTimeout( update );	

			if( option.times == 0 ){
				if( typeof option.funcion == 'function' ){
					option.funcion.call( this );
					option.times = 3;
				}
			}else{
				option.reset = false;
				option.times--;
				update = setTimeout( function(){
					$.temporizador( option );			
				}, option.time );
			}
		},
	});

})(jQuery);	

$(document).ready(function(){
	$(".down, .up").recojer();
	$(".form_tabs_animate").new_animate_tabs();
	$(".form_tabs").new_tabs();
});
