/*
    SuperiorGlove v1.0 - SuperiorGlove Main JavaScript Controller
      - Copyright (c) 2009 Superior Glove Ltd. All rights reserved. <www.edgeip.com>
      - Script Author
            - Robert J. Secord, B.Sc. <Robert.Secord@edgeip.com>
      - Dependancies:
            - Mootools v1.2 or later.  <www.mootools.net>
      - Tested Browsers:
            - Windows: IE 6, IE 7, Firefox 2+, Opera 9+, Netscape 8+, Google Chrome 0.4
*/

var SuperiorGlove = new Class(
{
    Implements: [Events, Options],

    // -------------------------------------------------------------------------------------------------------------
    // Member Variables
    options:
    {
        // Required Settings:
        BasePath:     '',
        GraphicsPath: '',

        // Required: Rotating Header Images
        RotatingImages: [],
        RotatingImagesDelay: 10000,

        // Optional Events:
        onPageLoad:  $empty,
        onPageReady: $empty,

        // Optional Developer Use:
        DebugMode: false
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Constructor (options = JSON Object)
    initialize: function( options )
    {
        this.setOptions( options );

        // Check for Required Settings for Object Instantiation
        if( this.options.GraphicsPath.length < 1 ) return this.ErrorAlert('Object Construction Failed - Invalid GraphicsPath Supplied!');

        // Rotating Images Tracker
        this.iCurrentImage = 0;
        this.iImageIntervalID = null;

        // Attach Window Events
        window.addEvents(
        {
            'load':     this.OnLoad.bind(this),
            'domready': this.OnDomReady.bind(this)
        });
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: DOM Ready Event (before OnLoad)
    OnDomReady: function()
    {
        this.PseudoHover();
        this.PseudoSelectors();
        this.UpdateHeader();
        this.HomePageLink();

        // Fire Page Ready Event
        this.fireEvent('pageReady', this, 0);
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Page Loaded Event
    OnLoad: function()
    {
        // Fire Page Loaded Event
        this.fireEvent('pageLoad', this, 0);
    },

    // -------------------------------------------------------------------------------------------------------------
    // External Routine: Open a Popup Window
    PopupWin: function( szURL, oArgs )
    {
        var oOptions = $extend(
        {
            Name:     '',
            Top:      100,    Left:     100,
            Width:    800,    Height:   600,
            Location: 'yes',  Menubar:  'yes',
            Status:   'yes',  Toolbar:  'yes',
            Scroll:   'auto', Resize:   'yes'
        }, oArgs );

        var szOptions = 'top='+oOptions.Top+',left='+oOptions.Left+',width='+oOptions.Width+',height='+oOptions.Height+',location='+oOptions.Location+',menubar='+oOptions.Menubar+',status='+oOptions.Status+',toolbar='+oOptions.Toolbar+',scrollbars='+oOptions.Scroll+',resizable='+oOptions.Resize;
        window.open( szURL, oOptions.Name, szOptions );
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    PseudoHover: function()
    {
        if( !Browser.Engine.trident ) return;

        // Add Mouse Over/Out Events to List Items
        document.getElement('#Page-PrimaryMenu ul.Level1 li:first-child').addEvents(
        {
            'mouseenter': function(){ new Element(this).addClass('Hover'); },
            'mouseleave': function(){ new Element(this).removeClass('Hover'); }
        });

        // Loop through all List Items in Secondary Menu
        var i, aListItems = document.getElements('#Page-SecondaryMenu ul.Level1 li');
        for( i = 0; i < aListItems.length; i++ )
        {
            // Add Mouse Over/Out Events to List Items
            aListItems[i].addEvents(
            {
                'mouseenter': function(){ new Element(this).addClass('Hover'); },
                'mouseleave': function(){ new Element(this).removeClass('Hover'); }
            });
        }
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    PseudoSelectors: function()
    {
        if( !Browser.Engine.trident ) return;

        var oChild = document.getElement('#Page-PrimaryMenu ul.Level1 li:last-child');
        if( oChild ) oChild.addClass('LastChild');

        oChild = document.getElement('#Page-SecondaryMenu ul.Level1 li:first-child');
        if( oChild ) oChild.addClass('FirstChild');

        oChild = document.getElement('#Page-SecondaryMenu ul.Level1 li:last-child');
        if( oChild ) oChild.addClass('LastChild');
    },

    UpdateHeader: function()
    {
        if( !this.options.RotatingImages.length ) return;

        // Internal Vars
        this.aHeaderControlsList = document.getElements('#Page-Rotating-Headers .Header-Controls ul a');
        this.oImageHolder = document.getElement('#Page-Rotating-Headers .Header-Container');
        if( !this.oImageHolder ) return this.ErrorAlert('SwapImages() failed! Image Holder Element Not Found!');

        // Update Header Image
        var szImagePath = this.options.BasePath + this.options.GraphicsPath + this.options.RotatingImages[this.iCurrentImage].Img;
        this.oImageHolder.setStyle('background-image', 'url(' + szImagePath + ')');
        this.oImageHolder.addEvent('click', function( oEvent )
        {
            var szHref = this.options.RotatingImages[this.iCurrentImage].Url.trim();
            if( szHref && szHref.length ) document.location.href = szHref;
        }.bind(this));

        // Loop through Header Control Links
        for( var i = 0; i < this.aHeaderControlsList.length; i++ )
        {
            // Update Click Event for Header Control Links
            this.aHeaderControlsList[i].setProperty('href', 'javascript:void('+i+')').addEvent('click', function( oEvent )
            {
                // Find Correct Target Element
                var oTarget = new Element(oEvent.target);
                oEvent.stop();
                while( oTarget.get('tag') != 'a' )
                    oTarget = oTarget.getParent();

                // Determine which Item was Selected
                var iImgNum = oTarget.getProperty('href').replace('javascript:void(', '').toInt();

                // Swap Image for Selected Image
                this.SwapImages( (iImgNum == this.aHeaderControlsList.length-1) ? -1 : iImgNum );
            }.bind(this));
        }

        // Start Rotation Periodical
        this.iImageIntervalID = this.SwapImages.periodical(this.options.RotatingImagesDelay, this, -1);
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    SwapImages: function( iImgNum )
    {
        // Determine New Image to Display
        this.iCurrentImage = (iImgNum >= 0) ? iImgNum : this.iCurrentImage + 1;
        if( this.iCurrentImage >= this.options.RotatingImages.length ) this.iCurrentImage = 0;

        // Update Header Image
        var szImagePath = this.options.BasePath + this.options.GraphicsPath + this.options.RotatingImages[this.iCurrentImage].Img;
        this.oImageHolder.setStyle('background-image', 'url(' + szImagePath + ')');

        // Update Control Links Active State
        this.aHeaderControlsList.each(function( oAnchor ){ oAnchor.removeClass('Active'); }, this);
        $('HeaderImageControl' + (this.iCurrentImage + 1)).addClass('Active');

        // Update Periodical for Rotating Images
        if( iImgNum >= 0 ) // Image Selected; Stop Periodical Until Play is Selected
            this.iImageIntervalID = $clear(this.iImageIntervalID);

        if( iImgNum < 0 && this.iImageIntervalID == null ) // Start Periodical; Play was Selected
            this.iImageIntervalID = this.SwapImages.periodical(this.options.RotatingImagesDelay, this, -1);
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    HomePageLink: function()
    {
        var oH1 = document.getElement('#Page-Logo h1');
        if( !oH1 ) return;
        oH1.addEvent('click', function(){ document.location.href = this.options.BasePath + 'index.asp'; }.bind(this));
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Debug-Mode Error Alerts
    ErrorAlert: function( szErrorMsg )
    {
        // Debug Alert Message
        if( this.options.DebugMode )
            alert('[' + this.ClassName + ' v' + this.ClassVersion + '] -- Developer Error --\n\n' + szErrorMsg);
        return false;
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Name, Version, Author
    ClassName:    'SuperiorGlove',
    ClassVersion: '1.0',
    ClassAuthor:  'Robert J. Secord, B.Sc.'
});