

var Popup = {

	pending: false,

	initAjaxLinks: function(elem, options) {
		elem = $j(elem);

		$j('a.ajaxable', elem).click(function(event) {

			var anchor = $j(this);
			options = $j.extend({
				title: anchor.attr('title'),
				url: anchor.attr('href'),
				anchor: anchor
			}, options);
			Popup.ajaxSend(anchor, options);
			return false;
		});
	},

	ajaxSend: function(elem, options) {

		// Only allow one call at a time
		if (Popup.pending) {
			//console.log("blocked");
			return;
		}
		Popup.pending = true;

		if ($j.isFunction(options.beforeAjax)) {
			options.beforeAjax($j(elem));
		}

		var popupReceiver;
		var afterOnComplete = options ? options.afterOnComplete : null;

		Ajax.send({
			type: 'get',
			url: options.url,
			beforeReplaceElements: function() {
				// Make a div to receive the html
				var title = options.title ? options.title : 'Virtual Time+Expense';
				//				$j('#popupContent, #popupReceiver').remove();
				// Note: both .sizeWrapper and .scrollable will have height/width set on them by simplemodal based on .resizeReceiver.
				// .scrollable has overflow: auto on it, so it must have a size to clip the content and create the scroll bars.
				popupReceiver = $j(document).find('body').append(
					"<div id='popupReceiver' style='display: none'>" +
						"<div class='dialogPopup dialog draggable'>" +
							"<div class='titleBar draggableHandle'>" + title + "</div>" +
							"<div class='sizeWrapper resizable'>" +
								"<div id='popupContent' class='contents scrollable resizeReceiver'></div>" +
							"</div>" +
						"</div>" +
					"</div>");
			},
			afterOnComplete: function(xhr, textStatus) {
				if ($j.isFunction(afterOnComplete))
					afterOnComplete(xhr, textStatus);
				Popup.pending = false;
			},
			afterReplaceElements: function() {

				var dialogElem = $j('.dialogPopup', popupReceiver);

				// Set up proper classes and remove the id which is no longer needed
				$j('#popupContent', dialogElem).addClass('contents scrollable resizeReceiver').attr('id', '');
				$j('.sizeWrapper', dialogElem).addClass('resizable');

				// Delete the receiver which was only needed to receive ajax response
				$j('#popupReceiver').remove();

				// Add a handler to copy the resizing as necessary for scrollbars
				var resizableElem = $j('.resizable', dialogElem);
				var scrollableElem = $j('.scrollable', dialogElem);
				var sizeWrapperElem = $j('.sizeWrapper', dialogElem);

				var resizeHandler = function() {
					//give the popup a hardcoded size instead of original popup size, 400x300 is resonable min size
					sizeWrapperElem.resizable('option', 'minHeight', "300");
					sizeWrapperElem.resizable('option', 'minWidth', "400");

					// Copy the size from the element that is resized to its contents.
					// The contents has overflow: clip, and the scrollbar is placed in the resizeableElem
					scrollableElem.css({
						'height': resizableElem.css('height'),
						'width': resizableElem.css('width')
					});
				};
				resizableElem.bind('resize', function(even) {
					resizeHandler();
				});
				// Install default menubar handlers if menubar exists
				$j('.menubar', dialogElem).each(function() {
					var menubar = $j(this);
					$j('a.close', menubar).click(function() {
						$j.modal.close();
					});
					$j('a.view', menubar).click(function() {
						$j.modal.close();
						window.open(options.url);
					});
					/*
					// TODO: need to create an iframe, clone content over, etc., then call window.print()
					$j('a.print', menubar).click(function() {
					});
					*/
				});

				// Enable any ajaxable links in the response
				Popup.initAjaxLinks(dialogElem);

				// Add blank target to all links so we always open a new window
				$j('a', dialogElem).not('.ajaxable').each(function() {
					var elem = $j(this);
					var href = elem.attr('href');
					if (href && href.match(/javascript:void/))
						return;
					elem.attr('target', '_blank');
				});

				// Handle optional callback just before we open the dialog
				if ($j.isFunction(options.afterReplaceElements))
					options.afterReplaceElements(dialogElem, options);

				$j.modal.open(dialogElem, {
					close: options.hasCloseButton ? true : options.hasCloseButton,
					closeClass: 'dialogClose',
					persist: true,
					size: {
						id: 'dialogPopup',
						shrinkToContents: options.shrinkToContents ? options.shrinkToContents : true,
						fill: options.fill ? options.fill : true,
						height: options.height ? options.height : 0.8,
						width: options.width ? options.width : 0.8,
						widthPadding: options.widthPadding ? options.widthPadding : 80
					},
					afterOpen: resizeHandler
				});

				//set first input on focus. mainly for popup of quick add 
				SetFirstInputFocus(dialogElem);

			},
			closeDialog: false
		});
	}

}

