// JavaScript Document

var divCalendar = null;
var divDates = null;
var divTitles = null;
var divDescription = null;

function getDates(year, month, day){
	var oXmlDom = zXmlDom.createDocument();
	oXmlDom.onreadystatechange = function(){
		if (oXmlDom.readyState == 4){
			showDates(oXmlDom, year, month, day);
		}
	}
	oXmlDom.load("calendar/getDates.php?year=" + year + "&month=" + month);
}

function showDates(xml, year, month, day){
	// get the num of dates
	var root = xml.documentElement;
	if (root.tagName == "error"){
		
	}
	else if (root.tagName == "calendar"){
		var daysNumEl = root.getElementsByTagName("daysNum");
		var daysNumStrEl = daysNumEl[0].firstChild;
		var daysNum = Number(daysNumStrEl.nodeValue);
	}
	
	// create table of the dates
	// cursor to next and previous months
	var cursor = document.createElement("div");
	cursor.id = "calendarCursor";
	showMonthCursors(cursor, day, month, year);
	divDates.appendChild(cursor);
	
	// table header
	var tblDates = document.createElement("table");
	var tblHead = document.createElement("thead");
	var tblHeadRow = document.createElement("tr");
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Lu"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Ma"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Me"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Gi"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Ve"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Sa"));
	tblHeadRow.appendChild(tblHeadTd);
	var tblHeadTd = document.createElement("th");
	tblHeadTd.appendChild(document.createTextNode("Do"));
	tblHeadRow.appendChild(tblHeadTd);
	tblHead.appendChild(tblHeadRow);
	tblDates.appendChild(tblHead);
	
	// table body
	var tblBody = document.createElement("tbody");
	var date = new Date();
	date.setFullYear(year);
	date.setMonth(month - 1);
	date.setDate(1);
	var start = date.getDay();
	if (start == 0){
		start = 6;
	}
	else {
		start--;
	}
	// create the table
	var lastRow = null;
	for (var i = 1 - start; i <= daysNum; i++){
		if ((i + start - 1) % 7 == 0){
			if (lastRow != null){
				tblBody.appendChild(lastRow);
			}
			lastRow = document.createElement("tr");
		}
		var cell = document.createElement("td");
		if (i > 0){
			var dayLink = document.createElement("a");
			dayLink.setAttribute("href", "javascript:showCalendar(" + year + ", " + month + ", " + i + ")");
			dayLink.appendChild(document.createTextNode(i));
			if (i == day){
				var strong = document.createElement("strong");
				strong.appendChild(dayLink);
				cell.appendChild(strong);
			}
			else {
				cell.appendChild(dayLink);
			}
		}
		lastRow.appendChild(cell);
	}
	tblBody.appendChild(lastRow);
	tblDates.appendChild(tblBody);
	divDates.appendChild(tblDates);
}

function getMessages(year, month, day){
	var oXmlDom = zXmlDom.createDocument();
	oXmlDom.onreadystatechange = function(){
		if (oXmlDom.readyState == 4){
			showMsgsTitles(oXmlDom, year, month, day);
		}
	}
	oXmlDom.load("calendar/getMessages.php?year=" + year + "&month=" + month + "&day=" + day);
}

function showMsgsTitles(xml, year, month, day){
	// get the num of messages
	var root = xml.documentElement;
	if (root.tagName == "error"){
		
	}
	else if (root.tagName == "calendar"){
		var msgsNumEl = root.getElementsByTagName("msgsNum");
		var msgsNumStrEl = msgsNumEl[0].firstChild;
		var msgsNum = Number(msgsNumStrEl.nodeValue);
	}
	
	// if none msg is present, print that
	if (msgsNum == 0){
		var noneMsg = document.createElement("p");
		noneMsg.appendChild(document.createTextNode("Nessun messaggio"));
		divTitles.appendChild(noneMsg);
		return;
	}
	
	// show all the message titles
	var msgsList = root.getElementsByTagName("msg");
	var listEl = document.createElement("ul");
	for (var i = 0; i < msgsList.length; i++){
		var listItemEl = document.createElement("li");
		var listItemLinkEl = document.createElement("a");
		listItemLinkEl.setAttribute("href", "javascript:getMsgDescription(" + year + ", " + month + ", " + day + ", " + (i + 1) + ")");
		listItemLinkEl.appendChild(document.createTextNode(msgsList[i].firstChild.nodeValue));
		listItemEl.appendChild(listItemLinkEl);
		listEl.appendChild(listItemEl);
	}
	divTitles.appendChild(listEl);
}

function getMsgDescription(year, month, day, msg){
	var oXmlDom = zXmlDom.createDocument();
	oXmlDom.onreadystatechange = function(){
		if (oXmlDom.readyState == 4){
			showMsgDescription(oXmlDom);
		}
	}
	oXmlDom.load("calendar/getMessage.php?year=" + year + "&month=" + month + "&day=" + day + "&msg=" + msg);
}

function showMsgDescription(xml){
	// get the num of messages
	var root = xml.documentElement;
	if (root.tagName == "error"){
		return;
	}
	else if (root.tagName == "calendar"){
		// msg title
		var msgTitleEl = root.getElementsByTagName("title");
		var title = msgTitleEl[0].firstChild.nodeValue;
		// msg description
		var msgDescriptionEl = root.getElementsByTagName("description");
		var description = msgDescriptionEl[0].firstChild.nodeValue;
	
		// show
		var titleEl = document.createElement("p");
		titleEl.id = "calendarMsgTitle";
		titleEl.appendChild(document.createTextNode(title));
		var descEl = document.createElement("p");
		descEl.appendChild(document.createTextNode(description));
		divDescription.innerHTML = "";
		divDescription.appendChild(titleEl);
		divDescription.appendChild(descEl);
		divDescription.style.visibility = "visible";
	}
}

function showMonthCursors(container, day, month, year){
	if (month == 12){
		nextMonth = 1;
		nextYear = year + 1;
	}
	else {
		nextMonth = month + 1;
		nextYear = year;
	}
	if (month == 1){
		prevMonth = 12;
		prevYear = year - 1;
	}
	else {
		prevMonth = month - 1;
		prevYear = year;
	}
	
	var months = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
	
	var spanCurrent = document.createElement("span");
	spanCurrent.appendChild(document.createTextNode(months[month - 1] + " " + year));
	
	var linkPrev = document.createElement("a");
	linkPrev.setAttribute("href", "javascript:showCalendar(" + prevYear + ", " + prevMonth + ", " + day + ")");
	linkPrev.appendChild(document.createTextNode("<<"));
	var linkNext = document.createElement("a");
	linkNext.setAttribute("href", "javascript:showCalendar(" + nextYear + ", " + nextMonth + ", " + day + ")");
	linkNext.appendChild(document.createTextNode(">>"));
	
	container.appendChild(linkPrev);
	container.appendChild(spanCurrent);
	container.appendChild(linkNext);
}

function showCalendar(year, month, day){
	if (typeof year == "undefined" || typeof month == "undefined" || typeof day == "undefined"){
		var date = new Date();
		year = date.getFullYear();
		month = date.getMonth() + 1;
		day = date.getDate();
	}
	// Calendar
	divCalendar = document.getElementById("calendar");
	divCalendar.innerHTML = "";
	var closeDiv = document.createElement("div");
	closeDiv.id = "calendarClose";
	var closeBut = document.createElement("a");
	closeBut.setAttribute("href", "javascript:closeCalendar()");
	closeBut.appendChild(document.createTextNode("Chiudi"));
	closeDiv.appendChild(closeBut);
	divCalendar.appendChild(closeDiv);
	// Get days and show them
	if (divDates == null){
		divDates = document.createElement("div");
		divDates.id = "calendarDates";
	}
	else {
		// remove all childs
		divDates.innerHTML = "";
	}
	getDates(year, month, day);
	divCalendar.appendChild(divDates);
	// Get the messages for the day and show them
	if (divTitles == null){
		divTitles = document.createElement("div");
		divTitles.id = "calendarTitles";
	}
	else {
		// remove all childs
		divTitles.innerHTML = "";
	}
	getMessages(year, month, day);
	divCalendar.appendChild(divTitles);
	if (divDescription == null){
		divDescription = document.createElement("div");
		divDescription.id = "calendarDescription";
	}
	else {
		// remove all childs
		divDescription.innerHTML = "";
	}
	divDescription.style.visibility = "hidden";
	divCalendar.appendChild(divDescription);
	divCalendar.style.visibility = "visible";
}

function closeCalendar(){
	divCalendar.style.visibility = "hidden";
}
