$(document).ready(function(){
	
	// Sub menu
	
	$("ul#menu li").hover(function(){
		$(this).children("ul").show();
	},function(){
		$(this).children("ul").hide();
	});
	
	// Course blurb "More" button
	
	$("div#course_type_blurb > *").not(":first").hide();
	
	$("a#course_blurb_more").show().click(function(){
		$("div#course_type_blurb > *").show();
		$(this).hide()
	});
	
	// Home page "More" button
	
	if( $("div#home_page").length == 1 ){
		$("div#home_page > *").not(":first,:last").not( $("div#home_page > *")[1] ).hide();
	}
	
	$("a#home_more_button").click(function(){
		$("div#home_page > *").show();
		$(this).hide();
	});
	
	// Course Dates
	
	$("ul#upcoming_dates a").click(function(){
		if( $(this).next("ul").is(":visible") ){
			$(this).next("ul").hide();
			$(this).removeClass("selected");
			return false;
		}
		$("ul#upcoming_dates ul").hide();
		$("ul#upcoming_dates a").removeClass("selected");
		$(this).addClass("selected").next("ul").show();
	});
	
	// Funding available link
	
	$("a.funding_avail_link").click(function(){
		$("div#funding_avail_info").show();
		$("div#funding_avail_info").click(function(){
			$("div#funding_avail_info").hide();
		});
	});
	
	// Generic course application
	
	if( $("select#f_apply_online_course").length == 1 ){
		
		$("select#location_copy").html( $("select#f_apply_online_preferred_location").html() );
		
		$("select#f_apply_online_course").change(function(){
			
			if( $(this).val() != "" ){
				
				if( $( "ul#course_list_data li#course_date_"+$(this).val()+" span.mentor" ).html() == "yes" ){
					$("#f_apply_online_mentor, #f_apply_online_mentors_address, #f_apply_online_mentor_nmc_pin").each(function(){
						$(this).parents("div.field:first").show();
					});
				}else{
					$("#f_apply_online_mentor, #f_apply_online_mentors_address, #f_apply_online_mentor_nmc_pin").each(function(){
						$(this).parents("div.field:first").hide();
					});
				}
				
				var old_location_val = $("select#f_apply_online_preferred_location").val();
				
				if( $( "ul#course_list_data li#course_date_"+$(this).val()+" span.locations" ).html() != "" ){
					
					var location_list = $( "ul#course_list_data li#course_date_"+$(this).val()+" span.locations" ).html().split(",");
					
					$("select#f_apply_online_preferred_location").html( '<option value=""></option>' );
					
					for( i=0; i<location_list.length; i++ ){
						$("select#f_apply_online_preferred_location").append( '<option value="'+location_list[i]+'">'+($("select#location_copy option[value="+location_list[i]+"]").html())+'</option>' );
					}
					
				}
				
				if( $("select#f_apply_online_preferred_location option[value="+old_location_val+"]").length == 1 ){
					$("select#f_apply_online_preferred_location").val( old_location_val );
				}
				
			}else{
				$("#f_apply_online_mentor, #f_apply_online_mentors_address, #f_apply_online_mentor_nmc_pin").each(function(){
					$(this).parents("div.field:first").hide();
				});
			}
			
		}).change();
		
	}
	
	// Register interest
	
	$("#register_interest_intro .register_interest").click(function(){
		$("#register_interest_form").slideDown(300,function(){
			$("#register_interest_intro").hide();
		});
		return false;
	});
	
	$("#register_interest_form .ajax_back").live("click",function(){
		$("#register_interest_form div.status").fadeOut(200,function(){
			$(this).remove();
		});
		$("#register_interest_form fieldset").fadeIn(200);
	});
	
	$("#register_interest_form form").submit(function(){
		
		$(this).height( $(this).height() );
		
		$(this)
			.children("fieldset")
			.fadeOut(200)
			.before('<div class="status" style="display: none;">Please wait, submitting...</div>')
			.prev("div.status")
			.fadeIn(200);
		
		$.ajax({
			url: $(this).attr("action"),
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function( json ){
				$("#register_interest_form div.status").html( json.message );
			},
			error: function(){
				$("#register_interest_form div.status").html('<span style="color: #b91600;">Sorry, there was an error dealing with your request.</span>');
			}
		});
		
		return false;
		
	});
	
	// Payment method
	
	$("input[name=payment_method]").click(function(){
		if( $(this).val() == "paypal" ){
			$("#f_payment_method_invoice_company, #f_payment_method_invoice_contact,"+
				"#f_payment_method_invoice_address, #f_payment_method_invoice_telephone").closest("div.field").hide();
			$("#paySubmitInvoice").hide();
			$("#paySubmitPayPal").show();
		}else if( $(this).val() == "invoice" ){
			$("#f_payment_method_invoice_company, #f_payment_method_invoice_contact,"+
				"#f_payment_method_invoice_address, #f_payment_method_invoice_telephone").closest("div.field").show();
			$("#paySubmitPayPal").hide();
			$("#paySubmitInvoice").show();
		}
	});
	
	$("input[name=payment_method]:checked").click();
	
	$("#paySubmitInvoice").click(function(){
		$("#paymentMethod form").submit();
		return false;
	});
	
});

