// JavaScript Document

// Creates the new object
var Cal;
if (!Cal) Cal = {}
else if (typeof Cal != "object") throw new Error("'Cal' already exists and is not an object");

// Define properties
Cal.scriptURL = "/ajax/calendar.aspx";
Cal.imageDir = "/images/calendar/";
Cal.imgPopupLeft = Cal.imageDir + "popup_left.png";
Cal.imgPopupRight = Cal.imageDir + "popup_right.png";
Cal.imgCloseIcon = Cal.imageDir + "close.gif";
Cal.imgCloseRolloverIcon = Cal.imageDir + "close_on.gif";
Cal.imgLoaderIcon = "/images/icons/loading_wheel.gif";
Cal.imgErrorIcon = "/images/icons/error.gif";
Cal.timer = null;

// Define all of the rollover button graphics
Cal.buttonImages = new Array('next','prev');


// Sets up image rollovers
Cal.setupButtons = function() {
	// Get all of the images on this page and iterate through them to determine if any are rollovers
	var pageImages = document.getElementsByTagName("img");
	for (var x = 0; x < pageImages.length; x++) {
		// Get the base name of the current image
		var tmparr1 = pageImages[x].src.split('/');
		var tmparr2 = tmparr1[tmparr1.length - 1].split('.');
		var imgname = tmparr2[0];
		// Check the current image name against the list of buttons and see if there is a match
		for (var i = 0; i < Cal.buttonImages.length; i++) {
			if (imgname == Cal.buttonImages[i]) {
				// Preload rollover image
				var normalURL = Cal.imageDir + imgname + '.gif';
				var rolloverURL = Cal.imageDir + imgname + '_on.gif';
				// Setup rollover events
				addRollover(pageImages[x], normalURL, rolloverURL, '');
				// Exit the loop
				break;
			}
		}
	}
}

// Sets up calendar events
Cal.setupEvents = function() {
	var container = document.getElementById("calEvents");
	if (container) {
		// Get all of the event divs
		var divs = container.getElementsByTagName("div");
		for (var x = 0; x < divs.length; x++) {
			if (divs[x].className == "calevent") {
				// Get each of the links
				var links = divs[x].getElementsByTagName("a");
				for (var i = 0; i < links.length; i++) {
					links[i].onclick = function() { Cal.showPopup(this); }
				}
			}
		}
	}
}

// Create event popup
Cal.setupPopup = function() {
	// Cache popup graphics
	(new Image()).src = Cal.imgPopupLeft;
	(new Image()).src = Cal.imgPopupRight;
	(new Image()).src = Cal.imgLoaderIcon;
	(new Image()).src = Cal.imgErrorIcon;
	// Create popup containers
	var popupbkg = document.createElement("div");
	popupbkg.id = "event-popupbkg";
	popupbkg.style.width = "217px";
	popupbkg.style.height = "195px";
	popupbkg.style.display = "none";
	document.body.appendChild(popupbkg);
	popupbkg.appendChild(document.createTextNode(" "));
	var popup = document.createElement("div");
	popup.id = "event-popup";
	popup.style.width = "169px";
	popup.style.height = "160px";
	popup.style.display = "none";
	document.body.appendChild(popup);
		
	// Create close box will rollover action and onclick action
	var img = document.createElement("img");
	img.src = Cal.imgCloseIcon;
	img.width = "16";
	img.height = "16";
	img.border = "0";
	popup.appendChild(img);
	addRollover(img, Cal.imgCloseIcon, Cal.imgCloseRolloverIcon, "");
	img.onclick = function() { Cal.hidePopup(); }
	
	// Popup status element
	var divStatus = document.createElement("div");
	divStatus.id = "event-status";
	popup.appendChild(divStatus);
	divStatus.style.display = '';
	
	// Popup body elements
	var divData = document.createElement("div");
	divData.id = "event-data";
	popup.appendChild(divData);
	divData.style.display = 'none';
	var h1 = document.createElement("h1");
	h1.id = "event-title";
	divData.appendChild(h1);
	var h2 = document.createElement("h2");
	h2.id = "event-date";
	divData.appendChild(h2);
	var h3 = document.createElement("h3");
	h3.id = "event-time";
	divData.appendChild(h3);
	var h4 = document.createElement("h4");
	h4.id = "event-location";
	divData.appendChild(h4);
	var p = document.createElement("p");
	p.id = "event-description";
	divData.appendChild(p);
	var a = document.createElement("a");
	a.id = "event-link";
	divData.appendChild(a);
	a.appendChild(document.createTextNode("More Info..."));
}

Cal.showPopup = function(obj) {
	// Get divs and event IDs
	var popupbkg = document.getElementById("event-popupbkg");
	var popup = document.getElementById("event-popup");
	var divStatus = document.getElementById("event-status");
	var divData = document.getElementById("event-data");
	var thisid = obj.id;
	// Get the position of the parent link
	var objpos = Cal.getObjPosition(obj);
	var popup_width = parseInt(popupbkg.style.width);
	var popup_height = parseInt(popupbkg.style.height);
	var popup_left = parseInt(objpos.x + obj.offsetWidth - 5);
	var popup_top = parseInt(objpos.y + (obj.offsetHeight/2) - 35);
	var window_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	if (popup_left + popup_width >= window_width) {
		popup_left = parseInt(objpos.x - popup_width + 10);
		if (browserName == "Microsoft Internet Explorer" && browserVer < 7) popupbkg.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + Cal.imgPopupLeft + '\');';
		else popupbkg.style.background = 'url(' + Cal.imgPopupLeft + ') top left no-repeat';
		popup.style.padding = '9px 33px 26px 15px';
	} else {
		if (browserName == "Microsoft Internet Explorer" && browserVer < 7) popupbkg.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + Cal.imgPopupRight + '\');';
		else popupbkg.style.background = 'url(' + Cal.imgPopupRight + ') top left no-repeat';
		popup.style.padding = '9px 17px 26px 31px';
	}
	popupbkg.style.left = '' + popup_left + 'px';
	popupbkg.style.top = '' + popup_top + 'px';
	popup.style.left = '' + popup_left + 'px';
	popup.style.top = '' + popup_top + 'px';
	popupbkg.style.display = '';
	popup.style.display = '';
	Cal.displayStatus(Cal.imgLoaderIcon, "Getting details...");
	Cal.getEventData(thisid);
}

Cal.hidePopup = function() {
	var popup = document.getElementById("event-popup");
	var popupbkg = document.getElementById("event-popupbkg");
	if (popup) popup.style.display = 'none';
	if (popupbkg) popupbkg.style.display = 'none';
}

Cal.getEventData = function(x) {
	if (Cal.timer != null) {
		clearTimeout(Cal.timer);
		Cal.timer = null;
	}
	if (request.readyState == 0 || request.readyState == 4) {
		request.open("POST", Cal.scriptURL, true);
		request.onreadystatechange = Cal.displayEventData;
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send("a=g&e=" + escape(x));
	} else Cal.timer = setTimeout(function() { Cal.getEventData(x) }, 0);
}

Cal.displayEventData = function() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			// Get the popup elements
			var divStatus = document.getElementById("event-status");
			var divData = document.getElementById("event-data");
			var h1 = document.getElementById("event-title");
			var h2 = document.getElementById("event-date");
			var h3 = document.getElementById("event-time");
			var h4 = document.getElementById("event-location");
			var p = document.getElementById("event-description");
			var a = document.getElementById("event-link");
			// Get the XML response
			var xmlDoc = request.responseXML;
			// Process text nodes
			Cal.processNode("Title", h1, xmlDoc);
			Cal.processNode("LongDateString", h2, xmlDoc);
			Cal.processNode("TimeString", h3, xmlDoc);
			Cal.processNode("Location", h4, xmlDoc);
			Cal.processNode("Description", p, xmlDoc);
			// Process link
			var linkurl = xmlDoc.getElementsByTagName("LinkURL");
			if (linkurl.length > 0 && linkurl[0].firstChild.nodeValue) {
				a.style.display = '';
				a.setAttribute("href", linkurl[0].firstChild.nodeValue);
				var linktarget = xmlDoc.getElementsByTagName("LinkTarget");
				if (linktarget.length > 0 && linktarget[0].firstChild.nodeValue) a.setAttribute("target", linktarget[0].firstChild.nodeValue);
			} else a.style.display = 'none';
			// Hide status message, display data
			divStatus.style.display = 'none';
			divData.style.display = '';
		} else {
			// Get failed status error message
			var message = "";
			message = request.statusText;
			if (message.length == 0) message = "Unspecified error " + request.status;
			// Display error message
			displayStatus(Cal.imgErrorIcon, message);
		}
	}
}

Cal.processNode = function(name, elem, xml) {
	var tmp = xml.getElementsByTagName(name);
	if (tmp.length > 0 && tmp[0].firstChild.nodeValue) {
		replaceText(elem, tmp[0].firstChild.nodeValue);
		elem.style.display = '';
	} else {
		replaceText(elem, "");
		elem.style.display = 'none';
	}
}

Cal.displayStatus = function(url, text) {
	var divStatus = document.getElementById("event-status");
	var divData = document.getElementById("event-data");
	while (divStatus.firstChild != null) divStatus.removeChild(divStatus.firstChild);
	// Update status image
	var img = document.createElement("img");
	img.src = url;
	img.width = "16";
	img.height = "16";
	img.border = "0";
	divStatus.appendChild(img);
	// Update status text
	var p = document.createElement("p");
	divStatus.appendChild(p);
	p.appendChild(document.createTextNode(text));
	divStatus.style.display = '';
	divData.style.display = 'none';
}

Cal.getObjPosition = function(e){
	Number.prototype.NaN0 = function() { return isNaN(this)?0:this; }
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}
	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
	return {x:left, y:top};
}

// Pricing OnLoad directive
Cal.doOnLoad = function() {
	// Sets up image rollovers
	Cal.setupButtons();
	// Sets up calendar events
	Cal.setupEvents();
	// Sets up calendar popup
	Cal.setupPopup();
}

if (window.addEventListener) window.addEventListener("load", Cal.doOnLoad, false);
else if (window.attachEvent) window.attachEvent("onload", Cal.doOnLoad);