/**
 * AS4EventsCalendarWidget
 * Provides calendar browsing functionality for the channel Events Calendar widget
 */
var AS4EventsCalendarWidget = Class.create({
	
	/**
	 * @type Hash Map month numbers to names
	 */
	monthNames: $H({
		0: { name: 'January', days: 31 },
		1: { name: 'February', days: 28 }, 
		2: { name: 'March', days: 31 },
		3: { name: 'April', days: 30 },
		4: { name: 'May', days: 31 },
		5: { name: 'June', days: 30 },
		6: { name: 'July', days: 31 },
		7: { name: 'August', days: 30 },
		8: { name: 'September', days: 31 },
		9: { name: 'October', days: 31 },
		10: { name: 'November', days: 31 },
		11: { name: 'December', days: 31 }
	}),
	
	/**
	 * @type string The current view mode, one of the declared AS4EventsCalendar view modes
	 */
	displayMode: null,
	
	/**
	 * @type integer The channel widgets id to which this controller is attached
	 */
	channelWidgetsId: null,

	/**
	 * @type integer The current day to render 
	 */
	currentDay: null,

	/**
	 * @type integer The current month to render 
	 */
	currentMonth: null,

	/**
	 * @type integer The current year to render
	 */
	currentYear: null,

	/**
	 * @type Element The day summary popup element
	 */
	targetPopupElement: null,
	
	/**
	 * @type Element Whether back from agenda view
	 */
	backtocalendar: null,
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

	/**
	 * @constructor
	 *
	 * @param {integer} channelWidgetsId The channel widget that this controller is attached to
	 */
	initialize: function(channelWidgetsId)
	{
		this.channelWidgetsId = channelWidgetsId;
		this.displayMode = AS4EventsCalendarWidget.MONTH_VIEW;
		this.reset();
	},

	/**
	 * Updates the current calendar display element with day panels for the current 
	 * month
	 */
	refresh: function() 
	{
		var url = '/widget/index';

		var params = {
			renderMode: 'update',
			display_mode: (this.displayMode ? this.displayMode : AS4EventsCalendarWidget.MONTH_VIEW),
			channel_widgets_id: this.channelWidgetsId,
			year: this.currentYear,
			month: this.currentMonth
		};

		if(this.currentDay)
			params.day = this.currentDay;
			
			
		if(this.backtocalendar=="yes")
		{
			var currentYear1 = (new Date).getFullYear();
			if (this.currentYear != currentYear1) 
			{
				this.currentYear =currentYear1;
				params.year = currentYear1;
			}
			this.backtocalendar=null;
		}
		
		$('channel_widgets_' + this.channelWidgetsId).down('.navigation').update('<img src="/app/common/assets/images/ajax-loader-16.gif" width="16" height="16" />');

		AS4Shell.getInstance().ajaxUpdate(url, params, $('channel_widgets_' + this.channelWidgetsId).down('.widget_content'), $A([]), {showIndicator: false});
	},

	/**
	 * Proxy function to forward to the appropriate action method when a user clicks on
	 * a day. Can be overridden in specific circumstances.
	 *
	 * @param {Event} event The click event
	 * @param {integer} year
	 * @param {integer} month 1-12
	 * @param {integer} day 1-31
	 */
	onDayClicked: function(event, year, month, day)
	{
		this.showDaySummaryPopup(event, year, month, day);
	},

	/**
	 * Display the events_calendar_popup for the selected day
	 * 
	 * @param {integer} year
	 * @param {integer} month 1-12
	 * @param {integer} day 1-31
	 */
	showDaySummaryPopup: function(event, year, month, day)
	{
		if(!$(this.targetPopupElement))
			return;

		$(this.targetPopupElement).update('<div id="content"><div class="centre"><img src="/app/common/assets/images/ajax-loader.gif" /><br />Loading...</div></div>');
		$(this.targetPopupElement).setStyle({
			left: Event.pointerX(event) + 'px',
			top: Event.pointerY(event) + 'px'
		});

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - 
		// Load the events summary
		var url = '/events_calendar/event_popup';
		var params = {
			renderMode: 'update',
			channel_id: this.currentChannel,
			year: year,
			month: month - 1,
			day: day
		};

		AS4Shell.getInstance().ajaxUpdate(url, params, $(this.targetPopupElement).down('#content'), $A([]), {showIndicator: false});

		$(this.targetPopupElement).show();
	},

	/**
	 * Reset the calendar to the current day/month/year
	 *
	 * @param {boolean} [afterRefresh] If true, the widget will be refreshed as well
	 */
	reset: function(afterRefresh)
	{
		this.currentMonth = new Date().getMonth();
		this.currentYear = new Date().getYear();
		this.currentDay = new Date().getDate();

		// Firefox calculates the date based on years since 2000
		if(!Prototype.Browser.IE)
			this.currentYear += 1900;

		if(afterRefresh)
			this.refresh();
	},

	/**
	 * Navigate and display the next consecutive month
	 */
	nextMonth: function()
	{
		this.currentMonth ++;
		if(this.currentMonth > 11)
		{
			this.currentMonth = 0;
			this.currentYear ++;
		}

		this.refresh();
	},

	/**
	 * Navigate and display the previous consecutive month
	 */
	previousMonth: function()
	{
		this.currentMonth --;
		if(this.currentMonth < 0)
		{
			this.currentMonth = 11;
			this.currentYear --;
		}

		this.refresh();
	},

	/**
	 * Navigate and display the next consecutive week
	 */
	nextWeek: function()
	{
		this.currentDay = this.currentDay + 7;
		
		this.refresh();
	},

	/**
	 * Navigate and display the previous consecutive week
	 */
	previousWeek: function()
	{
		this.currentDay = this.currentDay - 7;

		this.refresh();
	},

	/**
	 * Navigate and display the next consecutive year
	 */
	nextYear: function()
	{
		this.currentYear ++;
		this.refresh();
	},

	/**
	 * Navigate and display the previous consecutive year
	 */
	previousYear: function()
	{
		this.currentYear --;
		if(this.currentYear < AS4EventsCalendarWidget.EARLIEST_YEAR)
			this.currentYear = AS4EventsCalendarWidget.EARLIEST_YEAR;

		this.refresh();
	},

	/**
	 * Close the events popup
	 */
	closeEventPopup: function()
	{
		if(!this.targetPopupElement)
			return;

		$(this.targetPopupElement).hide();
	},

	/**
	 * Return a boolean indicating if the given year is a leap year
	 * @return boolean
	 */
	isLeapYear: function(year)
	{
		return (new Date(year, 1, 29).getDate() == 29);
	}
});

// Declare view mode constants
AS4EventsCalendarWidget.MONTH_VIEW = 'month_view';
AS4EventsCalendarWidget.AGENDA_VIEW = 'agenda_view';
AS4EventsCalendarWidget.EARLIEST_YEAR = 1990;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Static method to retrieve controller from channel_widgets_id
AS4EventsCalendarWidget.getInstance = function(channelWidgetsId)
{
	eval('var o = events_calendar_' + channelWidgetsId + ';');
	return o;
}
