var gmarkers = [];
var htmlside = [];
var mapmarkers = [];			
var transitMarkers = [];
var map;
var windowIsOpen;
//////////////////////////////////////
/// this is our basic map loader
///////////////////////////////////////
function mapload() {
	resizeMap(); // set our map div to the correct size
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.addControl(new GOverviewMapControl());
		var Latitude = document.getElementById('Latitude').value;
		var Longitude = document.getElementById('Longitude').value;		
		var InitZoom = document.getElementById('InitZoom').value;
		// we're using this to figure out if we want comparisons (seller) 
		// or favorites (buyer)
		// frankly, we could do featured this way too.
		var InitPropertyType = document.getElementById('PropertyType').value;
		// the zoom is not working right for some reason.  we'll do math
		// on it and make it a number
		var zoomToSet = 19 - InitZoom;

		// now set the map coords.
		map.setCenter(new GLatLng(Latitude,Longitude), zoomToSet);		

		findProperties(map, InitPropertyType);
   	 }

}
//////////////////////////////////////
/// this is the main Ajax property fetcher
//////////////////////////////////////
function findProperties(map,InitPropertyType) {

	resizeMap();			

	// ===== Start with an empty GLatLngBounds object =====     
    var bounds = new GLatLngBounds();

	// start up the Ajax
	var request = GXmlHttp.create();
	/// warn people to be patient...
	document.getElementById("pleasewait").innerHTML = "Please Wait...";

 	var client_id = document.getElementById("client_id").value;
 	
 	var url;
 	
 	if (InitPropertyType == 'favorites') {
 		url = '/getFavorites.cgi?client_id=' + client_id;
 	}
 	else if (InitPropertyType == 'comparisons') {
 		url = '/getComparisons.cgi?client_id=' + client_id; 
 	}
 	else if (InitPropertyType == 'featured') {
 		url = '/getFeatured.cgi?client_id=' + client_id; 
 	}
 	
	var bounds = new GLatLngBounds();

	request.open("GET", url, true);
	
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			//our markers length will match our other taglengths.
			var markers = [];
			markers = xmlDoc.documentElement.getElementsByTagName("marker");
			if (markers.length < 1) {
				document.getElementById("pleasewait").innerHTML = 'No Properties Meet Your Criteria';
				clearOldOverlays();
				document.getElementById("mapsidelist").innerHTML = '';				
				return;
			}
			//if (markers.length > 1) {			
		 	clearOldOverlays();
		 	//}
			
			gmarkers = [];
			htmlside = [];
			mapmarkers = [];
			document.getElementById("mapsidelist").innerHTML = " ";
			var sidelisthtml = '<table cellpadding="0" cellspacing="0" border="0" class="mapsidelist">';
 			
			for (var i = 0; i < markers.length; i++) {
				//alert("doing marker number " + i);
				var listingid = markers[i].getAttribute("listingid");
				var longi = parseFloat(markers[i].getAttribute("lng"));
				var lati = parseFloat(markers[i].getAttribute("lat"));
				var icontype = markers[i].getAttribute("icontype"); // transit has transit.
				var addressx = markers[i].getAttribute("address"); // has station things
				var address = addressx.replace(/%26/, "&");				
				var citystate = markers[i].getAttribute("citystate"); // has station things
				var client_id = markers[i].getAttribute("client_id");
				var beds = markers[i].getAttribute("beds");
				var baths = markers[i].getAttribute("baths");
				var PictureURLx = markers[i].getAttribute("picture"); // has transit lines
				var PictureURLy = PictureURLx.replace(/%3F/g, "?");
				var PictureURL = PictureURLy.replace(/%26/g, "&");		
				var listprice = markers[i].getAttribute("listprice"); // has station name
				var iconcolor = markers[i].getAttribute("iconcolor");
				var lotsize = markers[i].getAttribute("lotsize");
				var sfootage = markers[i].getAttribute("sfootage");
				var listingbroker = markers[i].getAttribute("listingbroker");
				var iconWidth = markers[i].getAttribute("iwidth");
				var iconHeight = markers[i].getAttribute("iheight");
				var iconURL = markers[i].getAttribute("iconurl");
				var listingarea = markers[i].getAttribute("listingarea");
				var icon = getMyIcon(icontype,iconcolor,iconURL,iconWidth,iconHeight);

				/////////////////////////////////
				/// Now that we know all this stuff, go make a marker
				/////////////////////////////////				
				var marker;
				marker = makeMarker(longi,lati,i,listingid,map,icon,icontype,iconcolor,address,citystate,client_id,beds,baths,PictureURL,listprice,listingbroker,lotsize,sfootage,InitPropertyType,listingarea);
			 	////////////////////////////////////////////////////////
			 	// add marker to the array so that we can remove it later
			 	////////////////////////////////////////////////////////			 	
	 			mapmarkers.push(marker);				
				////////////////////////////////////////////////////////
				// add the marker to the map
				////////////////////////////////////////////////////////
			 	map.addOverlay(marker);
			 	////////////////////////////////////////////////////////
			 	// extend our boundaries
			 	////////////////////////////////////////////////////////			 	
				bounds.extend(new GLatLng(lati, longi));
				//alert("done adding overlay for marker number " + i);			
	        	/////////////////////////////////////////////////////////
				//  do the sidelist item
				////////////////////////////////////////////////////////
				var thisside = getSidelist(marker,listprice,listingarea);
				sidelisthtml = sidelisthtml + thisside;
			}
			
			////////////////////////////////////////////////////////
			// do points of interest.  we already have the markers
			// they are called poimarkers.
			////////////////////////////////////////////////////////
			markers = xmlDoc.documentElement.getElementsByTagName("poimarker");
			if (markers.length > 0) {
				for (var i = 0; i < markers.length; i++) {
					//alert("doing marker number " + i);
					var poiname = markers[i].getAttribute("listingid");
					var longi = parseFloat(markers[i].getAttribute("lng"));
					var lati = parseFloat(markers[i].getAttribute("lat"));
					var icontype = markers[i].getAttribute("poitype");
					var name = markers[i].getAttribute("poiname");
					var iconWidth = markers[i].getAttribute("iwidth");
					var iconHeight = markers[i].getAttribute("iheight");
					var iconURL = markers[i].getAttribute("iconurl");
					var shadowFile = markers[i].getAttribute("shadowimage");
					var shadowWidth = markers[i].getAttribute("shadoww");				
					var shadowHeight = markers[i].getAttribute("shadowh");								
					var icon = makeMyPoiIcon(icontype,iconcolor,iconURL,iconWidth,iconHeight,shadowFile,shadowWidth, shadowHeight);
					
					/////////////////////////////////
					/// Now that we know all this stuff, go make a POI marker
					/////////////////////////////////				
					var marker;
					marker = makePoiMarker(longi,lati,i,map,icon,icontype,name);
					////////////////////////////////////////////////////////
					// add marker to the array so that we can remove it later
					////////////////////////////////////////////////////////			 	
					mapmarkers.push(marker);				
					////////////////////////////////////////////////////////
					// add the marker to the map
					////////////////////////////////////////////////////////
					map.addOverlay(marker);
				}
			}				
			/// complete the sidebar
			sidelisthtml = sidelisthtml + "<\/table>";
			/// put up the markers.
			// add the sidebar.
			document.getElementById("mapsidelist").innerHTML = sidelisthtml;				

			// this reads the stuff for the legend.  we're going to have
			// to just deal with this once.
			var legendcolors = [];
			legendcolors = xmlDoc.documentElement.getElementsByTagName("legendtext");
			document.getElementById("gray").innerHTML = legendcolors[0].getAttribute("gray");
			document.getElementById("richerblue").innerHTML = legendcolors[0].getAttribute("richerblue");			
			document.getElementById("bluegreen").innerHTML = legendcolors[0].getAttribute("bluegreen");						
			document.getElementById("green").innerHTML = legendcolors[0].getAttribute("green");
			document.getElementById("yellowgreen").innerHTML = legendcolors[0].getAttribute("yellowgreen");
			document.getElementById("yellow").innerHTML = legendcolors[0].getAttribute("yellow");							
			document.getElementById("orange").innerHTML = legendcolors[0].getAttribute("orange");
			document.getElementById("red").innerHTML = legendcolors[0].getAttribute("red");

			var lngCenter = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
			var latCenter = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
			var center = new GLatLng(latCenter,lngCenter);
			var zoomBounds = map.getBoundsZoomLevel(bounds);
			var adjustedZoom;
// 			if (InitPropertyType != 'featured') {
// 				adjustedZoom = zoomBounds - 1;
// 			}
// 			else {
				adjustedZoom = zoomBounds;
// 			}
			map.setCenter(center, adjustedZoom);			

			if (InitPropertyType == 'favorites') {
				document.getElementById("usermessage").innerHTML = "Not all your favorites may appear on the map.  We do not have the Latitude and Longitude for every listing in our database.";
			}				

			if (InitPropertyType == 'comparisons') {
				listblurb = xmlDoc.documentElement.getElementsByTagName("listingblurb");
				document.getElementById("usermessage").innerHTML = listblurb[0].getAttribute("blurbtext");				
			}
			// more listings exist?
			//document.getElementById("moreexist").innerHTML = listblurb[0].getAttribute("blurbtext");			

			/// done with setting the legend.
			document.getElementById("pleasewait").innerHTML = "&nbsp;";
			
			

		}	
	}
    request.send(null); 
}
function getSidelist(marker,listprice,listingarea) {

	var color = "";
	var iconcolor = marker.iconcolor;
	var beds = marker.beds;
	var baths = marker.baths;
	var number = marker.id;
	var icontype = marker.icontype;
	var address = marker.address;
	var html = marker.html;	
	var buckstoggle = marker.reciprocity;

	if (iconcolor == 'gray') {	color = "#B0B0B0"; }
	else if (iconcolor == 'bluegreen') { color = "#75CBCA"; }
	else if (iconcolor == 'slateblue') { color = "#7D87C4"; }
	else if (iconcolor == 'richerblue') { color = "#0067B2"; }
	else if (iconcolor == 'green') { color = "#3BAE46"; }
	else if (iconcolor == 'yellowgreen') {	color = "#C7EE57"; }
	else if (iconcolor == 'yellow') {	color = "#FFF800"; }
	else if (iconcolor == 'orange') {	color = "#FF951B"; }
	else if (iconcolor == 'red') {	color = "#F20704"; }	
	
	if (icontype == 'LOT' || icontype == 'COM') {
		beds = "0";
		baths = "0";
	}
	
	var sidebarhtml;

	if (buckstoggle == 2) {
		sidebarhtml = '<tr><td width="11" height="11" bgcolor="' + color + 	'" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">' + 	beds + "BR " + baths + "BA " + icontype + '</a> ' + address + ', ' + listingarea + '</nobr></td></tr>';		
	}
	else if (buckstoggle == 1) {
		// bucks county
//		alert("we're doing bucks for icon type :  " + icontype);
		if (icontype == 'transit') {
			// adjust sidebar html for icon.
			// alert("sidebar for a transit item");
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="black" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td width="25" height="20" bgcolor="white" class="propertydetails"><pre>     </pre></td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">Transit '  + listprice + '</a></nobr></td></tr>';
		}
		else {
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="' + color + 	'" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">' + 	beds + "BR " + baths + "BA " + icontype + '</a> ' + address + '</nobr></td></tr>';	
			}
			
	}
	else {
		if (icontype == 'APT' && baths > 0) {
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="' + color + 	'" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">' + 	beds + "BR " + baths + "BA " + " condo" + '</a> ' + address + '</nobr></td></tr>';
		}
		else if (icontype == 'APT') {
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="' + color + 	'" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">' + 	beds + "BR " + baths + "BA " + " income" + '</a> ' + address + '</nobr></td></tr>';
		}
		else if (icontype == 'transit') {
			// adjust sidebar html for icon.
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="black" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">Transit '  + listprice + '</a></nobr></td></tr>';
		}
		else {
			sidebarhtml = '<tr><td width="11" height="11" bgcolor="' + color + 	'" class="propertydetails">&nbsp;&nbsp;&nbsp;</td><td class="propertydetails" id="prop' + number + '" bgcolor="#FFFFFF"><nobr>' + 	'<a href="javascript:myclick(' + number + ')">' + 	beds + "BR " + baths + "BA " + icontype + '</a> ' + address + '</nobr></td></tr>';
		}
	}

	gmarkers[number] = marker;
	htmlside[number] = html;
	
	return sidebarhtml;

}
function myclick(i) {
	//alert("opening pop-up " + i);
	gmarkers[i].openInfoWindowHtml(htmlside[i]); 
}
function highlightSidebar(propid) {
	var sideListItem = document.getElementById(propid);
	var testcolor = sideListItem.getAttribute("bgcolor")
	sideListItem.setAttribute("bgcolor") = "#F5F5F5";
	testcolor = sideListItem.getAttribute("bgcolor")
}

function makeMarker(longitude,latitude,number,listingid,map,icon,icontype,iconcolor,address,citystate,client_id,beds,baths,PictureURL,listprice,listingbroker,lotsize,sfootage,InitPropertyType,listingarea) {

	 var point = new GLatLng(latitude, longitude);  // create the point
  	 var marker = new GMarker(point,icon);        // create our marker

	 var favoriteslink;

	if (client_id == 1 || client_id < 1 || InitPropertyType == 'favorites') {
		//not a real client
		favoriteslink = "";
	}
	else {
		favoriteslink = "<a href=javascript:addFavorite('" + listingid + "&client_id=" + client_id + "')>add to favorites</a>";
	}
  	
  	// html for popup.  this is so so ugly.
	 var html;
	
	if (listingarea != null) {
		html = '<html><head><title></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td><img align=left src="' + PictureURL + '" width=96 height=64 /></td><td><b>' + listprice + "</b><br /><a href=detail/" + listingid + ' target=_new class="gmaphead">Details</a><br /><nobr>' + address + "</nobr><br /><nobr>" + listingarea + "</nobr><br /><nobr>" + citystate + "</nobr><br />Lot Size: " + lotsize + "<br>" + favoriteslink + '</td></tr></table>';
		marker.reciprocity = 2;
	}
	else if (listingbroker != null) {
		if (icontype != 'lot') {
			html = '<html><head><title></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td><img align=left src="' + PictureURL + '" width=96 height=64 /></td><td><b>' + listprice + "</b><br /><a href=detail/" + listingid + ' target=_new class="gmaphead">Details</a><br /><nobr>' + address + "</nobr><br /><nobr>" + citystate + '</nobr><br /><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td>Beds: ' + beds + "</td><td>SqFt: " + sfootage + "</td></tr><tr><td>Baths: " + baths + "</td><td>Lot Size: " + lotsize + "</td></tr></table><br \>" + favoriteslink + '<br /><nobr><span class="tinydisclaimer">Listing Provided Courtesy Of</span>: ' + listingbroker + '</nobr><img src="/local/MLS_Logo.jpg" border=0></td></tr></table>';
		}
		else {
			html = '<html><head><title></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td><img align=left src="' + PictureURL + '" width=96 height=64 /></td><td><b>' + listprice + "</b><br /><a href=detail/" + listingid + ' target=_new class="gmaphead">Details</a><br /><nobr>' + address + "</nobr><br /><nobr>" + citystate + "</nobr><br />Lot Size: " + lotsize + "<br>" + favoriteslink + '<br /><nobr><span class="tinydisclaimer">Listing Provided Courtesy Of</span>: ' + listingbroker + '</nobr><br><img src="/local/MLS_Logo.jpg" border=0></td></tr></table>';	 
		 }
		 // this is used by the sidebar html as a bucks toggle
		 marker.reciprocity = 1;
	}	 
		
	else {
	 	if (icontype != 'lot') {
			html = '<html><head><title></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td><img align=left src="' + PictureURL + '" width=96 height=64 /></td><td><b>' + listprice + "</b><br /><a href=detail/" + listingid + ' target=_new class="gmaphead">Details</a><br /><nobr>' + address + "</nobr><br /><nobr>" + citystate + '</nobr><br /><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td>Beds: ' + beds + "</td><td>SqFt: " + sfootage + " </td></tr><tr><td>Baths: " + baths + "</td><td>Lot Size: " + lotsize + " </td></tr></table><br \>" +  favoriteslink + "</td></tr></table>";
		}
		else {
			html = '<html><head><title></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body><table width=200 bgcolor="#FFFFFF" class="gmap"><tr><td><img align=left src="' + PictureURL + '" width=96 height=64 /></td><td><b>' + listprice + "</b><br /><a href=detail/" + listingid + ' target=_new class="gmaphead">Details</a><br /><nobr>' + address + "</nobr><br /><nobr>" + citystate + "</nobr><br />Lot Size: " + lotsize + "<br>" + favoriteslink + "</td></tr></table>";	 
		}
	}
	 
	var sidelistid = String("prop" + number);
	var listen = GEvent.addListener(marker, "click", function()	{
		marker.openInfoWindowHtml(html);
		highlightSidebar(sidelistid);
	} );
	
	marker.x = longitude;
	marker.y = latitude;
	marker.id = number;
	marker.description = listen;
	marker.listingid; //doubt this does anything.  we'll leave it for luck.
	marker.address = address;
	marker.beds = beds;
	marker.baths = baths;
	marker.iconcolor = iconcolor;
	marker.html = html;
	marker.icontype = icontype;
	marker.lotsize = lotsize;
	marker.sfootage = sfootage;

	return marker;
	
}
function getMyIcon(icontype,iconcolor,iconURL,iconWidth,iconHeight) {

	var sfh = new GIcon();
	sfh.shadow = "http://static.envirian.com/map_icons3/SFH-shadow2.png";
	sfh.iconSize = new GSize(23, 25);
	sfh.shadowSize = new GSize(28, 25);
	sfh.iconAnchor = new GPoint(1, 28);
	sfh.infoWindowAnchor = new GPoint(14, 1);

	var townhouse = new GIcon();
	townhouse.shadow = "http://static.envirian.com/map_icons3/TNH-shadow2.png";
	townhouse.iconSize = new GSize(25, 39);
	townhouse.shadowSize = new GSize(41, 39);
	townhouse.iconAnchor = new GPoint(1, 29);
	townhouse.infoWindowAnchor = new GPoint(9, 1);	

	var apartment = new GIcon()		
	apartment.shadow = "http://static.envirian.com/map_icons3/APT-shadow2.png";
	apartment.iconSize = new GSize(21, 40);
	apartment.shadowSize = new GSize(42, 40);
	apartment.iconAnchor = new GPoint(1, 23);
	apartment.infoWindowAnchor = new GPoint(8, 1);

	var lot = new GIcon()
	lot.shadow = "http://static.envirian.com/map_icons3/09_LOT.png";
	lot.iconSize = new GSize(22, 16);
	lot.shadowSize = new GSize(22, 16);
	lot.iconAnchor = new GPoint(1, 23);
	lot.infoWindowAnchor = new GPoint(8, 1);

	var duplex = new GIcon()
	duplex.shadow = "http://static.envirian.com/map_icons3/DPX-shadow2.png";
	duplex.iconSize = new GSize(25, 39);
	duplex.shadowSize = new GSize(39, 39);
	duplex.iconAnchor = new GPoint(1, 23);
	duplex.infoWindowAnchor = new GPoint(8, 1);

	var multifamily = new GIcon()
	multifamily.shadow = "http://static.envirian.com/map_icons3/MFP-shadow2.png";
	multifamily.iconSize = new GSize(42, 26);
	multifamily.shadowSize = new GSize(42, 26);
	multifamily.iconAnchor = new GPoint(1, 23);
	multifamily.infoWindowAnchor = new GPoint(8, 1);

	var commercial = new GIcon()
	commercial.shadow = "http://static.envirian.com/map_icons3/COM-shadow2.png";
	commercial.iconSize = new GSize(26, 31);
	commercial.shadowSize = new GSize(39, 31);
	commercial.iconAnchor = new GPoint(1, 23);
	commercial.infoWindowAnchor = new GPoint(8, 1);

// 	var transit;	
// 	if (icontype == 'transit') {
// 		transit = new GIcon();
// 		transit.iconSize = new GSize(iconWidth, iconHeight);
// 		transit.iconAnchor = new GPoint(1, iconHeight);
// 		transit.image = iconURL;
// 		transit.infoWindowAnchor = new GPoint(8, 1);
// 	}

// 	'gray' #B0B0B0
// 	'purple' #A861A5
// 	'richerblue' #0067B2	3
// 	'slateblue' #7D87C4   4
// 	'green' #3BAE46    5
// 	'yellowgreen' #C7EE57
// 	'yellow' #FFF800
// 	'orangeyellow' #F2B701
// 	'orange' #FF951B
// 	'orangered' #FB4C00
// 	'red' #F20704
	
	var image;
//sfh
	if (icontype == 'SFH' && iconcolor == 'gray') {	sfh.image = "http://static.envirian.com/map_icons3/00_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'richerblue') {	sfh.image = "http://static.envirian.com/map_icons3/03_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'bluegreen') {	sfh.image = "http://static.envirian.com/map_icons3/04_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'green') { sfh.image = "http://static.envirian.com/map_icons3/05_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'yellowgreen') { sfh.image = "http://static.envirian.com/map_icons3/06_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'yellow') { sfh.image = "http://static.envirian.com/map_icons3/07_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'orange') { sfh.image = "http://static.envirian.com/map_icons3/09_SFH.png";}
	if (icontype == 'SFH' && iconcolor == 'red') { sfh.image = "http://static.envirian.com/map_icons3/11_SFH.png";}

//townhouse
	if (icontype == 'TNH' && iconcolor == 'gray') {	townhouse.image = "http://static.envirian.com/map_icons3/00_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'richerblue') {	townhouse.image = "http://static.envirian.com/map_icons3/03_TNH.png";}	
	if (icontype == 'TNH' && iconcolor == 'bluegreen') {	townhouse.image = "http://static.envirian.com/map_icons3/04_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'green') { townhouse.image = "http://static.envirian.com/map_icons3/05_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'yellowgreen') { townhouse.image = "http://static.envirian.com/map_icons3/06_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'yellow') { townhouse.image = "http://static.envirian.com/map_icons3/07_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'orange') { townhouse.image = "http://static.envirian.com/map_icons3/09_TNH.png";}
	if (icontype == 'TNH' && iconcolor == 'red') { townhouse.image = "http://static.envirian.com/map_icons3/11_TNH.png";}

//apartment
	if (icontype == 'APT' && iconcolor == 'gray') {	apartment.image = "http://static.envirian.com/map_icons3/00_APT.png";}
	if (icontype == 'APT' && iconcolor == 'richerblue') {	apartment.image = "http://static.envirian.com/map_icons3/03_APT.png";}	
	if (icontype == 'APT' && iconcolor == 'bluegreen') {	apartment.image = "http://static.envirian.com/map_icons3/04_APT.png";}
	if (icontype == 'APT' && iconcolor == 'green') { apartment.image = "http://static.envirian.com/map_icons3/05_APT.png";}
	if (icontype == 'APT' && iconcolor == 'yellowgreen') { apartment.image = "http://static.envirian.com/map_icons3/06_APT.png";}
	if (icontype == 'APT' && iconcolor == 'yellow') { apartment.image = "http://static.envirian.com/map_icons3/07_APT.png";}
	if (icontype == 'APT' && iconcolor == 'orange') { apartment.image = "http://static.envirian.com/map_icons3/09_APT.png";}
	if (icontype == 'APT' && iconcolor == 'red') { apartment.image = "http://static.envirian.com/map_icons3/11_APT.png";}

//commercial
	if (icontype == 'COM' && iconcolor == 'gray') {	commercial.image = "http://static.envirian.com/map_icons3/00_COM.png";}
	if (icontype == 'COM' && iconcolor == 'richerblue') {	commercial.image = "http://static.envirian.com/map_icons3/03_COM.png";}	
	if (icontype == 'COM' && iconcolor == 'bluegreen') {	commercial.image = "http://static.envirian.com/map_icons3/04_COM.png";}
	if (icontype == 'COM' && iconcolor == 'green') { commercial.image = "http://static.envirian.com/map_icons3/05_COM.png";}
	if (icontype == 'COM' && iconcolor == 'yellowgreen') { commercial.image = "http://static.envirian.com/map_icons3/06_COM.png";}
	if (icontype == 'COM' && iconcolor == 'yellow') { commercial.image = "http://static.envirian.com/map_icons3/07_COM.png";}
	if (icontype == 'COM' && iconcolor == 'orange') { commercial.image = "http://static.envirian.com/map_icons3/09_COM.png";}
	if (icontype == 'COM' && iconcolor == 'red') { commercial.image = "http://static.envirian.com/map_icons3/11_COM.png";}
//lots
	if (icontype == 'LOT' && iconcolor == 'gray') {	lot.image = "http://static.envirian.com/map_icons3/00_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'richerblue') {	lot.image = "http://static.envirian.com/map_icons3/03_LOT.png";}	
	if (icontype == 'LOT' && iconcolor == 'bluegreen') {	lot.image = "http://static.envirian.com/map_icons3/04_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'green') { lot.image = "http://static.envirian.com/map_icons3/05_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'yellowgreen') { lot.image = "http://static.envirian.com/map_icons3/06_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'yellow') { lot.image = "http://static.envirian.com/map_icons3/07_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'orange') { lot.image = "http://static.envirian.com/map_icons3/09_LOT.png";}
	if (icontype == 'LOT' && iconcolor == 'red') { lot.image = "http://static.envirian.com/map_icons3/11_LOT.png";}

// duplex
	if (icontype == 'DPX' && iconcolor == 'gray') {	duplex.image = "http://static.envirian.com/map_icons3/00_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'richerblue') {	duplex.image = "http://static.envirian.com/map_icons3/03_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'bluegreen') {	duplex.image = "http://static.envirian.com/map_icons3/04_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'green') { duplex.image = "http://static.envirian.com/map_icons3/05_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'yellowgreen') { duplex.image = "http://static.envirian.com/map_icons3/06_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'yellow') { duplex.image = "http://static.envirian.com/map_icons3/07_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'orange') { duplex.image = "http://static.envirian.com/map_icons3/09_DPX.png";}
	if (icontype == 'DPX' && iconcolor == 'red') { duplex.image = "http://static.envirian.com/map_icons3/11_DPX.png";}

// multifamily
	if (icontype == 'MFP' && iconcolor == 'gray') {	multifamily.image = "http://static.envirian.com/map_icons3/00_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'richerblue') {	multifamily.image = "http://static.envirian.com/map_icons3/03_MFP.png";}	
	if (icontype == 'MFP' && iconcolor == 'bluegreen') {	multifamily.image = "http://static.envirian.com/map_icons3/04_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'green') { multifamily.image = "http://static.envirian.com/map_icons3/05_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'yellowgreen') { multifamily.image = "http://static.envirian.com/map_icons3/06_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'yellow') { multifamily.image = "http://static.envirian.com/map_icons3/07_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'orange') { multifamily.image = "http://static.envirian.com/map_icons3/09_MFP.png";}
	if (icontype == 'MFP' && iconcolor == 'red') { multifamily.image = "http://static.envirian.com/map_icons3/11_MFP.png";}


// undefined types...
	if (icontype == '' && iconcolor == 'gray') {	sfh.image = "http://static.envirian.com/map_icons3/00_SFH.png";}
	if (icontype == '' && iconcolor == 'richerblue') {	sfh.image = "http://static.envirian.com/map_icons3/03_SFH.png";}	
	if (icontype == '' && iconcolor == 'bluegreen') {	sfh.image = "http://static.envirian.com/map_icons3/04_SFH.png";}
	if (icontype == '' && iconcolor == 'green') { sfh.image = "http://static.envirian.com/map_icons3/05_SFH.png";}
	if (icontype == '' && iconcolor == 'yellowgreen') { sfh.image = "http://static.envirian.com/map_icons3/06_SFH.png";}
	if (icontype == '' && iconcolor == 'yellow') { sfh.image = "http://static.envirian.com/map_icons3/07_SFH.png";}
	if (icontype == '' && iconcolor == 'orange') { sfh.image = "http://static.envirian.com/map_icons3/09_SFH.png";}
	if (icontype == '' && iconcolor == 'red') { sfh.image = "http://static.envirian.com/map_icons3/11_SFH.png";}

	// we've already made our transit icon...
	
	var icon;
	
	if (icontype == 'APT') {
		icon = apartment;
	}
	else if (icontype == 'TNH') {
		icon = townhouse;		
	}
	else if (icontype == "LOT") {
		icon = lot;		
	}
	else if (icontype == 'transit') {
		icon = transit;
		//alert("returning transit icon"); 		
	}
	else if (icontype == 'COM') {
		icon = commercial;
	}
	else if (icontype == 'DPX') {
		icon = duplex;
	}
	else if (icontype == 'MFP') {
		icon = multifamily;
	}
	else {
		icon = sfh;
	}
	
	return icon;
}
//////////////////////////////////////////////////////////////////////////////
// make a marker for a point of interest
//////////////////////////////////////////////////////////////////////////////
function makePoiMarker(longitude,latitude,number,map,icon,icontype,name) {

	 var point = new GLatLng(latitude, longitude);  // create the point
  	 var marker = new GMarker(point,icon);        // create our marker
	// html for popup.  this is so so ugly.
	 var html;
	 
	 if (icontype == 'Church') {
		 html = '<html><head><title name="' + icontype + '"></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body>' + '<table width="150" bgcolor="#FFFFFF" class="gmap"><tr><td><img src="' + icon.image + '"><td><b>' + icontype + '</b></td></tr><tr><td colspan="2"><nobr>' + name + '</nobr></td></tr></table>';	 
	 }
	 // else if (icontype == 'School') {
// 		 html = '<html><head><title name="' + icontype + '"></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body>' + '<table width="150" bgcolor="#FFFFFF" class="gmap"><tr><td><img src="' + icon.image + '"><td><b>' + icontype + '</b></td></tr><tr><td colspan="2"><nobr>' + name + '</nobr></td></tr></table>';	 
// 	 }
	 else {
		 html = '<html><head><title name="' + icontype + '"></title><link rel="stylesheet" type="text/css" href="blue.css" /></head><body>' + '<table width="150" bgcolor="#FFFFFF" class="gmap"><tr><td><img src="' + icon.image + '"><td><b>' + icontype + '</b></td></tr><tr><td colspan="2">' + name + '</td></tr></table>';
	}
	 
	var listen = GEvent.addListener(marker, "click", function()	{
		marker.openInfoWindowHtml(html);
	} );
	
	marker.x = longitude;
	marker.y = latitude;
	marker.id = number;
	marker.description = listen;
	marker.html = html;
	marker.icontype = icontype;
	marker.address = name;
	
	return marker;
}
//////////////////////////////////////////////////////////////////////////////
// make a point of interest icon
//////////////////////////////////////////////////////////////////////////////
function makeMyPoiIcon(icontype,iconcolor,iconURL,iconWidth,iconHeight,shadowFile,shadowWidth,shadowHeight) {

	var poiIcon = new GIcon();
	poiIcon.shadow = shadowFile;
	poiIcon.shadowSize = new GSize(shadowWidth,shadowHeight);
	poiIcon.iconSize = new GSize(iconWidth, iconHeight);
	poiIcon.iconAnchor = new GPoint(1, 1);
	poiIcon.infoWindowAnchor = new GPoint(1, 1);
	poiIcon.image = iconURL;
	
	return poiIcon;	
	
}
//////////////////////////////////////////////////////////////////////////////
//  getHeight()
//  get height of the browser window
//////////////////////////////////////////////////////////////////////////////

function getHeight() {
  var height = 0;
  if (self.innerHeight) {
    height = self.innerHeight; }
  else if (document.documentElement && document.documentElement.clientHeight) {
    height = document.documentElement.clientHeight; }
  else if (document.body) {
    height = document.body.clientHeight; }
  return height;
}

//////////////////////////////////////////////////////////////////////////////
//  getWidth()
//  get width of the browser window
//////////////////////////////////////////////////////////////////////////////

function getWidth() {
  var width = 0;
  if (self.innerWidth) {
    width = self.innerWidth; }
  else if (document.documentElement && document.documentElement.clientWidth) {
    width = document.documentElement.clientWidth; }
  else if (document.body) {
    width = document.body.clientWidth; }
  return width;
}
//////////////////////////////////////////////////////////////////////////////
//  resizeMap()
//  resize the map div to fill the window
//////////////////////////////////////////////////////////////////////////////

function resizeMap() {
  var width  = getWidth();
  var height = getHeight();
  document.getElementById('map').style.height = String(height - 75) + 'px';
  document.getElementById('map').style.width  = String(width - 450) + 'px';
}
//////////////////////////////////////////////////////////////////////////////
//
//  remove old overlays
//
//////////////////////////////////////////////////////////////////////////////

function clearOldOverlays() {
  var i;
  for (i=0; i < mapmarkers.length; i++) {
      map.removeOverlay(mapmarkers[i]);
  }
}

