// AJAX JavaScript Document for the Calendar

//Initialization Functionality
//window.onload = function() {
//	var currFunction = prepareBoxes;
//	eachBox(currFunction);
//};

function eachBox(passedFunction) {
	var el;
	for(var i=1; i <= 31; i++) {
		if(el = document.getElementById(i)) {
			passedFunction(el);
		}
	}
}

function prepareBoxes(el) {
	el.onmouseover = function() {
		if (this.style.backgroundColor != "rgb(172, 189, 219)" && this.style.backgroundColor != "#acbddb") {
			this.style.backgroundColor = "#DDDDDD";
		}
	};
	el.onmouseout = function() {
		if (this.style.backgroundColor != "rgb(172, 189, 219)" && this.style.backgroundColor != "#acbddb") {
			this.style.backgroundColor = "#FFFFFF";
		}
	};
	el.onclick = function(e) {
		var currFunction = clearColor;
		eachBox(currFunction);
		this.style.backgroundColor = "#ACBDDB";

		var xCoord = getXCoord(e);
		var yCoord = getYCoord(e);
		var linkInfo = this.getElementsByTagName("a")
		var data = linkInfo[0].getAttribute("href").split("?")[1];
		var url = "calendar/events.inc.php";
		return !sendData(url, data, "bubble", xCoord, yCoord);
	};
}

function initializePopup() {
	document.getElementById("close").onclick = function() {
		ClearContent("popupContent");
		HideContent("bubble");
		var currFunction = clearColor;
		eachBox(currFunction);
		return false;
	};
	
	//Initialize links for displaying event details
	var eventGroup = document.getElementById('popupContent');
	var events = eventGroup.getElementsByTagName('div');
	for (var i=0; i < events.length; i++) {
		if (events[i].className == "group") {
			events[i].getElementsByTagName('a')[0].onclick = function() {
				var data = this.getAttribute('href').split('?')[1];
				var id = data.split('&')[0];
				var num = id.split('=')[1];
				var name = document.getElementById("name" + num);
				var details = document.getElementById("detail" + num);
				if (details.style.display == "block") {
					details.style.display = "none";
					name.style.backgroundPosition = "0 0";
				} else {
					details.style.display = "block";
					name.style.backgroundPosition = "0 -289px";
				}
				return false;
			};
		}
	}
}


//AJAX Functionality
function sendData(url, data, tagId, xCoord, yCoord) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			parseData(request, 'popup', xCoord, yCoord);
		};
		request.open("POST", url, true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send(data);
		ShowContent(tagId);
		return true;
	} else {
		return false;
	}
}

function parseData(request, tagId, xCoord, yCoord) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var container = document.getElementById(tagId);
			positionPopup(xCoord, yCoord);
			container.innerHTML = request.responseText;
			initializePopup();
		}
	}
}


//Other Functionality
function ShowContent(tag)
{
	var el;
	el = document.getElementById(tag);
	el.style.display = 'block';
}

function HideContent(tag)
{
	var el;
	el = document.getElementById(tag);
	el.style.display = 'none';
}

function ClearContent(tag)
{
	var el;
	el = document.getElementById(tag);
	el.innerHTML = '';
}

function clearColor(el) {
	el.style.backgroundColor = "#FFFFFF";
}

function getXCoord(e) {
	if (!e) e=window.event;
	return e.clientX;
}

function getYCoord(e) {
	if (!e) e=window.event;
	return e.clientY;
}

function positionPopup(xCoord, yCoord) {
	var xOffset;
	var yOffset;
	var xExtra;
	var d = document.getElementById('bubble');
	var dh = d.offsetHeight;
	if (document.documentElement.scrollTop) {
		xOffset = document.documentElement.scrollLeft;
		yOffset = document.documentElement.scrollTop;
	} else {
		xOffset = document.body.scrollLeft;
		yOffset = document.body.scrollTop;
	}
	if (window.innerWidth) {
		xExtra = window.innerWidth;
	} else {
		if (document.body.clientWidth) {
			xExtra = document.body.clientWidth;
		} else {
			xExtra = document.documentElement.clientWidth;
		}
	}
	dh = 0; //DELETE THIS FOR BUBBLE APPEARING BOTTOM UP
	xExtra = (xExtra - 915) / 2;
	var x = xCoord - 215 - xExtra + xOffset - 21;
	var y = yCoord - 294 + yOffset - dh;
	//alert("xCoord = " + xCoord + "\r\nyCoord = " + yCoord + "\r\nxOffset = " + xOffset + "\r\nyOffset = " + yOffset + "\r\nxExtra = " + xExtra + "\r\ndh = " + dh + "\r\nx = " + x + "\r\ny = " + y);
	document.getElementById("bubble").style.top = y +"px";
	document.getElementById("bubble").style.left = x +"px";
}