function calcular_vales(){
    var tiempo;
    var v;
    var a = $("#hasta").val().split(':');
    var b = $("#desde").val().split(':');
    if(false){
        $("#consumidos").attr('value') = 0;
    }
    else{
        if(a[0] == '07')
            a = 7;
        else if (a[0] == '08')
            a = 8;
        else if(a[0] == '09')
            a = 9;
        else
            a = parseInt(a[0]);

        if(b[0] == '07')
            b = 7;
        else if (b[0] == '08')
            b = 8;
        else if(b[0] == '09')
            b = 9;
        else
            b = parseInt(b[0]);

        if (a > b){
            tiempo = a - b;
            box = eval("document.Reservar.extra");
            if (box.checked == false && b <= 12 && a >= 14)
                tiempo = tiempo-2;
            v = Math.ceil(tiempo/4);
            $("#consumidos").val(v * document.Reservar.consecsem.value * document.Reservar.consecdia.value);
        }
        else{
            $("#consumidos").val(1);
        }
        if (b <= 12 && a >= 14){
            document.getElementById("extra").disabled   =   false;
        } else {
            document.getElementById("extra").disabled   =   true;
            document.getElementById("extra").checked    =   false;
        }

    }
}

$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 100;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -( popup.height()+ 20),
          left: -90,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

$(document).ready(function(){

    $(".esconder").css( "display", "none" );
    
    //$(".tablesorter").tablesorter();
    
    $('.timeRange').timeEntry({
        showSeconds: true,
        beforeShow: customRange,
        beforeSetTime: firstHalfHourOnly
    }); 

    $(".calendario").datepicker({ 
        showOn: "both",
        dateFormat: "yy-mm-dd",
        buttonImage: url + "images/calendar.gif",
        buttonImageOnly: true 
    });

    $("#startDate,#endDate").datepicker({ 
        beforeShow: function(input){
            return { minDate: (input.id == "endDate" ? $("#startDate").datepicker("getDate") : null), 
                maxDate: (input.id == "startDate" ? $("#endDate").datepicker("getDate") : null)}; 
        }, 
        showOn: "both", 
        dateFormat: "yy-mm-dd", 
        buttonImage: url + "images/calendar.gif",
        buttonImageOnly: true 
    });

    $(".cancelar").bind("click", function(){
        if (confirm("¿Seguro que desea eliminar este registro?"))
            return true;
        else
            return false;
    });

    $(".rechazar").bind("click", function(){
        if (confirm("¿Seguro que desea rechazar esta reserva?"))
            return true;
        else
            return false;

    });

    $("#TipoUsuarioConsult").bind("change", function(){
        $.get(url + "index.php/users/get_users/" + $(this).val(), function(out){
            $("#usuario").html(out);
        });
    });

    $("#modificarFechas").bind("click", function(el){
        $(".esconder").toggle();
        if( $("#updateActivo").val() == "1" )
            $("#updateActivo").val("0");
        else
            $("#updateActivo").val("1");
    });

    $("#asignarVales").bind("click", function(){
        var a = $("#valesDependencia").val();
        var b = $("#valesAuditorio").val();
        if( a == 0 || b == 0){
            $("#valesEntregados").html('0');
            $("#valesUsados").html('0');
        } else {
            $.get( "update/"  + a + "/" + b + "/" + $("#agregar").val()  , function(out){
                actualizarVales();
            });
        }
    });

    $("#valesDependencia, #valesAuditorio").bind("change", actualizarVales );

    function actualizarVales(){
        var a = $("#valesDependencia").val();
        var b = $("#valesAuditorio").val();
        if( a == 0 || b == 0){
            $("#valesEntregados").html('0');
            $("#valesUsados").html('0');
        } else {
            $.get( "get_used_vouchers/" + a + "/" + b  , function(out){
                out = out.split('|');
                $("#valesEntregados").html(out[0]);
                $("#valesUsados").html(out[1]);
            });
        }
    }


    $('.vales, #desde, #hasta').bind('change', calcular_vales );

    $('.medio_dia').bind('change', function(){
        var a = $("#horaini").val();
        var b = $("#horafin").val();
        a = parseInt(a);
        b = parseInt(b);
        if (a <= 12 && b >= 14){
            document.getElementById("extra").disabled   =   false;
        } else {
            document.getElementById("extra").disabled   =   true;
            document.getElementById("extra").checked    =   false;
        }
    });
    
    $('#mesa').bind('click', function(){
        $("#npersonas").toggle();
        $("#mesap").toggle();
    });
    
    function customRange(input) { 
        return {minTime: (input.id == 'hasta' ? $('#desde').timeEntry('getTime') : null),  
            maxTime: (input.id == 'desde' ? $('#hasta').timeEntry('getTime') : null)}; 
    }
    
    function firstHalfHourOnly(oldTime, newTime, minTime, maxTime) { 
        var increment = (newTime - (oldTime || newTime)) > 0; 
        if (newTime.getHours() < 7) { 
            newTime.setHours(21); 
        }
        else if (newTime.getHours() > 21) { 
            newTime.setHours(7); 
        }
        return newTime; 
    }
    
    function checkmesa(){
        box = eval("document.Reservar.mesa");
        if (box.checked == true){
            mediodia("npersonas");
            document.Reservar.mesap.value = "Numero de personas";
            mediodia("mesap");
        }
        else{
            quitar("npersonas");
            quitar("mesap");        
            document.Reservar.mesap.value = "";
        }
    }
});
