
/* *************************************************************************************  
							INITIALIZE OTHER INFORMATION AND JQUERY  BEHAVIOURS
*/



function initialiseCal(){ // set up calendar behaviors
// when clicking on a single event in large calendar, popup a window
			$('.cal .calCell .event').click(function(){ 
					var eventHTML = $(this).html();
					$('.cal .eventPopup .popupContent').html(eventHTML); //create content in popup div
					$('.cal .eventPopup .popupContent a').click(function(){ // set link popup behaviour (if there are links in the content)
							   window.open(this.href, 
								'_blank',
								'scrollbars=yes,resizable=yes,width=800,height=700');
								return false;
					});
					$('.cal .eventPopup').fadeIn(); // show the popup div
					return false;
			});
// when clicking on the calendar date	in large calendar	
			$('.cal .calCell span.calendarDate a').click(function(){
					var eventsHTML = $(this).parents('.calCell').html();
					$('.cal .eventPopup .popupContent').html(eventsHTML);
					$('.cal .eventPopup').fadeIn();
					return false;
			});
// when clicking on a  cell with events in small calendar
			$('.minical div.isEvents').click(function(){
					var eventsHTML = $(this).html();
					$('.minical .eventPopup .popupContent').html(eventsHTML);
					$('.minical .eventPopup').fadeIn();
					return false;
			});
// when hovering  over a cell of event (mark it selected ,so that we avoid span:hover which doesn't work in ie 6)
			$('.cal div.calCell .title, .minical div.calCell.isEvents').hover(
					function(){
						$(this).addClass('selected');
					},
					function(){
						$(this).removeClass('selected');	
					}
			);
// when clicking on close button , fade out popup window			
			$('a.close').click(function(){
					$(this).parent().fadeOut();
					return false;
			});
			
// set nav bar year and month
			$('.month').html(months[month]);
			$('.year').html(year);	
// hide popup windows
			$('.eventPopup').hide();
// set calendar header
		$('.calHead').html(''); // empty out the heading
		for (var i=0;i<7;i++){
			var wd = daysOfTheWeek[i];
			$('.calHead').append('<div class="calCell">'+wd+'</div>');		
		}
			
};
/*
1. set behaviour for next and previous (on big calendar)

Note: When clicking on 'next' or  'previous' the calendar has to be rebuilt dynamically. This means that the behaviours also have to be re-bound , so 'initialisecal' is called again for that purpose.

2. display both the small calendar and the large calendar as well as the upcoming events

3. initialize calendar behaviours

*/
$(document).ready(function() {
// nav bar next
			$('.cal a.nextMonth').click(function(){
					var monthAndYear = nextMonthAndYear(month, year);
					month = monthAndYear[0];
					year = monthAndYear[1];
					day = (month==now.getMonth() & year==now.getFullYear())? day : 0;
					var nextCal = buildCalendar(day, month, year,buildCalendarEntry);
					$('.cal .calCells').html(nextCal);
					initialiseCal();
					return false;
			});
// nav bar previous
			$('.cal a.previousMonth').click(function(){
					var monthAndYear = previousMonthAndYear(month, year);
					month = monthAndYear[0];
					year = monthAndYear[1];
					day = (month==now.getMonth() & year==now.getFullYear())? day : 0;
					var previousCal = buildCalendar(day, month, year,buildCalendarEntry);
					$('.cal .calCells').html(previousCal);
					initialiseCal();
					return false;
			});
// put calendars in place
			var calendarString = buildCalendar(day, month, year,buildCalendarEntry);
			$('.cal .calCells').html(calendarString);
			$('.minical .calCells').html(calendarString);
// and upcoming events
			$('.upcomingEvents').html(buildEventList(day, month, year, 7));
// initialize rest of calendar			
			initialiseCal(); // all one function because calendar should be re-initialized when click to the next /previous month			
});
