// funciones Librerias


// para agregar eventos 
// Creamos la funcion para a�adir eventos
function nuevo_evento(elemento, evento, funcion) {
      if (elemento.addEventListener) 
            elemento.addEventListener(evento, funcion, false);
    	else 
            elemento.attachEvent("on"+evento, funcion);
}




// limitar numero caracteres texarea http://www.forosdelweb.com/showpost.php?p=499235&postcount=147 {modificado}
// onkeypress="return maximaLongitud(this,254)"
function maximaLongitud(e,texto,maxlong) {
  var tecla, in_value, out_value;
  tecla = (document.all) ? e.keyCode : e.which;
  if(tecla==8) 
  	return true;
  else if (texto.value.length > maxlong) 
    return false;

  return true;
}


// documentales llenos diciendo que era un caos esa situacion del mouse
// infromacion :: Cross browser ::
// source: http://evolt.org/article/Mission_Impossible_mouse_position/17/23335/index.html     = documental
// source: http://www.quirksmode.org/js/events_compinfo.html   = compatibilidades

function doSomething(e,obtener)
{
   var docX, docY;
   if( e )
   {
      if( typeof( e.pageX ) == 'number' )
      {
         docX = e.pageX;
         docY = e.pageY;
      }
      else
      {
         docX = e.clientX;
         docY = e.clientY;
      }
   }
   else
   {
      e = window.event;
      docX = e.clientX;
      docY = e.clientY;
      if( document.documentElement
        && ( document.documentElement.scrollTop
            || document.documentElement.scrollLeft ) )
      {
         docX += document.documentElement.scrollLeft;
         docY += document.documentElement.scrollTop;
      } 
      else if( document.body
         && ( document.body.scrollTop
             || document.body.scrollLeft ) )
      {
         docX += document.body.scrollLeft;
         docY += document.body.scrollTop;
      }
   }
      
	if(obtener=='X')
	return docX;
	else if(obtener=='Y')
	return docY;
}


	// funciones para AJAX
function createRequestObject() {
   var ro;
   var browser = navigator.appName;
   if(browser == "Microsoft Internet Explorer"){
       ro = new ActiveXObject("Microsoft.XMLHTTP");
   }else{
       ro = new XMLHttpRequest();
   }
   return ro;
}





// FUNCIONES PERSONALZIADAS

// insetar emotions textarea
function poner(texarea,texto) {
	texarea.value += texto;
}


/* pais */
function change_pais(elemento){
	var pais;
	pais=elemento.value;
	ajax_change_pais(pais);
	}

function ajax_change_pais(ident){
		idn_change_pais = createRequestObject();
		idn_change_pais.open('get', 'ajax_estados.php?pais='+ident);
		idn_change_pais.onreadystatechange = handleResponsepais;
		idn_change_pais.send(null);
	}

function handleResponsepais() {
   if(idn_change_pais.readyState == 4){
       var response = idn_change_pais.responseText;
		eval(response);
   }
}






/* para los dropsdows */

function addlist(listid,listext,listvalue){
	var coptions;
	coptions=document.getElementById(listid).options.length;// cantidad de options
	document.getElementById(listid).options[coptions]=new Option(listext,listvalue); // para agregar un nuevo elemento al princio
	}
	
function dellist(listid){
	document.getElementById(listid).options.length=0; // para resetar la lista en 0
	}


// para preguntar antes de eliminar
// onclick="return false,eliminar_link(' esta foto')";
function eliminar_link(txt){
	if(confirm("�Seguro que desea "+txt+"?"))
	  	return true;
	else
	   	return false;
	   }
	   

encuest_protect = function(){
	var encues_formulario=document.getElementById("encus_dia");
	if(encues_formulario){
		var total_opciones=encues_formulario.respuesta.length;
		encues_formulario.onsubmit=function(){
			valid=false;
			for(i=0;i<total_opciones;i++){
					if(encues_formulario.respuesta[i].checked)
						valid=true;
				}
				
			if(valid)
				return true;
			else{
				alert("Favor de seleccionar una opci�n")
				return false;
				}			
			}
		}
	}




avatar_title_window = function(){
  if (!document.getElementsByTagName) return false;
  
  if(!document.getElementById("avat_win_title")){
	 nueva_div=document.createElement("div"); 
		nueva_div.id="avat_win_title";
			nueva_div_p=document.createElement("p"); 
				nueva_div.appendChild(nueva_div_p);
			document.body.appendChild(nueva_div); 
	  }
  
  var enlaces = document.getElementsByTagName("a");
  for (i=0;i<enlaces.length;i++) {
    if (enlaces[i].className.match("win_title")) {
    	var temp_texto=enlaces[i].title;
    	temp_texto=temp_texto.replace("-", "<br />");
		enlaces[i].texttile=temp_texto;
		enlaces[i].title="";
		
		enlaces[i].onmouseover=function (){
			nueva_div.style.display="block";
			nueva_div.lastChild.innerHTML=this.texttile;
			}
		enlaces[i].onmouseout=function (){
			nueva_div.style.display="none";
			}
		enlaces[i].onmousemove=function (event){
			mosue_x=doSomething(event,'X');
				nueva_div.style.left=(mosue_x)+15+"px";
			mosue_y=doSomething(event,'Y');	
				nueva_div.style.top=(mosue_y)+15+"px";
			}
		
	}
  }
}



replyCommnNotes = function(){
	$("#comentarios .reply a").click(function(){
			idTempClick=$(this).attr("href").replace("#","");			
			$("#comentarios #ListFrm"+idTempClick).show();
			$("#comentarios #replyFrm"+idTempClick).slideDown("slow");
			$(this).hide();
			return false;
	});	
}

limiparLogos=function(){
	$("#bloqueConexion .rd").css("font-weight","normal");
	$("#bloqueConexion .tw").css("font-weight","normal");
	$("#bloqueConexion .fb").css("font-weight","normal");
}

facebookLogin=function(){
    if(FB.Connect.get_loggedInUser()==null){
         FB.Connect.requireSession();
         FB.Facebook.get_sessionState().waitUntilReady(function(){
             facebookLogin();
         });
    }else{
        limiparLogos();
        $("#bloqueConexion .fb").css("font-weight","bold");
        $("#bloqueConexion .lognRed").css("display","none");
        $("#bloqueConexion .lognTw").css("display","none");
        $("#bloqueConexion .lognFb").css("display","block");
        $("#bloqueConexion .tipo").val("2");
    }
}

loginVarios = function(){
	$("#bloqueConexion .rd").css("font-weight","bold");
        $("#bloqueConexion .lognRed").css("display","block");
	

	$("#bloqueConexion .rd").click(function(){
		limiparLogos();
		$(this).css("font-weight","bold");
		$("#bloqueConexion .lognRed").css("display","block");
		$("#bloqueConexion .lognTw").css("display","none");
		$("#bloqueConexion .lognFb").css("display","none");
		$("#bloqueConexion .tipo").val("0");
		return false;
	});
	$("#bloqueConexion .tw").click(function(){
		limiparLogos();
		$(this).css("font-weight","bold");
		$("#bloqueConexion .lognRed").css("display","none");
		$("#bloqueConexion .lognTw").css("display","block");
		$("#bloqueConexion .lognFb").css("display","none");
		$("#bloqueConexion .tipo").val("1");
		return false;
	});
	$("#bloqueConexion .fb").click(function(){
                facebookLogin();
		return false;
	});	
	
	
}


nuevo_evento(window,"load",avatar_title_window);
nuevo_evento(window,"load",encuest_protect);
nuevo_evento(window,"load",replyCommnNotes);

$(document).ready(function(){
    loginVarios();
});