  google.load("maps", "2", {"other_params":"sensor=false"});
  var map, miniMap, customMap;
  var markerOption = new Array(); 
  var mapPoint = new Array();
  
  function MapPoint () {
    this.categoryID = null;
	this.itemID = null;
	this.locationName = null;
	this.point = null;
    this.marker = null;
	this.isOnMap=false;
	this.infoHTML="This is HTML codes about this location.";	
  }  
  
  function initialize() {
    map = new google.maps.Map2(document.getElementById("container-map"));
	//miniMap = new google.maps.OverviewMapControl(new google.maps.Size(150, 150));
	centerMap();
	
	setCustomMap();
	
	map.addControl(new google.maps.LargeMapControl());
	map.addControl(new google.maps.ScaleControl());
	map.addControl(new google.maps.MapTypeControl());	
	//map.addControl(miniMap);
		
	// categories is an JSON object containing category ID and thumbtack image URL
	if (categories.CATEGORY.length == 0) return;
	//create markerOptions
	for (var i = 0; i < categories.CATEGORY.length; i++) {
	  var newIcon = new google.maps.Icon(G_DEFAULT_ICON);
	  newIcon.image = '/' + categories.CATEGORY[i].THUMBTACKIMAGE;
	  //newIcon.printImage = '/' + categories.CATEGORY[i].THUMBTACKIMAGE; //image for printing in IE; needs to be an gif with the same size
	  newIcon.iconSize = new google.maps.Size(15, 15);
	  newIcon.shadow="";
	  newIcon.iconAnchor= new google.maps.Point(8, 8);
	  newIcon.infoWindowAnchor= new google.maps.Point(8, 8);
	  newIcon.imageMap = [0,0, 15,0, 15,15, 0,15];
	  markerOption[i] = {icon:newIcon};
	}
	
	if (directory.LOCATION.length == 0) return;
	for (var i = 0; i < directory.LOCATION.length; i++) {
	  mapPoint[i] = new MapPoint();
	  mapPoint[i].categoryID=directory.LOCATION[i].CATEGORYID;
	  mapPoint[i].itemID=directory.LOCATION[i].ITEMID;
	  mapPoint[i].locationName=directory.LOCATION[i].LOCATIONNAME;
	  mapPoint[i].point = new google.maps.LatLng(directory.LOCATION[i].LAT, directory.LOCATION[i].LNG);	  
	  
	  var categoryID = directory.LOCATION[i].CATEGORYID;
	  var markerOpt = getMarkerOptionByCategoryID(categoryID);
	  
	  
	  var htmlCodes = '<div class="infoWindow"><div class="infoWindowTitle">' + directory.LOCATION[i].LOCATIONNAME + '</div>';
	  if (directory.LOCATION[i].THUMBNAILIMAGE != '') {
	    htmlCodes = htmlCodes + '<table><tr valign="top"><td width="90"><img src="' + directory.LOCATION[i].THUMBNAILIMAGE + '" class="thumbnail" align="left" style="max-width:80px;max-height:80px;">' +
					'</td><td>';
	  }	
	  
	  if (directory.LOCATION[i].ADDRESS != '')
	    htmlCodes = htmlCodes + '<b>Address: </b>' + directory.LOCATION[i].ADDRESS + '<br />';  
	  
	  if (directory.LOCATION[i].PHONE != '')
	    htmlCodes = htmlCodes + '<b>Phone: </b>' + directory.LOCATION[i].PHONE + '<br />';	
	  
	  
	  
	  if (directory.LOCATION[i].SHORTDESCRIPTION != '')
	    htmlCodes = htmlCodes + '<br />' + directory.LOCATION[i].SHORTDESCRIPTION + '<br />';	    
	  
	  htmlCodes = htmlCodes + '<p align="right"><a href="index.cfm?md=directory&tmp=detail&itemID=' + directory.LOCATION[i].ITEMID + '"><b>&raquo; More Info</b></a></p>';
	  
	  if (directory.LOCATION[i].THUMBNAILIMAGE != '') {
	    htmlCodes = htmlCodes + '</td></tr></table>';
	  }
	  
	  htmlCodes = htmlCodes + '</div>';
	  
	  
	  
	  mapPoint[i].infoHTML = htmlCodes;
	  mapPoint[i].marker=createMarker(mapPoint[i].point, markerOpt, htmlCodes);
	}
	uncheckAllCategories();
	//addPointsToMapByCategoryID(parkingGarageCategoryID);
  }
  
  function createMarker(point, markerOpt, htmlCodes) {
	var marker = new google.maps.Marker(point, markerOpt);
	google.maps.Event.addListener(marker, "click", function() {
	  map.openInfoWindowHtml(point, htmlCodes);
	});
	return marker;
  }  
  
  function setCustomMap() {
	//sets the copyrights for our mapn
	var copyright = new google.maps.Copyright(1,
			new google.maps.LatLngBounds(new google.maps.LatLng(30.43835107883601, -91.19909763336182),
				new google.maps.LatLng(30.474419917301873, -91.1749792098999)), 15, "Downtown Development District");
	var copyrightCollection = new google.maps.CopyrightCollection('');
	copyrightCollection.addCopyright(copyright);

	//sets the tiles for our overlay
	var gtl = new GTileLayer(copyrightCollection,14,19);	
	gtl.getTileUrl=function(a,b){
		var r = 1; var c = 1;
		if (b==15 && a.x>=8082 && a.x<=8085 && a.y>=13469 && a.y<= 13473) {
		  r = a.y - 13469 + 1;
		  c = a.x - 8082 + 1;		
		  return "images/gmap/z15/tile_x8082_y13469_z15_r" + r + "_c" + c + ".gif";
		} else {
		  return "http://www.google.com/mapfiles/transparent.gif";
		}
		/*
		if (b==14 && a.x>=4041 && a.x<=4042 && a.y>=6734 && a.y<= 6736) {
		  r = a.y - 6734 + 1;
		  c = a.x - 4041 + 1;		
		  return "images/gmap/z14/tile_x4041_y6734_z14_r" + r + "_c" + c + ".gif";
		} else if (b==15 && a.x>=8082 && a.x<=8085 && a.y>=13469 && a.y<= 13473) {
		  r = a.y - 13469 + 1;
		  c = a.x - 8082 + 1;		
		  return "images/gmap/z15/tile_x8082_y13469_z15_r" + r + "_c" + c + ".gif";
		} if (b==16 && a.x>=16165 && a.x<=16170 && a.y>=26938 && a.y<= 26946) {
		  r = a.y - 26938 + 1;
		  c = a.x - 16165 + 1;
		  return "images/gmap/z16/tile_x16165_y26938_z16_r" + r + "_c" + c + ".gif";
		} else {
		  return "http://www.google.com/mapfiles/transparent.gif";
		}
		*/
	}
	
	//adds the custom map with our tiles over the regular map
	customMap = new GMapType([G_NORMAL_MAP.getTileLayers()[0], gtl],
			 G_NORMAL_MAP.getProjection(), 'DDD',{errorMessage:"Out of bounds"});

	customMap.getMaximumResolution = function() {
		return 19;
	}

	map.addMapType(customMap);
	map.setMapType(customMap);
  }
  
  function addLocationToDisplayByItemID(itemID) {
    for (var i = 0; i < directory.LOCATION.length; i++) {  
	  if (directory.LOCATION[i].ITEMID == itemID) {
		addLocationToDisplay(i);
		return;
	  }
	}  
  }
  
  function addLocationsToDisplayByKeyword(keyword) {
	if (keyword.length <= 2) return;
	var found = false;
	var re = new RegExp(keyword, "gi");	
	for (var i = 0; i < hintWords.LOCATION.length; i++) {
	  if (re.test(hintWords.LOCATION[i].text)/*|| re.test(directory.LOCATION[i].SHORTDESCRIPTION)*/) {
		addLocationToDisplayByItemID(hintWords.LOCATION[i].ITEMID);
		found = true;
	  }
	}	
	
	if (!found) {
	  alert('Your search for "' + keyword + '" returned no results.');	
	}
  }
  
  function addLocationsToDisplayByCategoryID (categoryID) {
	var htmlCodes = '<div id="category-' + categoryID + '" class="panel-results">';
	
	for (var i = 0; i < categories.CATEGORY.length; i++) {
	  if (categories.CATEGORY[i].CATEGORYID == categoryID) {
	    htmlCodes = htmlCodes + '<div class="category-name">' + categories.CATEGORY[i].CATEGORYNAME + '</div>';
	  }
	}
	
	for (var i = 0; i < mapPoint.length; i++) {
	  if (mapPoint[i].categoryID == categoryID) {
		htmlCodes = htmlCodes + '<div class="item"><a href="javascript:openInfoWindowByItemID(' + mapPoint[i].itemID + ')">' + mapPoint[i].locationName + '</a></div>';
	  }
	}
	
	htmlCodes = htmlCodes + '</div>';
	$("#container-results").prepend(htmlCodes);
  }
  
  function removeLocationsFromDisplayByCategoryID (categoryID) {
    $("#category-" + categoryID).remove();
  }
  
  function addLocationToDisplay(i) {
	var htmlCodes = '<div class="panel-results"><a href="javascript:openInfoWindowByItemID(' + directory.LOCATION[i].ITEMID + ')">' + directory.LOCATION[i].LOCATIONNAME + '</a></div>';
	$("#container-results").prepend(htmlCodes);
  }
  
  function addPointToMapByItemID(itemID, openInfoWindow) {
	for (var i = 0; i < directory.LOCATION.length; i++) {  
	  if (directory.LOCATION[i].ITEMID == itemID) {
		addPointToMap(i);
		if (openInfoWindow) map.openInfoWindowHtml(mapPoint[i].point, mapPoint[i].infoHTML);
		return;
	  }
	}
  } 
  
  function addPointsToMapByKeyword(keyword, openInfoWindow) {
	if (keyword.length <= 2) {
	  alert("Please enter a keyword with at least three characters.");
	  return;
	}
	var isFound = false;
	var re = new RegExp(keyword, "gi");	
	
	for (var i = 0; i < directory.LOCATION.length; i++) {
	  if (re.test(directory.LOCATION[i].LOCATIONNAME)/*|| re.test(directory.LOCATION[i].SHORTDESCRIPTION)*/) {
		if (!isFound && openInfoWindow) {
		  addPointToMapByItemID(directory.LOCATION[i].ITEMID, true);
		} else {
		  addPointToMap(i);
		}
		isFound = true;
	  }
	}	
  }
    
  function addPointToMap(i) {
    if (mapPoint[i].isOnMap) return;
	map.addOverlay(mapPoint[i].marker); 
    mapPoint[i].isOnMap=true;
  }
  
  function addPointsToMapByCategoryID(categoryID) {
    for (var i = 0; i < mapPoint.length; i++) {
	  if (mapPoint[i].categoryID == categoryID) addPointToMap(i);
	}
  }
  
  function removePointFromMap(i) {
    if (!mapPoint[i].isOnMap) return;
	map.removeOverlay(mapPoint[i].marker);
    mapPoint[i].isOnMap = false;
  }
  
  function removePointsFromMapByCategoryID(categoryID) {
    for (var i = 0; i < mapPoint.length; i++) {
	  if (mapPoint[i].categoryID == categoryID) removePointFromMap(i);
	}
  }
  
  function getMarkerOptionByCategoryID(categoryID) {
	for (var i = 0; i < categories.CATEGORY.length; i++) {
	  if (categories.CATEGORY[i].CATEGORYID == categoryID) 
	    return markerOption[i];
	}
	return markerOption[0];
  }
  
  function openInfoWindowByItemID(itemID) {
	for (var i = 0; i < directory.LOCATION.length; i++) {  
	  if (directory.LOCATION[i].ITEMID == itemID) {
		map.openInfoWindowHtml(mapPoint[i].point, mapPoint[i].infoHTML);
		return;
	  }
	}
  }

  google.setOnLoadCallback(initialize);
  
  window.onunload= function () {google.maps.Unload();}
  
  function centerMap() {
    map.setCenter(new google.maps.LatLng(30.45088420161139, -91.18775725364685), 15);
	$("#container-results").html("");
  }
  
  function resetMap() {
    for (var i = 0; i < directory.LOCATION.length; i++) removePointFromMap(i);
	map.closeInfoWindow();
	map.setMapType(customMap);	
	centerMap();
	uncheckAllCategories();
	//addPointsToMapByCategoryID(parkingGarageCategoryID);
  }
  
  function uncheckAllCategories() {
    var form = document.frmSearch;
	for (var i = 0; i < form.categoryID.length; i++) form.categoryID[i].checked=false;
  }
 