//*************************************************************************************
// File     : bgGoogle.js
// Version  : 0.4.0
// Requires : jquery.js (version 1.3.2+), braingnat.js (version 0.4.0), and a Google Map API key included in project
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : August 18, 2009
// Modified : August 18, 2009
// Purpose  : BrainGnat.Google - A series of Google API-specific extensions for use with BrainGnat.
//*************************************************************************************

// 0.3.0
// Google               - New namespace for Google mashups
// Google.map           - New namespace for Google maps mashups
// Google.map.load(elem)- Loads elem (defined by css selecor). If selector is for more than one element, only selects the first one.
// Google.map.addMarker(latlng,info) - Loads point defined by latlng and adds a marker. If info has a value, puts it in an info bubble.
// Google.map.addMarkerByAddress(address,info) - loads point defined by address and adds a marker. If info has a value, puts it in a info bubble.
// Google.map.setCenterByAddress(address, zoom) - sets the center of map to a provided address.
// Google.map.loadFromHCard(card, map, zoom) - Loads a map, gets the address from an hcard and sets a marker there, and if zoom is provided, zooms to that level.
// 0.3.1
// Google.map.addDirections(elem) - Sets elem as a direction pane for a google map
// Google.map.getDirections(to, from) - Inserts into directions pane created through .addDirections() a series of directions to address "to" from address "from".
// 0.3.2
// Some Google.map code polishing
// 0.4.0
// BrainGnat.Google has been spun out into a separate script that must be included in projects that make use of it.
// Syntax is the same, it merely exists as an extension now.

BrainGnat.Google = function() {
    return {
        version: "BrainGnat.Google Extension - v0.4.0 (requires BrainGnat v0.4.0)",
        map: {
            // Using BrainGnat.Google.map requires a MAP API Key included before this script in the project.
            load: function(elem) {
                if (GBrowserIsCompatible()) {
                    var domElem = $(elem).get(0);
                    BrainGnat.Google.map.data = new GMap2(domElem);
                    BrainGnat.Google.map.data.addControl(new GLargeMapControl());
                    BrainGnat.Google.map.data.addControl(new GMapTypeControl());
                } else {
                    alert('Error: BGGM1 - There was an issue loading the Google map.');
                }
            },
            addDirections: function(elem) {
                var directionsPanel = $(elem).get(0);
                BrainGnat.Google.map.directions = new GDirections(BrainGnat.Google.map.data, directionsPanel);
            },
            addMarker: function(latLng, info) {
                var marker = new GMarker(latLng);
                BrainGnat.map.data.addOverlay(marker);
                if (!info) { } else {
                    marker.openInfoWindowHtml(info);
                }
            },
            addMarkerByAddress: function(address, info) {
                geocoder = new GClientGeocoder();
                geocoder.getLatLng(address, function(point) {
                    if (!point) {
                        alert('Error: BGGM2 - Provided address does not exist.');
                    } else {
                        var marker = new GMarker(point);
                        BrainGnat.Google.map.data.addOverlay(marker);
                        if (!info) { } else {
                            marker.openInfoWindowHtml(info);
                        }
                    }
                });
            },
            getDirections: function(to, from) {
                geocoder = new GClientGeocoder();
                geocoder.getLatLng(from, function(point) {
                    if (!point) {
                        alert('Error: BGGM2 - Provided address does not exist. Please make sure to include city, state, and zip code.');
                    } else {

                        BrainGnat.Google.map.directions.load("from: " + point + " to: " + to);
                    }
                });
            },
            setCenterByAddress: function(address, zoom) {
                if (!zoom) {
                    zoom = 13;
                }
                geocoder = new GClientGeocoder();
                geocoder.getLatLng(address, function(point) {
                    if (!point) {
                        alert('Error: BGGM2 - Provided address does not exist.');
                    } else {
                        BrainGnat.Google.map.data.setCenter(point, zoom);
                    }
                });
            },
            loadFromHCard: function(card, map, zoom, marker) {
                if (!zoom || zoom == null) {
                    zoom = 13;
                }
                if (!marker && marker != false) {
                    marker = null;
                }
                BrainGnat.Google.map.load(map);
                var address = "";
                address += $(card + ' .street-address').text();
                address += ' ' + $(card + ' .locality').text();
                address += ', ' + $(card + ' .region').text();
                address += ' ' + $(card + '. postal-code').text();
                BrainGnat.Google.map.setCenterByAddress(address, zoom);
                if (marker == null) {
                    BrainGnat.Google.map.addMarkerByAddress(address, address);
                } else if (marker == false) {
                    BrainGnat.Google.map.addMarkerByAddress(address);
                } else {
                    BrainGnat.Google.map.addMarkerByAddress(address, marker);
                }
            },
            data: null,
            directions: null
        }
    }
}();