/**
*	+-------------------------------+
*	|	CALENDAR Javascript file |
*	+-------------------------------+
*/


$(function() {


//datepickers on the form fields
//$('#dateField').datepicker({ dateFormat: 'D d MM, y', altField: '#date', altFormat: 'yy-mm-dd', showAnim: 'slideDown'});

//datepicker at top of page

	//we want the date picker to highlight the day that was linked to from outside the diary page if, applicable
	var gotoDate = readCookie('link_date');
	var viewedDate = readCookie('viewed_date');
	var d = new Date();
	var cookieSplit = new Array();
	var dateSplit = new Array();
	
	if(gotoDate != null && viewedDate == null) {
	
		cookieSplit = gotoDate.split('_');
		dateSplit = cookieSplit[0].split('-');
		
		d.setFullYear(dateSplit[0],dateSplit[1] - 1,dateSplit[2]);
	
	}
	
	else if(gotoDate == null && viewedDate != null) {

		cookieSplit = viewedDate.split('_');
		dateSplit = cookieSplit[0].split('-');
		
		d.setFullYear(dateSplit[0],dateSplit[1] - 1,dateSplit[2]);
	
	}
	
	$('#calendar').datepicker({
		
		maxDate: '+4m',
		
		minDate: '-0d',
		
		//will select current day, even if not today
		defaultDate: d,
			
		//will format its output to this date format.
		dateFormat: 'yy-mm-dd_DD_d',  
			
		//the above date format is what is referred to by the 'dateText' callback below.
		//onSelect is a function to perform when a date in the datepicker is clicked
			
			
		onSelect: function(dateText, inst) {
			
			/**
			*	WHAT HAPPENS ON SELECT
			*	----------------------
			*
			*	1. 	A cookie is created to reflect the date being viewed. We can save the dateText variable to the cookie as is, as it is in the correct format
			*	2.	The dateText variable is then split, we only needed it in this format to save to a cookie, now we need separate elements of it, not the whole thing. So we split it at the underscore
			*	3.	The different parts of the split string are named
			*	4.	The displayed date (day name and number), at top of the diary page, is updated with parts of the split string
			*	5.  The day links across the bottom of the calendar header are updated to reflect the day that has been selected, using dayLinksUpdater();
			*	6. 	The events and pagination for the original selected day is loaded.
			*/	
			
			
			// - 1
			createCookie('viewed_date', dateText, 0.01);

			// - 2
			var cookieSplitUp 			= new Array();
			cookieSplitUp 				= dateText.split('_');
			

			// - 3
			var selectedDate		= cookieSplitUp[0];
			var dayName 			= cookieSplitUp[1];
			var dayNum				= cookieSplitUp[2];
			
			
			// - 4
			$('.today').text(dayName.toUpperCase());
			$('.todayNum').text(dayNum);
			
			// - 5
			dayLinksUpdater(selectedDate);
			
			
			// - 6
				$.get('diary/no_of_events/'+selectedDate, function (html) {
			
			
					$('#noOfEvents').html(html);
			
				});
			
			
				$.get('diary/events_getter/' +selectedDate, function (html) {
					
					$('#events').html(html);
					
				});
				
				
				
				$.get('diary/pagination_setup/' +selectedDate, function (html) {
					
					$('#pagination-digg').html(html);
					
				});
		
		}
	
	});
	
});
