﻿GoogleMap = new function() {

    //** Private members **
    var self = this;
    var map;
    var marker;

    //** Public properties **
    this.defaultCountry = 'australia';
    this.accuracyDescriptions = [
		'Unknown',
		'Country',
		'State',
		'Shire/Municipality',
		'Town/Suburb',
		'Post code',
		'Street',
		'Nearest Intersection',
		'Exact Match'
	];

    //** Public methods **
    this.geocodeAddress = function(addressComponents, callback) {
        var addr;
        var geocoder = new GClientGeocoder();

        addr = addressComponents['street'].toLowerCase();
        addr += ' ' + addressComponents['postcode'];
        addr += ' ' + addressComponents['suburb'].toLowerCase();
        addr += ' ' + addressComponents['state'];
        addr += ' ' + ((addressComponents['country']) ? addressComponents['country'] : self.defaultCountry);

        geocoder.getLocations(addr, function(responseJSON) { handleGeocodeCallback(responseJSON, callback); });

    }

    this.bounceMarker = function(marker, height, speed) {
        if (!marker.Xa) {
            marker.Xa = true;
            marker.qo(false);
        }

        marker.Pa = height ? height : 20;
        marker.ri = height ? height : 20;
        marker.av = speed ? speed : 0.4;
        marker.tc();
    }

    //** Private Methods **
    function init() {
        google.load('maps', 2);
    }

    function handleGeocodeCallback(responseJSON, callback) {
        var response = null;

        var geoData = eval(responseJSON);

        if (geoData.Status.code != 200) {
            alert('Geocoding failed with error code: ' + geoData.Status.code + '\n\nPlease try an alternative or less specific address');
        }
        else {
            var place = geoData.Placemark[0];
            response = new Object();
            response['lng'] = place.Point.coordinates[0].toString();
            response['lat'] = place.Point.coordinates[1].toString();
            response['accuracy'] = parseInt(place.AddressDetails.Accuracy); ;
        }

        callback(response);
    }

    init();
    return this;
} ();


