/**
 * @author pfigueiredo
 */
AvailabilityCalendar.prototype.localization = new Array();

function AvailabilityCalendar(parent, id) {
	this.parent = parent;
	this.style = "Calendar";
	this.panel = null;
	this.id = null;
	this.editMode = false;
	this.data = null;
	this.setupObject = null;
	this.dataHandlerURL = null;
	this.operation = null;
	this.panel = document.createElement('div');
	this.panel.style.display = 'none';
	this.currentDate = new Date();
	this.setID(id);
	this.days = new Array();
	this.daysOfMonth = new Array();
}

AvailabilityCalendar.prototype.draw = function() {
	this.initMatrix();
	this.retrieveData(this.currentDate.getMonth(), this.currentDate.getFullYear());
}

AvailabilityCalendar.prototype.initMatrix = function() {
	var table = this.panel.appendChild(document.createElement('table'));
	var tHead = table.appendChild(document.createElement('thead'));
	var tHeadNav = tHead.appendChild(document.createElement('tr'));
	var tHeadDays = tHead.appendChild(document.createElement('tr'));
	var tBody = table.appendChild(document.createElement('tbody'));
	var monthDate = new Date();
	monthDate.setMonth(10);
	monthDate.setDate(1);

	table.className = '';
	tHead.className = '';
	tBody.className = '';

	//tHeadDays.appendChild(document.createElement('th'));

	/*this.titlePreviousMonth = tHeadNav.appendChild(document.createElement('th')).appendChild(document.createElement('div'));
	this.titlePreviousMonth.appendChild(document.createTextNode(''));
	this.titlePreviousMonth.className = 'CalendarPrevButton';
	this.titleText = tHeadNav.appendChild(document.createElement('th'));
	this.titleText.colSpan = 7;
	this.titleText.className = 'CalendarTitle';
	this.titleText.appendChild(document.createTextNode(' '));
	this.titleNextMonth = tHeadNav.appendChild(document.createElement('th')).appendChild(document.createElement('div'));
	this.titleNextMonth.appendChild(document.createTextNode(''));
	this.titleNextMonth.className = 'CalendarNextButton';*/
	var thisID = this;

	for (var i = 0, dayOfMonth = 1; i != 6; i++) {
		var tRow = tBody.appendChild(document.createElement('tr'));
		//tRow.appendChild(document.createElement('td'));
		this.days[i] = new Array();
		for (var k = 0; k != 7; k++) {
			if (i  == 0) {
				var th = document.createElement('th');
				th.className = 'CalendarDayName';
				var abbr = document.createElement('abbr');
				abbr.title = this.localization['weekDayName'][k];
				abbr.appendChild(document.createTextNode(this.localization['weekDayName'][k].substr(0, 3)));
				th.appendChild(abbr);
				tHeadDays.appendChild(th);
			}
			tRow.appendChild(this.days[i][k] = document.createElement('td'));
			tRow.style.textAlign = 'center';
			this.days[i][k].appendChild(document.createTextNode(' '));
			this.days[i][k].className = 'CalendarDay';
			this.days[i][k].onclick = function(e) {
				if (e == null) {
					e = event;
				}
				var target = null;
				if (e.target) {
					target = e.target;
				}
				else if (e.srcElement) {
					target = e.srcElement;
				}
				thisID.onDaySelected(e, target.rel);
			}
		}
	}
}

AvailabilityCalendar.prototype.onCalendarChanged = function(event, selectedMonth) {
}

AvailabilityCalendar.prototype.onDaySelected = function(event, selectedDay) {
}

AvailabilityCalendar.prototype.showMonth = function(monthToShow, monthYear) {
 	var monthDate = new Date();
	var nDayInMonth = this.getDaysInMonth(10, monthYear);
	monthDate.setDate(1);
	monthDate.setMonth(10);
	monthDate.setFullYear(monthYear);

	var thisID = this;
	/*this.titlePreviousMonth.onclick = function(e) {
		if (e == null) {
			e = event;
		}
		thisID.retrieveData(monthToShow - 1, monthYear);
	}*/
	var yearToShow = monthYear%100;
	yearToShow = (yearToShow < 10 ? '0' : '') + yearToShow;
	/*this.titleText.removeChild(this.titleText.childNodes[0]);
	this.titleText.appendChild(document.createTextNode(' ' + this.localization['monthName'][monthToShow] + ' \'' + yearToShow + ' '));
	this.titleNextMonth.onclick = function(e) {
		if (e == null) {
			e = event;
		}
		thisID.retrieveData(monthToShow + 1, monthYear);
	}*/

	for (var i = 0, dayOfMonth = 0; i != 6; i++) {
		for (var k = 0; k != 7; k++) {
			if (dayOfMonth  == 0 && (k == monthDate.getDay())) {
				dayOfMonth = 1;
			}
			if (dayOfMonth != 0 && dayOfMonth <= nDayInMonth) {
				this.daysOfMonth[dayOfMonth] = this.days[i][k];
			}
			var curDay = dayOfMonth == 0 || dayOfMonth > nDayInMonth ?  ' ' : dayOfMonth++;
			this.days[i][k].removeChild(this.days[i][k].childNodes[0]);
			this.days[i][k].rel = curDay;
			this.days[i][k].appendChild(document.createTextNode(curDay));
		}
	}

	this.onCalendarChanged(new Date(monthYear + '/' + (monthToShow + 1) + "/01"));
}

AvailabilityCalendar.prototype.setDate = function(month, year) {
	this.currentDate.setMonth(month - 1);
	this.currentDate.setFullYear(year);
	this.showMonth(this.currentDate.getMonth(), this.currentDate.getFullYear());
}

AvailabilityCalendar.prototype.getDaysInMonth = function (month,year) {
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 1) {
		return m[month];
	}
	if (year % 4 != 0) {
		return m[1];
	}
	if (year % 100 == 0 && year % 400 != 0) {
		return m[1];
	}
	return m[1] + 1;
}

AvailabilityCalendar.prototype.init = function(setupObject, operation, url) {
	this.panel.className = 'Calendar';
	this.operation = operation;
	this.setDataHandler(url);
	this.setup(setupObject);
	this.draw();
	this.write();
}

AvailabilityCalendar.prototype.getParent = function() {
	return this.parent;
}

AvailabilityCalendar.prototype.getStyle = function() {
	return this.style;
}

AvailabilityCalendar.prototype.getElement = function() {
	return this.panel;
}

AvailabilityCalendar.prototype.setParent = function(parent) {
	this.parent = parent;
}

AvailabilityCalendar.prototype.setStyle = function(style) {
	this.style = style;
}

AvailabilityCalendar.prototype.setElement = function(element) {
	this.panel = element;
}

AvailabilityCalendar.prototype.setID = function(id) {
	this.panel.setAttribute('id', id);
	this.id = id;
}

AvailabilityCalendar.prototype.setCssClass = function(cssClass) {
	this.panel.className = cssClass;
}

AvailabilityCalendar.prototype.setCssStyle = function(cssStyle) {
	this.panel.style.cssText = cssStyle;
}

AvailabilityCalendar.prototype.write = function() {
	if (this.parent != null && this.panel != null) {
		this.parent.appendChild(this.panel);
		return;
	}
	else {
	}
	alert("Invalid values:\n- Calendar.parent: " + this.parent + "\n- Calendar.element: " + this.panel);
}

AvailabilityCalendar.prototype.setup = function(object) {
	this.setupObject = object;
}

AvailabilityCalendar.prototype.setData = function(object) {
	this.data = object;
}

AvailabilityCalendar.prototype.getData = function() {
	return this.data;
}

AvailabilityCalendar.prototype.setDataHandler = function(url) {
	this.dataHandlerURL = url;
}

AvailabilityCalendar.prototype.getDataHandler = function() {
	return this.dataHandlerURL;
}

AvailabilityCalendar.prototype.retrieveData = function(monthToShow, monthYear) {
	if (monthToShow > 11) {
		monthToShow = 0;
		monthYear++;
	}
	if (monthToShow < 0) {
		monthToShow = 11;
		monthYear--;
	}
	this.currentDate.setMonth(10);
	this.currentDate.setFullYear(monthYear);
	this.setupObject.month = 11;
	this.setupObject.year = monthYear;

	if (this.dataHandlerURL == null) {
		this.data = this.setupObject;

		var event = new Object();
		event.data = this.data;
		this.onData(event, null);
		this.onSetup(event, null);
		this.write();
		return;
	}

	var connection = new AvailabilityCalendarConnection(this);
	try {
		connection.connect(this.dataHandlerURL, 'POST');
		connection.onReady = function(target, ajaxResponse, response) {
			try {
				target.setData(response);

				var event = new Object();
				event.data = response;
				target.onData(event, ajaxResponse);
				target.onSetup(event, ajaxResponse);
				target.write();
			}
			catch (jsonException) {
				alert(jsonException);
			}
		}
		connection.send(this.operation, this.setupObject);
	}
	catch (exception) {
		//alert(exception);
	}
}

AvailabilityCalendar.prototype.onData  = function(e, source) {
	this.showMonth(this.currentDate.getMonth(), this.currentDate.getFullYear());
}

AvailabilityCalendar.prototype.onSetup  = function(e, source) {
}

AvailabilityCalendar.prototype.onAbort  = function(e, source) {
	var input = null;
	if (!e) {
		var e = window.event;
	}
	if (e.target) {
		input = e.target;
	}
	else if (e.srcElement) {
		input = e.srcElement;
	}
}

AvailabilityCalendar.prototype.onBlur  = function(e, source) {
}

AvailabilityCalendar.prototype.onChange  = function(e, source) {
}

AvailabilityCalendar.prototype.onClick  = function(e, source) {
}

AvailabilityCalendar.prototype.onDblClick = function(e, source) {
}

AvailabilityCalendar.prototype.onError = function(e, source) {
}

AvailabilityCalendar.prototype.onFocus  = function(e, source) {
}

AvailabilityCalendar.prototype.onKeyDown  = function(e, source) {
}

AvailabilityCalendar.prototype.onKeyPress  = function(e, source) {
}

AvailabilityCalendar.prototype.onKeyUp  = function(e, source) {
}

AvailabilityCalendar.prototype.onLoad  = function(e, source) {
}

AvailabilityCalendar.prototype.onMouseDown  = function(e, source) {
}

AvailabilityCalendar.prototype.onMouseMove  = function(e, source) {
}

AvailabilityCalendar.prototype.onMouseOut  = function(e, source) {
}

AvailabilityCalendar.prototype.onMouseOver  = function(e, source) {
}

AvailabilityCalendar.prototype.onMouseUp  = function(e, source) {
}

AvailabilityCalendar.prototype.onReset  = function(e, source) {
}

AvailabilityCalendar.prototype.onResize  = function(e, source) {
}

AvailabilityCalendar.prototype.onSelect  = function(e, source) {
}

AvailabilityCalendar.prototype.onSubmit  = function(e, source) {
}

AvailabilityCalendar.prototype.onUnload  = function(e, source) {
}