
// OnDOMload function ---------------------------------------------------------------------------------------------------------------------
	$(function() {

		var url_vars	= document.location.search;		// get the url vars string
		var urlstring	= url_vars.substring(1);		// strip off the leading '?'
		var vals 		= urlstring.split("&");			// split the url in an array

		var days_events = '';							// Days events array
		var today 		= new Date();					// Create the today's date for further usage
		
		// Get the events list of the month/year
		$.getJSON('plugins/events_manager/frontend/ajax/manage_events.php', {mode: 'get_events_list', month: today.getMonth()+1, year: today.getFullYear(), url: vals[0]}, function(j) {

			days_events = j.days;					
	
			$('#datepick').datepick({onSelect: pickedDate, beforeShowDay: getEvents});	
	
			if ($.cookie('ev_' + vals[0])) {
				var s = new Date($.cookie('ev_' + vals[0]));
				$('#datepick').datepick('setDate', s);
			} else {
				$('#datepick').datepick('setDate');
			}

			// Show the events of the current day
			pickedDate($('#datepick').datepick('getDate'));
		});
		
		
				
		function refreshGetEvents (year, month, date, inst) {
			$.getJSON('plugins/events_manager/frontend/ajax/manage_events.php', {mode: 'get_events_list', month: month, year: year, url: vals[0]}, function(j) {
				days_events = j.days;
				getEvents(date);
			});
		}
				
		// Change the CSS for the days that have events
		function getEvents (date) {
			
			var t= false;
			$.each(days_events, function(i,row) {
				if (date.getMonth() == row.month && date.getDate() == row.day && date.getFullYear() == row.year) {   	
					t = true;
				}
			});   		

			return [true, (t == true ? 'day_with_events' : '')];
			
		}
		
		
		
		// Show the events list of the picked day
		function pickedDate(date) {
			var s = new Date(date);

			$('#events_detail').html('<img src="plugins/events_manager/frontend/assets/images/loader.gif" alt="" border="0" style="vertical-align: middle;" /> Loading Events List...');
			$.cookie('ev_' + vals[0], s.toDateString()); // set cookie
			
			$.post('plugins/events_manager/frontend/ajax/manage_events.php', {mode: 'show_event', datetime: s.toDateString(), url: vals[0]}, function(d) {
				$('#events_detail').html(d);
			});			
		}

	

	
	
	});
	
					
						

