// Globals
// Set flag to denote Internet Explorer browser
var isIE = document.all ? true : false;

// Trigger mouse move capture events for non-Internet explorer browsers
if (!(isIE))
	document.captureEvents(Event.MOUSEMOVE);

// Set function to call on mouse move events
document.onmousemove = setSummaryPos;

var calSummaryPopupActive = false; // True if the Summary layer is showing
var calSummaryPopupVisible = false; // True if the Summary layer is visible
var calDescPopupActive = false; // True is the Description layer is showing

// Function showSummary is called when a mouse over event occurs for an active date table cell
function showSummary(tabCell, day) {
	tabCell.style.backgroundColor = "#FFFF00";
	if (!(calDescPopupActive)) // Don't show Summary if the Description layer is visible
		if (!(calSummaryPopupActive)) { // Don't show Summary if it's already active 
			var title = document.getElementById('cal' + day + 't').value;
			var summary = (document.getElementById('cal' + day + 's')) ?
					document.getElementById('cal' + day + 's').value : "";
			var cont = (document.getElementById('cal' + day + 'd')) ?
					"Click to view more..." : "";
			document.getElementById('calDatePopupTitle').childNodes[0].nodeValue = title;
			// Summary may contain HTML <BR /> elements so set innerHTML
			document.getElementById('calDatePopupSummary').innerHTML = summary;
			document.getElementById('calDatePopupCont').childNodes[0].nodeValue = cont;
			calSummaryPopupActive = true; // Set the Summary layer flag as active
		}
}

// Function hideSummary is called when a mouse out event occurs for an active date table cell
function hideSummary(tabCell) {
	document.getElementById('calDatePopup').style.visibility = "hidden";
	tabCell.style.backgroundColor = "#FFFF99";
	calSummaryPopupActive = false; // Reset the Summary layer flag (i.e. hide any Summary layer)
	calSummaryPopupVisible = false; // Reset the Summary layer visible flag
}

// Function showDesc is called when an on click event occurs for an active data table cell
function showDesc(tabCell, day) {
	calDescPopupActive = true; // Set the Description layer flag as active
	hideSummary(tabCell); // Hide any active Summary layer
	var thisMonthYear = day + " " + document.getElementById('thisMonthYear').value;
	var title = document.getElementById('cal' + day + 't').value;
	var desc = (document.getElementById('cal' + day + 'd')) ?
				document.getElementById('cal' + day + 'd').value : "";
	document.getElementById('calDescPopupDate').childNodes[0].nodeValue = thisMonthYear;
	document.getElementById('calDescPopupTitle').childNodes[0].nodeValue = title;
	// Description may contain HTML <BR /> elements so set innerHTML
	document.getElementById('calDescPopupDesc').innerHTML = desc;
	document.getElementById('calDescPopup').style.visibility = "visible";
}

// Function hideDesc is called when the 'close window' button is pressed on the Description layer
function hideDesc() {
	document.getElementById('calDescPopup').style.visibility = "hidden";
	calDescPopupActive = false; // Reset the Description layer active flag
}

// Function setSummaryPos is called on mouse move events and is used to position the Summary layer
function setSummaryPos(e) {
	if (calSummaryPopupActive) { // Only calculate mouse X/Y if a Summary layer is active
		var mouseX;
		var mouseY;
		if (isIE) { // Internet Explorer mouse X/Y capture
    		mouseX = event.clientX + document.body.scrollLeft;
    		mouseY = event.clientY + document.body.scrollTop;
		}
		else { // Non-Internet Explorer mouse X/Y capture
    		mouseX = e.pageX;
    		mouseY = e.pageY;
		}
		// Move position 10 pixels so as not to obscure mouse pointer
		mouseX -= 145;
		mouseY -= 75;
		// Adjust for NS 4 bug (negative mouse co-ords)
		if (mouseX < 0)
			mouseX = 0;
		if (mouseY < 0)
			mouseY = 0;
		// Set layer top/left to mouseX/mouseY adjusted
		document.getElementById('calDatePopup').style.left = mouseX + "px";
		document.getElementById('calDatePopup').style.top = mouseY + "px";
		// Make this layer visible the first time we have calculated mouse co-ords
		if (!(calSummaryPopupVisible)) {
			document.getElementById('calDatePopup').style.visibility = "visible";
			calSummaryPopupVisible = true;
		}
	}
	return true;
}
