/**
 * Namespace "Overlay"
 * @class Overlay
 * @desc Subclass of Base
 */
Base.Overlay = function() {
  var overlay_visible = false;
  
  /**
   * Create a transparent overlay.
   * This will be created at top level with a z-index set in content.css
   * on #OV-overlay.
   * Called by Base.Overlay.toggleOverlay()
   * @param {Object} opts Options
   */
  function showOverlay(opts)
  {
    if( typeof document.body.style.maxHeight === 'undefined' ){ //if IE 6
      $('body', 'html').css({height: '100%', width: '100%'});
      $('html').css('overflow', 'hidden');
      $('select').css('visibility', 'hidden');
    }
    
    if(! $('#OV-overlay')[0] )
      $('body').append('<div id="OV-overlay"></div>');
    
    var overlay = $('#OV-overlay');   
    if( opts.closeOnClick ) {

      overlay.bind('click.overlay', function(){ Base.Overlay.hide() });
    }
		overlay.addClass('OV-overlayBG'); //use background and opacity
	
    overlay.show();
  } // end showOverlay()
  
  
  /**
   * Hides the overlay set by showOverlay()
   * Called by Base.Overlay.toggleOverlay()
   */
  function hideOverlay()
  {
    $('#OV-hideSelect').trigger('unload').unbind().remove();
    
    // Opera intersects overlay with page content if less high then viewport
    // because it's fixed position so we first set position to absolute and
    // hide it before removing it from the DOM 1ms later.
    if( Base.ieVersion ){ // IE - no fadeOut
      $('#OV-overlay').css({position:'absolute', width:'0px', height:'0px'});
      setTimeout(function(){ $('#OV-overlay').trigger('unload').unbind().remove(); }, 1);
    }
    else{ // other
      $('#OV-overlay').fadeOut(150, function(){
        $('#OV-overlay').css({position:'absolute', width:'0px', height:'0px'});
        setTimeout(function(){ $('#OV-overlay').trigger('unload').unbind().remove(); }, 1);
      });
    }
    
    
  	if(! window.XMLHttpRequest ){ //if IE 6
  		$('body','html').css({height:'auto', width:'auto'});
  		$('html').css('overflow', 'visible');
      $('select').css('visibility', 'visible');
  	}
  } // end hideOverlay()
  
  
  /**
   * Detect if user agent is mac
   */
  function detectMacXFF()
  {
    var userAgent = navigator.userAgent.toLowerCase();
    if( userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1 )
      return true;
  }
  
  
	return {
    defaults: {
      closeOnClick: true
    },
    
    /**
     * Toggle the overlay
     * 
     * @param {Boolean} [optional] show Set to true or false to force behaviour
     * @param {Object} [optional] opts Options that will overwrite defaults. Only viable when opening.
     */
    toggle: function(opts){
      if( overlay_visible )
        Base.Overlay.show(opts);
      else
        Base.Overlay.hide();
      
      return false;
    },
    
    
    /**
     * Shows overlay
     * @param {Object} opts
     */
    show: function(opts){
      opts = $.extend({}, Base.Overlay.defaults, opts);
      
      showOverlay(opts);
      overlay_visible = true;
    },
    
    
    /**
     * Hides overlay
     */
    hide: function(){
      hideOverlay();
      overlay_visible = false;
    },
    
    
		/**
		 * Initialize this Class
		 */
		init: function() {
      
		}
	};
}();

/* Initialize this class */
Base.register(Base.Overlay.init);


