
    function showTab(dt) {
        var tgt = $(dt).attr('target');
        $('#GLegend dd:visible').hide();
        $('#GLegend dt.tab_here').removeClass('tab_here');
        $('#'+tgt).show();
        $(dt).addClass('tab_here');
    }
    function init() {
        $('#GLegend dd').hide();
        $('#GLegend dt').bind('click',function(e) {
            showTab(this);
        });
        $('#GLegend dt:first').addClass('tab_here');
        $('#GLBusiness').show();   // Show first tab default
	if (GBrowserIsCompatible()) {
           // GLegend = document.getElementById('GLegend');
	  map = new GMap2(document.getElementById("map_canvas"));
	  map.addControl(new GSmallMapControl());
	  map.setCenter(new GLatLng(lat,lng), 10);
	  updateMarkers(true);
	  GEvent.addListener(map,'zoomend',function() {
	    updateMarkers(true);
	  });
	  GEvent.addListener(map,'moveend',function() {
	    updateMarkers(false);
	  });
	}
    }
    function setMessage() {
      document.getElementById('loadingMessage').style.left = "91px";
      setOpacity(document.getElementById('loadingMessage'),85);
    }
    function clearMessage() {
      fadeOut('loadingMessage',85);
    }

    function updateMarkers(clean) { 
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      var nowMapCenter = map.getCenter();
      if (clean == true || !mapCenterLat) {
	mapCenterLat = nowMapCenter.lat();
	mapCenterLng = nowMapCenter.lng();
	setMessage();
      }
      else {
	var centerLat = nowMapCenter.lat();
	var centerLng = nowMapCenter.lng();
	southWestArr = southWest.toUrlValue().split(',');
	northEastArr = northEast.toUrlValue().split(',');
	width = southWestArr[0] - northEastArr[0];
	width = Math.abs(width);
	latDist = Math.abs((mapCenterLat - centerLat));
	lngDist = Math.abs((mapCenterLng - centerLng));
	if ((latDist < width) && (lngDist < width)) {
	  return;
	}
	else {
	  mapCenterLat = nowMapCenter.lat();
	  mapCenterLng = nowMapCenter.lng();
	  setMessage();
	}
      }
      $('#GLBusiness').html('<p>No Businesses</p>');
      $('#GLCity').html('<p>No Cities</p>');
      $('#GLEvent').html('<p>No Events</p>');
      GBusinessHTML = '';
      GEventHTML = '';
      GCityHTML = '';
      map.clearOverlays();
      markers = [];
      
      var requestTag = new Date().valueOf().toString();
      var getVars = 'ne=' + northEast.toUrlValue()
	+ '&sw=' + southWest.toUrlValue() + '&req=' + requestTag + '&city=' + currCity;
      var request = GXmlHttp.create();
      request.open('GET','/map/request.php?'+getVars,true);
      request.onreadystatechange = function() {
	if (request.readyState == 4 && request.status == 200) {
	  var r = eval("("+request.responseText+")");
	  if (requestTag != r.requestTag) {
	    return;
	  }
	  var points = r.result
          var markers = [];
	  for (i in points) {
            var thisPoint = points[i];
	    var marker_type = thisPoint.marker_type;
            
            if (marker_type == 'event') {
	      point = new GLatLng(thisPoint.lat,thisPoint.lng);
	      infoWindow = '<h3 ' + 'class="title"><a href="' + thisPoint.permalink + '">' + thisPoint.title + '</a><' + '/h3>';
	      infoWindow += '<p ' + 'class="address">' + thisPoint.address + '<' + '/p>';
	      infoWindow += '<p ' + 'class="description">' + thisPoint.description + '<' + '/p>';
	      var marker = createMarker(thisPoint.id, point, infoWindow, marker_type);
              GEventHTML +=
                '<p lid="' + i + '" class="legendMarker';
              if ((i % 2) != 0) {
                GEventHTML += ' odd';
              }
              GEventHTML += '"><img src="' + currImage + '" /><span>' +
                thisPoint.title + '</span></p>';
	    }
            else if (marker_type == 'citycrimestat') {
	      var thisZoom = map.getZoom();
	      if (thisZoom < 11) {
		point = new GLatLng(parseFloat(thisPoint.lat) + 0.01,parseFloat(thisPoint.lng) + 0.01);
	      } else {
		point = new GLatLng(parseFloat(thisPoint.lat),parseFloat(thisPoint.lng));
	      }
	      var crime_percent = parseFloat(thisPoint.crime_percent);
		var dangerLevel = '';
                    if (crime_percent > .05) {
                      dangerLevel = 'Extremely High';
                    } else if (crime_percent > .01) {
                      dangerLevel = 'Very High';
                    } else if (crime_percent > .007) {
                      dangerLevel = 'High';
                    } else if (crime_percent > .005) {
                      dangerLevel = 'Above Average';
                    } else if (crime_percent > .003) {
                      dangerLevel = 'Slightly Above Average';
                    } else if (crime_percent > .001) {
                      dangerLevel = 'Slightly Below Average';
                    } else if (crime_percent > .0009) {
                      dangerLevel = 'Low to Average';
                    } else if (crime_percent > .0007) {
                      dangerLevel = 'Low';
                    } else if (crime_percent > 0) {
                      dangerLevel = 'Very Low';
                    } else {
                      dangerLevel = 'Extremely Low';
                    }
                    
	      infoWindow = '<a href="/'+'crimestats/'+thisPoint.state+'/'+thisPoint.city+'.html">';
	      infoWindow += '<h3 ' + 'class="crimestat">' + thisPoint.city + ', ' + thisPoint.state + '<' + '/h3></a>';
	      infoWindow += '<p>Population: ' + thisPoint.population + '<' + '/p>';
	      infoWindow += '<p>Danger Level: ' + dangerLevel + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-violentcrime">Violent Crimes: ' + thisPoint.violent_crime + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-murder">Murder/Manslaughters: ' + thisPoint.murder_manslaughter + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-rape">Rapes: ' + thisPoint.forceable_rape + '<' + '/p>';
	      //infoWindow += '<p ' + 'class="cs-robbery">Robberys: ' + thisPoint.robbery + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-assault">Assaults: ' + thisPoint.aggravated_assault + '<' + '/p>';
	      /*infoWindow += '<p ' + 'class="cs-propertycrime">Property Crimes: ' + thisPoint.property_crime + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-burglary">Burglarys: ' + thisPoint.burglary + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-larcenytheft">Larceny Thefts: ' + thisPoint.larceny_theft + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-vehicletheft">Vehicle Thefts: ' + thisPoint.vehicle_theft + '<' + '/p>';
	      infoWindow += '<p ' + 'class="cs-arson">Arson: ' + thisPoint.arson + '<' + '/p>';*/
	      var marker = createMarker(thisPoint.id, point, infoWindow, marker_type, crime_percent);
              GCityHTML +=
                '<p lid="' + i + '" class="legendMarker';
              if ((i % 2) != 0) {
                GCityHTML += ' odd';
              }
              GCityHTML += '"><img src="' + currImage + '" /><span>' +
                thisPoint.city + ', ' + thisPoint.state + '</span></p>';
	    }
	    else {  // looking at business
	      point = new GLatLng(thisPoint.lat,thisPoint.lng);
	      infoWindow = '<a href="'+'/business/'+thisPoint.business_id+'.html">';
	      infoWindow += '<h3 ' + 'class="title">' + thisPoint.business_name + '<' + '/h3></a>';
	      infoWindow += '<p ' + 'class="address">' + thisPoint.address + ' ';
	      infoWindow += thisPoint.city + ' ' + thisPoint.state + ' ';
	      infoWindow += thisPoint.zip + '<' + '/p>';
	      infoWindow += '<p ' + 'class="phone">' + thisPoint.phone + '<' + '/p>';
	      var marker = createMarker(thisPoint.id, point, infoWindow, marker_type);
              GBusinessHTML +=
                '<p lid="' + i + '" class="legendMarker';
              if ((i % 2) != 0) {
                GBusinessHTML += ' odd';
              }
              GBusinessHTML += '"><img src="' + currImage + '" /><span>' +
                thisPoint.business_name + '</span></p>';
	    }
	    map.addOverlay(marker);
            markers[i] = [marker,infoWindow];
	  }
          if (GBusinessHTML != '') {
            $('#GLBusiness').html(GBusinessHTML);
          }
          if (GEventHTML != '') {
            $('#GLEvent').html(GEventHTML);
          }
          if (GCityHTML != '') {
            $('#GLCity').html(GCityHTML);
          }
          $('.legendMarker').bind('click',function() {
            window.location = '#top';
            var lid = $(this).attr('lid');
            var thisMarker = markers[lid][0];
            var thisHtml = markers[lid][1];
            map.setCenter(thisMarker.getPoint(),false);
            thisMarker.openInfoWindow(thisHtml);
          });
          
	  setTimeout('clearMessage()', 500);
          
	}
      }
      request.send(null);
    }
    
    function createMarker(thisId, point, html, marker_type, crime_percent) {
      var thisIcon = new GIcon();
      thisIcon.imageSize = new GSize(20,30);
      thisIcon.iconAnchor = new GPoint(5, 34);
      thisIcon.infoWindowAnchor = new GPoint(5,2);
      if (marker_type == 'event') {
	thisIcon.image = '/map/images/BLUE.png';
        thisId = 'EP' + thisId;
      }
      else if (marker_type == 'citycrimestat') {
	var image = '';
	if (crime_percent > .05) {
	  image = 'siren_10.png';
	} else if (crime_percent > .01) {
	  image = 'siren_09.png';
	} else if (crime_percent > .007) {
	  image = 'siren_08.png';
	} else if (crime_percent > .005) {
	  image = 'siren_07.png';
	} else if (crime_percent > .003) {
	  image = 'siren_06.png';
	} else if (crime_percent > .001) {
	  image = 'siren_05.png';
	} else if (crime_percent > .0009) {
	  image = 'siren_04.png';
	} else if (crime_percent > .0007) {
	  image = 'siren_03.png';
	} else if (crime_percent > 0) {
	  image = 'siren_02.png';
	} else {
	  image = 'siren_01.png';
	}
	thisIcon.image = '/map/images/smallsirens/' + image;
        thisId = 'SP' + thisId;
      } else {  // business marker
	thisIcon.image = '/images/PINCAT/ICON_' + marker_type + '2.png';
        thisId = 'BP' + thisId;
      }
      currImage = thisIcon.image;
      var marker = new GMarker(point, thisIcon);
      GEvent.addListener(marker,'click',function() {
	//map.setCenter(marker.getPoint(),false);
        html = '<div class="mapInfoWindow">' + html + '</div>';
	marker.openInfoWindowHtml(html);
      });
      //markers.push([marker,html,thisId]);
      
      return marker;
    }

    function showAddress(address) {
	geocoder.getLatLng(
	  address,
	  function(point) {
	    if (!point) {
	    } else {
	      map.setCenter(point, 13);
	      var marker = new GMarker(point);
	      map.addOverlay(marker);
	      marker.openInfoWindowHtml(address);
	    }
	  }
	);
    }
    function setOpacity(obj, opacity) {
      opacity = (opacity == 100)?99.999:opacity;  
      // IE/Win
      obj.style.filter = "alpha(opacity:"+opacity+")";
      // Safari<1.2, Konqueror
      obj.style.KHTMLOpacity = opacity/100;
      // Older Mozilla and Firefox
      obj.style.MozOpacity = opacity/100;
      // Safari 1.2, newer Firefox and Mozilla, CSS3
      obj.style.opacity = opacity/100;
    }
    function fadeIn(objId,opacity) {
      if (document.getElementById) {
	obj = document.getElementById(objId);
	if (opacity <= 75) {
	  setOpacity(obj,opacity);
	  opacity += 5;
	  window.setTimeout("fadeIn('"+objId+"',"+opacity+")",100);
	}
      }
    }
    function fadeOut(objId,opacity) {
      if (document.getElementById) {
        obj = document.getElementById(objId);
        if (opacity >= 0) {
          setOpacity(obj, opacity);
          opacity -= 10;
          window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
        } else {
	  setOpacity(obj,0);
	  document.getElementById('loadingMessage').style.left = "-1000px";
	}
      }
    }
