
// Set up various global variables used across functions
var map; // The Google Map object
var marker_groups=[]; //grouping the markers for toggle
var points= [];

// call this function onload use to initialise the map if possible or deal with the fact it doesn't work
function load_map(center, zoom, type, geodata, fitMap)
{
  var typeID;
  switch (type){
  case 'HYBRID':
    typeID=google.maps.MapTypeId.HYBRID;
    break;
  case 'ROADMAP':
    typeID=google.maps.MapTypeId.ROADMAP;
    break;
  case 'SATELLITE':
    typeID=google.maps.MapTypeId.SATELLITE;
    break;
  case 'TERRAIN':
    typeID=google.maps.MapTypeId.TERRAIN;
    break;
  }

  var myOptions = {
    zoom: zoom,
    center: center,
    mapTypeId: typeID,
    scaleControl: true,
    navigationControl: true,
    navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN}
  };
  map = new google.maps.Map(document.getElementById("map"), myOptions);
  load_markers(geodata,fitMap);
}

 function load_markers(geodata,fitMap)
 {
   for (var i=0; i<geodata.length;i++){
     marker_groups[i]=[];
   for (var j= 0; j<geodata[i].length;j++)
     {
      if (geodata[i][j]['geo:lat'] && geodata[i][j]['geo:long'] ){

	posn=new google.maps.LatLng(geodata[i][j]['geo:lat'], geodata[i][j]['geo:long']);
	points.push(posn);
        if ( geodata[i][j]['colour']){
	      marker = new google.maps.Marker({
		map: map, 
	        position: posn,
		icon:'/core/public/images/'+geodata[i][j]['colour']+'_marker.png'
	     });
	    }else{
	marker = new google.maps.Marker({
              map: map, 
              position: posn
          });
	    }
	marker.visible=1;
	marker_groups[i].push(marker);
      }
     }}
   if(fitMap){
     fit_map(points);
    }
 }

function fit_map( points ) {
  var bounds = new google.maps.LatLngBounds();
  for (var i=0; i< points.length; i++) {
    bounds.extend(points[i]);
  }
  map.fitBounds(bounds);
}
//actually this doesn't switch them off, but maybe will at some point..
function toggleMarkers(j,fitMap){
     for (var i = 0; i < marker_groups[j].length; i++) {
        var marker = marker_groups[j][i];
        if (marker.getVisible()) {
          marker.visible=0;
        } else {
          marker.visible=1;
        }
      } 

}
