$(document).ready(function(){

  //trocar depois para a url NO AR
  //var url = 'http://osorio.lab/lab.samir/miolo/';
  var url = 'http://www.miolo.com.br/';

  $('#increase').click(function(event){

    event.preventDefault();

    if ($('#participants').val() < 900 && $('#participants').val() > 0){

      $('#participants').val( parseInt($('#participants').val()) +1);

    } else if ($('#participants').val() >= 900) {

      $('#participants').val(900);

    } else {

      $('#participants').val(1);

    }

  });

  $('#decrease').click(function(event){

    event.preventDefault();

    if ($('#participants').val() > 1){

      $('#participants').val( $('#participants').val() -1);

    }

  });

  $('input[name=typeEvent]').click(function(){

    switch(this.value){

      case '1':

      case '2':

      case '3':

        $('#tipo_bebida').css('display','block');

      break;

      case '4':

        $('input[name=typeDrink]').each(function(){

          this.checked = false;

        });

        $('input[name=anotherDrink]').each(function(){

          this.checked = false;

        });

        $('#tipo_bebida').css('display','none');

        $('#outra_bebida').css('display','none');

        $('#calculate').attr('disabled',false);

      break;

    }

  });

  $('input[name=typeDrink]').click(function(){

    $('#outra_bebida').css('display','block');

  });

  $('input[name=anotherDrink]').click(function(){

    $('#calculate').attr('disabled',false);

    $('#calculate').removeClass('disabled');

    $('#calculate').addClass('enabled');

    $('#calculate').attr('src',url+'web/img/btn_calcular.gif');

  });

  $('#calculate').click(function(){

    if ($('#participants').val() > 900){

      $('#participants').val(900);

    }

    if ($('#participants').val() < 1){

      $('#participants').val(1);

    }



    var typeEvent;

    $('input[name=typeEvent]').each(function(){

      if (this.checked == true){

        typeEvent = this.value;

      }

    });



    var divisor;

    $('input[name=anotherDrink]').each(function(){

      if (this.checked == true){

        if (this.value == 1){ // com outras bebidas

          switch ( typeEvent ){

            case '1':

              divisor = 4;

            break;

            case '2':

            case '3':

              divisor = 2;

            break;

          }

        } else if (this.value == 0) { // apenas vinho ou espumante

          divisor = 2;

        }

      } else {

        divisor = 4;

      }

    });



    var parcial = $('#participants').val() / divisor;

    var result = Math.ceil(parcial);



    var typeDrink;

    $('input[name=typeDrink]').each(function(){

      if (this.checked == true){

        typeDrink = this.value;

      }

    });

    if (typeDrink == 1 || typeDrink == 2){

      var numParts = Math.round( result*750 / 180 );

    } else {

      var numParts = Math.round( result*750 / 150 );

    }



    $('#calc_overlay').css('display','block');

    $('#calc_result').css('display','block');

    $('#calc_result_sombra').css('display','block');



    var produto = typeDrink == 1 ? 'Vinho Tinto' : (typeDrink == 2 ? 'Vinho Branco' : 'Espumante');

    var garrafa = "garrafa";

    if(result > 1) {

    	 garrafa = "garrafas";

	}

    var text = 'Você vai precisar de '+result+' '+garrafa+' de '+produto+' 750 ml (Serve aproximadamente '+numParts+' taças).';

    $('#p_return').html(text);

  });

  /*// return somente numero

  $('#participants').keypress(function(ev){

    var pressed = window.event ? event.keycode : ev.which;

    if (pressed > 47 && pressed < 58 || pressed == 8){

      return true;

    } else {

      return false;

    }

  });*/

  $('#participants').blur(function(){

    if ($('#participants').val() > 900)

      $('#participants').val(900);

  });

  $('#calc_result_close').click(function(){

    $('#calc_overlay').css('display','none');

    $('#calc_result').css('display','none');

    $('#calc_result_sombra').css('display','none');

  });

  $('#btn_result_close').click(function(){

    $('#calc_overlay').css('display','none');

    $('#calc_result').css('display','none');

    $('#calc_result_sombra').css('display','none');

  });

});
