$(document).ready(function() {
    $('#cargando').ajaxStart(function() {
		$('#btnRegistrar').attr('Disabled','Disabled');
        $(this).show();
        $("#modal").show();
    }).ajaxStop(function() {
	$('#btnRegistrar').removeAttr('Disabled');
        $(this).hide();
        $("#modal").hide();
    });
	$('img').live('mousedown',function(){ return false; });
});


$(document).ready(function(){
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-19959606-4']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();



    $( "#btnLogin" ).live("click",function(){
        $.post("index.php",$("#frmLogin").serialize(),function(data){
            if(!data.exito){
                $(".login-error").html(data.error);
            }else {
                window.location = "http://interdiarios.com/";
            }
        }, 'json');
    });

    $( "#selEstados" ).change(function(){
        $.post("index.php",{
            txtEvento:"listarMunicipios",
            id:$(this).attr("value")
        },function(data){
            $("#selMunicipios").html(data);
        });
    });

    $( "#selDia" ).change(function(){
        var $textFecha=$('#selDia').attr("value")+'-'+$('#selMes').attr("value")+'-'+$('#selAnio').attr("value");
                $('#txtFecha').val($textFecha);
        $.post("index.php",{
            txtEvento:"mes",
            dia:$(this).attr("value")
        },function(data){
            $("#selMes").html(data);
        });
    });

    $( "#selMes" ).change(function(){
        var $textFecha=$('#selDia').attr("value")+'-'+$('#selMes').attr("value")+'-'+$('#selAnio').attr("value");
                $('#txtFecha').val($textFecha);
        $.post("index.php",{
            txtEvento:"anio",
            mes:$(this).attr("value")
        },function(data){
            $("#selAnio").html(data);
        });
    });

    $( "#selAnio" ).change(function(){
        $.post("index.php",{
            txtEvento:"nacimiento",
            dia:$('#selDia').attr("value"),
            mes:$('#selMes').attr("value"),
            anio:$(this).attr("value")
        }, function(data){
            if(data.exito == "false") {
                $( "#selDia" ).attr("selectedIndex",0);
                $( "#selMes" ).attr("selectedIndex",0);
                $( "#selDia" ).trigger("change");
                $( "#selMes" ).trigger("change");
            }
            else
            {
                var $textFecha=$('#selDia').attr("value")+'-'+$('#selMes').attr("value")+'-'+$('#selAnio').attr("value");
                $('#txtFecha').val($textFecha);
            }
        },"json");
    });

    $( ".politica" ).hover(function(){
        $(this).addClass("politica-hover");
    },function(){
        $(this).removeClass("politica-hover");
    });

    $( ".politica" ).click(function(){
        $("#dialog").css({"width":660,"height":700});
        $("#dialog-header").css({"width":660,"height":30});
        $("#dialog-header span").html("Terminos del servicio");
        $("#dialog-body").css({"width":660,"height":670,"overflow":"scroll"});
        $.post("index.php",{
            txtEvento:"politica"
        },function(data){
            $("#dialog-body").html(data);
            $("#modal").show();
            $("#dialog").show("fast");
        });
    });

    $( "#btnCerrar" ).live("click",function(){
        $("#dialog").hide();
        $("#modal").hide();
    });

    $( "#frmRegistro" ).validate({
        rules:{
            txtCorreo:    {
                required:true,
                email:true,
                remote:{
                    url:  "index.php",
                    type: "post",
                    data:{
                        txtEvento:"verificaUsuario",                        
                        txtCorreo:function(){
                            return $("#txtCorreo").val();
                        }
                    }
                }
            },
            selEstados:   {
                required:true
            },
            selMunicipios:{
                required:true
            },
            selSexo:      {
                required:true
            },
            cbTerminos:   {
                required:true
            }
        },
        messages:{
            txtCorreo:    {
                required:"<br />*Dato Requerido",
                email:"<br />Su correo es invalido",
                remote:"<br />Su correo ya existe"
            },
            selEstados:   {
                required:"<br />*Escoja su estado de residencia"
            },
            selMunicipios:{
                required:"<br />*Escoja su municipio de residencia"
            },
            selSexo:      {
                required:"<br />*Escoja su genero"
            },
            cbTerminos:   {
                required:"<br />*Acepte los terminos del servicio"
            }
        },
        errorPlacement:function(error,element){
            if( element.attr("name") == "selDia" || element.attr("name") == "selMes" || element.attr("name") == "selAnio" ) {
                $( "#selError" ).html(error);
            } else {
                error.insertAfter(element);
            }
        },
        submitHandler:function(){
            var options={
                url: "index.php",
                type: "post",
                data: $('#frmRegistro').formSerialize(),
                success: function(data) {
                    var validator = $("#frmRegistro").validate();
                    validator.resetForm();
                    $("#dialog").css({"width":660,"height":250});
                    $("#dialog-header").css({
                        "width":660,
                        "height":30
                    });
                    $("#dialog-header span").html("Registro");
                    $("#dialog-body").css({
                        "width":660,
                        "height":270,
                        "overflow":"hidden"
                    });
                    $("#dialog-body").html(data);
                    $("#modal").show();
                    $("#dialog").show("fast");
                }
            };
            $('#frmRegistro').ajaxSubmit(options);
            return false;
        }
    });

    $("#frmPublicacion").validate({
        rules:{                        
            selDia:       {
                required:true
            },
            selMes:       {
                required:true
            },
            selAnio:      {
                required:true
            }
        },
        messages:{            
            selDia:       {
                required:"*Fecha incompleta "
            },
            selMes:       {
                required:"*Fecha incompleta "
            },
            selAnio:      {
                required:"*Fecha incompleta"
            }            
        },
        errorPlacement:function(error,element){
            if( element.attr("name") == "selDia" || element.attr("name") == "selMes" || element.attr("name") == "selAnio" ) {
                $( "#selError" ).html(error);
            } else {
                error.insertAfter(element);
            }
        },
        submitHandler:function(){
                var $textFecha=$('#selDia').attr("value")+'-'+$('#selMes').attr("value")+'-'+$('#selAnio').attr("value");
                $('#txtFecha').val($textFecha);
            var options={
                url: "index.php",
                type: "post",
                data: $('#frmPublicacion').formSerialize(),
                success: function(data) {
                    $('#contenidoPrincipal').html(data);
                    var $fecha=$('#textoTitulo').attr('value');
                    $(document).attr('title', $fecha);                                        
                }
            };
            $('#frmPublicacion').ajaxSubmit(options);
            return false;
        }
    });

    /**cambia el background de boton o imagen a la alternativa **/
    $('.boton').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover') {            
            $(this).css("cursor","pointer");
            var $extension = "";
            var $fondo=$(this).css('background-image');
            if($fondo!="none")
            {
                $extension=$(this).css('background-image').split(".");
                $extension=$extension[$extension.length-1].substring(0,3);
                $(this).css("background-image", "url(../img/etiquetas/"+$(this).attr("name")+"2."+$extension+")");
            }
            else
            {
                $extension = $(this).attr('src').split(".");
                $(this).attr("src","../img/etiquetas/"+$(this).attr("name")+"2."+$extension[$extension.length-1]);
            }
        }
        else {            
            $(this).css("cursor","arrow");
            var $extensions="";
            $fondo=$(this).css('background-image');
            $extensions= $(this).css('background-image').split(".");
            if($fondo!="none")
            {
                $extensions=$extensions[$extensions.length-1].substring(0,3);
                $(this).css("background-image", "url(../img/etiquetas/"+$(this).attr("name")+"."+$extensions+")");
            }
            else
            {
                $extensions = $(this).attr('src').split(".");
                $(this).attr("src","../img/etiquetas/"+$(this).attr("name")+"."+$extensions[$extensions.length-1]);
            }
        }
    });
});

