function Calendar(name, date, needTime) {
	this.name = name;
	this.needTime = (needTime==null)?true:needTime;
	this.callBack = null;
	this.items = null;
	this.time_items = null;
	this.selectedDate = null;
	this.selectedMonth = new Date();

	if (date) {
		this.selectedDate = new Date();
		this.setDateTime(this.selectedDate, date);
		this.selectedMonth.setTime(this.selectedDate);
	}
	this.selectedMonth.setDate(1);

	this.initItems();
	this.redraw();
}
Calendar.prototype.getMonthName = function (month) {
	var months = new Array("Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December");
	return months[month];
}
Calendar.prototype.setCallBack = function (cb) {
	this.callBack = cb;
}
Calendar.prototype.setDateTime = function (date, dateStr) {
	var dt, d, t;
	dt = dateStr.split(" ");

	d = dt[0].split("-");
	date.setFullYear(d[0]);
	date.setMonth(Number(d[1]) - 1);
	date.setDate(d[2]);

	if (this.needTime) {
		t = dt[1].split(":");
		date.setHours(t[0]);
		date.setMinutes(t[1]);
		date.setSeconds(0);
	}
}
Calendar.prototype.getDateTime = function() {
	var d = this.selectedDate;
	var str;
	if (d==null) return null;
	str = d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate();
	if (this.needTime)
		str += " " + d.getHours() + ":" + d.getMinutes();
	
	return str;
}
Calendar.prototype.getDateTimeStr = function() {
	var d = this.selectedDate;
	var str;
	if (d==null) return null;
	str = d.getFullYear() + " " + this.getMonthName(d.getMonth()) + " " + d.getDate() + "."
	if (this.needTime)
		str += " " + d.getHours() + " óra " + d.getMinutes() + " perc";
	
	return str;
}
Calendar.prototype.initItems = function () {
	var week, day, div_time;
	var item;
	this.items = new Array();
	for (week=0; week<6; week++) {
		this.items[week] = new Array();
		for (day=0; day<7; day++) {
			item = window.document.getElementById(this.name + "_" + week + "_" + day);
			item.calendar = this;
			item.onclick = this.selectDate;
			this.items[week][day] = item;
		}
	}
	this.title_item = window.document.getElementById(this.name + "_title");
	this.time_items = new Array();
	
	div_time = window.document.getElementById(this.name + "time");
	if (!this.needTime)
		div_time.style.display = "none";

	item = window.document.getElementById(this.name + "_hours");
	item.calendar = this;
	item.onblur = this.refreshTime;
	this.time_items.push(item);

	item = window.document.getElementById(this.name + "_minutes");
	item.calendar = this;
	item.onblur = this.refreshTime;
	this.time_items.push(item);

	item = window.document.getElementById(this.name + "_left");
	item.calendar = this;
	item.onclick = this.decMonth;

	item = window.document.getElementById(this.name + "_right");
	item.calendar = this;
	item.onclick = this.incMonth;

	item = window.document.getElementById(this.name + "_close");
	item.calendar = this;
	item.onclick = this.ignoreDate;

}
Calendar.prototype.redraw = function () {
	var dayOfMonth;
	var days = this.getMaxDate(this.selectedMonth);
	dayOfMonth = - (this.selectedMonth.getDay() + 6) % 7 + 1;
	var week, day;
	var item;
	for (week=0; week<6; week++) {
		for (day=0; day<7; day++) {
			item = this.items[week][day];
			if (dayOfMonth<=0 || dayOfMonth>days) {
				item.innerHTML = '';
				item.className="inactive";
			} else if (this.selectedDate &&
						(this.selectedMonth.getFullYear() == this.selectedDate.getFullYear()) &&
						(this.selectedMonth.getMonth() == this.selectedDate.getMonth()) &&
						(dayOfMonth == this.selectedDate.getDate())) {
				item.innerHTML = dayOfMonth;
				item.className="selected";
			} else {
				item.innerHTML = dayOfMonth;
				item.className="active";
			}
			dayOfMonth ++;
		}
	}
	if (this.selectedDate) {
		this.time_items[0].value = this.selectedDate.getHours();
		this.time_items[1].value = this.selectedDate.getMinutes();
	} else {
		this.time_items[0].value = "21";
		this.time_items[1].value = "00";
	}

	this.title_item.innerHTML = this.getMonthName(this.selectedMonth.getMonth()) + " " + this.selectedMonth.getFullYear();
}
Calendar.prototype.getMaxDate = function (date) {
	switch (date.getMonth()) {
		case 1: // February
			return 28 + ((date.getYear() % 4 == 0)?1:0);
		case 3: // April
		case 5: // June
		case 8: // September
		case 10: // November
			return 30;
		case 0: // January
		case 2: // March
		case 4: // May
		case 6: // July
		case 7: // August
		case 9: // October
		case 11: // December
			return 31;
	}
}
Calendar.prototype.decMonth = function () {
	this.calendar.selectedMonth.setMonth(this.calendar.selectedMonth.getMonth() - 1);
	this.calendar.redraw();
}
Calendar.prototype.incMonth = function () {
	this.calendar.selectedMonth.setMonth(this.calendar.selectedMonth.getMonth() + 1);
	this.calendar.redraw();
}
Calendar.prototype.selectDate = function () {
	if (this.innerHTML == "") return;
	if (this.calendar.selectedDate == null) this.calendar.selectedDate = new Date();
	this.calendar.selectedDate.setFullYear(this.calendar.selectedMonth.getFullYear());
	this.calendar.selectedDate.setMonth(this.calendar.selectedMonth.getMonth());
	this.calendar.selectedDate.setDate(this.innerHTML);
	this.calendar.redraw();
	if (this.calendar.callBack) this.calendar.callBack(this.calendar);
}
Calendar.prototype.ignoreDate = function () {
	this.calendar.selectedDate = null;
	this.calendar.redraw();
	if (this.calendar.callBack) this.calendar.callBack(this.calendar);
	return false;
}
Calendar.prototype.refreshTime = function () {
	var h = this.calendar.time_items[0].value;
	var m = this.calendar.time_items[1].value;

	if (this.calendar.selectedDate == null) this.calendar.selectedDate = new Date();
	this.calendar.selectedDate.setHours(h);
	this.calendar.selectedDate.setMinutes(m);
}


function showCalendar(name) {
	var item = window.document.getElementById(name);
	item.style.display = "block";
	// addEvent(window.document, "click", hideCalendarEvent);
}
function hideCalendar(name) {
	var item = window.document.getElementById(name);
	item.style.display = "none";
	// removeEvent(window.document, "click", hideCalendarEvent);
}
function hideCalendarEvent(e) {
	hideCalendar("calendar");
}
