// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(function() {
// jQuery - DOM Ready
// -----------------------------------------------------------------------------

$("select#campaign_total_tasks").change(function() {
    var price = $('#apple_application_price').val();
    var app_store_id = $('#apple_application_appstore_identifier').val();

    $("#directory-campaign a").attr('href', '/publisher/campaigns/directory?appstore_identifier='+ app_store_id + "&quantity=" + $(this).val());
    $("#featured-campaign a").attr('href', '/publisher/campaigns/featured?appstore_identifier='+ app_store_id + "&quantity=" + $(this).val());
    $("#rush-campaign a").attr('href', '/publisher/campaigns/rush?appstore_identifier='+ app_store_id + "&quantity=" + $(this).val());

    $.ajax({
      url: '/publisher/campaigns/calculate.json',
      dataType: 'json',
      data: {app_price: price, quantity: $(this).val()},
      success: function(data) {
        if(data['status'] == 'ok') {
          pricing = data['directory'];
          $("#directory-svc-fee").text("$" + pricing['total_service_fee'].toFixed(2));
          $("#directory-user-bonus").text("$" + pricing['total_user_bonus'].toFixed(2));
          $("#directory-app-fee").text("$" + pricing['total_app_price'].toFixed(2));
          $("#directory-total").text("$" + pricing['total'].toFixed(2));

          pricing = data['featured'];
          $("#featured-svc-fee").text("$" + pricing['total_service_fee'].toFixed(2));
          $("#featured-user-bonus").text("$" + pricing['total_user_bonus'].toFixed(2));
          $("#featured-app-fee").text("$" + pricing['total_app_price'].toFixed(2));
          $("#featured-total").text("$" + pricing['total'].toFixed(2));

          pricing = data['rush'];
          $("#rush-svc-fee").text("$" + pricing['total_service_fee'].toFixed(2));
          $("#rush-user-bonus").text("$" + pricing['total_user_bonus'].toFixed(2));
          $("#rush-app-fee").text("$" + pricing['total_app_price'].toFixed(2));
          $("#rush-total").text("$" + pricing['total'].toFixed(2));

          $("#apple-return span").text("$" + data['apple_return'].toFixed(2));
        }
      }
    })
});

// -----------------------------------------------------------------------------
}); // END jQuery - DOM Ready
