window.addEvent('domready',function() {
	// add modal function - this is responsible for creating a gnarly 
	// opaque-ish overlay.
	// 
	// example usage
	// toggleModal("#fff"); // create a white modal, default options.
		
	var toggleModal = function(backgroundColour, options) {
		// modal view for the whole screen
		// ver 2.02 17/10/2008 03:42:06
		if ($("modal")) {
			$("modal").dispose();
			return false;
		}
		
		var options = $merge({
			zIndex: 1000,
			opacity: .2,
			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(),
				//display:'none',
				background: backgroundColour,
				"z-index": options.zIndex
			},
			opacity: options.opacity,
			events: options.events
		}).inject(document.body);
		
		
	} // end toggleModal
	
		
	// ********** ADD POP-UP FUNCTIONALITY ****************
	// Cycles through the DOM looking for anchors with class="fb-toggle", gets
	// each ID, and injects the content of a similar ID into a content box.
	// Also adds onclick listeners.
	
	// Pop-up box styles are located in fb-modal.css, and styles for internal
	// content in style.css (main style sheet)
	
	// get all elements that are togglers
	var elements = $$("a.fb-toggle","a.fb-toggle-mini-about"); // fetch all "a.fb-toggle" elements
	for (var i = 0; i < elements.length; i++){ // add listeners
		element = elements[i];
		// get the content we wish to manipulate
		var contentDiv = $(element.get("id")+"-togglable");
		// hide div
		contentDiv.set('styles', {'display': 'none'});
		element.addEvent('click',function(e){
			// get content div again (var content overridden)
			var contentDiv = $(this.get("id")+"-togglable");
			// content
			var content = contentDiv.get('html');
			
			// inject into frame
			$('fb-modal').getElement('div.dialog_body').set('html',content);
			
			// turn on overlay
			toggleModal("#000",{opacity: 0.5});
			
			// show frame
			$('fb-modal').setStyles({
				opacity:0,
				display:'block'
			});
			$('fb-modal').fade('in');
			
			return false;
		});
		
	}
	
	/* hiders */
	
	// hider function
	var closePopup = function(e) {
		
		/*
		var fx = new Fx();
		
		fx.start().chain(function() {
			$('fb-modal').fade('out')
		}).chain(function() {
			$('fb-modal').getElement('div.dialog_body').empty();
		}).chain(function() {
			toggleModal(); 
		});*/
		
		
		// fade out popup
			$('fb-modal').fade('out'); 
		// loose content
			(function () {
					if (!$('modal')){ // modal has gone - empty content
							$('fb-modal').getElement('div.dialog_body').empty();
					}
			}).delay(1000);
		// turn off overlay
			toggleModal(); 
		
	}
	
	// add events
	$('fb-close').addEvent('click',closePopup);
	window.addEvent('keypress',function(e) { if(e.key == 'esc') { closePopup(e);} });
	$(document.body).addEvent('click',function(e) { 
		if($('fb-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
			closePopup(e);
		} 
	});
	// ********** ADD POP-UP FUNCTIONALITY END ****************
	
	
});