var map;


var marker = [];
var openInfo = '';
//Create an icon for the clusters
var iconCluster = new GIcon();
iconCluster.image = "/img/markerhomec.png";
//iconCluster.shadow = "http://googlemapsbook.com/chapter7/icons/cluster_shadow.png";
iconCluster.iconSize = new GSize(24, 24);
//iconCluster.shadowSize = new GSize(22, 20);
iconCluster.iconAnchor = new GPoint(12, 12);
iconCluster.infoWindowAnchor = new GPoint(20, 1);
//iconCluster.infoShadowAnchor = new GPoint(26, 13);

//create an icon for the pins
var iconSingle = new GIcon();
iconSingle.image = "/img/markerhome.png";
//iconSingle.shadow = "";
iconSingle.iconSize = new GSize(16, 16);
//iconSingle.shadowSize = new GSize(22, 20);
iconSingle.iconAnchor = new GPoint(8, 8);
iconSingle.infoWindowAnchor = new GPoint(10, 5);
//iconSingle.infoShadowAnchor = new GPoint(13, 13);


function initmap() {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

	//updateMarkers();
updateMarkers(new GLatLng(centerLatitude, centerLongitude));

	//Note: it is not necessary to trigger this event on
	//zoomend as moveend is automatically triggerd by zoomend.
	//GEvent.addListener(map,'zoomend',function() {
	//	updateMarkers();
	//});
	GEvent.addListener(map,'moveend',function() {
		updateMarkers();
		//alert (openInfo);
	});

/*
	    GEvent.addListener(map,'click',function(overlay,point) {
        //Pass in the point for the center
		//zoomIn();
		map.setCenter(point,map.getZoom()+1);
        //updateMarkers(point);
    });
*/

//	alert('Note: This example has been limited to data from Hawaii');
}

function updateMarkers(point) {

    //remove the existing points
    map.clearOverlays();

    //create the boundry for the data to provide
    //initial filtering
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();

    var getVars = 'ne=' + northEast.toUrlValue()
    + '&sw=' + southWest.toUrlValue()
    + '&center=' + map.getCenter()
	+ '&zoom=' + map.getZoom();

    //log the URL for testing
    //GLog.writeUrl('/maps/maps.php?'+getVars);
	//var zoomL=getZoom();
	//GLog.writeUrl('zoomL');
	

    //retrieve the points
    var request = GXmlHttp.create();
    request.open('GET', '/maps/maps.php?'+getVars, true);
    request.onreadystatechange = function() {
         if (request.readyState == 4) {
              var jscript = request.responseText;
              var points;
			  //GLog.writeUrl(request.responseText);
              eval(jscript);

              //create each point from the list
              for (i in points) {
                  var point = new GLatLng(points[i].lat,points[i].lng);
                  var marker = createMarker(point,points[i].type,i, points[i].html);
				  /*
				  	if(points[i].type=='c') {
					markers[i] = new GMarker(point,iconCluster);
						} else {
					markers[i] = new GMarker(point,iconSingle);
						}
				  //alert ('createMarker('+i+', point,points[i].type,points[i].html)'); 
				  //alert (markers[i]);
                  map.addOverlay(markers[i]);
				  if (points[i].html !== ''){
					//GEvent.clearListeners(markers[i], 'click');  
	GEvent.addListener(markers[i], "click", function() {
		markers[i].openInfoWindowHtml('label');
		markers[i].visited = true;
		GEvent.trigger(markers[i],"mouseout");
	});

					//markers[i].openInfoWindowHtml(points[i].html);
					
					//markers[i].openInfoWindowHtml(points[i].html);
					//alert (GEvent.addListener(markers[i], 'click', function(){markers[i].openInfoWindowHtml('click');}));
					//alert (i);
				  }else{
					GEvent.addListener(markers[i], 'click', function(){
					map.setCenter(point,map.getZoom()+1); 
					});
					}
					*/
              }
         }
    }
    request.send(null);
}


function createMarker(point, type, id, html) {

	//create the marker with the appropriate icon
	if(type=='c') {
		marker[id] = new GMarker(point,iconCluster);
	} else {
		marker[id] = new GMarker(point,iconSingle);
	}
	map.addOverlay(marker[id]);   
	if (html !== ''){
	GEvent.addListener(marker[id], 'click', function(){marker[id].openInfoWindowHtml(html); openInfo = id;}); 
	//GEvent.addListener(marker[id], "infowindowclose", function(){openInfo = '';});

	//alert (html); 
	}else{
	GEvent.addListener(marker[id], 'click', function(){map.setCenter(point,map.getZoom()+1);});
	}
	if (id == openInfo){
	marker[id].openInfoWindowHtml(html); 
	}
         
	//return marker;
}

