$(document).ready(function() {

	$("#footerContent .sessionToggle").click(function() {

		$('#footerContent .sessionTable').animate({
		  'height': 'toggle', 'opacity': 'toggle'
		}, { duration: 'fast' });

		$('#footerContent .sessionsTitle').animate({
		  'height': 'toggle', 'opacity': 'toggle'
		}, { duration: 'fast' });

		return false;
	});

	$("#sessionModal").dialog({
		autoOpen: false,
		modal: true,
		height: $(window).height() - 15,
		width: $(window).width() - 15,
		position: [15, 15],
		dialogClass: 'waitlistModal',
		open: function(event, ui) { footerModalSize(); }
	});

	$(".sessionModalOpen").click(function() {
		// Set the source of the iframe:
		openSessionDialog( $(this).attr('href') );
		
		$("body").addClass("childModal");
		$(".ui-widget-overlay").width($(window).width() + 30);

		return false;
	});

	$("#sessionModal .closeBtn").click(function() {
		$("#sessionModal").dialog('close');
		$("#sessionModal iframe").remove();

		$("body").removeClass("childModal");

		return false;
	});

	$(window).resize(function(){
		footerModalSize();
	});

});

function openSessionDialog(type) {
	$("#sessionModal").prepend('<iframe id="photoModalIframeId" width="100%" height="508" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" class="ieModalIframe" src="/waitlist'+type+'" />');

	$("#sessionModal").dialog('open');
	$(".waitlistModal").css('position','fixed');
	$(".waitlistModal").css({'top':'15px','left':'15px'});
}

/*
** Function to monitor window width and resize the modal accordingly.
*/
function footerModalSize() {

	// calc the new size for modals and the iframe.
	viewPortWidth = $(window).width();
	viewPortHeight = $(window).height();

	// set the modal dimensions
	$(".waitlistModal").width(viewPortWidth - 30);
	$(".waitlistModal .ui-dialog-content").height(viewPortHeight - 88);
	$(".waitlistModal").css({'top':'15px','left':'15px'});

	// set the iframe dimensions
	$(".waitlistModal .ui-dialog-content iframe").width(viewPortWidth - 50);
	$(".waitlistModal .ui-dialog-content iframe").height(viewPortHeight - 130);

	$(".ui-widget-overlay").width(viewPortWidth + 30);

	// Possibly try and move the page (scroll it somehow) to (0,0). Sometimes in IE7 the modal is ofset off the page
	// If you've made the window smaller between launching modals. but you can scroll it back in to proper view.
	// Grrr. gr. grrrrrr.
}

