if(!window['cranio']) {
	window['cranio'] = {};
	var cranio = window['cranio'];
}
(function() {
    var $ = jQuery;
    
	$(document).ready(function() {
		cranio.subscribe();
	});
	
	$(window).load(function() {
		
	});
    
    
    /* startFn - cranio.subscribe() deals with all user inputs, calculating running total, and prepping choices for cart_add */
    cranio.subscribe = function() {
    	// We want our vars local to this function
    	var ship_zone = "", subscription_level = "", subscription_type = "", subscription_length = "", subscription_start = "", total = "", productname = "", subscription_shipping=0;
    	
    	/* startFn - getsubprice() - Our Ajax query for subscription price & setting productname */
    	function getsubprice(level, type, length, zone, subscription_shipping) {
			
			// We need a way to know when we have all the values needed
			if ((level == "") || (type == "") || (length == "") || (zone == "")) {
				return;
			}
			// Looks like we can make our Ajax request
			else {
	    		$.ajax({
	    			url:	'/getsubprice/' + level + '/' + type + '/' + zone +'/',
	    			success: function(response) {
	    				var slug_total = response.split("_");
	    				productname = slug_total[0];
	    				total 		= slug_total[1];
	    				// 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 (length == 1) {
	    					calc = (parseFloat(total) + parseFloat(subscription_shipping));
	    					//alert(calc);
	    					total = calc;
	    				}
	    				else {
	    					calc = parseFloat((parseFloat(total)*parseFloat(length)) - (parseFloat(total)*.15)) + parseFloat(subscription_shipping);
	    					//alert(calc);
	    					total = calc;
	    				}
	    			
	    				$("#total").empty();
			    		$("#total").append(parseFloat(total).toFixed(2));
			    		$("input[name='productname']").attr({value:productname});
			    		$("input[name='subscription_length']").attr({value:length});
	    			}
	    		});
			}
		}
		/* endFn - getsubprice() */
		
		/* startFn - show_step_five() checks if ship_zone is us/intl & shows step 5 if intl */
		function show_step_five(zone){
			if ((zone == "") || (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_five").hide();
			}
			else if((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_five").hide();
			}
			else {
				$("#step_five").show();
			}
		}
		/* endFn - show_step_five() */
		
		/* 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_five(ship_zone);
			getsubprice(subscription_level, subscription_type, subscription_length, ship_zone, subscription_shipping);
    		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();
    	});
    	
    	subscription_start = $("input[name='subscription_start']:checked").val();
    	sub_refresh();
    }
    /* endFn - cranio.subscribe() */
})();

