jQuery(function($){
    $.datepicker.regional['es'] = {
        closeText: 'Cerrar',
        prevText: '&#x3c;Ant',
        nextText: 'Sig&#x3e;',
        currentText: 'Hoy',
        monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio',
        'Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
        monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun',
        'Jul','Ago','Sep','Oct','Nov','Dic'],
        dayNames: ['Domingo','Lunes','Martes','Mi&eacute;rcoles','Jueves','Viernes','S&aacute;bado'],
        dayNamesShort: ['Dom','Lun','Mar','Mi&eacute;','Juv','Vie','S&aacute;b'],
        dayNamesMin: ['Do','Lu','Ma','Mi','Ju','Vi','S&aacute;'],
        weekHeader: 'Sm',
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $.datepicker.setDefaults($.datepicker.regional['es']);
});


$(document).ready(function() {

$(document).keypress(function(e) {   // deshabilita enter
if(e.keyCode == 13) {
return  ;
}
});


$("#nav-shadow li ").hover(function() {  // barra de navegacion con imagenes
			var e = this;
		    $(e).find("img").stop().animate({marginTop: "-14px",width:"60px",height:"60px"}, 250, function() {
		    	$(e).find("img").animate({marginTop: "-10px"}, 250);

                });

		},function(){
			var e = this;
		    $(e).find("img").stop().animate({marginTop: "4px",width:"50px",height:"50px"}, 250, function() {
		    	$(e).find("img").animate({marginTop: "0px"}, 250);
		   });

		});



    function updateTips(t) {
        tips
        .text(t)
        .addClass('ui-state-highlight');
        setTimeout(function() {
            tips.removeClass('ui-state-highlight', 1500);
        }, 500);
    }

    function checkLength(o,n,min,max) {

        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            updateTips("Longitud de  " + n + " debe estar entre  "+min+" y "+max+".");
            return false;
        } else {
            return true;
        }

    }

    function checkRegexp(o,regexp,n) {

        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }

    }


    function enviamail(cfname,mail,mensaje) {
	var dataString = 'nombre='+ cfname + '&correo=' + mail + '&mensaje=' + mensaje;
	$.ajax({
      type: "POST",
      url: "./ajax/enviamail.php",
      data: dataString,
      success: function(datos) {
                                var $dialog = $('<div></div>')
                               .html(datos)
                               .dialog({
                                  autoOpen: false,
                                  title: 'Mensaje',
                                  height:120,
                                  closeText:'ok',
                                  modal: true,
                                  buttons: {
                                   'Cerrar': function() {
                                            $(this).dialog('close');
                                               }
                                   }
                               });
                              $dialog.dialog('open');
      }
     });

    }


    function login(n,p) {
        $.post(postFile, {
            u: n,
            p: p
        }, function(data) {
            if(data.status=="true"){               
               window.location=data.url; 
//                window.open(data.url);
            } else {
                updateTips(data.message);
                $(name).focus();
            }
        }
        ,'json');
    }
    var postFile='./include/login.post.php',  // para el login
    name = $("#name"),  // id del selector del  usuario de login
    password = $("#password"), // id del selector de la clave
    allFields = $([]).add(name).add(password),
    tips = $(".validateTips");



    var cfname = $("#cfname"),  // apellido del formulario de contacto
    mail = $("#mail"), // mail del formulario de contacto
    mensaje=$("#mensaje"),  // mensaje del formulario de contacto
    allFieldscf = $([]).add(cfname).add(mail).add(mensaje);
  


    $("#login-form").dialog({
        autoOpen: false,
        height: 315,
        width: 300,
        modal: true,

        buttons: {
            'Ingresar': function() {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLength(name,"usuario",4,16);

                bValid = bValid && checkLength(password,"clave",4,16);

                bValid = bValid && checkRegexp(name,/^([0-9a-z_])+$/i,"El nombre del usuario puede consistir de: a-z, 0-9, guion bajo.");
                bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"La Clave solo admite : a-z 0-9");

                if (bValid) {
                    login (name.val(),password.val());


                //$(this).dialog('close');
                }
            },
            'Cancelar': function() {

                $(this).dialog('close');
            }
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
    });



    $("#contact-form").dialog({
        autoOpen: false,
        height: 560,
        width: 550,
        modal: true,
        buttons: {
            'Enviar': function() {
                var bValid = true;
                allFieldscf.removeClass('ui-state-error');
                bValid = bValid && checkLength(cfname,"apellido",3,50);               
bValid = bValid && checkRegexp(mail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"ejemplo. aa@algo.com");
bValid = bValid && checkLength(mensaje,"mensaje",10,200);

                if (bValid) {
                   
                   enviamail(cfname.val(),mail.val(),mensaje.val());

                    $(this).dialog('close');
                }
            },
            'Cancelar': function() {

                $(this).dialog('close');
            }
        },
        close: function() {
            allFieldscf.val('').removeClass('ui-state-error');
        }
    });
  

//    $("#buscapro-form").dialog({
//        autoOpen: false,
//        height: 300,
//        width: 360,
//        modal: true,
//        buttons: {
//            'Buscar': function() {
//                var profe=$('#profe')
//                 var bValid = true;
//                  allFields.removeClass('ui-state-error');
//
//                bValid = bValid && checkLength(profe,"apellido",3,16);
//         if (bValid) {
//             buscaprofe(profe.val());
//         }
//          return false;
//                //$(this).dialog('close');
//                
//            },
//            'Cancelar': function() {
//                $(".validateTips").empty();
//                $(this).dialog('close');
//            }
//        },
//        close: function() {
//            allFields.val('').removeClass('ui-state-error');
//             $(".validateTips").empty();
//        }
//    });

//function buscaprofe(profe){
//     $.ajax({
//            data: "profe="+profe,
//            type: "POST",
//            url: 'buscaprofe2.php',
//            success: function(datos){
//                     $(".validateTips").html(datos);
//                    return false;
//
//
//            }
//        });
//
//
//}




    $('#usuarios,.usuarios')          //imagen de  acceso arriba, para activar login
    .click(function(){
        $(".validateTips").empty();
 
        $('#login-form').dialog('open');
        $(".validateTips").empty();
        $("#name").empty().focus();
    });




    $('#salir')   
    .click(function(){
        window.location= 'index.php?logoff=1';
    });

    $('#contacto,.contacto')
       .click(function() {
        $('#contact-form').dialog('open');
        $("#cfname").focus();
            });

//      $('#contactoinferior')
//    .click(function() {
//        $('#contact-form').dialog('open');
//           });

//
//    $('#busprofe')
//    .click(function() {
//        $(".validateTips").empty();
//        $('#buscapro-form').dialog('open');
//        $("#cfname").focus();
//    });


 $('#busprofes').button().click(function(){             // busca  profesional 
        var profe=$('#profe').val();
      
//        $('#contenido').empty();
//        ImgCargaShow("contenido");
      if (profe){  $.ajax({
            data: "profe="+profe,
            type: "POST",
             url: 'buscaprofe2.php',
            success: function(datos){
                if (datos=="Profesional Inexistente!"){
                     }
                    var $dialog = $('<div></div>')
                    .html(datos)
                    .dialog({
                        autoOpen: false,
                        title: 'Resultado de la Búsqueda',
                        height:240,
                        width:320,
                        closeText:'ok',
                        modal: true,
                        buttons: {
                            'Cerrar': function() {
                                $(this).dialog('close');
                            }
                        }
                    });
//                    ImagenCargaBorra();
                    $dialog.dialog('open');
//                    $("#contenido").hide();
                    return;
               
//                $("#contfijo").hide();
//                $(".validateTips").show().html(datos);
//                $.getScript("./js/jquery.jprint.js");
//                $.getScript("./js/jquery-1.6.2.min.js");
            }
        });
      }
        return false;
    });

$('#busprofe')
    .click(function() {
        $(".validateTips").empty();
        
        $('#buscapro-form').dialog('open');
       
    });

    $.ajaxSetup ({
        cache: false
    });
    var ajax_load = "<img class='loading' src='./js/img/loading.gif' alt='Cargando...' />";

// enlaces a los congresos y hpc
$(".iframe").fancybox({
				'width'				: '100%',
				'height'			: '100%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});


$("#enlaces2").tooltip({
                         effect: 'slide',
                         position: "bottom center",
                         tip:'.tipenlaces'});

$("#nav-shadow li img[title]").tooltip({
                         effect: 'slide',
                         position: "bottom center"
                        });


}); // fin document ready




