var map;
var bounds;
var markerGroups = { "cottage": [], "hotel": [], "bandb": [], "hostel": [], "camping": [], "farmstay": [], "inn": []};


//Create an icon for the clusters
var iconCluster = new GIcon();
iconCluster.image = "http://www.cornwalls.co.uk/images/gmap/accom_cluster_gray.png";
iconCluster.shadow = "http://www.cornwalls.co.uk/images/gmap/cluster_shadow.png";
iconCluster.iconSize = new GSize(34, 34);
iconCluster.shadowSize = new GSize(52, 34);
iconCluster.iconAnchor = new GPoint(17, 17);
iconCluster.infoWindowAnchor = new GPoint(17, 17);
//iconCluster.infoShadowAnchor = new GPoint(17, 17);

//create an icon for the pins
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.cornwalls.co.uk/images/gmap/accom_shadow.png";
baseIcon.iconSize = new GSize(20, 32);
baseIcon.shadowSize = new GSize(37, 32);
baseIcon.iconAnchor = new GPoint(3, 30);
baseIcon.infoWindowAnchor = new GPoint(10, 4);
//baseIcon.infoShadowAnchor = new GPoint(6, 6);

var iconBlue = new GIcon(baseIcon);
iconBlue.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_blue.png";

var iconRed = new GIcon(baseIcon);
iconRed.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_red.png";

var iconGreen = new GIcon(baseIcon);
iconGreen.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_green.png";

var iconOrange = new GIcon(baseIcon);
iconOrange.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_orange.png";

var iconYellow = new GIcon(baseIcon);
iconYellow.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_yellow.png";

var iconGray = new GIcon(baseIcon);
iconGray.image = "http://www.cornwalls.co.uk/images/gmap/accom_marker_gray.png";

//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);


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 min_zoom;}
    mt[i].getMaximumResolution = function() {return 18;}
  }

		
		
		map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.addControl(new GScaleControl()); 
	  //map.addControl(new GOverviewMapControl());



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

//Pass in a initial point for the center
    bounds = updateMarkers(new GLatLng(centerLatitude, centerLongitude));
    //alert(bounds);
    
		var point = new GLatLng(centerLatitude, centerLongitude);
		map.addOverlay(new GMarker(point));
    
    map.setZoom(map.getBoundsZoomLevel(bounds));
  	map.setCenter(bounds.getCenter());

    //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 += '/' + num_results
	+ '/' + type
	+ '/' + price
	+ '/' + options;

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

    //retrieve the points
    var request = GXmlHttp.create();
    request.open('GET', '/accommodation/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].name, points[i].id, points[i].town, points[i].short_desc, points[i].url, points[i].level);
                  var bounds = new GLatLngBounds();
							    
                  map.addOverlay(marker);
              }
         }
    }
    
    request.send(null);
    return(bounds);
}



function createMarker(point, type, name, item, town, short_desc, url, level) {
		//create the marker with the appropriate icon
	if(type=='c') {
			var marker = new GMarker(point,iconCluster,true);
		
	} else {
		type = parseInt(type);
		switch(type){
			case 3:
			  var marker = new GMarker(point,iconOrange);
			  markerGroups['hotel'].push(marker);
			  break;
			case 14:
			  var marker = new GMarker(point,iconBlue);
			  markerGroups['cottage'].push(marker);
			  break;
			case 16:
			  var marker = new GMarker(point,iconYellow);
			  markerGroups['hostel'].push(marker);
			  break;
			case 17:
			  var marker = new GMarker(point,iconGreen);
			  markerGroups['camping'].push(marker);
			  break;
			case 19:
			  var marker = new GMarker(point,iconRed);
			  markerGroups['bandb'].push(marker);
			  break;
			  
			default:
			  var marker = new GMarker(point,iconGray);
			}
	}
			 GEvent.addListener(marker,'click',function(overlay,point) {
			    html = '';
			    if(level>1){
			    	html += '<a href="'+url+'"><img src="/public/accommodation/'+item+'s.jpg" alt="" width="80" align="left" /></a>';
			    }
			    html += '<a href="'+url+'"><b>'+name+'</b></a><br />'+short_desc;
			    marker.openInfoWindowHtml('<div class="gBox">'+html+'</div>');
			    if(scroll_list){
			    	dropTo(item);
			    }
			    //alert(html);
			});

	
	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;

