// JavaScript Document


	  $j=jQuery.noConflict();
//console.log("single location map");



$j(document).ready(function() {
    loadMapScript(GMapsKey);
});


   var gmarkers = [];
        var htmls = [];
        var i = 0;


function loadMapScript(key) {
    var script = document.createElement("script");
    script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2.x&key=" + key + "&c&async=2&callback=showAddress");
    script.setAttribute("type", "text/javascript");
    document.documentElement.firstChild.appendChild(script);
}


function showAddress(address) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(35.6870, -105.9378 ), 12);    // Santa Fe
        geocoder = new GClientGeocoder();
        map.setUIToDefault();
        map.disableScrollWheelZoom();
        GDownloadUrl("Buildit-this could be a php file with info", function() {
            
            
            
            for (var i = 0; i < mapAddresses.length; i++) {
                var address = mapAddresses[i];
                var addressbox = mapAddressesBox[i];
                addMarkerAtGeocode(address,addressbox)
            }
            
            function addMarkerAtGeocode(address,addressbox) {
                geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        
                        document.getElementById('notFound').innerHTML += address + " - not found<br>";
                        
                        } else {
                        var marker = createMarker(point, address,addressbox);
    
                    }
                }
                );
            }
            
        });
        
        
        //Below arrays for mouse over effect
     
        
        var bounds = new GLatLngBounds();
        
        function createMarker(point, address,addressbox) {
            var marker = new GMarker(point);
            var html = addressbox;
            //below for mouseover effect
            gmarkers[i] = marker;
            htmls[i] = html;
            i++;
            

            bounds.extend(point);
            // ===== determine the zoom level from the bounds =====
            map.setZoom(map.getBoundsZoomLevel(bounds)-1);
            
            // ===== determine the centre from the bounds ======
            map.setCenter(bounds.getCenter());
			
			map.addOverlay(marker);
            
            //GEvent.addListener(marker, 'click', function() {
              GEvent.addListener(marker, 'mouseover', function() {
                marker.openInfoWindowHtml(html);
            });
            return marker;
        }
    
        
        
        
}}

        
 // This function picks up the click and opens the corresponding info window
function mapClick(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
}   
