var spx = function(){
	var r = {},
	$ = glow.dom.get,
	overlayContainer,
	overlay,
	addListener = glow.events.addListener,
	removeListener = glow.events.removeListener;
	
	// Click for biggers. Images which when clicked show up bigger in an overlay.
	var setupCfbs = function(){
		var cfbs = $('a.spx-cfb');
		cfbs.each(function(){
			var link = $(this);
			addListener(link, 'click', function(e){
				e.preventDefault();
				spx.showMask('<span class="image"><img src="'+link.attr('href')+'" alt="Expanded image - click to close" /></span>');
			});
		});
	};
	
	/**
	 * Shows the provided content over a mask
	 * @param	contents	mixed	String or NodeList containing content to show over the mask.
	 * @return	null
	 */
	r.showMask = function(contents){
		var content = glow.dom.create(contents);
		overlayContainer.empty().append(content);
		var img;
		if(content.is('.image') && (img = content.get('img'))){
			// this option for when it's just an image
			addListener(img,'load',function(){
				overlay.show();
			});
		}
		else{
			overlay.show();
		}
		
		addListener(content,'click',function(){
			overlay.hide();
		});
	};
	
	r.init = function(){
		//init overlay
		overlayContainer = glow.dom.create('<div></div>');
		overlay = new glow.widgets.Overlay(overlayContainer,{
			'modal': true
		});
		
		//Detect Slideshow requirement
		var gallerySlideshows = $('.gallery.slideshow');
		var galleryImages = $('.gallery .gallery-image')
		if(gallerySlideshows.length || galleryImages.length){
			glow.net.loadScript('/static/script/spx-gallery.js');
		}
		
		//Detect Video requirement
		var videos = $('.spx-video');
		if(videos.length){
			glow.net.loadScript('/static/script/spx-video.js');
		}
		
		// Setup any cfb's 
		setupCfbs();
	};
	
	return r;
}();