// labels for the left-hand Controls
var Label_MouseDefaults   = 'Put the map into grab-and-drag mode';
var Label_ZoomBox         = 'Draw a box and zoom in to the selected area';
var Label_ZoomToMaxExtent = 'Zoom the map out as far as possible';

// which basemap can be printed? if its not this, the warning will be shown when they try to print
var PRINTING_BASEMAP_NAME = 'HortPlus base map';
var PRINTING_NOBASEMAP_WARNING = 'Google basemaps cannot be printed.\nIf you want a basemap in your printout, please select a different basemap.\nContinue printing?';;

// printMap() assumes that an iframe named 'map' contains a Map object named 'map'
// It should be very easy to change those first two lines to accommodate your situation.
function printMap(geotiff) {
  // if they are using a Google basemap, warn them that it cannot be printed
  if (map.baseLayer.name != PRINTING_BASEMAP_NAME && !confirm(PRINTING_NOBASEMAP_WARNING)) return
  // proceed
  var printurl = 'print.php';
  var size = map.getSize();
  // go through all layers, and collect a list of objects
  // each object is a tile's URL and the tile's pixel location relative to the viewport
  var tiles = [];
  for (layername in map.layers) {
    // if the layer isn't visible at this range, or is turned off, skip it
    var layer = map.layers[layername];
    if (!layer.getVisibility()) continue;
    if (!layer.calculateInRange()) continue;
    // iterate through their grid's tiles, collecting each tile's extent and pixel location at this moment
    for (tilerow in layer.grid) {
    for (tilei in layer.grid[tilerow]) {
      var tile     = layer.grid[tilerow][tilei]
      var url      = layer.getURL(tile.bounds);
      var position = tile.position;
      var opacity  = layer.opacity ? parseInt(100*layer.opacity) : 100;
      tiles[tiles.length] = {url:url, x:position.x, y:position.y, opacity:opacity};
    }
    }
  }
  var tiles_json = JSON.stringify(tiles);
  var printparams  = 'width='+size.w + '&height='+size.h + '&tiles='+escape(tiles_json) ;
  if (geotiff) printparams += '&bbox=' + map.getExtent().toBBOX();

  // finally, the prepared AJAX call
  alert('Printing takes a moment. Please be patient.');
  OpenLayers.Request.POST(
    { url:'print.php',
      data:OpenLayers.Util.getParameterString({width:size.w,height:size.h,tiles:tiles_json}),
      headers:{'Content-Type':'application/x-www-form-urlencoded'},
      callback: function(request) {
         window.open(request.responseText);
      }
    }
  );
}



function redrawMap() {
    if (document.getElementById('map').contentWindow) {
        map = document.getElementById('map').contentWindow.map;
    }
    for (i in map.layers) map.layers[i].redraw(true);
}


function zoomToXYZ(xyz) {
   if (!xyz) return;
   var xyz = xyz.split(' '); // lon, lat, zoom
   map.setCenter(new OpenLayers.LonLat(xyz[0],xyz[1]) , xyz[2] );
   var oldbasemap = map.baseLayer;
   map.setBaseLayer(layers['basemap']);
   map.setBaseLayer(oldbasemap);
}
