$(document).ready(function (){
  setupPricingProfileListeners();
});

function executeAction() {
  var action = $("#action").val();
  
  switch(action) {
    case "round99":
      $(".sale-price").each(function(){
        $(this).find("input").val((Math.ceil(parseFloat($(this).find("input").val())+0.01)-0.01).toFixed(2));
        if($(this).find("input").val() < 0) { $(this).find("input").val(0); }
        saleChangeRaw($(this).find("input"));
      });
      break;
    case "round00":
      $(".sale-price").each(function(){
        $(this).find("input").val(Math.ceil(parseFloat($(this).find("input").val())).toFixed(2));
        if($(this).find("input").val() < 0) { $(this).find("input").val(0); }
        saleChangeRaw($(this).find("input"));
      });
      break;
    case "increase":
      jPrompt('How much would you like to increase the markups? (USD):', 1.00, 'Increase Markup', function(r) {
        if(r) {
          $(".markup").each(function(){
            $(this).find("input").val((parseFloat($(this).find("input").val())+parseFloat(r)).toFixed(2));
            if($(this).find("input").val() < 0) { $(this).find("input").val(0); }
            markupChangeRaw($(this).find("input"));
          }); 
        }
      });
      break;
    case "decrease":
      jPrompt('How much would you like to decrease the markups? (USD):', 1.00, 'Decrease Markup', function(r) {
        if(r) {
          $(".markup").each(function(){
            $(this).find("input").val((parseFloat($(this).find("input").val())-parseFloat(r)).toFixed(2));
            if($(this).find("input").val() < 0) { $(this).find("input").val(0); }
            markupChangeRaw($(this).find("input"));
          });
        }
      });
      break;
    case "increase_percent":
      jPrompt('What percent of the wholesale price would you like your markups to be? (%):', 50, 'Percent Increase From Wholesale', function(r) {
        if(r) {
          $(".markup").each(function(){
            $(this).find("input").val((parseFloat($(this).parent().find(".wholesale:first").html().replace('$','')) * (parseFloat(r)/100)).toFixed(2));
            if($(this).find("input").val() < 0) { $(this).find("input").val(0); }
            markupChangeRaw($(this).find("input"));
          });
        }
      });
      break;
  }
  
}

function markupChange(e) {
  markupChangeRaw(e.target);
}

function markupChangeRaw(ele) {
  var anum = ($(ele).val() == "" ? 0 : parseFloat($(ele).val()));
  $(ele).parent().parent().find(".sale-price input:first").val((anum+parseFloat($(ele).parent().parent().find(".wholesale:first").html().replace("$",""))).toFixed(2));
}

function saleChange(e) {
  saleChangeRaw(e.target);
}

function saleChangeRaw(ele) {
  var anum = ($(ele).val() == "" ? 0 : parseFloat($(ele).val()));
  $(ele).parent().parent().find(".markup input:first").val((anum-parseFloat($(ele).parent().parent().find(".wholesale:first").html().replace("$",""))).toFixed(2));
}

function includedChange(e) {
  var ele = e.target;
  var disable = !$(ele).attr('checked');
  $(ele).parent().parent().find('.sale-price input:first').attr('disabled', disable);
}

function setupPricingProfileListeners() {
  $(".markup input").bind("keyup", markupChange);
  $(".sale-price input").bind("keyup", saleChange);
  $(".check-included").bind("change", includeProductChange);
}

function includeProductChange(e) {
  includeProductChangeRaw(e.target);
}

function includeProductChangeRaw(ele) {
  if ($(ele).is(':checked')) {
    $(ele).parent().parent().find(".markup input:first").removeAttr('disabled');
    $(ele).parent().parent().find(".sale-price input:first").removeAttr('disabled');
    $(ele).parent().parent().find(".package-price input:first").removeAttr('disabled');
    $(ele).parent().parent().find(".hidden-purchasable-id").removeAttr('disabled');
    $(ele).parent().parent().find(".hidden-delete-checkbox").attr('checked', false);
    $(ele).parent().parent().removeClass("disabled")
  } else {
    $(ele).parent().parent().find(".markup input:first").attr('disabled', 'true');
    $(ele).parent().parent().find(".sale-price input:first").attr('disabled', 'true');
    $(ele).parent().parent().find(".package-price input:first").attr('disabled', 'true');
    $(ele).parent().parent().find(".hidden-purchasable-id").attr('disabled', 'true');
    $(ele).parent().parent().find(".hidden-delete-checkbox").attr('checked', true);
    if(!$(ele).parent().parent().hasClass("disabled")) {
      $(ele).parent().parent().addClass("disabled");
    }
  }
}
