
//(function() {
//    new YAHOO.util.YUILoader({ require: ['json', 'cookie'], base: '/js/yui/2.7.0/build/', loadOptional: false, allowRollup: true, onSuccess: function() {
//        YAHOO.util.Get.script('/js/googlemapjs.ashx', {
//            onSuccess: function() {
//                YAHOO.util.Get.script('http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22maps%22%2C%22version%22%3A%222%22%2C%22callback%22%3A%22GSPK.Geocode.LoadGoogleMapsAPI%22%7D%5D%7D&key=' + GSPK.GoogleMap.GetKeyForLocation());
//            }
//        });
//    }
//    }).insert();
//})();

(function() {
    var js = document.createElement('script');
    js.src = '/js/googlemapjs.ashx';
    js.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(js);
})();

if (!window['GSPK']) {
    window['GSPK'] = {};
}

if (!window['GSPK']['Geocode']) {
    window['GSPK']['Geocode'] = {};

    GSPK.Geocode.Cache = null;
    GSPK.Geocode.ClientGeocoder = null;

    GSPK.Geocode.GeocodeAddress = function(address, callback) {
        address = GSPK.Geocode.CheckUKPostalCode(address);

        if (!GSPK.Geocode.Cache) {
            GSPK.Geocode.Cache = YAHOO.util.Cookie.getSubs("geocodeCache");
        }

        if ((GSPK.Geocode.Cache) && (GSPK.Geocode.Cache[address])) {
            var obj = GSPK.Geocode.Cache[address];

            if (typeof obj == 'undefined') {
                // uh oh
                return null;
            } else if (typeof obj == 'string') {
                if (YAHOO.lang.JSON.isValid(obj)) {
                    return callback(YAHOO.lang.JSON.parse(GSPK.Geocode.Cache[address]));
                } else {
                    // uh oh
                    return null;
                }
            } else if (typeof obj == 'object') {
                return callback(GSPK.Geocode.Cache[address]);
            } else {
                return null;
            }
        } else if (!GSPK.Geocode.ClientGeocoder) {
            try {
                GSPK.Geocode.ClientGeocoder = new GClientGeocoder();
                GSPK.Geocode.GoogleGeocodeAddress(address, callback);
            } catch (ex) { GSPK.Geocode.ClientGeocoder = null; };

            GSPK.Geocode.CallbackArgs = {
                address: address,
                callback: callback
            };

            GSPK.Geocode.LoadGoogleMapsAPI();
        } else {
            GSPK.Geocode.GoogleGeocodeAddress(address, callback);
        }
    };

    GSPK.Geocode.GoogleGeocodeAddress = function(address, callback) {

        address = GSPK.Geocode.CheckUKPostalCode(address);

        if (GSPK.Geocode.ClientGeocoder.geocode == undefined) {
            GSPK.Geocode.ClientGeocoder = new google.maps.Geocoder();
        }

        if (GSPK.Geocode.CallbackArgs) {
            GSPK.Geocode.GeocodeAddress(GSPK.Geocode.CallbackArgs.address, GSPK.Geocode.CallbackArgs.callback);
        }


        GSPK.Geocode.ClientGeocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                
                console.log(results);

                var ret = {
                    accuracy: 5,
                    lat: results[0].geometry.location.lat(),
                    lng: results[0].geometry.location.lng(),
                    search: address
                };

                callback(ret);
                
                //                
            } else {
                callback(null);
            }
        });
    };


    GSPK.Geocode.GoogleGeocodeAddressMulti = function(address, callback) {
        address = GSPK.Geocode.CheckUKPostalCode(address);

        if (GSPK.Geocode.ClientGeocoder.geocode == undefined) {
            GSPK.Geocode.ClientGeocoder = new google.maps.Geocoder();
        }

        GSPK.Geocode.ClientGeocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                
                console.log(results);

            } else {

            }

        });

        //        GSPK.Geocode.ClientGeocoder.reset();
        //        GSPK.Geocode.ClientGeocoder.geocode( { 'address': address },  function(results, status) {
        //        
        //            address,
        //            function(point) {
        //                if (!point) {
        //                    callback(null);
        //                } else {
        //                    callback(point);
        //                }
        //            }
        //        );
    };

    GSPK.Geocode.CheckUKPostalCode = function(address) {
        var regexPattern = '^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$';

        if (address.match(regexPattern)) {
            address = address + ', UK';
        }

        return address;
    };
}
