
$(document).ready(loadSite);

function votar(idpost,tvoto){
	var voto;
	if (tvoto=='positivo'){
		voto=1;
	}
	else{
		voto=0;
	}

	// imprimimos la cookie
	datosPost="idpost=" + idpost + "&voto=" + voto;
	$.ajax({
		type: "POST",
	   url: "../procesos/votar.php",
	   data: datosPost,
	   success: function(datos){
			var arrDatos = new Array();
			arrDatos=datos.split("--");
			if (arrDatos[0].indexOf("true") > -1){

				// obtenemos el ID de la capa aumentar en 1 
				var capaAumentar,nombreCapa;
				if (voto==1){
					nombreCapa="#pcount" + idpost;
					capaAumentar=$(nombreCapa);
					valor= capaAumentar.text();
					valor=parseInt(valor) + 1;
				}
				else{
					nombreCapa="#ncount" + idpost;
					capaAumentar=$(nombreCapa);
				}
					
				valor= capaAumentar.text();
				valor=parseInt(valor) + 1;
				capaAumentar.fadeOut("fast");
				capaAumentar.text(valor);
			 	capaAumentar.fadeIn("slow");
				 mensajeVotar(1,idpost);
			}
			else {
				mensajeVotar(0,idpost);
			}
		}
	});
}

function votarC(idcomentario, idusuario, tvoto){
	var voto;
	if (tvoto=='positivo'){
		voto=1;
	}
	else{
		voto=0;
	}

	// imprimimos la cookie
	datosPost="idcomentario=" + idcomentario + "&voto=" + voto;
	$.ajax({
		type: "POST",
	   url: "../procesos/votarConsejero.php",
	   data: datosPost,
	   success: function(datos){
			var arrDatos = new Array();
			arrDatos=datos.split("--");
			if (arrDatos[0].indexOf("true") > -1){

				// obtenemos el ID de la capa aumentar en 1 
				var capas,nombreCapa;
				
				nombreCapa=".pc" + idusuario;
				capas=$(nombreCapa);
				
				capas.each(function(index) {
					valor = $(this).text();
	
					if (voto==1){
						valor=parseInt(valor) + 1;
					}
					else{
						valor=parseInt(valor) - 1;
					}
						
					$(this).fadeOut("fast");
					$(this).text(valor);
				 	$(this).fadeIn("slow");
        
				});

				mensajeCVotar(1,idcomentario);
			}
			else {
				mensajeCVotar(0,idcomentario);
			}
		}
	});
}

function reportar(idpost){

	// imprimimos la cookie
	datosPost="idpost=" + idpost;
	$.ajax({
		type: "POST",
	   url: "../procesos/reportar.php",
	   data: datosPost,
	   success: function(datos){

			// obtenemos el ID de la capa aumentar en 1 
			var capaAumentar,nombreCapa;
				
			nombreCapa="#reported" + idpost;
			capaAumentar=$(nombreCapa);
	   	
	   	var arrDatos = new Array();
			arrDatos=datos.split("--");
			if (arrDatos[0].indexOf("true") > -1){
				capaAumentar.fadeOut("fast");
				capaAumentar.text('Reportado ok!');
			 	capaAumentar.fadeIn("slow");
				chau(nombreCapa);
			}
			else {
				capaAumentar.fadeOut("fast");
				capaAumentar.text('Ya habias reportado esta...');
			 	capaAumentar.fadeIn("slow");
				chau(nombreCapa);
			}
		}
	});
}

var capaVotar=$("#msgVotar");
function mensajeVotar(estado,id){
	var nombre="#msgVotar" + id;
	var capaVotar=$(nombre);
	
	if (estado==1){
		capaVotar.fadeIn("slow");
		capaVotar.css("color","green");
		capaVotar.text("El confesionario se ha expresado!");
		//$(this).delay(1000,function(){
		chau(nombre);
		
		
	}else{
		capaVotar.fadeIn("slow");
		capaVotar.css("color","red");
		capaVotar.text("Ya votaste esta confesion!");
		chau(nombre);
	}
}

function mensajeCVotar(estado,idcomentario){
	var nombre="#pcmsg" + idcomentario;
	var capaMsg=$(nombre);
	
	if (estado==1){
		capaMsg.fadeIn("slow");
		capaMsg.css("color","green");
		capaMsg.text("voto contado!");
		//$(this).delay(1000,function(){
		chau(nombre);
	}
	else{
		capaMsg.fadeIn("slow");
		capaMsg.css("color","red");
		capaMsg.text("Ya habias votado este comentario!");
		chau(nombre);
	}
}


function chau(idcapa){
	var capaVotar=$(idcapa);
	// Espera de 1 segundo...
	capaVotar.fadeOut(5000);
}

function loadSite(){
	var botonSearch;
 	botonSearch=$("#boton1");
  	botonSearch.click(pasarDatos);
	if (gup ('id')!=""){
	  setValue("tx_search_id",gup ('id'));
	}
	if (gup ('sexo')>-1){
	  seleccionarItemById("sexo",gup ('sexo'));
	}
	  if (gup ('orden')>-1){
	  seleccionarItemById("orden",gup('orden'));
	}
	
	$('.report, .consejero, .superconsejero').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250 
	});
	
	$('.tmasCom').focus(function(){
		$(this).val('');
		$(this).css('height', '200px');
	});
	
	$('.tmasCom').blur(function(){
		$(this).css('height', '30px');
	});
	

	$(".newsticker-jcarousellite").jCarouselLite({
		vertical: true,
		hoverPause:true,
		visible: 5,
		auto:1500,
		speed:5500
	});

	$('.closeable').click(function(){
		$(this).parent().css('display', 'none');
		tcOverlayClose();
	});
	
} 


function pasarDatos(){
	var datos;
	datos="id=" + limpiar(getValue("tx_search_id","")) + "&sexo=" + 
	limpiar(getValue("sexo","combo")) + "&orden=" + limpiar(getValue("orden","combo"));
	location.href="/index.php?" + datos;
}

// funciones comunes que utilizo 
function getFechaBySql(fecha){

	/* recibe una fecha en formato de texto
	 * que sacamos de Mysql y la devolvemos como fecha
	 * de javascript
	*/
	if (fecha!=""){
	var ano=fecha.substr(0,4)
	var mes=fecha.substr(5,2);
	var dia=fecha.substr(8,2);
	var nuevaFecha=new Date(ano,mes,dia);
	
	return(nuevaFecha);
}
}

function obtenerStringMes(numeroMes){

	switch (numeroMes){
		case "1":
			return("Enero");
		break;
		case "2":
			return("Febrero");
		break;
		case "3":
			return("Marzo");
		break;
		case "4":
			return("Abril");
		break;
		case "5":
			return("Mayo");
		break;
		case "6":
			return("Junio");
		break;
		case "7":
			return("Julio");
		break;
		case "8":
			return("Agosto");
		break;
		case "9":
			return("Septiembre");
		break;
		case "10":
			return("Octubre");
		break;
		case "11":
			return("Noviembre");
		break;
		case "12":
			return("Diciembre");
		break;
		default:
			return("El numero es..." + numeroMes);
		break;
	}	
}

function getValue(elemento,tipo){
	if (document.getElementById(elemento)){
		if (tipo=="combo"){
			combo= document.getElementById(elemento);
		return(combo[combo.selectedIndex].value);
			}else{
				
				return(document.getElementById(elemento).value);
			}
		
	}else{
		return("");
	}
	}
	
	function getRadioButtonSelectedValue(ctrl)
{
    for(i=0;i<ctrl.length;i++)
        if(ctrl[i].checked) return ctrl[i].value;
}

function setValue(elemento,valor){
	if (document.getElementById(elemento)){
			document.getElementById(elemento).value=valor;
	}
}

	function limpiar(cadena){
		return(encodeURIComponent(cadena));
	}


// VALIDAMOS UNA DIRECCION DE EMAIL 
//

function isEmailAddress(email)
{
  if( !(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(email)) ) {
   alert("El email no es correcto");
  return false;

}
else{
   return true;
}

}

function seleccionarItem(controlSelect,cadena){
	
	var obj=document.getElementById(controlSelect);
   if (obj){
	 for (var opcombo=0;opcombo < obj.length;opcombo++){
          if(obj[opcombo].text.toLowerCase()==cadena.toLowerCase()){
          obj.selectedIndex=opcombo;
          }
       } 
      }else{
      	//alert("no se encuentra el control" + controlSelect);
	}
}

function seleccionarItemById(controlSelect,id){
    //alert(controlSelect + "y el id es.." + id);
	var obj=document.getElementById(controlSelect);
	 for (var opcombo=0;opcombo < obj.length-1;opcombo++){
          if(obj[opcombo].value==id){
          obj.selectedIndex=opcombo;
          }
       }
}

function str_len(cadena){
	return(cadena.length);
}

function get_state(cadena){
	var arr_cadena=cadena.split("--");
	var arr_formado=new Array();
	for (x=0;x<count(arr_cadena)-1;x++){
		if (x==0){
			arr_formado['estado']=arr_cadena[x];
			}else{
				arr_formado['mensaje']=arr_cadena[x];
				}
	}
	return(arr_formado);
}

function count(array){
	return(array.length);
}

function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}

function tcOverlayShow(){
	$('#tc_overlay').css('display', 'block');
}

function tcOverlayClose(){
	$('#tc_overlay').css('display', 'none');
	$('#facebook_logged').css('display', 'none');
}


