// PLEASE COMMENT YOUR JS WELL
$(function(){

	// Invoke dropdown menus
	$("#menu-header .nav li.main").hover(
		function () {
			$(this).children(".sub").fadeIn("fast");
		}, 
		function () {
			$(this).children(".sub").fadeOut("fast");
		}
	);
	
	// Homepage header slideshow
	if ( $("#slideshow").length ){
		$('#slideshow').cycle();
	}
	
	// Prevent Saturday and Sunday from being selected for interview
	$(".date-picker.interview-date").live('blur', function(){
		//alert('start');
		var foo = $(this).val();
		var error = 0;
		var inputError = $(this).closest("td").find(".input-error");
		var weekendText = "You have chosen a weekend date, please choose another date.";
		var formatText = "Please enter a date in the format YYYY/MM/DD.";
		if ( foo != '' ){
			// Test for YYYY/MM/DD format
			if ( foo.match(/^[0-9]{4}\/{1}[0-9]{2}\/{1}[0-9]{2}?/) ){
				error = 0;
				foo = new Date(foo);
				foo = foo.getDay();
				// Test for weekends - Sunday = 0, Saturday = 6
				if ( foo == 0 || foo == 6 ){
					if ( inputError.length < 1 ){
						$(this).after("<span class='input-error'>" + weekendText + "</span>");
					} else {
						// Error input already exists, just replace text
						inputError.text(weekendText);	
					}
					error = 1;
				}
			} else { // Date didn't pass regex
				if ( inputError.length < 1 ){
					$(this).after("<span class='input-error'>" + formatText + "</span>");
				} else {
					inputError.text(formatText);	
				}
				error = 1;
			}
		}
		// If no error, make sure that error blocks are removed
		if ( error == 0 ){
			$(this).closest("td").find(".input-error").remove();
		}
	});
	
	// Prevent holidays from being selected for boarding
	var thanks = new Array('2011/11/24', '2012/11/22', '2013/11/28', '2014/11/27', '2015/11/26', '2016/11/24', '2017/11/23', '2018/11/22', '2019/11/28', '2020/11/26', '2021/11/25', '2022/11/24', '2023/11/23', '2024/11/28', '2025/11/27', '2026/11/26', '2027/11/25', '2028/11/23', '2029/11/22', '2030/11/28');
	
	$(".date-picker.boarding-date").live('blur', function(){
		var foo = $(this).val();
		var error = 0;
		var inputError = $(this).closest("td").find(".input-error");
		var holidayText = "You have chosen a Holiday Closed date, please choose another date.";
		var formatText = "Please enter a date in the format YYYY/MM/DD.";
		if ( foo != '' ){
			// Test for YYYY/MM/DD format
			if ( foo.match(/^[0-9]{4}\/{1}[0-9]{2}\/{1}[0-9]{2}?/) ){
				error = 0;
				chunks = $(this).val().split("/");	
				if ( chunks[1] == '12' && chunks[2] == '25' ){ // Test for Christmas (YYYY/12/25)
					if ( inputError.length < 1 ){
						$(this).after("<span class='input-error'>" + holidayText + "</span>");
					} else {
						// Error input already exists, just replace text
						inputError.text(holidayText);	
					}
					error = 1;
				} else if ( chunks[1] == '01' && chunks[2] == '01' ){ // Test for New Years Day (YYYY/01/01)
					if ( inputError.length < 1 ){ 
						$(this).after("<span class='input-error'>" + holidayText + "</span>");
					} else {
						inputError.text(holidayText);	
					}
					error = 1;
				} else {
					// Thanksgiving is a floating holiday, use the thanks array to test - good through 2030
					for ( i=0; i < thanks.length; i++ ){
						if ( foo == thanks[i] ){
							if ( inputError.length < 1 ){
								$(this).after("<span class='input-error'>" + holidayText + "</span>");
							} else {
								inputError.text(holidayText);	
							}
							error = 1;
							break;
						} 
					}
				}
			} else { // Date didn't pass regex
				if ( inputError.length < 1 ){
					$(this).after("<span class='input-error'>" + formatText + "</span>");
				} else {
					inputError.text(formatText);	
				}
				error = 1;
			}
		}
		// If no error, make sure that error blocks are removed
		if ( error == 0 ){
			inputError.remove();
		}
	});
	
	// Add zoom to .pdf links
	$("a[href*=.pdf]").each(function(){
		var href = $(this).attr("href");
		$(this).attr("href", href+"#zoom=90");								  
	});

	// Prepare to submit a form
	$(".btn-submit").click(function(){
		var target = $(this).closest("form");
		target.find("input[name='submitted']").val("y");
	});


});

// PLACE CUSTOM FUNCTIONS HERE




















