var centerLatitude = 42.228;
var centerLongitude = -83.90;
var startZoom = 10;


var map;
// Create our "tiny" marker icon 
var baseIcon = new GIcon();
baseIcon.image = "redmarker.png";
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(16, 32);
baseIcon.infoWindowAnchor = new GPoint(16, 1);
baseIcon.transparent = "mm_20_transparent.png";

var flagIcon = new GIcon();
flagIcon.image = "redflag.png";
flagIcon.shadow = "flagshadow.png";
flagIcon.iconSize = new GSize(32, 32);
flagIcon.shadowSize = new GSize(59, 32);
flagIcon.iconAnchor = new GPoint(16, 32);
flagIcon.infoWindowAnchor = new GPoint(16,0);
flagIcon.transparent = "transflag.png";

var starIcon = new GIcon();
starIcon.image = "http://maps.google.com/mapfiles/kml/pal4/icon47.png";
starIcon.shadow = "http://maps.google.com/mapfiles/kml/pal4/icon47s.png";
starIcon.iconSize = new GSize(32, 32);
starIcon.shadowSize = new GSize(56, 32);
starIcon.iconAnchor = new GPoint(16, 32);
starIcon.infoWindowAnchor = new GPoint(16,0);
starIcon.transparent = "transflag.png";

var income = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/redmarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var incomeresidential = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/redmarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var industrial = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/orangemarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var business = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/yellowmarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var vacantsale = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/greenmarker.png", null, "http://www.google.com/mapfiles/shadow50.png");      

var office = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/bluemarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var retail = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/purplemarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

var white = new GIcon(baseIcon, "http://www.swishercommercial.com/pix/whitemarker.png", null, "http://www.google.com/mapfiles/shadow50.png");

//flags
var incomepending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/redflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var incomeresidentialpending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/redflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var industrialpending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/orangeflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var businesspending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/yellowflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var vacantsalepending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/greenflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var officepending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/blueflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var retailpending = new GIcon(flagIcon, "http://www.swishercommercial.com/pix/purpleflag.png", null, "http://www.swishercommercial.com/flagshadow.png");

var goldstar = new GIcon(starIcon, "http://maps.google.com/mapfiles/kml/pal4/icon47.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon47s.png");


var tmarker; //global temp marker
var gmarkers=[]; //global

function addMarker(latitude, longitude, listingid, propertytype) {

  var marker = new GMarker(new GLatLng(latitude, longitude),eval(propertytype));
  marker.type=propertytype;
  gmarkers.push(marker); //needed to store markers for turning on/off
  GEvent.addListener(marker, 'click',
      function() {
      //first we clear propinfo
      document.getElementById('propinfo').innerHTML='';
      
      //now we use ajax to get new propinfo
      sndReqArg('getPropertyInfo',listingid)
      //show('propinfo');

     tmarker=marker; //set local marker equal to global temp marker so we can access it later.
     //alert(listingid);
     setTimeout("tmarker.openInfoWindowHtml(document.getElementById('propinfo').innerHTML)",1000);
     /*************************************************************************************************
     //firefox bug: if we run the function below right away, it pulls outdated information
     //from DOM.  Firefox apparently only updates its DOM after control is returned from
     //a running function.  So instead we run the function via timeout so it actually
     //runs after control is returned to the browser
     **************************************************************************************************/
     //marker.openInfoWindowHtml(document.getElementById('propinfo').innerHTML);
      }
      );

  map.addOverlay(marker);
}

function init() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

    for(id in markers) {
      addMarker(markers[id].latitude, markers[id].longitude, markers[id].listingid, markers[id].propertytype);
    }
  }
}

window.onload = init;
window.onunload = GUnload;

