var Calendar = Class.create({

	initialize: function(table, year, month, day, startYearDropdown, startMonthDropdown, startDayDropdown, endYearDropdown, endMonthDropdown, endDayDropdown) {
		this.table = $(table);
		this.state = 0;
		this.first = null;
		this.last = null;
		this.initial = null;
		this.cells = this.table.select("td.cell");
		this.year = year;
		this.month = month;
		this.day = day;
		this.startDayDropdown = $(startDayDropdown);
		this.startMonthDropdown = $(startMonthDropdown);
		this.startYearDropdown = $(startYearDropdown);
		this.endDayDropdown = $(endDayDropdown);
		this.endMonthDropdown = $(endMonthDropdown);
		this.endYearDropdown = $(endYearDropdown);
		
		for (var i=0; i<this.cells.length; ++i) {
			var cell = this.cells[i];
			Event.observe(cell, 'mouseover', this._over.bindAsEventListener(this, cell));
			Event.observe(cell, 'mouseout', this._out.bindAsEventListener(this, cell));
			Event.observe(cell, 'mousedown', this._down.bindAsEventListener(this, cell));
			Event.observe(cell, 'mouseup', this._up.bindAsEventListener(this, cell));
			Event.observe(cell, 'mousemove', this._move.bindAsEventListener(this, cell));
		}

		this.table.onselectstart = function() { return false; };
		this.table.unselectable = "on";
		this.table.style.MozUserSelect = "none";

	},
	_over : function(event) {
		var el = Event.element(event)
		el.addClassName("hover");
	},
	_out : function(event) {
		var el = Event.element(event)
		el.removeClassName("hover");
	},
	_down : function(event) {
		var el = Event.element(event)
		this.initial = el;
		this.first = el;
		this.last = el;
		this.state = 1;
		Event.stop(event);
		this._report();
	},
	_move : function(event) {
		if (this.state == 1) {
			var el = Event.element(event)
			if (this.cells.indexOf(el) < this.cells.indexOf(this.last)) {
				this.first = el;
				this.last = this.initial;
			}
			else {
				this.last = el;
				this.first = this.initial;
			}
			Event.stop(event);
			this._report();
		}
	},
	_up : function(event) {
		var el = Event.element(event)
		if (this.cells.indexOf(el) < this.cells.indexOf(this.last)) {
			this.first = el;
			this.last = this.initial;
		}
		else {
			this.last = el;
			this.first = this.initial;
		}
		this.state = 0;
		Event.stop(event);
		this._report();
	},
	_report : function() {
		var startOffset = 1*this.first.id.substring(1)
		var endOffset = 1*this.last.id.substring(1);
		
		var startDate = new Date(); startDate.setFullYear(this.year); startDate.setMonth(this.month-1); startDate.setDate(this.day);
		var endDate = new Date(); endDate.setFullYear(this.year); endDate.setMonth(this.month-1); endDate.setDate(this.day);
		
		startDate.setDate(startDate.getDate()+startOffset);
		endDate.setDate(endDate.getDate()+endOffset);
		
		this.startDayDropdown.value = startDate.getDate();
		this.startMonthDropdown.value = 1+startDate.getMonth();
		this.startYearDropdown.value = startDate.getFullYear();

		this.endDayDropdown.value = endDate.getDate();
		this.endMonthDropdown.value = 1+endDate.getMonth();
		this.endYearDropdown.value = endDate.getFullYear();

		if (this.first && this.last) {
			var state = "before";

			for (var i=0; i<this.cells.length; ++i) {
				var cell = this.cells[i];

				if (cell == this.first)
					state = "during";

				switch (state) {
					case "before": cell.removeClassName("selected"); break;
					case "during": cell.addClassName("selected"); break;
					case "after": cell.removeClassName("selected"); break;
				}

				if (cell == this.last)
					state = "after";
			}
		}
	}
});
