/*
    StudyinCanada 1.0 - StudyinCanada Main JavaScript Controller
      - Copyright (c) 1995-2008 EDge Interactive. 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 StudyinCanada = new Class(
{
    Implements: [Options],

    // -------------------------------------------------------------------------------------------------------------
    // Member Variables
    options:
    {
        // Required Settings:
        RightSideGMaps: false,

        // Optional Developer Use:
        DebugMode: false
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Constructor (options = JSON Object)
    initialize: function( options )
    {
        this.setOptions( options );

        // Google Maps Vars
        this.oMap = null;
        this.oGeoCoder = null;
        this.oMapCenter = null;

        // Attach Window Events
        window.addEvents(
        {
            'load':     this.OnLoad.bind(this),
            'unload':   this.OnUnload.bind(this),
            'domready': this.OnDomReady.bind(this)
        });
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: DOM Ready Event (before OnLoad)
    OnDomReady: function()
    {
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Page Loaded Event
    OnLoad: function()
    {
        // Load Google Maps
        if( this.options.RightSideGMaps !== false ) this.LoadGoogleMaps();
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Page Unloaded Event
    OnUnload: function()
    {
        // Unload Google Maps API
        if( this.options.RightSideGMaps !== false ) this.UnloadGoogleMaps();
    },

    // -------------------------------------------------------------------------------------------------------------
    // Google Maps API Routines:
    // -------------------------------------------------------------------------------------------------------------

    // -------------------------------------------------------------------------------------------------------------
    // External Routine:
    LoadGoogleMaps: function()
    {
        if( !GBrowserIsCompatible() ) return this.ErrorAlert('The Google Maps API is not compatible with your browser.');

        // Default Center Location / Depth
        var oSchoolLocation = {Lat: 43.808734293456276, Lng: -79.3342924118042, Depth: 14};

        // Load Google Maps API with GeoCoder
        this.oMap = new GMap2(document.getElementById("SchoolProfileMap"));
        this.oGeoCoder = new GClientGeocoder();

        // Build Search Address from School Address
        var szSearchAddress = '';
        if( this.options.RightSideGMaps.Address.length ) szSearchAddress += this.options.RightSideGMaps.Address.trim() + ', ';
        if( this.options.RightSideGMaps.City.length ) szSearchAddress += this.options.RightSideGMaps.City.trim() + ' ';
        if( this.options.RightSideGMaps.Province.length ) szSearchAddress += this.options.RightSideGMaps.Province.trim() + ', ';
        if( this.options.RightSideGMaps.Country.length ) szSearchAddress += this.options.RightSideGMaps.Country.trim() + ' ';
        if( this.options.RightSideGMaps.Postal.length ) szSearchAddress += this.options.RightSideGMaps.Postal.trim();

        // Get Location from School Address (GeoCoder)
        this.oGeoCoder.getLatLng(szSearchAddress, function( oPoint )
        {
            if( !oPoint )
            {
                // Send Alert for School Address Not Found
                this.ErrorAlert('Could not locate School Address! Using Default Location. (' + szSearchAddress + ')');

                // To hide the map block:
                //var oMapsBlock = $('SchoolProfileGoogleMaps');
                //if( oMapsBlock ) oMapsBlock.setStyle('display', 'none');

                // Set Default Center Point for Location
                this.oMapCenter = new GLatLng(oSchoolLocation.Lat, oSchoolLocation.Lng);
                this.oMap.setCenter(this.oMapCenter, 2);

                // Set Marker for School Location
                this.oMap.addOverlay(new GMarker(this.oMapCenter));
            }
            else
            {
                // Set Default Center Point for Location
                this.oMapCenter = oPoint;
                this.oMap.setCenter(this.oMapCenter, oSchoolLocation.Depth);

                // Set Marker for School Location
                this.oMap.addOverlay(new GMarker(this.oMapCenter));
            }
        }.bind(this));
    },

    // -------------------------------------------------------------------------------------------------------------
    // External Routine:
    UnloadGoogleMaps: function()
    {
        GUnload();
    },

    // -------------------------------------------------------------------------------------------------------------
    // Internal Routine: Debug-Mode Error Alerts
    ErrorAlert: function( szErrorMsg )
    {
        // Debug Alert Message
        if( this.options.DebugMode )
            alert('[' + this.ClassName + ' ' + this.ClassVersion + '] -- Developer Error --\n\n' + szErrorMsg);
        return false;
    },

    // -------------------------------------------------------------------------------------------------------------
    // Class Name, Version, Author
    ClassName:    'StudyinCanada',
    ClassVersion: '1.0',
    ClassAuthor:  'Robert J. Secord, B.Sc.'
});