// Parameters (mapParms) object
function Parameters() {
   this.trail = "";
   this.trail_id = 0;
   this.zoom = 0;
   this.defaultZoom = 9;
   this.midLevel = 10;
   this.lat = null;
   this.lon = null;
   this.vw = 0;
   this.mark_id = 0;
   this.markertype = 0;
   this.entry_id = 0;
   this.event_id = 0;
   this.simple = 0;
   this.edit = 0;
   this.dist = 0;
   this.dmode = 0;
   this.print = 0;
   this.sv = 0;
   this.url_viewJournal = "/journal/viewJournal.php?";
   this.url_getTraceData = "/gmap/getTraceData.php?";
   this.url_getMarkerData = "/gmap/getMarkerData.php?";
   this.url_viewMarkerData = "/gmap/viewMarkerData.php?";
   this.url_updateMarkerData = "/gmap/updateMarkerData.php?";


   this.updateBookmark = function () {
      if(!gmap.isLoaded()) return false;

      var url = 'http://postholer.com/gmap/gmap.php?trail_id=' + mapParms.trail_id + '&';
      var pt = gmap.getCenter();
      var lat = parseFloat(pt.lat());
      var lon = parseFloat(pt.lng());
      url += 'lat=' + lat.toFixed(6) + '&lon=' + lon.toFixed(6) + '&';
      url += 'zoom=' + gmap.getZoom() + '&';
      url += 'dist=' + mapParms.dist + '&';
   
      if(document.mapForm) {
         var val = document.mapForm.markertypes.value
         if(val > 0) url += 'markertype=' + mapParms.markertype + '&';
      }
   
      if(mapParms.event_id > 0 && mapParms.entry_id <= 0) url += 'event_id=' + mapParms.event_id + '&';
      if(mapParms.entry_id > 0) url += 'entry_id=' + mapParms.entry_id + '&';
   
      var mapTypes = gmap.getMapTypes();
      var curType = gmap.getCurrentMapType();
      var curName = curType.getName();
      for(k = 0, vvw = 0; k < mapTypes.length; k++) {
         var s1 = mapTypes[k].getName();
         if(s1 == curName) vvw = k;
      }
      url += 'vw=' + vvw;
   
      var bm = document.getElementById("bookmark");
      if(bm) bm.href = url;
   
   };
}

// MarkerData (markers.markers array ) object
function MarkerData() {
   this.mark_id = 0;
   this.mark_lat = null;
   this.mark_lon = null;
   this.mark_desc = '';
   this.mark_act = 1;
   this.mark_spot = 0;
   this.marker = null;
}

// Markers (markers) object
function Markers(opt_labelDiv) {
   this.labelDiv = opt_labelDiv || null;

   this.centerLat = null;
   this.centerLon = null;
   this.centerZoom = 10;
   this.markertype = 0;
   this.markertype_img_url = "";
   this.spot_img_url = "";
   this.trail_id = 0;
   this.markers = new Array();
   this.editMarker = null;

   this.getDataUrl = function () { 
      var ret = mapParms.url_getMarkerData;
      if(mapParms.trail_id) ret += 'trail_id=' + parseInt(mapParms.trail_id) + "&";
      if(mapParms.entry_id > 0) ret += 'entry_id=' + mapParms.entry_id + "&";
      if(mapParms.event_id > 0) ret += 'event_id=' + mapParms.event_id + "&";
      if(mapParms.mark_id > 0) ret += 'mark_id=' + mapParms.mark_id + "&";
      if(mapParms.markertype > 0) ret += 'markertype=' + mapParms.markertype + "&";
      return ret;
   };

   this.show = function () { 
      if(!gmap || !gmap.isLoaded()) return 0;
      this.hide();
      for(i = 0; i < this.markers.length; i++)
         if(this.markers[i].marker)
             gmap.addOverlay(this.markers[i].marker);
   };

   this.hide = function () { 
      if(!gmap || !gmap.isLoaded()) return 0;
      for(i = 0; i < this.markers.length; i++)
         if(this.markers[i].marker)
             gmap.removeOverlay(this.markers[i].marker);
   };

   this.reset = function () { 
      this.hide();
      this.markers.length = 0;
   };


   this.data = function (url) { 
      if(!markers) return 0;
      this.reset();
      markers.centerLat = null;
   
      GDownloadUrl(url, function(data, responseCode) {
         if(responseCode == 200) {
            var xmlDoc = GXml.parse(data);
            var opts = null;
            if(xmlDoc && (opts = xmlDoc.getElementsByTagName("mark"))) {
               var info = xmlDoc.getElementsByTagName("info") || null;
               if(info[0]) {
                  if((tid = parseInt(info[0].getAttribute("trail_id"))) > 0)
                     mapParms.trail_id = markers.trail_id = tid;
                  if((lon = parseFloat(unescape(info[0].getAttribute("lon"))))) 
                     markers.centerLon = lon;
                  if((lat = parseFloat(unescape(info[0].getAttribute("lat"))))) 
                     markers.centerLat = lat;
                  if((mtype = parseFloat(info[0].getAttribute("markertype"))) > 0) 
                     markers.markertype = mapParms.markertype = mtype;
                  markers.markertype_img_url = unescape(info[0].getAttribute("markertype_img_url"));
                  markers.spot_img_url = unescape(info[0].getAttribute("spot_img_url"));
               }
               markers.hide(); markers.markers.length = 0;
               for(i = idx = 0; i < opts.length; i++) {
                  var mark_id = parseInt(opts[i].getAttribute("mark_id"));
                  if(mark_id < 1) continue;
                  markers.markers[idx] = new MarkerData();
                  markers.markers[idx].mark_id = mark_id;
                  markers.markers[idx].mark_desc = unescape(opts[i].getAttribute("mark_desc"));
                  markers.markers[idx].mark_img_url = unescape(opts[i].getAttribute("mark_img_url"));
                  markers.markers[idx].mark_lat = parseFloat(opts[i].getAttribute("mark_lat"));
                  markers.markers[idx].mark_lon = parseFloat(opts[i].getAttribute("mark_lon"));
                  markers.markers[idx].mark_act = parseFloat(opts[i].getAttribute("mark_act"));
                  markers.markers[idx].mark_spot = parseInt(opts[i].getAttribute("mark_spot"));
                  if(markers.markers[idx].mark_id > 0) markers.markers[idx].marker = markers.createMarker(markers.markers[idx]);
                  idx++;
               }
            }
         }
         markers.markerChange();
         mapParms.updateBookmark();
      });
   };

   this.markerChange = function () {
      this.setSelectOptions();
      markerWin.hide();
      var zoom = mapParms.zoom || mapParms.defaultZoom;
      if(markers.centerLat && !gmap.isLoaded()) {
         mapParms.lat = markers.centerLat;
         mapParms.lon = markers.centerLon;
         //mapState.zoom = mapState.level = mapParms.zoom = zoom;
         mapParms.markertype = markers.markertype;
         gmap.setCenter(new GLatLng(markers.centerLat, markers.centerLon), zoom);
         mapState.update();
      } else if(!gmap.isLoaded()) {
         if(mapParms.lat) gmap.setCenter(new GLatLng(mapParms.lat, mapParms.lon), zoom);
         mapState.update();
      }
      markers.show();
   };

   this.setSelectOptions = function () {
      docForm = document.getElementById("markers");
      if(!docForm) return 0;
      
      docForm.options.length = 0;
      docForm.options[0]=new Option("Select location", "0", false, false);
      for(i = 0; i < this.markers.length; i++) {
         docForm.options[i+1]=new Option(this.markers[i].mark_desc, this.markers[i].mark_id, false, false);
         if(parseInt(this.markers[i].mark_id) == parseInt(mapParms.mark_id)) docForm.options[i+1].selected = true;
       }
       if(!i) docForm.options[0]=new Option("No markers", "0", false, false);

      if(this.markertype > 0) {
         docForm = document.getElementById("markertypes");
         if(!docForm) return 0;
       //  for(i = 0; i < docForm.options.length; i++)
      //      if(parseInt(docForm.options[i].value) == this.markertype) docForm.options[i].selected = true;
      }   
      return 1;
   };

  this.createMarker = function (prms) {
      if(!prms) return null;

      var img_url = markers.markertype_img_url;
      if(prms.mark_img_url && prms.mark_img_url.length > 4) img_url = prms.mark_img_url;

      if(prms.mark_spot) img_url = markers.spot_img_url;

      if(img_url.length) { // && mapParms.event_id < 1 && mapParms.entry_id < 1) {
         var mIcon = new GIcon();
         mIcon.image = img_url;
         mIcon.iconSize = new GSize(18, 18);
         mIcon.iconAnchor = new GPoint(0, 0);
         mIcon.infoWindowAnchor = new GPoint(12,2);
         var markerOpts = {icon:mIcon};
         var marker = new GMarker(new GLatLng(parseFloat(prms.mark_lat), parseFloat(prms.mark_lon)), markerOpts);
      } else {
         var marker = new GMarker(new GLatLng(parseFloat(prms.mark_lat), parseFloat(prms.mark_lon)));
      }

      if(markers.labelDiv) {
         GEvent.addListener(marker,"mouseover", function() {
            var pt = gmap.fromLatLngToContainerPixel(marker.getLatLng());
            markers.labelDiv.topLeft(pt.y + 'px', (pt.x + 20) + 'px');
            markers.labelDiv.write(prms.mark_desc);
         });
         GEvent.addListener(marker,"mouseout", function() {
            markers.labelDiv.hide();
         });
      }

      if(! prms.mark_spot) {
         GEvent.addListener(marker,"click", function() {
            if(prms.mark_act && parseInt(prms.mark_act) > 0) {
               if(opener) opener.document.location = mapParms.url_viewJournal + '&entry_id=' + parseInt(prms.mark_id);
               else window.document.location = mapParms.url_viewJournal + '&entry_id=' + parseInt(prms.mark_id);
            } else {
               markers.viewMarkerData(prms.mark_id, markerWin);
            }
         });
      } else {
         if(prms.mark_id && parseInt(prms.mark_id) > 1) {
            GEvent.addListener(marker,"click", function() {
               gmap.setCenter(new GLatLng(parseFloat(prms.mark_lat), parseFloat(prms.mark_lon)), 13);
               if(opener) opener.document.location = mapParms.url_viewJournal + '&entry_id=' + parseInt(prms.mark_id);
               else window.document.location = mapParms.url_viewJournal + '&entry_id=' + parseInt(prms.mark_id);
            });
         } else {
            GEvent.addListener(marker,"click", function() {
               gmap.setCenter(new GLatLng(parseFloat(prms.mark_lat), parseFloat(prms.mark_lon)), 13);
            });
         }
      }
      return marker;
   };

   this.viewMarkerData = function (mark_id, markerWin) {
      if(parseInt(mark_id) < 1) return false;
      var url = mapParms.url_viewMarkerData + 'mark_id=' + parseInt(mark_id);
      markerWin.write('<p style="padding: 10px;">Loading...</p>');
      GDownloadUrl(url, function(data, responseCode) {
         if(responseCode == 200) {
            var xmlDoc = GXml.parse(data);
            if(xmlDoc) {
               var data = xmlDoc.getElementsByTagName("data");
               var html = unescape(data[0].getAttribute("html"));
               var lat = parseFloat(data[0].getAttribute("mark_lat"));
               var lon = parseFloat(data[0].getAttribute("mark_lon"));
               var type = parseInt(data[0].getAttribute("mark_type"));
               if(parseInt(type) > 0) markerWin.setText(html);
               else markerWin.hide();
            }
         } else {
            markerWin.setText('<strong>Data unavailable</strong>');
         }
         mapParms.updateBookmark();
      });
   };

   this.setCenter = function (opt_lat, opt_lon, opt_zoom) {
      if(opt_lat.length && opt_lon.length)
         gmap.setCenter(new GLatLng(parseFloat(opt_lat), parseFloat(opt_lon)), parseInt(opt_zoom));
   };

   this.hideUpdateForm = function (opt_trail_id, opt_markertype) {
      updateWin.hide();
      if(this.editMarker) {
        this.editMarker.hide();
      }
   };

   this.showUpdateForm = function (opt_trail_id, opt_markertype) {
      var url = mapParms.url_updateMarkerData + 'trail_id=' + parseInt(opt_trail_id) + '&markertype=' + parseInt(opt_markertype);

//      html = '<div class="closeWin"><a href="javascript:parent.markers.hideUpdateForm();">Close</a></div>'   
      html = '<iframe scrolling="no" frameborder="0" width="300px" height="500px" name="updateIframe" id="updateIframe" src="' + url + '"></iframe>';

      updateWin.write(html);
      this.createEditMarker();
   };

   this.setEditMarker = function (opt_lat, opt_lon) {
      var lat = parseFloat(opt_lat);
      var lon = parseFloat(opt_lon);
      if(!lat && !lon) return;

      if(gmap && this.editMarker) this.editMarker.setLatLng(new GLatLng(lat, lon));
   };

   this.createEditMarker = function () {
      if(!gmap.isLoaded()) return null;
      if(this.editMarker) {
         this.editMarker.show();
         return this.editMarker;
      } 

      this.editMarker = new GMarker(gmap.getCenter(), {clickable:false,draggable:true});
      this.editMarker.draggable();

      GEvent.addListener(this.editMarker, 'dragend', function (pt) {
         if(!pt) return;
         var lat = parseFloat(pt.lat()); var lon = parseFloat(pt.lng());

         var ifrdoc = null;
         var ifr = document.getElementById('updateIframe');
         if ( ifr && ifr.contentDocument ) { // DOM
             var ifrdoc = ifr.contentDocument;
         } else if ( ifr && ifr.contentWindow ) { // IE win
             var ifrdoc = ifr.contentWindow.document;
         }

         if(ifrdoc && ifrdoc.getElementById('ph_mapLat')) {
            if((fld = ifrdoc.getElementById('ph_mapLat'))) fld.value = lat.toFixed(6);
            if((fld = ifrdoc.getElementById('ph_mapLon'))) fld.value = lon.toFixed(6);
         } else if(opener && opener.document.getElementById("ph_mapLat")) { 
            if((fld = opener.document.getElementById("ph_mapLat"))) fld.value = lat.toFixed(6);
            if((fld = opener.document.getElementById("ph_mapLon"))) fld.value = lon.toFixed(6);
         }
      });

      GEvent.addListener(gmap, 'click', function (s, pt, j) {
         if(!pt) return;
         var lat = parseFloat(pt.lat()); var lon = parseFloat(pt.lng());
         markers.editMarker.setLatLng(pt);  

         var ifr = document.getElementById('updateIframe');
         if ( ifr && ifr.contentDocument ) { // DOM
             var ifrdoc = ifr.contentDocument;
         } else if ( ifr && ifr.contentWindow ) { // IE win
             var ifrdoc = ifr.contentWindow.document;
         }

         if(ifrdoc && ifrdoc.getElementById('ph_mapLat')) {
            if((fld = ifrdoc.getElementById('ph_mapLat'))) fld.value = lat.toFixed(6);
            if((fld = ifrdoc.getElementById('ph_mapLon'))) fld.value = lon.toFixed(6);

         } else if(opener && opener.document.getElementById("ph_mapLat")) { 
            if((fld = opener.document.getElementById("ph_mapLat"))) fld.value = lat.toFixed(6);
            if((fld = opener.document.getElementById("ph_mapLon"))) fld.value = lon.toFixed(6);
         }
      });

      gmap.addOverlay(this.editMarker);
      this.editMarker.show();
   };
}

// Distance Object
function Distance(opt_div) {
   this.div = opt_div || null;
   this.markers = new Array();
   this.distances = new Array();
   this.elevations = new Array();
   this.count = 0;

   this.createMarkers = function (distStr) {
      var str = distStr.split(" ");
      var items, key, tot = 0;

      var img_url = '/gmap/images/distanceClear.png';
      var img_clear_url = '/gmap/images/distanceClear.png';

      for(var i = 0; i < str.length; i++) {
         items = str[i].split(",");
         if(items.length < 3) continue;

         var key = items[0] + '_' + items[1];
         if(!i && this.count) tot = parseFloat(items[2]);
         else tot += parseFloat(items[2]);

         this.elevations[this.count] = '';
         if(items.length == 4) this.elevations[this.count] = items[3];

         var mIcon = new GIcon();
         mIcon.image = img_url;
         mIcon.transparent = img_clear_url;
         mIcon.iconSize = new GSize(12, 12);
         mIcon.iconAnchor = new GPoint(6, 6);
         mIcon.infoWindowAnchor = new GPoint(1,1);
         var markerOpts = {icon:mIcon};

         this.markers[this.count] = new GMarker(new GLatLng(parseFloat(items[0]), parseFloat(items[1])), markerOpts);
         this.distances[this.count] = tot;
         this.addListener(this.markers[this.count],  this.distances[this.count], this.elevations[this.count], distDiv);
         this.count++;
      }

      return i;
   };

   this.addListener = function (marker, tot, elev, div) {
      if(div) {
         GEvent.addListener(marker,"mouseover", function() {
            var pt = gmap.fromLatLngToContainerPixel(marker.getLatLng());
            div.topLeft(pt.y + 'px', (pt.x + 20) + 'px');
            var ostr = (tot ? Math.round(tot * 100000) / 100000 : "0.00") + ' mi';
            ostr += elev.length ? '<br>' + elev + ' ft' : '';
            div.write(ostr);
         });
         GEvent.addListener(marker,"mouseout", function() {
            div.hide();
         });
         GEvent.addListener(marker,"click", function() {
            if (typeof(curDistance) == "undefined"  || curDistance == -1) {
               curDistance = tot;
            } else {
               var startDistance = curDistance;
               curDistance = Math.abs(curDistance - tot);
               var pt = gmap.fromLatLngToContainerPixel(marker.getLatLng());
               div.topLeft(pt.y + 'px', (pt.x + 20) + 'px');
               var str = 'Start: ' + (startDistance ? Math.round(startDistance * 100000) / 100000 : "0.00") + '<br>';
               str += 'End: ' + (tot ? Math.round(tot * 100000) / 100000 : "0.00") + '<br>';
               str += 'Length: ' + (curDistance ? Math.round(curDistance * 100000) / 100000 : "0.00") + '<br>';
               str += (curDistance ? Math.round((curDistance / 10) * 10) / 10 : "0.00") + ' days @ 10mpd' + '<br>';
               str += (curDistance ? Math.round((curDistance / 15) * 10) / 10 : "0.00") + ' days @ 15mpd' + '<br>';
               str += (curDistance ? Math.round((curDistance / 20) * 10) / 10 : "0.00") + ' days @ 20mpd' + '<br>';
               str += (curDistance ? Math.round((curDistance / 25) * 10) / 10 : "0.00") + ' days @ 25mpd' + '<br>';
               str += (curDistance ? Math.round((curDistance / 30) * 10) / 10 : "0.00") + ' days @ 30mpd';

               div.write(str);
               curDistance = -1;
            }
         });
      }
   };
}

// Trace Object
function Trace() {
   this.midLevel = mapParms.midLevel;

   if(document.mapForm) this.trail_id = mapParms.trail_id || document.mapForm.trails.value;
   this.view = mapParms.vw;
   this.zoom = parseInt(mapParms.zoom);
   this.level = this.zoom;
   this.lastLevel = 0;
   this.traceLoaded = 1;
   this.isLoading = 0;

   this.N = null;
   this.E = null;
   this.W = null;
   this.S = null;

   this.lastGetN = 999;
   this.lastGetE = 999;
   this.lastGetS = -999;
   this.lastGetW = -999;

   this.NE = null;
   this.SW = null;
   this.lastNE = null;
   this.lastSW = null;

   this.getTraceUrl = function () {
      var srcUrl = mapParms.url_getTraceData;

      var level = this.level;

      if(this.N && this.E) {
         var n =  parseFloat(this.N);
         var e =  parseFloat(this.E);
         var w =  parseFloat(this.W);
         var s =  parseFloat(this.S);
         retUrl = srcUrl + 
             'trail_id=' + parseInt(mapParms.trail_id) + 
             '&level=' + parseInt(level) +
             '&n=' + n.toFixed(5) +
             '&e=' + e.toFixed(5) +
             '&w=' + w.toFixed(5) +
             '&s=' + s.toFixed(5);
      } else {
         retUrl = srcUrl + 
             'trail_id=' + parseInt(mapParms.trail_id) + 
             '&level=' + parseInt(level);
      }
      retUrl += '&dist=' + parseInt(mapParms.dist);

      return retUrl;
   };

   this.update = function () {
      var ret = 0;

      if(!gmap) return 0;
      if(gmap.isLoaded()) this.updateState(); 

      if(!gmap.isLoaded() || !trace.length) {
         ret = 1;
      } else if(this.level <= this.midLevel && this.lastLevel > this.midLevel) {
         ret = 2;
      } else if(this.level > this.midLevel && this.lastLevel <= this.midLevel) {
         ret = 3;
      } else if(this.lastGetS) {
         if( ((this.lastGetN <= this.N && this.lastGetW >= this.W) || (this.lastGetS >= this.S && this.lastGetE <= this.E)) ||

             ((this.lastGetN <= this.N && this.lastGetN >= this.S && this.lastGetE < this.E && this.lastGetE > this.W) ||
             (this.lastGetN <= this.N && this.lastGetN >= this.S && this.lastGetW > this.W && this.lastGetW < this.E) ||
             (this.lastGetS <= this.N && this.lastGetS >= this.S && this.lastGetW > this.W && this.lastGetW < this.E) ||
             (this.lastGetS <= this.N && this.lastGetS >= this.S && this.lastGetE < this.E && this.lastGetE > this.W))
           ) ret = 4;
      }

      if(ret > 0) this.updateTrace();
      mapParms.updateBookmark();

      return ret;
   };

   this.updateState = function () {

      var bounds = gmap.getBounds();
      this.lastNE = this.NE;
      this.lastSW = this.SW;
      this.NE = bounds.getNorthEast();
      this.SW = bounds.getSouthWest();
     
      this.N = this.NE.lat(); this.N = this.N.toFixed(5);
      this.E = this.NE.lng(); this.E = this.E.toFixed(5);
      this.W = this.SW.lng(); this.W = this.W.toFixed(5);
      this.S = this.SW.lat(); this.S = this.S.toFixed(5);

      this.lastLevel = this.level;
      this.level = parseInt(gmap.getZoom());

      return 0;
   };

   this.updateTrace = function () {

      if(!mapParms.trail_id || mapState.isLoading) return 0;
      mapState.isLoading = 1;

      var url = mapState.getTraceUrl();
      //statusWin.write('<img src="/maps/images/penguin.gif">');
      statusWin.write('<p><strong>Loading...</strong></p>');
   
      for(i = 0; i < trace.length; i++) {
         gmap.removeOverlay(trace[i]);
      }
      for(i = 0; i < distances.count; i++) {
         if(distances.markers[i]) gmap.removeOverlay(distances.markers[i]);
      }

      trace.length = 0;
      distances = new Distance(distDiv);

      GDownloadUrl(url, function(data, responseCode) {
         if(responseCode == 200) {
            for(i = 0; i < trace.length; i++) {
               gmap.removeOverlay(trace[i]);
            }
            trace.length = 0;

            var xmlDoc = GXml.parse(data);
            if(xmlDoc) {
                var map = xmlDoc.getElementsByTagName("map");
                document.title = unescape(map[0].getAttribute("title"));
                mapcenter = xmlDoc.getElementsByTagName("mapcenter");
                latCenter = mapcenter[0].getAttribute("lat");
                lonCenter = mapcenter[0].getAttribute("lon");
                startZoom = parseInt(mapcenter[0].getAttribute("startZoom"));
   
                dims =  xmlDoc.getElementsByTagName("dims");
                mapState.lastGetN = parseFloat(dims[0].getAttribute("n"));
                mapState.lastGetE = parseFloat(dims[0].getAttribute("e"));
                mapState.lastGetW = parseFloat(dims[0].getAttribute("w"));
                mapState.lastGetS = parseFloat(dims[0].getAttribute("s"));
   
                parms = xmlDoc.getElementsByTagName("parms");
                var wgt = parseInt(parms[0].getAttribute("weight"));
                var opc = parseFloat(parms[0].getAttribute("opacity"));
                var zom = parseInt(parms[0].getAttribute("zoomFactor"));
                var nlv = parseInt(parms[0].getAttribute("numLevels"));
                var clr = parms[0].getAttribute("color");
   
                var lines = xmlDoc.getElementsByTagName("line");
                for(i = 0; i < lines.length; i++) {
                   trace[i] = new GPolyline.fromEncoded({
                      color: clr,
                      weight: wgt,
                      opacity: opc,
                      points: lines[i].getAttribute("points"),
                      levels: lines[i].getAttribute("levels"),
                      zoomFactor: zom,
                      numLevels: nlv
                   });
                   if(mapParms.dist && lines[i].getAttribute("distances")) distances.createMarkers(lines[i].getAttribute("distances"));
                }
   
                if(! gmap.isLoaded()) {
                   gmap.setCenter(new GLatLng(latCenter, lonCenter), startZoom);
                }
                for(i = 0; i < trace.length; i++) {
                   gmap.addOverlay(trace[i]);
                }
                for(i = 0; i < distances.count; i++) {
                   gmap.addOverlay(distances.markers[i]);
                }

                mapState.updateState();
                statusWin.hide();
            }
         } else {
            statusWin.hide();
         }
         mapState.isLoading = 0;
         mapParms.updateBookmark();
      });
   }

}


// CustomDiv (statusWin, labelDiv) object
function CustomDiv(parm_id, opt_class) {
   if(!parm_id || parseInt(parm_id) < 1) return null;
   var className = opt_class || "";

   //properties
   this.div = document.createElement("div");
   this.div.id = 'cw_div_' + parseInt(parm_id);
   if(className.length) this.div.className = className;

   this.parentDivId = "";
   this.visible = 0;
   this.id = parseInt(parm_id);
   this.text = "";

   //methods

   this.topLeft = function (top, left) { this.div.style.top = top; this.div.style.left = left; };
   this.left = function (left) { this.div.style.left = left; };
   this.width = function (width) { this.div.style.width = width; };
   this.getDivId = function () { return this.div.id; };
   this.show = function () { 
      if(this.div.parentNode) this.div.parentNode.removeChild(this.div); 
      if(this.parentDivId.length) document.getElementById(this.parentDivId).appendChild(this.div);
      else document.body.appendChild(this.div); 
      this.visible = 1;
   };

   this.hide = function () { 
      if(this.div.parentNode) this.div.parentNode.removeChild(this.div); 
      this.visible = 0;
   };

   this.write = function (text) {
      this.div.innerHTML = text || this.text;
      this.text = this.div.innerHTML;
      this.div.innerHTML = this.div.innerHTML;
      this.show();
   };

   this.changeParent = function (divId) { 
      this.parentDivId = divId || "";
   };

   this.setText = function (text) {
      this.div.innerHTML = text || "";
      this.text = this.div.innerHTML;
      this.div.innerHTML = this.div.innerHTML;
   };

   this.className = function (className) {
      var name = className || "";
      if(name.length) this.div.className = name;
   };
}

