jQuery(document).ready(function($){
	navigation();
	day_info();
});

var remoteUrl = 'system/calendar/backend.php';


// NAVIGATION (next/prev)
function navigation(){
	jQuery('#calendar div.navigation a').each(function(i,item){ 
		jQuery(this).click(function(event){ 
			jQuery('#calendar').slideToggle('fast');
			jQuery("#calendar").load(remoteUrl+"?calendar="+$(this).attr("id"),function(){
				// callbacki fkc
				navigation();
				day_info();
				// alert(1);
			});
			jQuery('#calendar').slideToggle('fast');
			event.preventDefault();
		});
		
	});
}
// catch all links on DAYS
function day_info() {
	var remoteCache = new Array();

	jQuery('table.calendar a').each(function(i,item){
		var linkId = item.id;
		var linkUrl = remoteUrl+'?timestamp='+linkId;
		
		jQuery(this).click(function(){
			var calendarEvents = $('#calendar-events');			
			calendarEvents.slideUp('fast',function(){			
				if( remoteCache[linkId] != undefined ) { 
					calendarEvents.html(remoteCache[linkId]);
					calendarEvents.slideDown('fast');
				} else {
					calendarEvents.load(linkUrl,function(){
						remoteCache[linkId] = calendarEvents.html();
						calendarEvents.slideDown('fast');
					});
				}
			});
			return false;
		});
	});	
}
