  var boxStyleOpts = {
          opacity: .2,
          border: "2px solid red"
        }

        /* second set of options is for everything else */
        var otherOpts = {
          buttonHTML: "<img src='Images/zoomin1.gif' />",
          buttonZoomingHTML: "<img src='Images/zoomin2.gif' />",
          buttonStartingStyle: {width: '23px', height: '20px'}
        };

        /* third set of options specifies callbacks */
        var callbacks = {};
var whichLayer;
function customGetTileURL(a,b) {
      //converts tile x,y into keyhole string
	  var mapBounds=new GLatLngBounds(new GLatLng(46.54316,-69.31111),new GLatLng(60.13239,-50.15704));
      if (whichLayer==25) {
	   if (b>10) { return "WPOregions-tiles/blank-tile.png"; };
      }else if (whichLayer==26){
       if (b>10) { return "remoteness_classes_2007-tiles/blank-tile.png"; };     
      }else if (whichLayer==27){
       if (b>10) { return "rurality-tiles/blank-tile.png"; };			
      }
	  
      var c=Math.pow(2,b);
      var x=360/c*a.x-180;
      var y=180-360/c*a.y;
      var x2=x+360/c;
      var y2=y-360/c;
      var lon=x; //Math.toRadians(x); //would be lon=x+lon0, but lon0=0 degrees
      var lat=(2.0*Math.atan(Math.exp(y/180*Math.PI))-Math.PI/2.0)*180/Math.PI; //in degrees
      var lon2=x2;
      var lat2=(2.0*Math.atan(Math.exp(y2/180*Math.PI))-Math.PI/2.0)*180/Math.PI; //in degrees
      var tileBounds=new GLatLngBounds(new GLatLng(lat2,lon),new GLatLng(lat,lon2));
      
	  if (whichLayer==24) {
	   if (!tileBounds.intersects(mapBounds)) { return "WPOregions-tiles/blank-tile.png"; };
      }else if (whichLayer==25){
       if (!tileBounds.intersects(mapBounds)) { return "remoteness_classes_2007-tiles/blank-tile.png"; };    
      }else if (whichLayer==26){
       if (!tileBounds.intersects(mapBounds)) { return "rurality-tiles/blank-tile.png"; };		
      }
     
        var d=a.x;
        var e=a.y;
        var f="t";
        for(var g=0;g<b;g++){
            c=c/2;
            if(e<c){
                if(d<c){f+="q"}
                else{f+="r";d-=c}
            }
            else{
                if(d<c){f+="t";e-=c}
                else{f+="s";d-=c;e-=c}
            }
        }

      if (whichLayer==25) {
	   return "WPOregions-tiles/"+f+".png"
      }else if (whichLayer==26){
       return "remoteness_classes_2007-tiles/"+f+".png"    
      }else if (whichLayer==27){
       return "rurality-tiles/"+f+".png"	
      }
    }
 
 
   function changeOpacity(op) {
	//this works as long as there are at least two map types
        var current=map.getCurrentMapType();
        if (current==map.getMapTypes()[0])
        	map.setMapType(map.getMapTypes()[1]);
	else
		map.setMapType(map.getMapTypes()[0]);
        opacity=op;
        map.setMapType(current); //was map.getMapTypes()[1]
    }


    function getWindowHeight() {
        if (window.self&&self.innerHeight) {
            return self.innerHeight;
        }
        if (document.documentElement&&document.documentElement.clientHeight) {
            return document.documentElement.clientHeight;
        }
        return 0;
    }
     

    function resizeMapDiv() {
        //Resize the height of the div containing the map.
        //Do not call any map methods here as the resize is called before the map is created.
    	var d=document.getElementById("map");
        var offsetTop=0;
        for (var elem=d; elem!=null; elem=elem.offsetParent) {
            offsetTop+=elem.offsetTop;
        }
        var height=getWindowHeight()-offsetTop-16;
        if (height>=0) {
            d.style.height=height+"px";
        }
    }

    function showPolygons(whichPolygons, onoroff) {
		var centreLat=53.68927;
        var centreLon=-60.23719;
		var n_buttonText="Map"; //Text that shows up on the button for the custom layer (n=normal, s=sat)
        var s_buttonText="Satellite";
		var h_buttonText="Hybrid";
		var opacity=0.5;
		var initialZoomLevel=5;
		var TSize = new GSize(100,100);
		var mapBounds=new GLatLngBounds(new GLatLng(46.54316,-69.31111),new GLatLng(60.13239,-50.15704));


        if (onoroff==true) {
        resizeMapDiv();
        
        var copyrightCollection = new GCopyrightCollection("GMapCreator");
        copyrightCollection.addCopyright(
          new GCopyright("CASA",
          new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
          0,
          "<a href=\"http://www.casa.ucl.ac.uk\">CASA</a>")
        );
        
		whichLayer=whichPolygons;
        //create a custom G_NORMAL_MAP layer
        var n_tileLayers = [ G_NORMAL_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection , 0, 17)];
        n_tileLayers[1].getTileUrl = customGetTileURL;
        n_tileLayers[1].isPng = function() { return false; };
        n_tileLayers[1].getOpacity = function() { return opacity; };
        var n_customMap = new GMapType(n_tileLayers, new GMercatorProjection(n_tileLayers[0].maxResolution()+1), n_buttonText,
            {maxResolution:10, minResolution:0, errorMessage:"Data not available"});

        //create a custom G_SATELLITE_MAP layer
        var s_tileLayers = [ G_SATELLITE_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection , 0, 17)];
        s_tileLayers[1].getTileUrl = customGetTileURL;
        s_tileLayers[1].isPng = function() { return false; };
        s_tileLayers[1].getOpacity = function() { return opacity; };
        var s_customMap = new GMapType(s_tileLayers, new GMercatorProjection(s_tileLayers[0].maxResolution()+1), s_buttonText,
            {maxResolution:10, minResolution:0, errorMessage:"Data not available"});
            
        //create a custom G_HYBRID_MAP layer
        var h_tileLayers = [ G_HYBRID_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection , 0, 17),G_HYBRID_MAP.getTileLayers()[1]];
        h_tileLayers[1].getTileUrl = customGetTileURL;
        h_tileLayers[1].isPng = function() { return false; };
        h_tileLayers[1].getOpacity = function() { return opacity; };
        var h_customMap = new GMapType(h_tileLayers, new GMercatorProjection(h_tileLayers[0].maxResolution()+1), h_buttonText,
            {maxResolution:10, minResolution:0, errorMessage:"Data not available"});

        //Now create the custom map. Would normally be G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP
        map = new GMap2(document.getElementById("map"),{mapTypes:[n_customMap,s_customMap,h_customMap]});
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
		//map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, callbacks));
        map.enableContinuousZoom();
        map.enableScrollWheelZoom();
		map.enableGoogleBar();
        map.setCenter(new GLatLng(centreLat, centreLon), initialZoomLevel, n_customMap);
		//map.addControl(new GOverviewMapControl(TSize));
		create_and_add_Markers();
		if (whichLayer==25) {
         document.CA_Mapper.remoteness.checked=false;
		 document.CA_Mapper.rurality.checked=false;
		 document.legendimage.src = "Images/wop_legend.gif";
		 document.getElementById("boxA").style.visibility="visible";
         document.getElementById("boxA").style.zIndex=10;
        }else if (whichLayer==26){
         document.CA_Mapper.wpo_districts.checked=false;
		 document.CA_Mapper.rurality.checked=false;
		 document.legendimage.src = "Images/remoteness_legend.gif";
         document.getElementById("boxA").style.visibility="visible";
         document.getElementById("boxA").style.zIndex=10;
        }else if (whichLayer==27){
         document.CA_Mapper.wpo_districts.checked=false;
		 document.CA_Mapper.remoteness.checked=false;
		 document.legendimage.src = "Images/rurality_legend.gif";
         document.getElementById("boxA").style.visibility="visible";
         document.getElementById("boxA").style.zIndex=10;
        }
      }else {
          map = new GMap2(document.getElementById("map"));
          map.addControl(new GLargeMapControl());
          map.addControl(new GMapTypeControl());
		  map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, callbacks));
          map.enableContinuousZoom();
		  map.enableScrollWheelZoom();
		  map.enableGoogleBar();
          map.setCenter(new GLatLng(53.68927, -60.23719), 5);
		  // map.addControl(new GOverviewMapControl(new GSize(100,100)));
		  create_and_add_Markers();
          document.getElementById("boxA").style.visibility="hidden";
          document.getElementById("boxA").style.zIndex=1;
      } 
    }