var customIcon = new GIcon();
customIcon.image = "/Content/2009/images/googleMaps/pointer.png";
customIcon.shadow = "/Content/2009/images/googleMaps/shadow.png";
customIcon.iconSize = new GSize(32, 32);
customIcon.shadowSize = new GSize(49, 32);
customIcon.iconAnchor = new GPoint(16, 24);
customIcon.infoWindowAnchor = new GPoint(16, 16);

// Address class
function Address(streetAddress, friendlyName, latLng) {
    this.friendlyName = friendlyName;
    this.streetAddress = streetAddress;
    this.latLng = latLng;
}

function Initialize() {
    if (GBrowserIsCompatible()) {
        var geoCoder = new GClientGeocoder();
        if (geoCoder) {
            $(addresses).each(
				function(index, address) {
				    address.streetAddress = address.streetAddress.replace(/\bSPACE\b/, 'SUITE').replace(/\bSPC\b/, 'STE');
				}
			);

            $(addresses).each(
				function(index, address) {
				    var split = address.latLng.split(',');
				    var point = new GLatLng(split[0], split[1]);
				    SetLatLngForAddress(address, point);
				}
			);
        }

        if (addresses.length > 0) {
            MapAddresses();
        }
        else {
            SetDefaultMap();
        }
    }
}

function SetDefaultMap() {
    var zoom = false;
    var map = GetMap(zoom);
    //map.setCenter(new GLatLng(59.5, -98.5), 3);
    //map.setCenter(new GLatLng(39.50, -98.35), 3);
    map.setCenter(defaultLatLng, 3);
    map.clearOverlays();
}

function SetLatLngForAddress(address, latLng) {
    address.googleCallbackComplete = true;

    if (latLng) {
        address.latLng = latLng;
    }
    else {
        address.latLng = undefined;
    }
}

function MapAddresses() {
    var addressesWithLatLng = [];
    $(addresses).each(
		function(index, address) {
		    if (address.latLng) {
		        addressesWithLatLng.push(address);
		    }
		}
	);

    if (addressesWithLatLng.length == 0) {
        SetDefaultMap();
        return;
    }

    var bounds = new GLatLngBounds(addressesWithLatLng[0].latLng, addressesWithLatLng[0].latLng);

    $(addressesWithLatLng).each(
		function(index, address) {
		    if (address.latLng) {
		        bounds.extend(address.latLng);
		    }
		}
	);

    var zoom = true;
    var map = GetMap(zoom);
    map.clearOverlays();
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 3);

    $(addressesWithLatLng).each(
		function(index, address) {
		    if (address.latLng) {
		        var marker = new GMarker(address.latLng, { icon: customIcon });

		        GEvent.addListener(marker, "click", function() {
		            marker.openInfoWindowHtml(address.friendlyName);
		        });

		        map.addOverlay(marker);
		        if (addressesWithLatLng.length == 1) {
		            marker.openInfoWindowHtml(address.friendlyName);
		        }
		    }
		}
	);
}

function GetMap(zoom) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GMapTypeControl());
    if (zoom) {
        map.addControl(new GSmallMapControl());
    }
    else {
        map.disableDoubleClickZoom();
    }
    return map;
}

function ShowStoreLocation(itemIndex) {
    var zoom = true;
    var map = GetMap(zoom);
    var geocoder = new GClientGeocoder();

    if (geocoder) {
        var point = addresses[itemIndex].latLng;
        if (point) {
            map.setCenter(point, 13);
            var marker = new GMarker(point, { icon: customIcon });
            map.addOverlay(marker);
            marker.openInfoWindowHtml(addresses[itemIndex].friendlyName);
            window.scrollTo(0, 0);
        }
        else {
            SetDefaultMap();
            alert('We\'re sorry, this location cannot be mapped.');
        }
    }


}

function ShowNewStoreLocation(streetAddress, friendlyName) {
    ShowStoreLocation(AddAddress(streetAddress, friendlyName, null));
}

function AddAddress(streetAddress, friendlyName, latLng) {
    var rtrn = -1;
    if (addresses) {
        addresses.push(new Address(streetAddress, friendlyName, latLng));
        rtrn = addresses.length - 1;
    }
    return rtrn;
}

$(document).ready(Initialize);
$(window).unload(GUnload);

