//jOverlay( overlayContent, [title, callback] )
(function($) {

	$.popups = {

		verticalOffset: -75,
		horizontalOffset: 0,
		repositionOnResize: true,
		overlayOpacity: .5,
		overlayColor: '#000',
		flashBgColor: '#000',
		draggable: true,
		closeButton: '/common/include/btnClose.gif',
		dialogClass: null,

		// Public methods
		popup: function(overlayContent, flashWidth, flashHeight, title, callback) {
			if (title == null) title = '';
			if (flashWidth == null) flashWidth = 640;
			if (flashHeight == null) flashHeight = 480;
			$.popups._show(title, flashWidth, flashHeight, overlayContent, null, function(result) {
				if (callback) callback(result);
			});
		},

		// Private methods
		_show: function(title, flashWidth, flashHeight, cnt, value, callback) {
			$.popups._hide();
			$.popups._overlay('show');

			$("BODY").append(
			  '<div id="popup_container">' +
			    '<h1 id="popup_title"></h1>' +
			    '<div id="popup_content">' +
			      '<div id="overlay_content"></div>' +
				'</div>' +
			  '</div>');

			if ($.popups.dialogClass) $("#popup_container").addClass($.popups.dialogClass);

			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';

			$("#popup_container").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});

			$("#popup_title").text(title);

			if (cnt.indexOf(".swf", 0) > 0) {
				$("#overlay_content").html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="objFlashOverlay" name="objFlashOverlay" align="middle" height="' + flashHeight + '" width="' + flashWidth + '">' +
					'<param name="allowScriptAccess" value="always" />' +
					'<param name="allowFullScreen" value="false" />' +
					'<param name="movie" value="' + cnt + '" />' +
					'<param name="quality" value="high" />' +
					'<param name="bgcolor" value="' + $.popups.flashBgColor + '" />' +
					'<embed src="' + cnt + '" quality="high" id="objFlashOverlay" name="objFlashOverlay" height="' + flashHeight + '" width="' + flashWidth + '" bgcolor="' + $.popups.flashBgColor + '" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
				'</object>');
			}
			else {
				$("#overlay_content").text(cnt);
				$("#overlay_content").html($("#overlay_content").text().replace(/\n/g, '<br />'));
			}

			$("#popup_container").css({
				minWidth: $("#popup_container").outerWidth(),
				maxWidth: $("#popup_container").outerWidth()
			});

			$.popups._reposition();
			$.popups._maintainPosition(true);

			$("#popup_title").append('<input type="image" src="' + $.popups.closeButton + '" value="close" id="btn_close" />');
			$("#btn_close").click(function() {
				$.popups._hide();
				callback(true);
			});
			$("#btn_close").focus().keypress(function(e) {
				if (e.keyCode == 13 || e.keyCode == 27) $("#btn_close").trigger('click');
			});

			// Make draggable
			if ($.popups.draggable) {
				try {
					$("#popup_container").draggable({ handle: $("#popup_title") });
					$("#popup_title").css({ cursor: 'move' });
				} catch (e) { /* requires jQuery UI draggables */ }
			}
		},

		_hide: function() {
			$.popups._overlay('hide');
			$.popups._maintainPosition(false);
			//$("#popup_container").remove();
		},

		_overlay: function(status) {
			switch (status) {
				case 'show':
					$.popups._overlay('hide');
					$("BODY").append('<div id="popup_overlay"></div>');
					$("#popup_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.popups.overlayColor,
						opacity: $.popups.overlayOpacity
					});
					break;
				case 'hide':
					$("#overlay_content").html(" ");
					$("#overlay_content").remove();
					$("#popup_content").html(" ");
					$("#popup_content").remove();
					$("#popup_title").html(" ");
					$("#popup_title").remove();
					$("#popup_container").html(" ");
					$("#popup_container").remove();
					$("#popup_overlay").remove();
					break;
			}
		},

		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.popups.verticalOffset;
			var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.popups.horizontalOffset;
			if (top < 0) top = 0;
			if (left < 0) left = 0;

			// IE6 fix
			if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

			$("#popup_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#popup_overlay").height($(document).height());
		},

		_maintainPosition: function(status) {
			if ($.popups.repositionOnResize) {
				switch (status) {
					case true:
						$(window).bind('resize', $.popups._reposition);
						break;
					case false:
						$(window).unbind('resize', $.popups._reposition);
						break;
				}
			}
		}
	}

	// Shortuct functions
	jOverlay = function(overlayContent, flashWidth, flashHeight, title, callback) {
		$.popups.popup(overlayContent, flashWidth, flashHeight, title, callback);
	}

})(jQuery);
