if (Object.isUndefined(NVB)) { var NVB = { } }
NVB.Calendar = Class.create({
	initialize: function(target, ajax_url) {
		var e = Prototype.emptyFunction;
		this.ie = Prototype.Browser.IE;
		this.target = target;
		this.ajax_url = ajax_url;
		this.busy = false;
		
		this.events = {
			_click: this.changeMonth.bind(this),
			_onCreate: this.onCreate.bind(this),
			_onComplete: this.onComplete.bind(this),
			startup: this.startup.bind(this)
		}
		Ajax.Responders.register({
			onCreate: this.events._onCreate,
			onComplete: this.events._onComplete
		});
		new PeriodicalExecuter(this.events.startup, 0.05);
	},
	startup: function(pe){
		
		if($('calendar').select('ul li a')){
			if(pe)pe.stop();
			this.previous = $('calendar').select('ul li a').first();
			this.next =  $('calendar').select('ul li a').last();

			this.previous.writeAttribute('onclick');
			this.next.writeAttribute('onclick');
			this.previous.observe('click',this.events._click);
			this.next.observe('click',this.events._click);
		}
	},
	onCreate: function() {
		this.busy = true;
	},
	onComplete: function() {
		this.busy = false;
		
		this.previous = $$(this.target).last()
		this.next = $$(this.target).first();
		
			this.previous.writeAttribute('onclick');
			this.next.writeAttribute('onclick');
			this.previous.observe('click',this.events._click);
			this.next.observe('click',this.events._click);
	},
	changeMonth: function(evt){
		var elm = evt.element();
		var param = {
			date:elm.readAttribute('title')
		}
		new Ajax.Updater('calendar', this.ajax_url, {
			parameters:param
		});
		evt.stop();
		return false;
		
	}
});
document.observe('dom:loaded', function(){
	new NVB.Calendar('#calendar ul li a', '/www/scripts/includes/calendar.ajax.php');
});