$(document).ready(function() {
  Order.refresh();
});

var Order = {
  refresh: function(amount, cost) {
    if (amount) {
      $('#ajorder_count').html(amount);
      $('#ajorder_summ').html(cost);
    }
    else {
      $.getJSON('/Orders/amount_cost.json', function(resp) {
        $('#ajorder_count').html(resp.amount);
        $('#ajorder_summ').html(resp.cost);
      });
    }
  },

  add: function(id) {
    var size   = $('#product-size').val();
    var color  = $('#product-color').val();
    var amount = $('#cat_count').val();

    var params = {
      'id'     : id,
      'color'  : color,
      'size'   : size,
      'amount' : amount
    };

    $.getJSON('/Orders/add.json', params, function(resp) {
      if (resp.status) {
        Order.refresh(resp.amount, resp.cost);
      }
      else {
        alert(resp.message)
      }
    })
  },

  // NOTE: Unused for now
  remove: function(id, invocant) {
    $.getJSON('/Orders/remove.json', { 'id': id }, function(resp) {
      if (resp.status) {
        $(invocant).closest('tr').remove();
      }
      else {
        alert(resp.message)
      }
    })
  },

  recalc: function(id) {
    var amount = $('#amount-'+id).val();
    window.location = '/Orders/refresh_amount/'+id+'?amount='+amount;
  }
};

