// JavaScript Document
// Mathews Javascripts - Scripts on the bows - Find a Retailer layout
// Sleeping Giant Studios, LLC
// Created by David Ellenwood - 12/07/2006

function loadGoogleMap() {

    if (GBrowserIsCompatible()) {
        
        // Create the map and necessary variables
        var map         = new GMap2(document.getElementById("googleMap"));
        var geocoder    = new GClientGeocoder();
        var bounds      = new GLatLngBounds();
        
        // Get search results from the document
        var resultsList = document.getElementsByTagName('address');
        
        // Add controls to the map
        map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
        
        // We HAVE to set the center point of the map to something...
        map.setCenter(new GLatLng(0,0),0);
        
        
        // Places a marker on the map and links the result title to that marker
        function placeAddress(address,resultNum) {
        
            geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                    
                        // If the address wasn't found, don't do anything
                        //alert(address + " not found");
                        
                    } else {
                    
                        // Else, create a marker for the address
                        var marker          = new GMarker(point);
                        var dlr             = document.getElementById('address_'+resultNum);                     
                       	var dlrTitleSpan    = document.getElementById('resultTitle_'+resultNum);
                        var toDlr           = 'http://maps.google.com/maps?f=d&z=13&daddr='+address;
                        var fromDlr         = 'http://maps.google.com/maps?f=d&z=13&saddr='+address;
                        
                        var infoWindowHtml  =	'<div class="infoWin">'
												+ '<strong>' + dlrTitleSpan.innerHTML + '</strong><br />'
												+ dlr.innerHTML + '<br /><span class="getDir">Get directions: <a id="to_'+resultNum+'" title="Get directions to this location via Google&trade; Maps (opens new window)" target="_blank" href="'+toDlr+'">'
												+ 'To here</a> - <a id="from_'+resultNum+'" title="Get directions from this location via Google&trade; Maps (opens new window)" target="_blank" href="'+fromDlr+'">From here</a></span>'
												+ '</div>';

                        GEvent.addListener(marker,"click",function() {
                            marker.openInfoWindowHtml(infoWindowHtml,200);
                        });
                        
                        // Add this marker to the map
                        map.addOverlay(marker);
                        
                        // Extend the map bounds with this point
                        bounds.extend(point);
                        
                        //Get the center point of the map
                        var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
                        var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
                        
                        // Set the zoom level and center point based on the extended bounds
                        map.setZoom(map.getBoundsZoomLevel(bounds));
                        map.setCenter(new GLatLng(clat,clng));

                        dlrTitle = dlrTitleSpan.innerHTML;
                        var dlrLink = 'viewResult_'+resultNum;
                        var toDlr   = 'to_'+resultNum;
                       	var fromDlr = 'from_'+resultNum;
                        dlrTitleSpan.innerHTML = '<a title="Select this result on the map" id="'+dlrLink+'" href="#resultsHeading">'+dlrTitle+'</a>';
                        
                        document.getElementById(dlrLink).onclick = function() {
                            marker.openInfoWindowHtml(infoWindowHtml,200);
                        }
                        
                    }
                }
            );
        } // END placeAddress function
		
		//placeAddress("2323 Pine St., La Crosse, WI 54601",0);
        
        for(var x=0; x<resultsList.length; x++) {
        
		    var resultSpans = resultsList[x].getElementsByTagName('span');
		    var dlrAddress;
		    
		    for(var y=0; y<resultSpans.length; y++) {
			    if(resultSpans[y].className == 'street') {
				    dlrAddress = resultSpans[y].innerHTML + ', ';
			    }
			    if(resultSpans[y].className == 'city') {
				    dlrAddress += resultSpans[y].innerHTML + ', ';
			    }
			    if(resultSpans[y].className == 'state') {
				    dlrAddress += resultSpans[y].innerHTML + ' ';
			    }
			    if(resultSpans[y].className == 'zip') {
				    dlrAddress += resultSpans[y].innerHTML + ' ';
			    }
		    }
			
			//alert(dlrInfoWinText.zip);
			
			// For each result address in the document, get and place a marker
            //  on the map and link up the result title in the listing
			placeAddress(dlrAddress,x);
              
        } // END for(resultsList) Loop        
        
    } else {
        // Display an error message for non-compatable browsers
        alert("Sorry, the Google Maps API is not compatible with this browser");    
    }
    
} // END loadGoogleMap function


// Load functions that require initialization
function initializePage() {
	loadGoogleMap();			// loads map
}


window.onload = initializePage;
window.onunload	= GUnload;
