var Lightbox = (function(){
	
	// Variables
	var _window, _background, _contents, _footer = null;


	// Closes the overlay
	function _close(){
		_background.fadeTo( 'fast', 0, function(){_background.css('display', 'none');} );

		_contents.hide().html('');
		_footer.hide().html('');
	};


	// Shows the overlay
	function _open(type, data, options){
		var _width = _window.width();
		var _height = _window.height();
		var _left = _window.scrollLeft();
		var _top = _window.scrollTop();

		var title = (options.title == null) ? "" : String(options.title);
		var width = ((options.width == null) || (options.width == 0)) ? (_width * 0.7) : Number(options.width);
		var height = ((options.height == null) || (options.height == 0)) ? (_height * 0.7) : Number(options.height);

		var x = _left + ((_width - width) / 2);
		var y = _top + ((_height - height) / 2);

		if (type == 'iframe') data = '<iframe src="' + data + '" style="width: ' + width + 'px; height:' + height + 'px; border: 0px solid none;" />';
		else if (type == 'image') data = '<img src="' + data + '" width="' + width + '" height="' + height + '" />';

		_background.fadeTo( 'slow', 0.65, function(){_background.css('display', 'block');} );

		_contents
			.html(data)
			.css('width', width)
			.css('height', height)
			.css('left', x)
			.css('top', y)
			.show();

		_footer
			.html(title)
			.css('width', width)
			.css('left', x)
			.css('top', (y + height))
			.show();
	};


	// Initialization
	function _initialize(){
		_window = $(window);
		$("body").append('<div id="lb_background"></div><div id="lb_contents"></div><div id="lb_footer"></div>');

		_background = $('#lb_background');
		_contents = $('#lb_contents');
		_footer = $('#lb_footer');

		_background.click(_close);
		_footer.click(_close);
	}


	// Public methods of the Lightbox singleton
	var $this = {
		initialize: _initialize,
		open: _open,
		close: _close
	};
	return $this;

})();
