﻿// JavaScript Document
// GCS Land Search Application - script for Google Maps Add-in located at ~/UserControls/MapDetails.aspx
// Sleeping Giant Studios, LLC
// Created by David Ellenwood - 04/16/2007

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 the property address from the document
        var displayAddress = document.getElementById('propertyAddress');
        
        // 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) {
            
            geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                    
                        // If the address wasn't found, display an error message
                        document.getElementById('addressError').style.display = "block";
                        
                    } else {
                    
                        // Else, create a marker for the address
                        var marker          = new GMarker(point);
                        
                        /* 
                            -- Used if displaying links in the info window for driving directions --
                            
                            var dlr             = document.getElementById('propertyAddress');                     
                            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  = dlr.innerHTML + '<span class="getDir">Get directions: <a id="" title="Get directions to this location via Google&trade; Maps (opens new window)" target="_blank" href="'+toDlr+'">'
                                                                + 'To here</a> - <a id="" title="Get directions from this location via Google&trade; Maps (opens new window)" target="_blank" href="'+fromDlr+'">From here</a></span>';
                            
                        */
                        
                        var infoWindowHtml = '<span class="infoWinHtml">' + address.replace(",",",<br />"); + '</span>';
                        
                        GEvent.addListener(marker,"click",function() {
                            marker.openInfoWindowHtml(infoWindowHtml);
                        });
                        
                        // Add this marker to the map and display it
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(infoWindowHtml);
                        
                        // 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);
                        }
                        */
                    }
                }
            );
        } // END placeAddress function
        
        var resultSpans = displayAddress.getElementsByTagName('span');
	    var workingAddress;
	    
	    for(var y=0; y<resultSpans.length; y++) {
		    if(resultSpans[y].className == 'street') {
			    workingAddress = resultSpans[y].innerHTML + ', ';
		    }
		    if(resultSpans[y].className == 'city') {
			    workingAddress += resultSpans[y].innerHTML + ', ';
		    }
		    if(resultSpans[y].className == 'state') {
			    workingAddress += resultSpans[y].innerHTML + ' ';
		    }
		    if(resultSpans[y].className == 'zip') {
			    workingAddress += resultSpans[y].innerHTML + ' ';
            }
		}
		
		//alert(workingAddress);
		
		// Get and place a marker on the map and link up the result title in the listing
		placeAddress(workingAddress);      
        
    } else {
        // Display an error message for non-compatable browsers
        alert("Sorry, the Google Maps API is not compatible with this browser");    
    }
    
}
// END loadGoogleMap function

/*
//Function to add events to the window.onload statement

function addEventSimple(obj,evt,fn) {
    if(obj.addEventListener) {
        obj.addEventListener(evt,fn,false);
    }
    else if(obj.attachEvent) {
        obj.attachEvent('on'+evt,fn);
    }
}


//Function to remove events to the window.onload statement

function removeEventSimple(obj,evt,fn) {
    if(obj.removeEventListener) {
        obj.removeEventListener(evt,fn,false);
    }
    else if(obj.detachEvent) {
        obj.detachEvent('on'+evt,fn);
    }
}


// Load functions that require initialization

function loadPage() {
	loadGoogleMap();
}

function unloadPage() {
    GUnload();
}


addEventSimple(window,"load",loadPage);
addEventSimple(window,"unload",unloadPage);

window.onload   = loadPage;
window.onunload	= unloadPage;
*/
