﻿var map;
var geocoder;

var title = null;
var address = null;
var logo = null;
var link = null;
var pushPinIcon = null;
var gdir;
function LoadMap(divMapId)
{
      try
      {
          map = new GMap2(document.getElementById(divMapId));
            gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
          map.setCenter(new GLatLng(35, -75), 8);
          //map.setZoom(15);            
        //  map.setMapType(G_HYBRID_MAP); 
          map.setUIToDefault();   
          
          geocoder = new GClientGeocoder();                
          
      }
      catch(err){}
}
      
function UnloadMap()
{
      try
      {
        GUnload();
      }
      catch(err){}
}
      
    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
function callback(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200)
      {
        alert("Sorry, we were unable to geocode that address");
      }
      else
      {
            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1],
                                place.Point.coordinates[0]);
            marker = new GMarker(point);            

            map.addOverlay(marker);
            if(pushPinIcon!=null && pushPinIcon!='')
                marker.setImage(pushPinIcon);
            
            var markerHtml='<div style="float:left;"><b>'+title + '</b><br />' +
                           address;
            var logoImgTag='<div style="float:left;"><img src="'+logo+'" title="'+title+'" /></div>';            
            
            if(link!=null && link!='')
                markerHtml=markerHtml+'<br /><a href="'+link+'" class="blue">more...</a></div><div style="clear:both"></div>';
            else
                markerHtml=markerHtml+'</div><div style="clear:both"></div>';
            
            if(logo!=null && logo!='')
                markerHtml=logoImgTag+markerHtml;
                
            marker.openInfoWindowHtml(markerHtml); 
            
            GEvent.addListener(marker, "click", function(){ marker.openInfoWindowHtml(markerHtml); });
            
       
      }
}
    
function showControllerDivId(controllerDivId)
{        
    document.getElementById(controllerDivId).style.visibility='visible';
    document.getElementById(controllerDivId).style.height='auto';
    document.getElementById(controllerDivId).style.overflow='auto';
}
function hideControllerDivId(controllerDivId)
{
    document.getElementById(controllerDivId).style.visibility='hidden';
    document.getElementById(controllerDivId).style.height='0px';
    document.getElementById(controllerDivId).style.overflow='hidden';
}

    
function FindLoc(seacrhTitle,searchLoc,controllerDivId,customPushPinIcon,moreInfoLink,logoURL)
{
    if(map == null)
        LoadMap('theMap');

    showControllerDivId(controllerDivId);
    title=seacrhTitle;
    address=searchLoc;
    logo = logoURL;            
    link = moreInfoLink;            
    pushPinIcon = customPushPinIcon;
    if(geocoder == null)
        geocoder = new GClientGeocoder();    
    geocoder.getLocations(address, callback);    
}