var map;


//Create an icon for the clusters
var iconAttrCluster = new GIcon();
iconAttrCluster.image = "http://www.cornwalls.co.uk/images/gmap/icons/cluster.png";
iconAttrCluster.shadow = "http://www.cornwalls.co.uk/images/gmap/icons/cluster_shadow.png";
iconAttrCluster.iconSize = new GSize(46, 50);
iconAttrCluster.shadowSize = new GSize(72, 50);
iconAttrCluster.iconAnchor = new GPoint(23, 25);
iconAttrCluster.infoWindowAnchor = new GPoint(23, 25);
iconAttrCluster.infoShadowAnchor = new GPoint(23, 25);

var attrIcon = new GIcon();
attrIcon.shadow = "http://www.cornwalls.co.uk/images/gmap/icons/shadow.png";
attrIcon.iconSize = new GSize(32, 37);
attrIcon.shadowSize = new GSize(51, 37);
attrIcon.iconAnchor = new GPoint(16, 18);
attrIcon.infoWindowAnchor = new GPoint(16, 18);
attrIcon.infoShadowAnchor = new GPoint(16, 18);

var iconGenral = new GIcon(attrIcon);
iconGenral.image = "http://www.cornwalls.co.uk/images/gmap/icons/info.png";

var iconMine = new GIcon(attrIcon);
iconMine.image = "http://www.cornwalls.co.uk/images/gmap/icons/mine.png";

function init() {
	
	map = new GMap2(document.getElementById("bigMap"));
	
	//Restricting the range of Zoom Levels
  // Get the list of map types      
  var mt = map.getMapTypes();
  // Overwrite the getMinimumResolution() and getMaximumResolution() methods
  for (i in mt) {
    mt[i].getMinimumResolution = function() {return 9;}
    mt[i].getMaximumResolution = function() {return 18;}
  }

			map.addMapType(G_PHYSICAL_MAP);
      map.addControl(new GLargeMapControl3D());
      map.addControl(new GMapTypeControl());

	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom, G_PHYSICAL_MAP);

//Pass in a initial point for the center
    updateMarkers();

}

function updateMarkers() {

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

    
    //retrieve the points
    var request = GXmlHttp.create();
    request.open('GET', '/content/map_data/'+ mapType, true);
    request.onreadystatechange = function() {
         if (request.readyState == 4) {
              var jscript = request.responseText;
              var points;
              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,points[i].list_type, points[i].name, points[i].id, points[i].town, points[i].short_desc, points[i].url, points[i].img);
                  
							    
                  map.addOverlay(marker);
              }
         }
    }
    request.send(null);
}



function createMarker(point, type, list_type, name, item, town, short_desc, url, img) {
		//create the marker with the appropriate icon
	if(type=='c') {
			var marker = new GMarker(point,iconAttrCluster,true);

			
	} else {
		list_type = parseInt(list_type);
		switch(list_type){
			case 33:
			  var marker = new GMarker(point,iconBar);
			  break;
			  
			default:
			  var marker = new GMarker(point,iconGenral);
			}
	}

			GEvent.addListener(marker,'click',function(overlay,point) {
			    html = '';
			    if(img!=''){
			    	html += '<a href="'+url+'"><img src="'+img+'" alt="" width="80" align="left" /></a>';
			    }
			    html += '<a href="'+url+'"><b>'+name+'</b></a><br />'+short_desc;
			    marker.openInfoWindowHtml('<div class="gBox">'+html+'</div>');
			    //dropTo(item);
			    //alert(item);
			});
	
	return marker;
}

function zoomToPoint(lat, lon){
	map.setCenter(new GLatLng(lat, lon), 12);
}



window.onload = init;

