/*------------------------------------------------------------------------------
| location.js
| Lehrhaus Judaica
| Author: Richa Avasthi
| Created: 2009-05-28
|
| Javascript utilities for the courses by location page.
------------------------------------------------------------------------------*/

// TODO: replace this with jQuery-specific JS and remove Prototype

/*------------------------------------------------------------------------------
| createMapWidget(element, address)
|
| Create a MapWidget for the specified markup element, with the specified
| address.
------------------------------------------------------------------------------*/
function createMapWidget(element, address)
{
    new MapWidget(element, { address: address });
}


/*------------------------------------------------------------------------------
| MapWidget
|
| This widget embeds a Google map centered at the given street address into the 
| page inside the specified element. Markup for the widget should be as follows:
|
|   <div class="location-widget group">
|     <div class="map-container"></div>
|     …
|   </div> <!-- /about-this-location -->
|
| Default options:
|   {
|       address: "",    -> The address to display in the map
|       mapOptions: {   -> A dictionary representing the Google maps options:
|           zoom: 15,   -> The zoom level
|           mapTypeId: google.maps.MapTypeId.ROADMAP
|                       -> The type of map to show.
|       }
|   }
------------------------------------------------------------------------------*/
var MapWidget = Class.create({
    DefaultOptions:
    {
        address: "",
        mapOptions: {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
    },
    
    initialize: function(element, options)
    {
        // Set the options
        this.options = Object.extend(Object.extend({ }, this.DefaultOptions), options || { });
        
        // properties
        this.element = $(element);
        this.mapContainer = this.element.down("div.map-container");
        this.geocoder = new google.maps.Geocoder();
        this.map = new google.maps.Map(this.mapContainer, this.options.mapOptions);

        if(this.geocoder)
        {
            var map = this.map;
            this.geocoder.geocode(
                { address: this.options.address },
                function(results, status)
                {
                    if (status == google.maps.GeocoderStatus.OK && results.length) 
                    {
                        // if(window.console)
                        // {
                        //     window.console.log("Geocode status OK.");
                        // }
                        // You should always check that a result was returned, as
                        // it is possible to return an empty results object.
                        if (status != google.maps.GeocoderStatus.ZERO_RESULTS)
                        {
                            map.setCenter(results[0].geometry.location);
                            var marker = new google.maps.Marker({
                                position: results[0].geometry.location,
                                map: map
                            });

                            // if(window.console)
                            // {
                            //     window.console.log("address: " + 
                            //                        results[0].formatted_address);
                            //     var mc = map.getCenter();
                            //     window.console.log("lat: " + mc.lat() + 
                            //                        ", lng: " + mc.lng());
                            //     window.console.log("mapTypeId: " + 
                            //                        map.getMapTypeId());
                            // }
                        }
                        // No results were returned
                        else
                        {
                            this.mapContainer.hide();
                        }
                    }
                    else
                    {
                        this.mapContainer.hide();
                        // if(window.console)
                        // {
                        //     window.console.log("Geocode was unsuccessful due to: " + status);
                        // }
                    }
                }
            );
        }
        else
        {
            if(window.console)
            {
                window.console.log("No geocoder available.");
            }
        }
    }
});


