//**************** FUNCION PARA OBTENER OBJETOS POR CLASE ****************/
document.getElementsByClassName = function(cl){
    var retnode = [];
    var myclass = new RegExp('\\b' + cl + '\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) 
            retnode.push(elem[i]);
    }
    return retnode;
};
//FUNCION ENCUESTAS
//objeto globo con pregunta
var globo = new Array();
$('.globo').ready(function(){
	var temp = $('.globo').html();
	globo.push('<div class="globo clearfix">'+temp+'</div>');
});
function agregaNumeros(){
	if ($('.wp-polls-ul li') != null) {
		var preguntas = new Array();
		var preguntas = $('.wp-polls-ul li');
		var contenidoPreguntas = new Array();
		jQuery.each(preguntas, function(i){
			var temp = $('.wp-polls-ul li').eq(i).html();
			var nuevoContent = '<span><strong>' + (i + 1) + '.' + '</strong></span>' + temp;
			$('.encuestas .wp-polls-ul li').eq(i).html(nuevoContent);
		});
	}
}
function cambiaContenido(){
	if ($('.encuestas .EncuestasHome')[0] != null) {
		var respuestas = new Array();
		var respuestas = $('.wp-polls-ul').html();
		var votos = $('.wp-polls h4').html();
		var enlacePool = '<a class="sigueLeyendo sigueDetallePost" href="./pollsarchive">Ver encuestas anteriores</a>';
		//$('.wp-polls-ans').html(globo+'<ul class="wp-polls-ul">'+respuestas+'</ul>'+'<h4>'+votos+'</h4>'+ enlacePool);
		//agrega numeros
		var preguntas = new Array();
		var preguntas = $('.numeroLista');
		jQuery.each(preguntas, function(i){
			var nuevoContent = '<strong>' + (i + 1) + '.' + '</strong>';
			$('.numeroLista').eq(i).html(nuevoContent);
		});
	}
}
//AGREGANDO NUMEROS POLLSARCHIVE
$(document).ready(function(){
	var encuestas = new Array();
	var encuestas = $('.wp-polls-ul');
	var numeroPreguntas = new Array();
	//var preguntas = new Array();
	jQuery.each(encuestas, function(i){
		numeroPreguntas.push($(encuestas[i]).find('.numeroLista'));
	});
	jQuery.each(encuestas, function(i){
		jQuery.each(numeroPreguntas[i], function(e){
			var numeroPregDef = e+1;
			numeroPreguntas[i][e].innerHTML = '<strong>'+ (e+1)+' .</strong>' ;
		});
	});
});
//TEXTOS SEDAL ACONSEJA
$(document).ready(function(){
	if ( $('#textoSedalAconseja') != null) {
		var textos = $('#textoSedalAconseja');
		if (textos[0] != null) {
			var textoActual = $('#textoSedalAconseja').text();
			for (var i = 0; i < textosSabiasQue.length; i++) {
				$('#textoSedalAconseja').append("<p class=" + "Pconsejos" + " style=" + "display:none" + ">" + textosSabiasQue[i] + "</p>");
				$('.Pconsejos:first').css("display", "block");
			}
			var iTips = 0;
			$('#verMasTips').click(function(){
				iTips++;
				$('.Pconsejos').hide().eq(iTips).show();
				
				if (iTips == $('.Pconsejos').length - 1) {
					iTips = -1;
				}
			});
		}
		else {
			return false;
			
		}
	}else {
		return false;
	}
});

//VALIDAR COMENTARIOS
/*function contar(texto, e){
	console.log(e.which	== 17);
	if (texto.length > 2000) {
		if (navigator.appName == "Netscape") 
			tecla = e.which
		else 
			tecla = e.keyCode
		if (tecla != 8) 
		return false
	}
	return true;
}*/

/*function contar(value, evt){
	var key = evt.keyCode ? evt.keyCode : evt.which ;
	if (value.length > 10) {
		return (key == 8) || (key == 127);
	} else {
		return (key <= 31 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key == 8) || (key == 127));
	}
}*/
function maximo(evento, elemento){
			var key = evento.keyCode ? evento.keyCode : evento.which ;
			if(key == 8) return true;
			return (elemento.value.length <= 2000)?true:false;
		} 

$(document).ready(function(){
	if($('#commentform') != null){
		$('.btnPublicarComentario').click(function(){
			//Rescatando valores
			var nombre = $('#author').val();
			var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#commentform').find('input[name=email]').val());
			//var sitioWeb = $('#url').val();
			var comentario = $('#comment').val();
			
			//validando
			if(nombre == "" || nombre == " " || nombre == "  "){
				$('.alertaNombre').fadeIn('slow');
			}else if(nombre != "" && email == false){
				$('.alertaMail').fadeIn('slow');
			}else if(nombre != "" && email && comentario == "" || comentario == " " || comentario == "   "){
				$('.alertaComentario').fadeIn('slow');
			}else {
				document.forms[2].submit();
				return false;
			}
			return false;
		});
	}else {
		return false;
	}
	//OCULTANDO ALERTAS
	if ($('#formComentarios') != null) {
		
		//oculta alerta nombre
		$('#author').focus(function(){
			var alertaNombre = $('.alertaNombre')[0].style.display;
			if(alertaNombre == "block"){
				$('.alertaNombre').fadeOut();
			}
		});
		
		//oculta alerta mail
		$('#email').focus(function(){
			var alertaMail = $('.alertaMail')[0].style.display;
			if(alertaMail == "block"){
				$('.alertaMail').fadeOut();
			}
		});
		
		//oculta alerta comentario
		$('#comment').focus(function(){
			var alertaComentario = $('.alertaComentario')[0].style.display;
			if(alertaComentario == "block"){
				$('.alertaComentario').fadeOut();
			}
		});
	}
});

//PRIMERA LETRA GRANDE EN POST
// $(".postContent").html($(".postContent").html().replace(/\<p\>(\n|\r|\s)*([A-Za-z0-9])/, "<p><span style='font-size: 2.2em'>$2</span>"));

$(document).ready(function(){
	if ($('.more-link') != null) {
		//$('.more-link').parent().remove();
	}
	if($('.imgPost') != null){
		var imagenes = new Array();
		var imagenes = $('.imgPost');
		jQuery.each(imagenes, function(i){
			if (imagenes[i].width >= "460") {
				imagenes[i].width = "460";
			}
			else {
				if (imagenes[i].height >= "400") {
					imagenes[i].height = "400";
				}
			}
		});
	}
});

//IMAGEN LO MAS VISTO
$('document').ready(function(){
	var imagenesOcultas = new Array();
	var imagenesOcultas = document.getElementsByClassName('imagenOculta');
	var enlaces = new Array();
	//obteniendo enlaces y metiendolas en arreglo 'enlaces'
	jQuery.each(imagenesOcultas , function(i){
		var temp = imagenesOcultas[i].src;
		enlaces.push(temp);
	//poniendo url en imagenes
	jQuery.each(enlaces, function(i){
		$('.imgPost').eq(i).attr('src', enlaces[i]);
	});
	});
});

//ENVIANDO ENCUESTA
$(document).ready(function(){
	if ($('.seleccionResp') != null) {
		$('.encuestas .btnDesact').click(function(){
			$('.alertaEncuestas').show();
		});
		$('.seleccionResp').click(function(){
			$('.btnOpinar').removeClass('btnDesact');
			$('.btnOpinar').addClass('btnNegro');
			$('.alertaEncuestas').hide();
			//obtiene rel
			var EnviaPool = $('.btnOpinar').attr('rel');
			$('.btnOpinar').click(function(){
				$('.btnOculto').trigger('click');
				$('.alertaEncuestas').hide();
			});
		});
	}
});

//TAGET BLANK
$(document).ready(function(){
	$('._blank').attr('target','_blank');
});

//fix segunda caja experto home
$(document).ready(function(){
	if($('.cajaExperto') != null){
		$('.cajaExperto').eq(1).addClass('segundaCaja');
	}
});

//fecha comentarios
$(document).ready(function(){
	$('.comentarioFecha').each(function(){
		var asd = ["","Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
		var fecha = $(this).text().split('-');
		
		var out = fecha[0] + " de " + asd[parseInt(fecha[1])] + " de " + fecha[2];
		$(this).html(out);
	});
	
	$('.postFecha').each(function(){
		var asd = ["","Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
		var fecha = $(this).text().split('/');
		
		var out = fecha[0] + " de " + asd[parseInt(fecha[1])] + " de " + fecha[2];
		$(this).html(out);
	});
	
});



