/* 
	author: Jason Albright
	Date: September 24, 2009
*/


$(document).ready(function(){
	
	//define constant
	var STARTDATE = new Date(2009,07,23);				//remember that months in java or 0-11

	$('#datepicker').datepicker({						//bind the datepicker to a button on the page
		dateFormat: 'yy,mm,dd',							//set the date format to be jquery friendly
		onSelect: function(dateText, inst){				//when a date is selected bind function
			//reset the button text (this must be done because by default the datepicker ui will reset the value
			//of the button each time you select a date
			$('#datepicker').attr("value",$('#datepicker').attr('rel'));
			//get the selected date into a date object
			var selectedDate = new Date(inst.selectedYear,inst.selectedMonth,inst.selectedDay);
			calcMenu(selectedDate, STARTDATE);			//call the calc function to calculate the menu
		}
	});
	
});

function calcMenu(select, start){
	//PreCon: date contains a date formatted yy-mm-dd
	//PostCon: a number of a 22 day menu cycle is returned
	//Function: to calculate what day we are on based on 22 day menu cycle
	
	var one_day=1000*60*60*24	//calculate the value of one day
	var menunum = ((Math.ceil((select.getTime()- start.getTime())/(one_day))) % 22 );	//calculate menu number
	window.location = "/forms/menus/DUCCycleMenus%20(" + menunum + ").pdf";		//open the pdf form
	
}