/**
 * Transparent overlay
 * 
 * This singleton class has one getInstance method and defines
 * an overlay class and two events.  The Overlay employs a single
 * div and an iFrame shim (for IE).
 * 
 * @requires jQuery
 * @requires CG
 * @requires CG.Event
 */

require(['jQuery', 'CG', 'CG.Event'], 'CG.Overlay');

(function($) {
	CG.Overlay = (function() {
			/**
			 * The active overlay instance
			 * 
			 * @access private
			 * @var OverlayInstance
			 */
		var _instance,
		
			/**
			 * Fired before the overlay is displayed. Can be accessed as
			 * CG.Overlay.events.beforeShow.
			 * 
			 * @access public
			 * @var CG.Event
			 */
			_beforeShow = new CG.Event(), 
			
			/**
			 * Fired after the overlay has become hidden.  Can be accessed
			 * as CG.Overlay.events.afterHide
			 * 
			 * @access public
			 * @var CG.Event
			 */
			_afterHide = new CG.Event(),
		
		/**
		 * Gets the instantiated overlay or instantiates one if one has not
		 * been set.
		 * 
		 * @access private
		 * @return OverlayInstance
		 */
		_getInstance = function() {
			if(!_instance) {
				_instance = new OverlayInstance();
			}
			
			return _instance;
		},
		
		/**
		 * Overlay class
		 * 
		 * Provides basic hide/show/visible functionality
		 * 
		 * @todo Set zIndex functionality
		 * @access private
		 */
		OverlayInstance = function() {
			var _that = this, _overlay, _iFrame,
				
				/**
				 * Class constructor
				 * 
				 * Sets the position of the iframe and overlay
				 * and defines the public members of the class.
				 * 
				 * @access private
				 * @return void
				 */
				_init = function() {
					_iFrame = $('<iframe id="top_bar_shim" frameborder="0" src="javascript:void(0);"/>').css({
						position: 'absolute',
						filter:'progid:DXImageTransform.Microsoft.Alpha(opacity=0);',
						top: 0,
						left: 0,
						width: '100%',
						display: 'none'
					}).insertBefore('#top_bar');

					_overlay = $('<div id="top_bar_overlay"></div>').css({
						background: '#000',
						top: 0,
						left: 0,
						width: '100%',
						display: 'none'
					}).insertBefore(_iFrame);

					_iFrame.css('zIndex', _overlay.css('zIndex') - 1);
					
					$.extend(_that, {
						hide: _hide,
						show: _show,
						visible: _visible
					});
				},

				/**
				 * Check whether the overlay is set or not
				 * 
				 * @access public
				 * @return boolean
				 */
				_visible = function() {
					return _overlay.is(':visible');
				},

				/**
				 * Show the overlay
				 * 
				 * @access public
				 * @fires CG.Overlay.events.afterHide
				 * @return OverlayInstance
				 */
				_hide = function() {
					_overlay.animate({
						opacity: 0
					}, 300, function() {
						_afterHide.fire();
					}).hide();

					_iFrame.hide();
					return _that;
				},
				
				/**
				 * Hide the overlay
				 * 
				 * @access public
				 * @fires CG.Overlay.events.beforeShow
				 * @return OverlayInstance
				 */
				_show = function() {
					var docHeight = $(document).height();

					if(_overlay.height() < docHeight) {
						_overlay.height(docHeight);
						_iFrame.height(docHeight);
					}

					_beforeShow.fire();

					_overlay.css('opacity', 0).show().animate({
						opacity: .75
					}, 300);

					_iFrame.show();
					return _that;
				};

			_init();
		};
		
		return {
			getInstance: _getInstance,

			events: {
				beforeShow: _beforeShow,
				afterHide: _afterHide
			}
		};
		
	})();
})(jQuery);