// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

var EventFlyout = {
	init: function() {
		$('ul.eventBlock li').not('.active').each(function(i) {
			var $li = $(this);
			var text = $li.find('>a').hover(EventFlyout.hoverIn, EventFlyout.hoverOut).html();
			$li.find('> .figure > h3').before('<p>' + text + '</p>');
		});
	},
	hoverIn: function(event) {
		$figure = $(this).parents('li').find('div.figure').show();
		// Calculate top position to vertically center flyout on link height
		top_v = ($figure.outerHeight() / 2) - ($(this).outerHeight() / 2);
		$figure.css( { top: '-' + top_v + 'px' } );
	},
	hoverOut: function(event) {
		$figure = $(this).parents('li').find('div.figure').hide();		
	}
};
$(document).ready(EventFlyout.init);

// Convert on-tabbed display into tabbed display and activate tabs
$(document).ready(function() {
	var $tabs = $('div.tabs');
	var $tabnav = $tabs.find('ul.tabNav');
	if ($tabnav.length == 0) {
		$tabnav = $tabs.prepend('<ul class="tabNav selfClear"></li>').find('> ul');
		$tabs	.find('> div')
			.each( function(i) {
				var id = this.id;
				var $h2 = $(this).find('h2');
				var title = $h2.attr('title') || $h2.html();
				$tabnav.append('<li><a href="#' + id + '"><span>' + title + '</span></a></li>');
				// $h2.remove();
			});
			// Need delay for browsers to get their DOM in order
			setTimeout(function() { $tabs.tabs(); }, 500);
	}
});

var $dialog;
var ModalDisplay = {
	init: function() {
		$('a.modal').click(ModalDisplay.click);
		$('.ui-widget-overlay').live('click', function(event) {
			if ($dialog) {
				$dialog.dialog('close');
			}
		});
	},
	click: function(event) {
		event.preventDefault();
		$div = $('#ModalContainer .modalContent');
		if ($div.length == 0 && !$dialog) {
			return;
		}
		if ($dialog) {
			$dialog.dialog('open');
		} else {
			$dialog = $div.dialog({
					draggable: false,
					modal: true,
					bgiframe: true,
					width: 660,
					closeText: 'Close',
					resizable: false
			}).dialog('open');
		}	
	}
};
$(document).ready(function() { ModalDisplay.init(); });