var filters = {
		requerido: function(el) {return ($(el).val() != '' && $(el).val() != -1 && $(el).val() != 'undefined' || $(el).attr('value') != '');},
		email: function(el) {return /^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());},
		telefono: function(el){return /^[0-9]*$/.test($(el).val());},
		cotizarDefault: function(el){return $(el).val() != 'tu email';},
		buscarDefault: function(el){return $(el).val() != 'Texto para buscar';}
	};
	
$.extend({
	stop: function(e){
		if (e.preventDefault) e.preventDefault();
		if (e.stopPropagation) e.stopPropagation();
	}
});

$(function(){
	$("form").bind("submit", function(e){
		if (typeof filters == 'undefined') return;
	    $(this).find("input, textarea, select").each(function(x,el){ 
	        if ($(el).attr("className") != 'undefined') { 
		$(el).removeClass("error");
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
	            if ($.isFunction(filters[klass]))
	                if (!filters[klass](el))  $(el).addClass("error");
	        });
	        }
	    });
		if ($(this).find(".error").size() > 0) {
			$.stop(e || window.event);
			jAlert("Necesita completar los campos marcados","El formulario no es valido");
			return false;
		}else{
	    	return true;
		}
	});
	
	if($('p.meterFecha').size() > 0){
		$('p.meterFecha').each(function(){
			$(this).children('span').prependTo($(this).next('p'));
			$(this).remove();
		});
	}
	
	if($('div.missingChildren').size() > 0){
		$('div.missingChildren').flash({
			swf:'http://www.missingchildren.org.ar/p/140x200.swf',
			height:200,
			width:140
		});
	}
	
	if($('#loMas').size() > 0){
		$('#loMas ul').hide();
		$('#loMas li a.active').next().show();
			
		$('#loMas li a').click(function(){
			$('#loMas li a').removeClass('active');
			$(this).addClass('active');
			
			var checkElement = $(this).next();
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#loMas ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		});
	}
	
	if($('form#encuesta').size() > 0){
		$('form#encuesta').submit(function(){
			var divContainer = $(this).parent();
			$.ajax({
				type: 'POST',
				url: 'votar.php',
				data: $(this).serialize(),
				beforeSend: function(objeto){
					$(divContainer).html('<img src="images/loadingAnimation.gif" alt="Cargando" />');
				},
				success: function(respuesta){
					$(divContainer).html(respuesta);
				}
			});
		});
	}
	
	$('input[name="q"]').focus(function(){
		if($(this).val() == 'Texto para buscar'){
			$(this).attr('value','');
		}
	});
	
	$('.frmComentarSubmitAjax').submit(function(e){
		if (typeof filters == 'undefined') return;
	    $(this).find("input, textarea, select").each(function(x,el){ 
	        if ($(el).attr("className") != 'undefined') { 
		$(el).removeClass("error");
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
	            if ($.isFunction(filters[klass]))
	                if (!filters[klass](el))  $(el).addClass("error");
	        });
	        }
	    });
		if ($(this).find(".error").size() > 0) {
			$.stop(e || window.event);
			jAlert("Necesita completar los campos marcados","El formulario no es valido");
			return false;
		}else{
			$.stop(e || window.event);
			$.ajax({
			   type: 'POST',
			   data: $('form#frmComentar').serialize(),
			   url: 'postearComentario.php',
			   beforeSend: function(obj){
					$('#ajaxResult').html('<img src="images/loadingAnimation.gif" alt="Cargando" />');
			   },
			   success: function(resp){
					$('#ajaxResult').html(resp);
					tb_init('a.thickbox, area.thickbox, input.thickbox');
			   }
			});
		}
	});
	
});