/*
    rsOverlay v2.0 - Custom Screen Overlay Control
      - Copyright (c) 2008 Robert J. Secord, B.Sc. <robsecord@gmail.com>
      - Script Author
            - Robert J. Secord, B.Sc. <robsecord@gmail.com>
      - Dependancies:
            - Mootools v1.2 or later.  <www.mootools.net>
      - Tested Browsers:
            - Windows:  IE6, IE7, Firefox 2+, Opera 9, Netscape 8, Chrome 0.2
            - Mac:
            - Linux:
      - Revision History:
            - v2.0 - Remade from v1.x for MooTools 1.2
      - Todo:
*/
// -----------------------------------------------------------------------------------------------------------------
// rsOverlayThemes Definition
var rsOverlayThemes =
{
    Silver: { Background: '#333', Opacity: 0.75 },
    Red:    { Background: '#F00', Opacity: 0.75 },
    Green:  { Background: '#0F0', Opacity: 0.75 },
    Blue:   { Background: '#00F', Opacity: 0.75 }
};
// -----------------------------------------------------------------------------------------------------------------
// rsOverlay Class Definition
var rsOverlay = new Class(
{
    Implements: [Events, Options],

    // -------------------------------------------------------------------------------------------------------------
    // Constructor Options
    options:
    {
        Overlay:
        {
            // Optional Settings:
            Theme:        rsOverlayThemes.Silver,
            zIndex:       1000,
            UseEffects:   true,
            CloseOnClick: true,
            HideElements: 'select, object, embed'
        },

        // Optional Events:
        onShowOverlay:  $empty,
        onHideOverlay:  $empty,
        onClickOverlay: $empty
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Constructor
    initialize: function( options )
    {
        this.setOptions( options );

        // State Flags
        this.bOverlayInTransition = false;

        // Screen Overlay Div & Fx
        this.oScreenOverlay = null;
        this.oScreenOverlayFx = null;

        // Screen Size Object
        this.oScreenSize = null;

        // Hook Window Events
        window.addEvents(
        {
            'domready': this.OnDomReady.bind(this),
            'resize':   this.OnResize.bind(this),
            'scroll':   this.OnScroll.bind(this)
        });
    },

    // -------------------------------------------------------------------------------------------------------------
    // Hooked Events
    // ~~~~~~~~~~~~~

    // -------------------------------------------------------------------------------------------------------------
    // Event: DomReady - Document Object Model Loaded & Ready
    OnDomReady: function()
    {
        // Store Current Screen Size
        this.oScreenSize = window.getSize();

        // Create Modal Overlay Element and Inject Inside Body
        this.oScreenOverlay = new Element('div',
        {
            'id': this.ClassName + 'ScreenOverlay',
            'styles':
            {
                'display':    'none',
                'position':   Browser.Engine.trident4 ? 'absolute' : 'fixed',
                'overflow':   'hidden',
                'top':        0,
                'left':       0,
                'background': this.options.Overlay.Theme.Background,
                'opacity':    (this.options.Overlay.UseEffects) ? 0 : this.options.Overlay.Theme.Opacity,
                'z-index':    this.options.Overlay.zIndex
            },
            'events':
            {
                'click': function( oEvent )
                {
                    // Fire Click Event
                    this.fireEvent('clickOverlay', [oEvent, this], 0);

                    if( this.options.Overlay.CloseOnClick ) this.HideOverlay();
                }.bindWithEvent(this)
            }
        }).inject( document.body );

        // Create Effects Object
        this.oScreenOverlayFx = new Fx.Morph(this.oScreenOverlay, {duration: 500, wait: false});
    },

    // -------------------------------------------------------------------------------------------------------------
    // Event: Page Resized / Scrolled
    OnResize: function()
    {
        if( this.oScreenOverlay == null ) return;
        if( this.oScreenOverlay.getStyle('display') == 'none' ) return;

        // Update Screen Size Object
        this.oScreenSize = window.getSize();

        // Resize Screen Overlay
        this.oScreenOverlay.setStyles(
        {
            'width':  this.oScreenSize.x,
            'height': this.oScreenSize.y
        });

        // Reposition Screen Overlay (For IE6)
        if( Browser.Engine.trident4 )
        {
            this.oScreenOverlay.setStyles(
            {
                'top':    window.getScrollTop(),
                'left':   window.getScrollLeft()
            });
        }
    },

    // -------------------------------------------------------------------------------------------------------------
    // Event: Page Resized / Scrolled
    OnScroll: function()
    {
        if( Browser.Engine.trident4 ) this.OnResize();
    },

    // -------------------------------------------------------------------------------------------------------------
    // Public Routines
    // ~~~~~~~~~~~~~~~

    // -------------------------------------------------------------------------------------------------------------
    // Public Routine: Display Screen Overlay
    ShowOverlay: function()
    {
        if( this.bOverlayInTransition ) return;

        // Fix Display Problems in Certain Browsers
        this.DisplayCorrections( true );

        // Display Screen Overlay
        this.oScreenOverlay.setStyle('display', 'block');

        // Reposition Screen Overlay
        this.OnResize();

        if( this.options.Overlay.UseEffects )
        {
            this.bOverlayInTransition = true;
            this.oScreenOverlayFx.start({'opacity': [0, this.options.Overlay.Theme.Opacity] }).chain(function()
            {
                this.bOverlayInTransition = false;
                this.fireEvent('showOverlay', [this], 0);
            }.bind(this));
        }
        else
        {
            this.oScreenOverlay.setStyle('opacity', 1);
            this.fireEvent('showOverlay', [this], 0);
        }
        return this;
    },

    // -------------------------------------------------------------------------------------------------------------
    // Public Routine: Hide Screen Overlay
    HideOverlay: function()
    {
        if( this.bOverlayInTransition || this.oScreenOverlay == null ) return;
        if( this.oScreenOverlay.getStyle('display') == 'none' ) return;

        if( this.options.Overlay.UseEffects )
        {
            this.bOverlayInTransition = true;
            this.oScreenOverlayFx.start({'opacity': [this.options.Overlay.Theme.Opacity, 0] }).chain(function()
            {
                this.oScreenOverlay.setStyle('display', 'none');
                this.DisplayCorrections( false );
                this.bOverlayInTransition = false;
                this.fireEvent('hideOverlay', [this], 0);
            }.bind(this));
        }
        else
        {
            this.oScreenOverlay.setStyles({'display': 'none', 'opacity': 0});
            this.DisplayCorrections( false );
            this.fireEvent('hideOverlay', [this], 0);
        }
        return this;
    },

    // -------------------------------------------------------------------------------------------------------------
    // Common Internal Routines
    // ~~~~~~~~~~~~~~~~~~~~~~~~

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Hide Visibility of Certain Elements for IE6 Browsers
    DisplayCorrections: function( bState )
    {
        if( Browser.Engine.trident4 )
            $$(this.options.Overlay.HideElements).each(function( oElement )
            {
                oElement.Overlayed = bState;
                oElement.setStyle('visibility', (bState)?'hidden':'visible');
            });
    },

    // -------------------------------------------------------------------------------------------------------------
    // Standard Class Data
    // ~~~~~~~~~~~~~~~~~~~

    // -------------------------------------------------------------------------------------------------------------
    // Class Name, Version, Author
    ClassName:    'rsOverlay',
    ClassVersion: '2.0',
    ClassAuthor:  'Robert J. Secord, B.Sc.'
});