function createMarker(point,name,popupcontent,labels)
{
   var marker = new GMarker(point, baseIcon);
   var i = centermarkers.length;
   marker.tooltip = '<div class="tooltip">'+name+'</div>';

   // The info window version with the "to here" form open
   directions[i] = popupcontent[0] + '<br /><b>Driving directions:</b>' +
      '<br />Enter the Starting address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
      '<input type="text" SIZE=40 name="saddr" id="saddr" value="" /><br />' +
      '<INPUT value="Get Directions" TYPE="SUBMIT" />' +
      '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
      '"/>';
 
   popupcontent[0] = popupcontent[0] + '<br /><a href="javascript:tohere('+i+')">Get directions</a> to '+name;

   GEvent.addListener(marker, "click", function()
      {
      /*if (popupcontent.length > 2)
					{
			  		popupcontent[0] = '<div style="width:'+popupcontent.length*100+'px">' + popupcontent[0] + '</div>';
					}
		*/
      var tabs = [];
      for (var j=0; j<popupcontent.length; j++)
         {
         tabs.push(new GInfoWindowTab(labels[j],popupcontent[j]));
         }
      marker.openInfoWindowTabsHtml(tabs);
		}
	);

	centermarkers[i++] = marker;

   GEvent.addListener(marker,"mouseover", function() { showTooltip(marker); } );
   GEvent.addListener(marker,"mouseout", function() { tooltip.style.visibility="hidden"; } );
   return marker;
   }

function tohere(i)
   {
   centermarkers[i].openInfoWindowHtml(directions[i]);
   }


function showTooltip(marker)
   {
   tooltip.innerHTML = marker.tooltip;
   var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
   var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
   var anchor=marker.getIcon().iconAnchor;
   var width=marker.getIcon().iconSize.width;
   var height=tooltip.clientHeight;
   var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
   pos.apply(tooltip);
   tooltip.style.visibility="visible";
   }


function loadMarkers()
   {
	centers = req.responseXML.getElementsByTagName("centers")[0];
   centerlist = centers.getElementsByTagName("center");
	clLen = centerlist.length;
   showMarkers();
   }

function showMarkers()
   {
   var max = clLen<loaded+addBump?clLen:loaded+addBump;
   var mShortList = [];
   var ind = 0;
   for (var i = loaded; i<max; i++)
      {
      if (typeof(centerlist[i]) != 'undefined')
         {
         mShortList[ind] = createMarker(
            new GLatLng(centerlist[i].getAttribute("lat"),centerlist[i].getAttribute("lon")),
            centerlist[i].getAttribute("name"),
            ["<div class='infoWindows'><img src=\"/images/engines/"+centerlist[i].getAttribute("brand")+
            "_badge.jpg\" alt=\""+centerlist[i].getAttribute("brand")+"\" /><br />"+
            "<strong><a href=\"/dealers/"+centerlist[i].getAttribute("localurl")+"/index.html\">"+
            centerlist[i].getAttribute("name")+
            "</a></strong><br />"+centerlist[i].getAttribute("street")+"<br />"+
            centerlist[i].getAttribute("city")+", "+centerlist[i].getAttribute("state")+" "+
            centerlist[i].getAttribute("zip")+"<br />"+centerlist[i].getAttribute("phone")+"</div>"
				],
            ["location"]);
         loaded++;
         map.addOverlay(mShortList[ind]);
         ind++;
         }
      }
   if (loaded < clLen - 1)
      {
      setTimeout('showMarkers()',0);
      }
   else
      {
      elem = document.getElementById("loading");
	  if (elem != null)
		 {
		 elem.style.display='none';
		 }
      }
   }

function loadXMLDoc(url, callback) {
   var req = null;
   // branch for native XMLHttpRequest object
   if (window.XMLHttpRequest)
      {
      req = new XMLHttpRequest();
      req.onreadystatechange = function () {callback(req)};
      req.open("GET", url, true);
      // send referer header
      req.setRequestHeader("Referer", document.location);
      req.send(null);
      }
   else if (window.ActiveXObject)
      {
      // branch for IE/Windows ActiveX version
      isIE = true;
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req)
         {
         req.onreadystatechange = function () {callback(req)};
         req.open("GET", url, true);
         req.send();
         }
      }
   return req;
}

function processReq(datareq)
   {
	req = datareq;

	// only if req shows "loaded"
	if (req.readyState == 4) 
      {
      // only if "OK"
		if (req.status == 200) 
         {
         window.setTimeout(loadMarkers, 0);
         }
      else
         {
			alert("There was a problem retrieving the XML data:\n" +
					req.statusText+"("+req.status+")");
         }
      }
}

GMap2.prototype.addOverlays=function(a){
        var b=this;
        for (i=0;i<a.length;i++) {
                try {
                        this.overlays.push(a[i]);
                        a[i].initialize(this);
                        a[i].redraw(true);
                } catch(ex) {
                        alert('err: ' + i + ', ' + ex.toString());
                }
        }
        this.reOrderOverlays();
};

var req;
var map;
var tooltip;
var centermarkers = [];
var directions = [];
var mgrOptions = { borderPadding: 0, trackMarkers: true };
var allLoaded = false;
var loaded = 0;
var centers;
var centerlist;
var addBump = 25;

var baseIcon = new GIcon();
baseIcon.image = "/images/mapstar_medium.png";
baseIcon.shadow = "/images/mapstar_shadow_medium.png";
baseIcon.iconSize = new GSize(25, 19);
baseIcon.shadowSize = new GSize(25, 19);
baseIcon.iconAnchor = new GPoint(11, 11);
baseIcon.infoWindowAnchor = new GPoint(11, 11);



