

window.addEvent('domready', function() {
	var div = $('calendar-container');

	if(div) {
		window.calendar = new CalendarWidget(div);

		var dt = new Date();
		var y = dt.getFullYear(); var m = dt.getMonth()+1; var d = dt.getDate();
		if(m < 10) m = '0' + m;
		if(d < 10) d = '0' + d;

		window.calendar.selectDay(y+'-'+m+'-'+d);
	}
});

CalendarWidget = new Class({

	initialize: function(div) {
		this.container = $(div);
	},

	selectMonth: function(strMonth) {

		new Request({
			method: 'post',
			url: 'index.php',
			data: {
				request: 'main.ajax_calendar_select_month',
				month: strMonth
			},
			onComplete: function(response) {
				//test(response);
				if(response) {
					this.updateCalendar(response);
				}
			}.bind(this)
		}).send();

	},

	selectDay: function(strDate, showEvents) {

		if(strDate == this.selectedDate) {
			if( $('calendar-events') && $('calendar-events').getStyle('display') == 'block' ) {
				this.hideEvents(); return;
			}
		}
		this.hideEvents();

		var day = strDate.split(/-/).pop();
		var td = $('td-'+strDate);

		this.container.getElements('td.selected').each(function(el) {
			el.removeClass('selected');
		});
		if(td) td.addClass('selected');

		this.selectedDate = strDate;
		this.curAnchor = td;

		if(showEvents) this.showEvents(strDate);
	},

	showEvents: function(strDate) {
		new Request({
			method: 'post',
			url: 'index.php',
			data: {
				request: 'main.ajax_calendar_select_day',
				date: strDate
			},
			onComplete: function(response) {
				//test(response);
				if(response) {
					this.updateEvents(response);
				}
			}.bind(this)
		}).send();
	},

	startHideTimer: function() {
		this.__timer = setTimeout(function() {
			this.hideEvents();
		}.bind(this), 500);

	},

	stopHideTimer: function() {
		clearTimeout(this.__timer);

	},

	hideEvents: function() {

		try {
			$('calendar-events').setStyle('display', 'none');
		} catch(e) {};

	},

	updateCalendar: function(buf) {
		new Fx.Tween(this.container, {
			duration: 300,
			onComplete: function() {
				this.container.empty();
				this.container.innerHTML = buf;
				new Fx.Tween(this.container, {
					duration: 150
				}).start('opacity', 1);
			}.bind(this)
		}).start('opacity', 0);

	},

	updateEvents: function(buf) {
		if(!this.curAnchor) return;

		var div = $('calendar-events');

		if(!div) {
			div = new Element('div', { id: 'calendar-events' });

			div.addEvent('mouseover', function() {
				this.stopHideTimer();
			}.bind(this));

			div.addEvent('mouseout', function() {
				this.startHideTimer();
			}.bind(this));

			div.inject($(document.body));
		}

		div.setStyles({
			'opacity': 0,
			'display': 'block',
			'top': this.curAnchor.getTop() + this.curAnchor.getHeight(),
			'left': this.curAnchor.getLeft()
		});


		new Fx.Tween(div, {
			duration: 300,
			onComplete: function() {
				div.empty();
				div.innerHTML = buf;
				new Fx.Tween(div, {
					duration: 300
				}).start('opacity', 1);
			}
		}).start('opacity', 0);

	}

});




















