var lang = {};
lang.delete_confirm = 'Deseja remover este registro?';
lang.pay1_confirm = 'Deseja marcar este lançamento como quitado?';
lang.pay0_confirm = 'Deseja marcar este lançamento como em aberto?';
lang.delete_confirm = 'Deseja remover este registro?';
lang.amount_in = 'Crédito';
lang.amount_out = 'Débito';
lang.datePickerWeek = ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado'];
lang.datePickerMonth = lang.monthPickerMonth = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
lang.datePickerOther = lang.monthPickerOther = {p:'&lt;&lt;', n:'&gt;&gt;', c:'Fechar', b:'Escolher data'};
lang.datePickerDateFormat = 'dmy';
lang.datePickerDateSeparator = '/';

var options = {};
options.year = (new Date()).getFullYear();
options.root_url = 'http://localhost/clientes/paguei.com.br/sistema/';
options.start_year = 2007;

function checked_operations() {
  var data = [];
  $(':checkbox').each(function(){
    if (this.checked) {
      data.push(this.value);
    }
  });
  return data.join(';');
}


$(function () {
  $('.flash_success, .flash_error').prependTo('#content');
  $('.flash_success, .flash_error').show();
    $('body').prepend("<div id='loading'>Loading...</div>");
    $('#loading').hide();
    $().ajaxStart(function() { $('#loading').show(); });
    $().ajaxStop(function() { $('#loading').hide(); });

    $('#OperationModeMode').change( function () {
      $('#OperationPendingBox').hide();
      $('#OperationPayDateBox').hide();
      $('#OperationPendingYes').attr('checked', true);
      $('#OperationPendingNo').attr('checked', false);
      $('#OperationMode1').hide();
      $('#OperationMode2').hide();
      $('#OperationMode3').hide();
      if ($(this).val()==1) {
        $('#OperationMode1').show();
        $('#OperationPendingBox').show();
      } else if ($(this).val()==2) {
        $('#OperationMode2').show();
      } else {
        $('#OperationMode3').show();
      }
    });

    $('#OperationMode2').hide();
    $('#OperationMode3').hide();
    $(".grid tbody tr td:not(.check):not(.actions)").click(function(){
       location.href = $(this).parents('tr').find('.actions a.edit').attr('href');
    });

    $('span.select_status').click( function () {
      if (confirm('Deseja alterar a situação dos lançamentos selecionados?')) {
        location.href = get_url('operations/pay/id/' + checked_operations());
      }
      return false;
    });

    $('.delete').click( function () {
      return confirm(lang.delete_confirm);
    });


    $('span.select_remove').click( function () {
      if (confirm(lang.delete_confirm)) {
        location.href = get_url('operations/delete/id/' + checked_operations());
      }
      return false;
    });

    if ($.datePicker !== undefined) {
      // datePicker
      $.datePicker.setLanguageStrings(
        lang.datePickerWeek,
        lang.datePickerMonth,
        lang.datePickerOther);
      $.datePicker.setDateFormat(lang.datePickerDateFormat, lang.datePickerDateSeparator);
      $('input.datepicker').datePicker({startDate:'01/07/2006'});
    }
  });

  toCurrency = function(amount) {
    amount = amount.replace('.', '');
    amount = amount.replace(',', '.');
    amount = parseFloat(amount);
    amount = amount.toFixed(2);;
    return isNaN(amount)? 0 : amount;
  }
  toReal = function(amount) {
    amount = isNaN(amount)? 0 : amount;
    return amount.toFixed(2).toString().replace('.', ',');
  }

  // Operations
  Operations = {};
  Operations.filter = function () {
    $('#OperationsList tr').show();
    if ($('#FilterPending').val() == 0) {
      $('#OperationsList tr.pending').hide();
    } else if ($('#FilterPending').val() == 1) {
      $('#OperationsList tr.notpending').hide();
    }
    if ($('#FilterLate').attr("checked")) {
      $('#OperationsList tr.othermonth').hide();
    }
    Operations.balance();
  }

  Operations.balance = function() {
    var flow_in = 0, flow_out = 0;

    $('#OperationsList tbody').find(':checkbox').each(function(){
      if ((this.checked && $('#FiltersJustChecked').attr("checked")) || !$('#FiltersJustChecked').attr("checked")) {
        //alert($(this).parent('tr .flow_in').text());

        flow_in = flow_in + eval(toCurrency($(this).parent().parent('tr:visible').children('.flow_in').text()));
        flow_out = flow_out + eval(toCurrency($(this).parent().parent('tr:visible').children('.flow_out').text()));
      }
    });

/*
    $('tr:visible .flow_in').each( function () {
      flow_in = flow_in + eval(toCurrency($(this).text()));
    });

    $('tr:visible .flow_out').each( function () {
       flow_out = flow_out + eval(toCurrency($(this).text()));
    });
*/
    $('#OperationsTotalIn').text(toReal(flow_in));
    $('#OperationsTotalOut').text(toReal(flow_out));
    $('#OperationsTotalPeriod').text(toReal(flow_out + flow_in));
  }