var chartPopup = function(data) {
	
	var 

	// Ugly workaround for IE and/or jQuery's poor XML string support
	parseXml = function(xml) {
		if (typeof (xml) === 'string' && jQuery.browser.msie) {
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.loadXML(xml);
			xml = xmlDoc;
		}
		return $(xml);
	},

	$data = parseXml(data),

	// Returns the incoming date object as a YYYYMMDD formatted string
	makeDateStamp = function(date) {
		var 
		month = date.getMonth() + 1,
		day = date.getDate(),
		year = date.getFullYear();
		month = month.toString().length < 2 ? '0' + month : month;
		day = day.toString().length < 2 ? '0' + day : day;
		return year + month + day;
	},

	closePopups = function(e) {
		$('.multiPopup').remove();
	},

	dayClick = function(e) {

		var 
		dateTitle = $(e.target).attr('title'),
		date = makeDateStamp(new Date(dateTitle)),
		$archive = $data.find('[date="' + date + '"]');

		if ($archive.length > 0 && $('#multiPopup-' + date).length === 0) {

			closePopups(null);

			var 
			id = 'multiPopup-' + date,
			out = '<div id="' + id + '" class="multiPopup"><ul><li class="date"><strong>' + dateTitle + '</strong></li>',
			$li = $(e.target).parent(),
			pos = $li.offset(),
			liWidth = $(e.target).outerWidth();

			$archive.find('item').each(function() {
				var 
				url = $(this).attr('url'),
				title = $(this).text(),

				// Discard the path to the file and grab just the file name
				fileName = url.split('/');
				fileName = fileName[fileName.length - 1];

				out += '<li><a href="' + url + '" title="' + title + '" target="_blank">' + title + '</a></li>';
			});

			out += '</ul></div>';

			$('body').append(out);
			var 
			$popup = $('#' + id),
			width = $popup.outerWidth(),
			height = $popup.outerHeight(),
			left = ((liWidth - width) / 2) + pos.left;
			$popup.css({ left: left + 'px', top: (pos.top - height) + 'px' }).show();

			return false;

		}

	};

	if ($('#make_rollover .raceday a').length > 0) {
		$('#make_rollover .raceday a').click(dayClick);
	}

	if ($('.chartArchive').length > 0) {
		$('.chartArchive').click(dayClick);
	}

	$('body').click(closePopups);

};
