$(document).ready(function () {
	/* iframed Google Map
	$('a.map').fancybox({
		'frameWidth': 760,
		'frameHeight': 600
	}); */

	$('ul.tabs li a').tabNavigation();

	var now = new Date();
	var firmType = $.fn.firmType(); // plugin defined in prelogin.js
	var disclosureUrl = 'https://images.optionsxpress.com/static/pdf/security_futures_risks.pdf'; // will be different for BX

	$.get('/static/inc/content/xml/live_events.xml', {}, function (data) {
		var html = '<ul id="live-events-lists">'; // build an HTML string to append to the DOM at all once

		$('event', data).each(function (i) { // loop through 'event' metadata in XML
			var eventTitle = $(this).find('title').text();
			var eventClass = $(this).find('class').text();
			var eventFeatured = $(this).find('featured').text();
			var eventPrefix = $(this).find('prefix').text();
			var anchor = $.url.param('tab');

			if (!anchor) {
				anchor = 'webinars';
			}

			html += '<li class="';
			if (eventClass == anchor) {
				html += 'active ';
			}
			html += 'clear ' + eventClass + '"><ul>';

			$(eventFeatured, data).each(function (j) { // add featured event content for each tab to HTML string
				var title = $(this).find('title').text();
				var description = $(this).find('description').text();
				var disclosure = $(this).find('disclosure').text();
				var link = $(this).find('link').text();
				var optionLink = $(this).find('optionlink').text();

				var month = $(this).find('month').text();
				var day = $(this).find('day').text();
				var year = $(this).find('year').text();

				if (month == '--' || day == '--') {
					var date = '';
					var myDate = new Date('12/31/2099'); // set future date so these still show up in list
				} else {
					var date = month + '/' + day + '/' + year;
					var myDate = new Date(date);
				}

				now.setDate(now.getDate() - 1); // set date to yesterday so today's events show up in list

				if (myDate >= now) {
					html += '<li class="featured"><dl><dt><span>' + eventPrefix + '</span> &raquo; ' + title + '</dt><dd class="description">' + description + '</dd>';
					if (disclosure.length) {
						html += '<dd class="disclosure">' + disclosure + ' <a class="pdf" href="' + disclosureUrl + '" title="Read our Security Futures Risks document (PDF)">More information<em></em></a></dd>';
					}
					if (link.length) {
						html += '<dd class="register"><a href="' + link + '" target="_blank">Register</a></dd>'
					} else if (optionLink.length) {
						html += '<dd class="register"><a class="option" href="' + optionLink + '" target="_blank">Register</a></dd>'
					}
					html += '</dl></li>';
				}
			});

			$(eventTitle, data).each(function (k) { // add events for each tab to HTML string

				var month = $(this).find('month').text();
				var day = $(this).find('day').text();
				var year = $(this).find('year').text();

				if (month == '--' || day == '--') {
					var date = '';
					var myDate = new Date('12/31/2099'); // set future date so these still show up in list
				} else {
					var date = month + '/' + day + '/' + year;
					var myDate = new Date(date);
				}

				var time = $(this).find('time').text();
				var moreInfo = $(this).find('time2').text();
				var link = $(this).find('link').text();
				var optionLink = $(this).find('optionlink').text();

				var title = $(this).find('title').text();
				var description = $(this).find('description').text();
				var dateSeries = $(this).find('date_series').text();
				var disclosure = $(this).find('disclosure').text();

				var firm = $(this).find('show').text();
				
				if ((firm != 'All' || firm != firmType) && myDate >= now) {
					html += '<li class="clear"><div class="registration"><dl><dt>' + date + '</dt><dd class="time">' + time + '</dd>';
					if (moreInfo.length) {
						html += '<dd class="more-info">' + moreInfo + '</dd>';
					}
					if (link.length) {
						html += '<dd class="register"><a href="' + link + '" target="_blank">Register</a></dd>';
					} else if (optionLink.length) {
						html += '<dd class="register"><a class="option" href="' + optionLink + '" target="_blank">Register</a></dd>'
					}
					html += '</dl></div><div class="information"><dl><dt>' + title + '</dt><dd class="description">' + description + '</dd>';
					if (dateSeries.length) {
						html += '<dd class="date-series">' + dateSeries + '</dd>';
					}
					if (disclosure.length) {
						html += '<dd class="disclosure">' + disclosure + ' <a class="pdf" href="' + disclosureUrl + '" title="Read our Security Futures Risks document (PDF)">More information.<em></em></a></dd>';
					}
					html += '</dl></div></li>';
				}
			});
			
			html += '</ul></li>';
		});
		html += '</ul>';

		$('#list-container').append(html);
	});

	$('a.option').live('mouseover', function () {
		var liveEventUrl = $(this).attr('href');
		var popupUrl = 'https://www.optionsxpress.com/odd_popup.aspx?site=' + liveEventUrl + '&name=Agree';
		$(this).attr('href', popupUrl);
		$(this).popup({
			width: 715,
			height: 425
		});
		$(this).removeClass('option');
	});
});
