var single_price = [];
var joint_price = [];

$(document).ready(function() {
	// add JQuery for donation amount selector
	$(".amount-select").click(function(){
	  // on click set amount
	  var value = $(this).attr('href');
	  value = value.substring(1);
	  $("#amount").attr('value', value);
	});
	
	$("form a.info-link").click(function(e) {

		var url = $(this).attr("href");

		// make a temporary div to house the modal, 
		// load the #content block from the url into it
		// and display the modal dialog
		$("<div id='dialog' title='Information'></div>")
		  .load(url + " #content_panel")
		  .dialog({
		  	width: 500,
		  	height: 500,
		  	resizable: false,
		  	modal: true
		  });
		
		// stop hyperlink
		return false;
		
	});
	
	$("select#adult,select#child,select#student,input#parkingPass_option_1,input#parkingPass_option_2").change(function() {
                
		if ($(this).val()) {
						
						// highlight the box
						$(this).parent().parent().addClass("highlight");
						
		}
		else {
		
						// remove the highlight
						$(this).parent().parent().removeClass("highlight");
						
		}
		
		// calculate the total
		//var adult_price = 53;
		//var joint_adult_price = 84;
		//var child_price = 26;
		//var student_price = 34;
		
		var table = "";
		var total = 0;
		
		if ($("select#adult").val()) {
			var jointA = parseInt($("select#adult").val() / 2);
			var singleA = $("select#adult").val() % 2;
			var jointPA = joint_price[$("select#adult").attr('id')];
			var singlePA = single_price[$("select#adult").attr('id')];
			if (jointA > 0){
			    if(!isNaN(jointPA)){
				table += "<tr><th>" + jointA + " x Joint Adult Memberships (&pound;" + jointPA + " each):</th><td>&pound;" + jointA * jointPA + "</td></tr>";
				total += jointA * jointPA;
			    }else{
			    	singleA += 2 * jointA;
			    }
			}
			if (singleA > 0 && !isNaN(singlePA)){
				table += "<tr><th>" + singleA + " x Single Adult Memberships (&pound;" + singlePA + " each):</th><td>&pound;" + singleA * singlePA + "</td></tr>";
				total += singleA * singlePA;
			}
			
		}
		
		if ($("select#child").val()) {
						
			var jointC = parseInt($("select#child").val() / 2);
			var singleC = $("select#child").val() % 2;
			var jointPC = joint_price[$("select#child").attr('id')];
			var singlePC = single_price[$("select#child").attr('id')];
			if (jointC > 0){
			    if(!isNaN(jointPC)){
				table += "<tr><th>" + jointC + " x Joint Child Memberships (&pound;" + jointPC + " each):</th><td>&pound;" + jointC * jointPC + "</td></tr>";
				total += jointC * jointPC;
			    }else{
			    	singleC += 2 * jointC;
			    }
			}
			if (singleC > 0 && !isNaN(singlePC)){
				table += "<tr><th>" + singleC + " x Single Child Memberships (&pound;" + singlePC + " each):</th><td>&pound;" + singleC * singlePC + "</td></tr>";
				total += singleC * singlePC;
			}
		}
		
		if ($("select#student").val()) {
						
			var jointS = parseInt($("select#student").val() / 2);
			var singleS = $("select#student").val() % 2;
			var jointPS = joint_price[$("select#student").attr('id')];
			var singlePS = single_price[$("select#student").attr('id')];
			if (jointS > 0){
			    if(!isNaN(jointPS)){
				table += "<tr><th>" + jointS + " x Joint Student Memberships (&pound;" + jointPS + " each):</th><td>&pound;" + jointS * jointPS + "</td></tr>";
				total += jointS * jointPS;
			    }else{
			    	singleS += 2 * jointS;
			    }
			}
			if (singleS > 0 && !isNaN(singlePS)){
				table += "<tr><th>" + singleS + " x Single Student Memberships (&pound;" + singlePS + " each):</th><td>&pound;" + singleS * singlePS + "</td></tr>";
				total += singleS * singlePS;
			}
		
		}
		
		if ($('input#parkingPass_option_1').attr('checked') == true) {
			table += "<tr><th>Annual Parking Pass:</th><td>&pound;" + parking_price + "</td></tr>";
			total += parking_price;
		}
		
		table += "<tr><th>Total fees:</th><td>&pound;" + total + "</td></tr>";
		
		$("div#total_table")
		  .empty()
		  .html("<table>" + table + "</table>");
		
                
	});
	
	$("input#parkingPass_option_1,input#parkingPass_option_2").change(function() {
		if ($('input#parkingPass_option_1').attr('checked') == true) {
			table += "<tr><th>Annual Parking Pass:</th><td>&pound;" + parking_price + "</td></tr>";
			total += parking_price;
		}
	});
	
	if (table != ''){
		table += "<tr><th>Total fees:</th><td>&pound;" + total + "</td></tr>";
		
		$("div#total_table")
		  .empty()
		  .html("<table>" + table + "</table>");
	}
	
	// show and hide disabled adult/child
	if ($("input#disabled_option_1").attr('checked') == false){
		$('.disabled-note').hide();
	}
	$("input#disabled_option_1").change(function() {
		if ($(this).attr('checked') == true) {
			// show the notes
			$('.disabled-note').show();
		}
		
	});
	
	$("input#disabled_option_2").change(function() {
												 
		if ($(this).attr('checked') == true){
			$('.disabled-note').hide();
		}
	});
	
	// swap out the drop-down for a hidden field and remove the label
	$("select#adoptionLevel").replaceWith("<input type='hidden' id='adoptionLevel' name='adoptionLevel' value='' />");
	$("div#adoptionLevelField label").remove();
	
	// add adoption level highlights	
	$("div#bronzeFieldWrapper,div#silverFieldWrapper,div#goldFieldWrapper,div#platinumFieldWrapper").click(function() {
        
		// remove highlight class from all first
		$("div#bronzeFieldWrapper,div#silverFieldWrapper,div#goldFieldWrapper,div#platinumFieldWrapper").removeClass("highlight");
		
		// add to current selected element
		$(this).addClass("highlight");
		
		// set hidden value
		var id = $(this).attr("id");
		var value = id.substring(0,1).toUpperCase() + id.substring(1, id.length - 12);
		
		$("input#adoptionLevel").val(value);
	});
});
