// JavaScript Document

function iniciarPublicacion(){
	
	tb_remove('bienvenida');
}
function tb_remove_bienvenida() {
	
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function selecTipoRefer(valor, textoobligatorio,pregunta){
	//alert(valor+"-"+textoobligatorio+"-"+pregunta);
	
	if(textoobligatorio=='1'){
		
		$("#textoPregunta").css('display','block');
		$("#textoPregunta").html(pregunta);
		$("#text_pregunta").css('display','block');
	}
	if(textoobligatorio=='0'){
		$("#textoPregunta").css('display','none');
		$("#textoPregunta").html('');
		$("#text_pregunta").css('display','none');
	}
	document.getElementById("idTipo").value=valor; 
	document.getElementById("flag_texto").value=textoobligatorio; 
	
}

function enviaReferencia(){
	
	var flag_tipo	=document.getElementById("idTipo").value; 
	var flag_texto	=document.getElementById("flag_texto").value; 
	var cont_texto	=document.getElementById("text_pregunta").value; 
	if(flag_tipo==''){
		alert('Selecciona una de las opciones');
		
	} else if(flag_texto=='1' && cont_texto==''){
		alert('Debes responder a la pregunta');
		document.getElementById("text_pregunta").focus();
		
	} else {
	
	var j_tipo	=document.getElementById("idTipo").value;  
	var j_texto	=document.getElementById("text_pregunta").value;
	j_accion="referencia";
	$.post("librerias/lb_boletin.php",{texto:j_texto,tipo:j_tipo,accion:j_accion},
					  function(data){ 
						  if(data=='0'){
							alert("Hubo un problema al registrar los datos. \nPor favor refresque la ventana de su navegador e intente nuevamente.");
						  } else {
							 alert("Gracias por colaborar con nosotros.");
							tb_remove(data);
							//$("a#wellcomebox").click();
						  }
					}); 
	}
}

function  selecTipo(valor){
	document.getElementById("idTipo").value=valor;  //alert(j_email); 
}

function suscribeBoletin(){

	var j_email		=document.getElementById("txt_email_bolet").value;  //alert(j_email); 
	var j_nombre	=document.getElementById("txt_nombre_bolet").value;  //alert(j_email); 
	var j_tipo		=document.getElementById("idTipo").value;  //alert(j_email); 
	
	var msg=$("#div_respuesta_msg");
	if(!j_nombre) {
		msg.html("Ingresa tu nombre!");
		$("#txt_nombre_bolet").focus();
		  }
	else if(!j_email) {
		msg.html("Ingresa tu e-mail!");
		$("#txt_email_bolet").focus();
		  }
		 else if(!valida_email_bol(j_email)) { 
			msg.html("Debes Ingresar un email valido!");
			$("#txt_email_bolet").focus();
			} 
			else if(j_tipo=='') { 
			msg.html("Selecciona qué tipo de usuario eres!");
			
				} else { 
				msg.html("Enviando petición...");	
				j_accion="suscribir";
				$.post("librerias/lb_boletin.php",{email:j_email,nombre:j_nombre,tipo:j_tipo,accion:j_accion},
					  function(data){
						  if(data=='1'){
							msg.html("El e-mail ya ha sido registrado antes!");
						  } else {
							var rpta=$("#suscrip_boletin");
							rpta.html(data);
							//document.getElementById("txt_email").value		='';
						  }
					}); 

				  }

}

function valida_email_bol(valor)
{
	var reg=/(^[a-zA-Z0-9._-]{1,30})@([a-zA-Z0-9.-]{1,30})[.][a-zA-Z0-9.-]{1,30}($)/;
	if(reg.test(valor)) return true;
	else return false;
}


$(function () {
        $('.bubbleInfo').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $('.trigger', this);
            var info = $('.popup', this).css('opacity', 0);


            $([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;
					
					var pos_izq = (screen.width-600)/2;
					
                    info.css({
                        top: 35,
						left: pos_izq,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });
