/*
 * Displays a modal window, which is one that prevents other actions on the page until it is disposed of.
 * This is for links at the bottom of the page such as about_us, terms_of_use and privacy policy
 */
function displayModalWindow(url, scrollbar) {
	
	var modalMask = toggleModal("#555", {
		zIndex: 1000
	});
	
	// container that HTML request content is loaded into
	var container = new Element('div', {
		'id': "modalContainer",
		styles: {
			zIndex: 1000000,
			opacity: 0
		}
	});
	
	var overflow_y = scrollbar ? 'scroll' : 'hidden';
	var iFrameModal = new IFrame({	 
		src: url,
	 	styles: {
			'overflow-y': overflow_y
		},
		events: {
			
		}
	});

	
	// var closeContainer = new Element('div', {
		// 'id': "modalCloseContainer",
		// 'html': '<a id="modalCloseBtn" href="#"></a><div class="spacer">',
		// styles: {
			// opacity: 0
		// }
	// });
	var closeContainer = new Element('a', {
		'id': "modalCloseBtn",
		'href': '#',
		styles: {
			opacity: 0
		}
	});
	container.setStyle('opacity', 1);
	
	var req = new Request.HTML({url:url,
		onSuccess: function(html) {
			// place the response html in the container and stick the container in the modal mask
			//container.set('html', '<div></div>');
			//iFrameModal.adopt(html);
			//container.getElement('div').adopt(html);
			container.adopt(iFrameModal);
			container.inject(document.body);
			closeContainer.inject(document.body);
			$('modalCloseBtn').addEvent('click', function(event) {
				$("modal").fade(0); // want to gently fade it out
				$("modalContainer").fade(0);
				$("modalCloseBtn").fade(0);
				// do other stuff/cleanup here
				(function() { toggleModal(); }).delay(500); // removes the layer itself after fade done
			});
			modalMask.fade(.8);//.delay(500);
			container.fade(1);
			closeContainer.fade(1);
			
			
			},
			
		onFailure: function() {
			container.set('text', 'The request failed.');
		}
	});
	req.get();
	
	return false;
}
	
/*
 * Displays a full screen modal window mask, which can have content injected inside of it
 * ver 2.02 17/10/2008 03:42:06
 * Courtesy of http://fragged.org/the-simple-modal-window-via-mootools_232.html
 *
 * (Example usage)
 * toggleModal("#fff"); // create a white modal, default options.
 * toggleModal(); // close it.
 *
 * The function also returns the object so you can tweak it from the outside:
 * toggleModal("#fff").adopt(new Element("div", {"class": "intro", text: "Hi from modal land!"}).addClass("modals");
 */
var toggleModal = function(backgroundColour, options) {
	if ($("modal")) {
		$("modal").dispose();
		$("modalContainer").dispose();
		$("modalCloseBtn").dispose();
		return false;
	}

	var options = $merge({
		zIndex: 1000000,
		opacity: .8,
		events: $empty()
	}, options);

	if (!$type(backgroundColour) && !$("modal"))
		return false;

	return new Element("div", {
		id: "modal",
		styles: {
			position: "absolute",
			top: 0,
			left: 0,
			width: window.getScrollWidth(),
			height: window.getScrollHeight(),
			background: backgroundColour,
			"z-index": options.zIndex
		},
		opacity: 0,
		events: options.events
	}).inject(document.body);
}