/**
*	---------------------------------
*	AJAX EVENT GETTER FUNCTIONS
*	---------------------------------
*
*	These functions retrieve events
*	from the diary entry table, in the database,
*	and populate the diary with retrieved events
*/
//____________________________________________________________________________________________________________________________________________________________________________________________
 
 
 
 
	/**
	*	EVENT_AUTOLOADER()
	*	------------------
	*
	*	This function is responsible for populating the diary
	*	with the correct information, when it is first loaded or reloaded.
	*	
	*	• If the diary page is being accessed arbitrarily, it loads today's events as default
	*	• If the diary page is being linked to from a mini diary, or other link, it will load a date saved in a cookie, under the title 'link_date', it will delete this cookie afterwards
	*	• If the diary page is being refreshed, a cookie should have been set called 'viewed_date', which should help to reload the correct day on refresh
	*/

	
	function event_autoloader() {
		
		
		//gotoDate - - - - - will contain any cookie information passed, under the name 'link_date', by a link to the diary page from outside
		var gotoDate 		= readCookie('link_date');
		
		//viewedDate - - - - - will contain any cookie information passed, under the name 'viewed_date', by a link from inside the diary page
		var viewedDate 	= readCookie('viewed_date');
		
		
		//cookie elements
		var dateSplit = new Array();
		
		var linkedDate;
		var lastViewedDate;
		var displayDay;
		var displayDayNum;
		
		//dpd = datepickerday
		var dpd = new Array();
		var date = new Date();
		

	if (gotoDate == null && viewedDate == null) {

		//if there is no cookie set, then we pass nothing to events_getter, as it will retrieve today's date if we pass no date to it.
		
		$.get('diary/no_of_events/', function (html) {
			
			
			$('#noOfEvents').html(html);
			
		});
		
		$.get('diary/events_getter/', function (html) {
			
			
			$('#events').html(html);
			
		});
		
		$.get('diary/pagination_setup/', function (html) {
		
			$('#pagination-digg').html(html);
			
		});

	}
	
	else if (gotoDate == null && viewedDate != null) {
		
		
		dateSplit				= viewedDate.split('_');
		lastViewedDate		= dateSplit[0];
		displayDay 			= dateSplit[1];
		displayDayNum 		= dateSplit[2];
		
	
		$(document).ready(function() {
			
		
			$('.today').text(displayDay.toUpperCase());
			$('.todayNum').text(displayDayNum);
			
			dayLinksUpdater(lastViewedDate);
		
		});
		
		$.get('diary/no_of_events/'+lastViewedDate, function (html) {
			
			
			$('#noOfEvents').html(html);
			
		});
		
		$.get('diary/events_getter/' +lastViewedDate, function (html) {
			
			$('#events').html(html);
		
		});
		
		
		$.get('diary/pagination_setup/' +lastViewedDate, function (html) {
		
			$('#pagination-digg').html(html);
			
		});
		
		
	
	
	}

	else {
		
		dateSplit 				= gotoDate.split('_');
		linkedDate 			= dateSplit[0];
		displayDay 			= dateSplit[1];
		displayDayNum 		= dateSplit[2];
		
		
		dpd = linkedDate.split('-');
		
		date.setFullYear(dpd[0], dpd[1] - 1, dpd[2]);
		
		$('#calendar').datepicker('setDate', date);

		
		$(document).ready(function(){
			
		
			$('.today').text(displayDay);
			$('.todayNum').text(displayDayNum);
			
			dayLinksUpdater(linkedDate);
			
			
		});

		$.get('diary/no_of_events/'+linkedDate, function (html) {
			
			
			$('#noOfEvents').html(html);
			
		});		
		
		$.get('diary/events_getter/' +linkedDate, function (html) {
			
			$('#events').html(html);
		
		});
		
		
		$.get('diary/pagination_setup/' +linkedDate, function (html) {
		
			$('#pagination-digg').html(html);
			
		});
		
		
		createCookie('viewed_date', linkedDate+'_'+displayDay.toUpperCase()+'_'+displayDayNum, 0.01);
		eraseCookie('link_date');
	}

}

	
	
	/**
	*	DIARY CLICKS
	*	------------------
	*
	*	This function will return the correct information for the day link that is clicked on the top calendar
	*/



	
$(function() {
	
	$('.restofweek').click(function (event) {
	
		var clickedDate	= (this.getAttribute('name'));
		var dateLong 	= (this.getAttribute('title'));
		var displayDay 	= new Array();
		

		event.preventDefault();
		
		displayDay = dateLong.split(' ');
		
		createCookie('viewed_date', clickedDate+'_'+displayDay[0].toUpperCase()+'_'+displayDay[1], 0.01);
		
		var date = new Date();
		var d = new Array();
		d = clickedDate.split('-');
		
		date.setFullYear(d[0], d[1] - 1, d[2]);
		
		$('#calendar').datepicker('setDate', date);
		
		$('.today').text(displayDay[0].toUpperCase());
		$('.todayNum').text(displayDay[1]);
	
		/**
		*	Put Events to #events div
		*/

		$.get('diary/no_of_events/'+clickedDate, function (html) {
			
			
			$('#noOfEvents').html(html);
			
		});
		
		$.get('diary/events_getter/' +clickedDate, function (html) {
		
			$('#events').html(html);
			
		});
		
		/**
		*	Put Pagination to #pagination div
		*/
	
		$.get('diary/pagination_setup/' +clickedDate, function (html) {
		
			$('#pagination-digg').html(html);
			
		});
		
		
	});
	
	
});