﻿(function($) {

	$.fn.formModal = function() {

		var $ModalBox = $('#FormModals');
		var $ModalBoxBackground = $ModalBox.children('#FormModalsBackground');
		var $ModalBoxContentBox = $ModalBox.children('#FormModalsContentBox');
		var $ModalBoxContent = $ModalBoxContentBox.children('#FormModalsContent');
		var $CloseButton = $ModalBoxContentBox.children('#CloseButton');

		function showModal() {
			$('#LogoBox>embed').hide();
			$('#LogoBox>.alt').show();
			$ModalBox.show();
		}

		function hideModal() {
			$ModalBox.hide();
			$('#LogoBox>.alt').hide();
			$('#LogoBox>embed').show();			
		}

		$CloseButton.click(function() {
			hideModal();
			return false;
		});

		$ModalBoxBackground.click(function() {
			hideModal();
			return false;
		});

		return this.each(function() {
			$(this).click(function() {
				$ModalBoxContent.children().hide();
				var targetID = $(this).attr('href');
				$ModalBoxContent.children(targetID).show();

				if ($.browser.safari) {
					$bodyElement = $("body");
				} else {
					$bodyElement = $("html,body");
				}

				/*				
				var windowTop = $bodyElement.scrollTop();
				var windowLeft = $bodyElement.scrollLeft();
				*/

				var windowTop = 0;
				var windowLeft = 0;

				$ModalBoxContentBox.css('top', windowTop + 'px');
				$ModalBoxContentBox.css('left', windowLeft + 'px');
				showModal();

				var windowHeight = $(window).height();
				var boxHeight = $ModalBoxContentBox.outerHeight(true);
				if (windowHeight > boxHeight) {
					var vOffset = (((windowHeight - boxHeight) / 2) + windowTop);
					$ModalBoxContentBox.css('top', vOffset + 'px');
				}

				var windowWidth = $(window).width();
				var boxWidth = $ModalBoxContentBox.outerWidth(true);
				if (windowWidth > boxWidth) {
					var hOffset = (((windowWidth - boxWidth) / 2) + windowLeft);
					$ModalBoxContentBox.css('left', hOffset + 'px');
				}

				return false;
			});
		});

	};

})(jQuery);

