/// <reference path="jquery-vsdoc.js" />
// bgGoogle.js 0.8 | requires jquery.js 1.3.2+ | made by Kyle Weems of Mindfly Web Studio (http://mindfly.com/)
// Created Aug 18, 2009 | Last Modified : Jun 10, 2011
if (!bg) {
    var bg = {};
    var BrainGnat = bg;
}
bg.Google = {
    version: "bg.Google Extension - v0.7 (requires jquery.js 1.3.2+)",
    map: {
        load: function(elem, sensor, z, callback) {
            if (!sensor) {
                sensor = false;
            }
            if (!z) {
                z = 13;
            }
	    bg.Google.map.mapElem = elem;
            bg.Google.map.z = z;
            if (!callback) { } else {
                bg.Google.map.mapCallback = callback;
            }
            var script = "http://maps.google.com/maps/api/js?sensor=" + sensor + "&async=2&callback=bg.Google.map.runload";
            $.getScript(script, function() {});
        },
		runload: function(data){
                var ll = new google.maps.LatLng(0, 0);
                var domElem = $(bg.Google.map.mapElem).get(0);
                var myOptions = { zoom: bg.Google.map.z, center: ll, mapTypeId: google.maps.MapTypeId.ROADMAP };
                bg.Google.map.data = new google.maps.Map(domElem, myOptions);
                if (bg.Google.map.mapCallback != null) {
                    google.maps.event.addListener(bg.Google.map.data, 'tilesloaded', function() {
                        bg.Google.map.mapCallback();
                        google.maps.event.clearListeners(bg.Google.map.data, "tilesloaded");
                    })
                }

		},
		/*
        addDirections: function(elem) {
            var directionsPanel = $(elem).get(0);
            bg.Google.map.directions = new GDirections(bg.Google.map.data, directionsPanel);
        }*/
        addMarker: function(latLng, info, index, clickFunc) {
            if (latLng.length > 0){
                var ll = new google.maps.LatLng(latLng[0], latLng[1]);
	    } else {
                var ll = latLng;
            }
            if (index == null) {
                var marker = new google.maps.Marker({ map: bg.Google.map.data, position: ll, title: info, html: info });
            } else {
                var letter = String.fromCharCode("A".charCodeAt(0) + index);
                var marker = new google.maps.Marker({ map: bg.Google.map.data, position: ll, title: info, html: info, icon: 'http://www.google.com/mapfiles/marker' + letter + '.png' });
            }
            marker.setMap(bg.Google.map.data);
            if (clickFunc != null) { google.maps.event.addListener(marker, 'click', function() { clickFunc(this); }); }
        },
        addMarkerByAddress: function(address, info, index, clickFunc) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    bg.Google.map.addMarker([results[0].geometry.location.lat(), results[0].geometry.location.lng()], info, index, clickFunc);
                }
            });
        }, /*
        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 {

                    bg.Google.map.directions.load("from: " + point + " to: " + to);
                }
            });
        },*/
        setCenter: function(latlng, callback) {
            if (latlng.lng()) {
                var ll = latlng;
            } else {
                var ll = new google.maps.LatLng(latlng[0], latlng[1]);
            }
            bg.Google.map.data.setCenter(ll);
            if (callback != null) {
                callback();
            }
        },
        setCenterByAddress: function(address, callback) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    bg.Google.map.data.setCenter(results[0].geometry.location);
                    if (callback != null) {
                        callback();
                    }
                } else {
                }
            });
        },
        loadFromHCard: function(card, map, sensor, z, callback) {
            if (!z) {
                z = 13;
            }
            var address = bg.Google.map.getAddressFromHCard(card);
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address, function() { callback(); }) });
            }
        },
        loadFromGeo: function(geo, map, sensor, z, callback) {
            if (!z) {
                z = 13;
            }
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.getLatLngFromGeo(geo, function(ll) { bg.Google.map.setCenter(ll); }) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.getLatLngFromGeo(geo, function(ll) { bg.Google.map.setCenter(ll, callback); }) });
            }
        },
        loadFromAddress: function(address, map, sensor, z, callback) {
            if (!z) {
                z = 13;
            }
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address, function() { callback(); }) });
            }
        },
        getAddressFromHCard: function(card) {
            var address = "";
            address += $(card + ' .street-address').text();
            address += ' ' + $(card + ' .locality').text();
            address += ', ' + $(card + ' .region').text();
            address += ' ' + $(card + ' .postal-code').text();
            return address;
        },
        geo: function(address) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    alert(results[0].geometry.location[0] + ', ' + results[0].geometry.location[1]);
                } else {
                }
            });
        },
        getLatLngFromGeo: function(geo, callback) {
            var ll = new google.maps.LatLng($(geo + ' .latitude').text(), $(geo + ' .longitude').text());
            if (callback == null) {
                return ll;
            } else {
                callback(ll);
            }
        },
        data: null,
        directions: null,
        mapName: null,
        mapCallback: null,
	mapElem: null,
        z: null
    }
}
