// calendar.js -- Dudeplace's Radio Calendar component
// Written by Oo
// require MooTools to be function

var Calendar = new Class({
	Implements: [Events, Options],

	initialize: function(container, options) {
		this.setOptions(options);
		this.container = $(container);
		this.title = this.container.getElement('.title');
		this.dayList = this.container.getElement('.daylist').getElement('ul');
		this.bindEvent(this.container.getElement('a.back'), this.move.bind(this, [-1]));
		this.bindEvent(this.container.getElement('a.next'), this.move.bind(this, [+1]));
		this.setValue(this.options.value);
		return this;
	},

	setValue: function(value) {
		this.value = value ? new Date(value.getFullYear(), value.getMonth(), value.getDate()) : null;
		if (this.value)  {
			this.currentMonth = this.value.getMonth();
			this.currentYear = this.value.getFullYear();
		}
		else {
			var today = new Date();
			this.currentMonth = today.getMonth();
			this.currentYear = today.getFullYear();
		}
		this.show();
		this.fireEvent('click', [this.value]);
		return this;
	},

	goToToday: function() {
		var today = new Date();
		this.currentMonth = today.getMonth();
		this.currentYear = today.getFullYear();
		this.show();
		return this;
	},

	move: function(dm) {
		var cm = this.currentYear * 12 + this.currentMonth + dm;
		this.currentYear = Math.floor(cm / 12);
		this.currentMonth = cm - (this.currentYear * 12);
		this.show();
		return this;
	},

	show: function() {
		var m = this.currentMonth;
		var y = this.currentYear;
		var mm = m < 11 ? m + 1: 0;
		var yy = m < 11 ? y : y + 1;
		var firstDayOfMonth = new Date(y, m, 1);
		var lastDayOfMonth = new Date(yy, mm, 1);
		var firstDayOfCalendar = new Date(firstDayOfMonth.valueOf() - firstDayOfMonth.getDay() * 86400000);
		var lastDayOfPastMonth = new Date(firstDayOfMonth.valueOf() - 86400000);
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate()).valueOf();

		var className = '';
		var current = this.value ? this.value.valueOf() : 0;
		var count = 0;
		var i;
		var day;
		var dayList = this.dayList;
		dayList.getChildren("li").dispose();
		for (i = firstDayOfCalendar; i < firstDayOfMonth; i = new Date(i.valueOf() + 86400000)) {
			className = 'past';
			this.appendElement(dayList, i, "&nbsp;", className);
			++count;
		}
		for (i = firstDayOfMonth; i < lastDayOfMonth; i = new Date(i.valueOf() + 86400000)) {
			className = '';
			if (current == i.valueOf())
				className += ' active';
			if (i.valueOf() == today)
				className += ' today';
			this.appendElement(dayList, i, i.getDate(), className);
			++count;
		}
		while (count < 42) {
			className = 'next';
			this.appendElement(dayList, i, "&nbsp;", className);
			i = new Date(i.valueOf() + 86400000);
			++count;
		}

		this.title.set('text', Calendar.monthNames[this.currentMonth] + " " + this.currentYear);
		return this;
	},

	appendElement: function(parent, day, text, className) {
		li = new Element('li', {'class': className});
		a = new Element('a', {href: 'javascript:;'});
		a.set('html', text);
		a.addEvent('click', function(event) { this.setValue(day); }.bind(this));
		a.inject(li);
		li.inject(parent);
		return this;
	},

	bindEvent: function(element, callback) {
		if (element)
			element.addEvent('click', callback);
		return this;
	},

	options: {
		value: null
	}
});

Calendar.monthNames = new Array(
	'January',
	'Febuary',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December'
);


var EventCalendar = new Class({
	Implements: [Events, Options],

	initialize: function(container, list, options) {
		this.setOptions(options);
		this.list = $(list).getElement('ul');
		this.calendar = new Calendar(container, options);
		this.jaxon = new Jaxon({
			method: 'get',
			url: HOST_PATH + "/ajax/front/event-calendar.php",
			wait: 'silent',
			onSuccess: function(text, tree, message, data) {
				this.list.empty();
				this.list.adopt(tree.getElements('li'));
			}.bind(this)
		});
		this.calendar.addEvent('click', function(value) { this.load(value); }.bind(this));
		this.load(this.calendar.value);
		return this;
	},

	load: function(date) {
		var d = date ? Math.floor(date.valueOf() / 1000) : 0;
		var parameter = "t=" + this.options.memberType + "&d=" + d + "&_" + $time();
		this.jaxon.send(parameter);
		return this;
	},

	options: {
		memberType: 0
	}
});


