$(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();
		
	}
	
});
