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 iconBeach = new GIcon(attrIcon);
iconBeach.image = "http://www.cornwalls.co.uk/images/gmap/icons/beach.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 11;}
    mt[i].getMaximumResolution = function() {return 18;}
  }


      map.addControl(new GLargeMapControl3D());
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());


	map.setCenter(new GLatLng(centerLatitude, centerLongitude), 13, G_HYBRID_MAP);

//Pass in a initial point for the center
    updateMarkers(new GLatLng(centerLatitude, centerLongitude));

    //GEvent.addListener(map,'click',function(overlay,point) {
        //Pass in the point for the center
        //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 = centerLatitude + ',' + centerLongitude
	+ '/' + northEast.toUrlValue()
	+ '/' + southWest.toUrlValue();
	
	getVars += '/' + mapType;

    //log the URL for testing
    //GLog.writeUrl('server_cps.php?'+getVars);

    //retrieve the points
    var request = GXmlHttp.create();
    request.open('GET', '/town/map_data/'+getVars, 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);
                  
							    
                  map.addOverlay(marker);
              }
         }
    }
    request.send(null);
}



function createMarker(point, type, list_type, name, item, town, short_desc, url) {
		//create the marker with the appropriate icon
	if(type=='c') {
		switch(mapType){
			case 'attractions':
			var marker = new GMarker(point,iconAttrCluster,true);
			break;
			case 'accommodation':
			var marker = new GMarker(point,iconCluster,true);
			break;
		}
			
	} else {
			var marker = new GMarker(point,iconBeach);
	}

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

	
	return marker;
}

function toggleGroup(type) {
      for (var i = 0; i < markerGroups[type].length; i++) {
        var marker = markerGroups[type][i];
        if (marker.isHidden()) {
          marker.show();
        } else {
          marker.hide();
        }
      } 
    }


window.onload = init;

