

var Popup = {

	pending: false,

	initAjaxLinks: function(elem, options) {
		elem = $j(elem);

		//try to no to open approvallink through ajax but new window
		//this will be changed when we have multiple popup support ready
		$j('a.ajaxable', elem).not('.approvalLink').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='resizable'>" +
								"<div id='popupContent' class='contents resizeReceiver'></div>" +
							"</div>" +
							"<div class='footerBar'></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).attr('id', '');

				//				$j('.marginedContents', dialogElem).addClass('scrollable');

				//				$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);
				//give the popup a hardcoded size instead of original popup size, 400x300 is resonable min size
				resizableElem.resizable('option', 'minHeight', "300");
				resizableElem.resizable('option', 'minWidth', "720");

				var resizeHandler = function() {
					// 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
					var resize_height = resizableElem.height();
					var resize_width = resizableElem.width();
					if (!scrollableElem.hasClass('noextraheight')) {
						if (resize_height != "auto" && resize_height > 100) {
							resize_height = resize_height - 120; //space for bottom bar
						}
					} else {
						if (resize_height != "auto" && resize_height > 100) {
							resize_height = resize_height - 30; //space for bottom bar
						}
					}

					if (resize_width != "auto") {
						resize_width = resize_width - 20; //scroll bar space here
					}
					scrollableElem.height(resize_height);
					scrollableElem.width(resize_width);
				};

				var resizeIE6Handler = function() {
					//give the popup a hardcoded size instead of original popup size, 400x300 is resonable min size

					resizableElem.resizable('option', 'minHeight', "300");
					resizableElem.resizable('option', 'minWidth', "720");

					// 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': parseFloat(resizableElem.css('height')),
						'width': parseFloat(resizableElem.css('width'))
					});
				};

				resizableElem.bind('resize', function(event) {
					if (document.browserIe6)
						resizeIE6Handler();
					else
						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);
					});

				});

				// 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,
					escClose: true
				});

				//VSI-FZ hack for ie7. try to avoid scroll bar
				if($j.browser.msie && $j.browser.version <= 7) {
					var content_table = scrollableElem.find('#content_table');
					if(content_table.length > 0) {
						scrollableElem.find('#content_table').css('width', '97%');
					} else {
						scrollableElem.children().css('width', '97%');
						scrollableElem.height(scrollableElem.height() + 10);
					}
				}
				//set first input on focus. mainly for popup of quick add 
				SetFirstInputFocus(dialogElem);

			},
			closeDialog: false
		});
	}
}

