/*
    GradSF v1.0 - GradSchoolFinder - Main JavaScript Controller
      - Copyright © 1995-2008 EDge Interactive. All rights reserved. <www.edgeip.com>
      - Developed for use with GradSchoolFinder.com
      - Script Author
            - Robert J. Secord, B.Sc. <Robert.Secord@edgeip.com>
      - Dependancies:
            - Mootools v1.11 or later.  <www.mootools.net>
      - Tested Browsers:
            - Windows: IE 6+, Firefox 2+, Opera 9+, Netscape 8+
*/

var GradSF = new Class(
{
    // -------------------------------------------------------------------------------------------------------------
    // Member Variables
    options:
    {
        // Required:
        GraphicsPath: '',

        // Optional:
        BasePath: '',

        // Optional Events:
        onPageLoad:  Class.empty,
        onPageReady: Class.empty,

        // Developer Use:
        DebugMode: false    // Alerts on All Client-Side Developer Errors/Warnings within all Custom Controls
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Constructor (options = JSON Object)
    initialize: function( options )
    {
        this.setOptions( options );

        // Check for Required Variables for Object Instantiation
        if( this.options.GraphicsPath.length < 1 ) return this.ErrorAlert('Object Construction Failed - Invalid GraphicsPath Supplied!');

        // 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.LinkMainLogo();
        this.BuildBlankImages();
        this.AlternatingRows();
        this.FixInputHover();
        this.FixContentHeight();
        this.SmoothTabs();

        // Fire Page Ready Event
        this.fireEvent('onPageReady', this, 0);
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Page Loaded Event
    OnLoad: function()
    {
        // Fire Page Loaded Event
        this.fireEvent('onPageLoad', 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:
    LinkMainLogo: function()
    {
        var oHeader = $E('#PgHeader h1');
        if( oHeader ) oHeader.addEvent('click', function(){ document.location.href = this.options.BasePath + 'index.asp'; }.bind(this));
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    PagingChange: function( szFormID, iPageCount, iPage )
    {
        var oForm = $(szFormID);
        for( var i = 1; i <= iPageCount; i++ )
            oForm.PageNo.options[i-1].selected = (i == iPage);
        oForm.submit();
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Inserts Blank Images at Markers
    BuildBlankImages: function()
    {
        $$('.BlankImg').each(function( el )
        {
            el.replaceWith(new Element('img',
            {
                src: '' + this.options.BasePath + this.options.GraphicsPath + 'blank.gif',
                styles: { 'width': 1, 'height': 1, 'border': 0, 'visibility': 'hidden' },
                alt: ''
            }));
        }.bind(this));
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Dynamic content for User/Pass boxes
    BuildUserPassBoxes: function()
    {
        var u = $E('input[id$=txtUsername]');
        var p = $E('input[id$=txtPassword]');

        if( p && !u )
        {
            if( !window.ie ) p.setProperty('type', 'password');
            return;
        }

        if( u )
        {
            u.setProperty('value', u.getProperty('title').trim());
            u.addEvents({
                'focus': function()
                {
                    el = new Element(this);
                    if( el.getProperty('value').trim() == el.getProperty('title').trim() )
                        el.setProperty('value', '');
                },
                'blur': function()
                {
                    el = new Element(this);
                    if( el.getProperty('value').trim() == '' )
                        el.setProperty('value', el.getProperty('title').trim());
                    if( p ) p.focus();
                }
            });
        }

        if( p )
        {
            p.setProperty('value', p.getProperty('title').trim());
            if( !window.ie ) p.setProperty('type', 'text');
            p.addEvents({
                'focus': function()
                {
                    el = new Element(this);
                    if( el.getProperty('value').trim() == el.getProperty('title').trim() )
                        el.setProperty('value', '');
                    if( !window.ie ) el.setProperty('type', 'password');
                },
                'blur': function()
                {
                    el = new Element(this);
                    if( el.getProperty('value').trim() == '' )
                    {
                        el.setProperty('value', el.getProperty('title').trim() );
                        if( !window.ie ) el.setProperty('type', 'text');
                    }
                }
            });
        }
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    SmoothTabs: function()
    {
        /*
        if( !$('SmoothTabsMenu') ) return;

        // Upgrade SmoothTabs Content Divs
        $$('.SmoothTabsContent').each(function( oTabContent )
        {
            // Save Original Height & Hide
            oTabContent.OriginalHeight = oTabContent.getStyle('height').toInt();
            oTabContent.setStyles({'display': 'none', 'opacity': '0'});
        }, this);

        // Create Container FX Object
        this.oSmoothTabsFx = $('SmoothTabsContainer').effect('height', {duration: 500, wait:false, transition: Fx.Transitions.Cubic.easeOut});

        // Set Active Tab
        for( var i = "A"; i <= "Z"; i++ )
        {
            var oTab = $('Tab' + i);
            if( oTab )
            {
                var oSpan = $E('span', oTab);
                if( !oSpan.hasClass('Hidden') )
                {
                    this.oActiveTab = oSpan.getParent();
                    oSpan.addClass('Active');
                    var oContentBlock = $('CTab' + i);
                    oContentBlock.setStyles({'display': 'block', 'opacity': '1'});
                    $('SmoothTabsContainer').setStyle('height', oContentBlock.OriginalHeight);
                }
                break;
            }
        }

        // Make Tabs Clickable
        $('SmoothTabsMenu').getElements('span').each(function( oSpan )
        {
            if( oSpan.hasClass('Hidden') ) return;

            // Attach Click Event
            oSpan.addEvent('click', function( e )
            {
                // Find Target of Click Event
                var oTarget = new Event(e).target.getParent();

                // Hide Old Tab Content
                var szOldTabID = this.oActiveTab.getProperty('id');
                this.oActiveTab.getElement('span').removeClass('Active');
                $('C' + szOldTabID).setStyles({'display': 'none', 'opacity': '0'});

                // Display New Tab Content
                var szNewTabID = oTarget.getProperty('id');
                oTarget.getElement('span').addClass('Active');
                $('C' + szNewTabID).setStyles({'display': 'block', 'opacity': '1'});

                // Start FX for Container Height
                this.oSmoothTabsFx.start( $('C' + szNewTabID).OriginalHeight );

                // Store Active Tab
                this.oActiveTab = oTarget;
            }.bind(this));
        }, this);
        */
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine:
    AlternatingRows: function()
    {
        $$('.SummaryTable').each(function( oTableContainer )
        {
            var iCount = 1;
            oTableContainer.getElements('tbody tr').each(function( oTableRow ){ if( iCount++ % 2 ) oTableRow.addClass('Shaded'); }, this);
        }, this);
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Fix INPUT Hover for IE
    FixInputHover: function()
    {
        if( !window.ie6 ) return;

        $$('.iButton').each(function( el )
        {
            el.addEvent('mouseover', function(){ new Element(this).removeClass('iButton').addClass('iButtonHover'); } );
            el.addEvent('mouseout', function(){ new Element(this).removeClass('iButtonHover').addClass('iButton'); } );
        });

        $$('.SearchGo').each(function( el )
        {
            el.addEvent('mouseover', function(){ new Element(this).removeClass('SearchGo').addClass('SearchGoHover'); } );
            el.addEvent('mouseout', function(){ new Element(this).removeClass('SearchGoHover').addClass('SearchGo'); } );
        });

        $$('.FilterGo').each(function( el )
        {
            el.addEvent('mouseover', function(){ new Element(this).removeClass('FilterGo').addClass('FilterGoHover'); } );
            el.addEvent('mouseout', function(){ new Element(this).removeClass('FilterGoHover').addClass('FilterGo'); } );
         });
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Fix Content Height according to Left Menu
    FixContentHeight: function()
    {
        var oLeftMenu = $('PgMenuLeft'), oContent = $('PgContent');
        if( !oLeftMenu || !oContent ) return;

        var iLeftHeight = oLeftMenu.getStyle('height').toInt() + oLeftMenu.getStyle('padding-top').toInt() + oLeftMenu.getStyle('padding-bottom').toInt();
        var iContentHeight = oContent.getStyle('height').toInt();
        if( iContentHeight < iLeftHeight ) oContent.setStyle('height', iLeftHeight);
    },


    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Debug-Mode Error Alerts
    ErrorAlert: function( szErrorMsg )
    {
        // Debug Alert Message
        if( this.options.DebugMode )
            alert('[' + this.ClassName + ' ' + this.ClassVersion + '] -- DEV-ERROR --\n\n' + szErrorMsg);
        return false;
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Name, Version, Author
    ClassName:    'GradSF',
    ClassVersion: '1.0',
    ClassAuthor:  'Robert J. Secord, B.Sc.'
});

GradSF.implement(new Options, new Events);
