var Calendar = new Class({
	url: '',
	target: null,
	msgTargetId: null,
	currentDay: null,
	currentLabel: null,
	oAjax: null,
	calendarUrl: null,

    initialize: function(url, target, msgTargetId) {
		this.url = url;
		this.target = $(target);
		this.msgTargetId = msgTargetId;
    },
	
	loadCalendar: function(currentDay, currentLabel) {
		if (this.oAjax != null && this.oAjax.running) return;
		if (!this.target || !this.url) return;
		
		var t = $(this.msgTargetId) ? $(this.msgTargetId) : this.target;
		
		t.empty();
		var p = new Element('p').injectInside(t);
		p.set('text', 'Calendar is loading...');
		
		if (typeof(currentDay) == 'string') {
			this.currentDay = currentDay;
		}
		if (typeof(currentLabel) == 'string') {
			this.currentLabel = currentLabel;
		}
		
//		this.oAjax = new Ajax(this.url, {
//			method: 'get',
//			update: this.target, 
//			data: {
//				currentDay: this.currentDay,
//				currentLabel: this.currentLabel
//			}
//		});
//		this.oAjax.request();
		this.target.load(this.url + '?currentDay=' + this.currentDay + '&currentLabel=' + this.currentLabel);
	},
	
	loadCalendarOverview: function (calendarUrl) {
		if (this.oAjax != null && this.oAjax.running) return;
		if (!this.target || !this.url) return;
		
		this.calendarUrl = calendarUrl;
		
		var t = $(this.msgTargetId) ? $(this.msgTargetId) : this.target;
		
		t.empty();
		var p = new Element('p').injectInside(t);
		p.set('text', 'Calendar is loading...');
		
		this.oAjax = new Request.HTML({
			url: this.url,
//			onSuccess: this.checkPath.bind(this)
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var target = this.target.getParent();
				this.target.dispose();
				this.target = target;
				this.checkPath(responseTree, responseElements, responseHTML, responseJavaScript);
			}.bind(this)
		}).get({overview: 1});
	},
	
	checkPath: function (responseTree, responseElements, responseHTML, responseJavaScript) {
		this.target.innerHTML = this.target.innerHTML + responseHTML.replace(/#calendarUrl#/g, this.calendarUrl);
	}
});

