if(!window['cranio']) {
  window['cranio'] = {};
  var cranio = window['cranio'];
}
(function() {
  var $ = jQuery;
  
  $(document).ready(function() {
    cranio.subscribe();
  });
  
  $(window).load(function() {
    
  });
    
    cranio.subscribe = function() {
      /* startFn - cranio.subscribe() deals with all user inputs, calculating running total, and prepping choices for cart_add */
      
      // We want our vars local to this function
      var ship_zone = "", 
        subscription_level = "", 
        subscription_type = "", 
        subscription_length = "", 
        subscription_start = "", 
        total = 0, 
        productname = "", 
        subscription_shipping=0,
        dir_on = false,
        dir_price = 0;
      
      /* startFn - getsubprice() - Our Ajax query for subscription price & setting productname */
      function getsubprice() {
      
        if ((subscription_level == "") || (subscription_type == "") || (subscription_length == "") || (ship_zone == "")) {
          // We need all required values
          return;
        }
        else {
          // Looks like we can make our Ajax request
          $.ajax({
            url:  '/getsubprice/' + subscription_level + '/' + subscription_type + '/' + ship_zone +'/',
            success: function(response) {
              var slug_total = response.split("_");
              productname = slug_total[0];
              total = slug_total[1];
              
              $("input[name='productname']").attr({value:productname});
              $("input[name='subscription_length']").attr({value:subscription_length});
              
              calc_total();
            }
          });
        }
    }
    
    /* startFn - calc_total() recalculates running total & updates display */
    function calc_total() {
      // We need to do a little calculating via parseFloat so we can format to two decimal places
      // Without parseFloat, JS acts stupid & concatenates the two numbers
      if (subscription_length == 1) {
        calc = (parseFloat(total) + parseFloat(subscription_shipping) + parseFloat(dir_price));
        //alert(calc);
        total = calc;
      }
      else if (subscription_length == 2) {
        calc = parseFloat((parseFloat(total)*parseFloat(subscription_length)) - (parseFloat(total)*.15)) + parseFloat(subscription_shipping) + parseFloat(dir_price);
        //alert(calc);
        total = calc;
      }
      else {
        total = 0;
      }
      // Update DOM elements
      $("#total").empty();
      if (total > 0) {
        $("#total").append(parseFloat(total).toFixed(2));
      }
      else {
        $("#total").append("");
      }
    }
    
    /* startFn - show_dir_price() shows correct dir price for give term */
    function show_dir_price() {
      if (dir_on) {
        // Only do this when we know we have checked Yes
        if (subscription_length == 1) {
          $("span.two_year").hide();
          $("span.one_year").show();
          dir_price = parseFloat($("#one_year_dp_price").html());
        }
        else if (subscription_length == 2) {
          $("span.one_year").hide();
          $("span.two_year").show();
          dir_price = parseFloat($("#two_year_dp_price").html());
        }
      }
      else {
        // No changes, but verify total is recalculated
        $("span.two_year").hide();
        $("span.one_year").hide();
        //$("input[name='dir_prod']:first").attr("checked", "checked");
        dir_price = 0;
      }
    }
    
    /* startFn - show_step_six() checks if ship_zone is us/intl & shows step 5 if intl */
    function show_step_six(){
      if ((ship_zone == "") || (ship_zone == 'us')){
        // Either no choice or going back to US - check standard shipping & set shipping=0
        $("input[name='subscription_shipping']:first").attr("checked","checked");
        subscription_shipping = 0;
        $("#step_six").hide();
      }
      else if((ship_zone == 'intl') && (subscription_type == "" || subscription_type == "2")) {
        // Either no level choice or Online only - check standard shipping & set shipping=0
        $("input[name='subscription_shipping']:first").attr("checked","checked");
        subscription_shipping = 0;
        $("#step_six").hide();
      }
      else {
        $("#step_six").show();
      }
    }
    /* endFn - show_step_six() */
    
    /* startFn - enable_subscribe() checks that all necessary input is available before enabling Subscribe button */
    function enable_subscribe(){
        if ((subscription_level == "") || (subscription_type == "") || (subscription_length == "") || (ship_zone == "") || (subscription_start == "")){
          $("input[type='submit']").attr({disabled:"disabled"});
        }
        else {
          $("input[type='submit']").removeAttr("disabled");
        }
      }
      /* endFn - enable_subscribe() */
      
      /* startFn -- sub_refresh() collects all our updating functions in one place */
    function sub_refresh(){
      show_step_six();
      show_dir_price();
      getsubprice();
      enable_subscribe();
    }
    /* endFn - sub_refresh() */
        
      /* Set our variables with each click & call on sub_refresh() */
      $("input[name='ship_zone']").click(function() {
        ship_zone = $(this).val();
        sub_refresh();
      });
      
      $("input[name='subscription_level']").click(function() {
        subscription_level = $(this).val();
        sub_refresh();
      });
      
      $("input[name='subscription_type']").click(function() {
        type_length = $(this).val().split("_");
        
        // Assign our separate type & length values
        subscription_type   = type_length[0];
        subscription_length = type_length[1];
        sub_refresh();
      });
      
      $("input[name='subscription_start']").click(function() {
        subscription_start = $(this).val();
        sub_refresh();
      });
      
      $("input[name='subscription_shipping']").click(function(){
        var answer = $(this).val();
        if (answer == "true") {
          subscription_shipping = 50;
        }
        else {
          subscription_shipping = 0;
        }
        sub_refresh();
      });
      
      $("input[name='dir_prod']").click(function() {
        if ($(this).val() == 'true') {
          dir_on = true;
        }
        else {
          dir_on = false;
        }
        sub_refresh();
      });
      
      subscription_start = $("input[name='subscription_start']:checked").val();
      sub_refresh();
    }
    /* endFn - cranio.subscribe() */
})();


