var state = 'hidden';

function showhide(layer_ref) {

if (state == 'visible') {
state = 'hidden';
}
else {
state = 'visible';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.visibility = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].visibility = state;
}
if (document.getElementById && !document.all) {
maxwell_smart = document.getElementById(layer_ref);
maxwell_smart.style.visibility = state;
}
}


if (GBrowserIsCompatible()) { 

	
var marker;
var plotPoint;


function showAddress(show_this,plotPoint,   address, t_place, t_address) {
	  	map.setCenter(plotPoint, 15,  custommap);
				if(show_this!="nh"){
					var html = pretty(t_place,t_address); 
					var marker = createMarker(plotPoint,html);
					map.addOverlay(marker);
				}
    }
	


	
	//==== base icon ===
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(25, 60);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(7, 58);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);


	//==== custom marker ===
	// Create a marker using the custom script eWindow
      function createMarker(point,html) {
			var icon = new GIcon(baseIcon);
  			icon.image = "./media/mt_tiles_png/sample_icon.gif";
			icon.iconSize = new GSize(25, 60);
  			var marker = new GMarker(point, icon);
  
			ew.openOnMarker(marker,html);
			// ========== Open the EWindow instead of a Google Info Window ==========
			GEvent.addListener(marker, "click", function() {
				ew.openOnMarker(marker,html);
       		});
        	return marker;
      }
      
	//==== Create map ===
	var map = new GMap2(document.getElementById("map"));
	
	   // ======== CUSTOM TILES ====================================================
      // ===== Write our own getTileUrl function ========
      // This particular one checks to see if the tiles are in range
      // if so, returns the URL of the actual tile
      // Otherwise returns the URL of the Google Map tileserver tile
      CustomGetTileUrl=function(a,b){
	  //	$timage_name.=" tile info ".($i+12305)." ".($z + 9644);
	
        if (b==15 || b==11 && a.x>=9646 && a.x<=9654 && a.y>=12306 && a.y<= 12321) {
			c_x= a.x -9644;
			c_y= a.y -12305;
			if(c_x<10){
				c_x="0"+c_x;
			}
			if(c_y<10){
				c_y="0"+c_y;
			}
			new_tile="./media/mt_tiles_png/mt_"+c_x+"_"+c_y+".png";
			//alert(new_tile);
          return new_tile;
        } else {
          return G_NORMAL_MAP.getTileLayers()[0].getTileUrl(a,b);
        }
      }
      
	  var copyright = new GCopyright(1,
          new GLatLngBounds(new GLatLng(40.705, -74.0148925781),new GLatLng(40.8221235752, -73.934) ),
          15, "Mappetite Maps");



      var copyrightCollection = new GCopyrightCollection('Mappetite Data:');
      var copyrightCollection = new GCopyrightCollection(' ');
      copyrightCollection.addCopyright(copyright);
      
		// allow zoom for only levels 13 (Neighborhood) or 15 (street)
      var tilelayers = [new GTileLayer(copyrightCollection,13,15)];
      tilelayers[0].getTileUrl = CustomGetTileUrl;
      
      // ============================================================
      // === If we know the copyright, return it, otherwise look for the G_NORMAL_MAP copyright =====      
      tilelayers[0].getCopyright = function(a,b) {
        var c= copyrightCollection.getCopyrightNotice(a,b);
        if (!c) {
          c = G_NORMAL_MAP.getTileLayers()[0].getCopyright(a,b);
        }
        return c;
      }
      
      
      
      var custommap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), "Mappetite Maps",{errorMessage:"Data Not Available"});
      map.addMapType(custommap);

      // === END CUSTOM TILES ===== 
	  
	  
      // ============================================================
      // === ALLOW ADDRESS LOOK UP ===== 
		// create geocoder to look up addresses
        geocoder = new GClientGeocoder();
      // === END ALLOW ADDRESS LOOK UP ===== 
	  
      // ============================================================
	// get main point
	if(rest_lat &&  rest_long){
	}else{
		rest_lat=40.759167;
		rest_long=-73.980278;
		rest_name +="<br>-ERROR-";
		rest_details="We're sorry, this location <br>was not found.";
	}
		plotPoint = new GLatLng(rest_lat,rest_long);
	
	// Set map center
       map.setCenter(plotPoint, 15,  custommap);
	
		
		
	 // ============================================================
      // === RESTRICT BOUNDS ===== 
		// Add a move listener to restrict the bounds range
      		GEvent.addListener(map, "move", function() {
        		checkBounds();
      		});

      	// The allowed region which the whole map must be within
      		var allowedBounds = new GLatLngBounds(new GLatLng(40.705, -74.0148925781),new GLatLng(40.8221235752, -73.934) );
			
      		// If the map position is out of range, move it back
      		function checkBounds() {
        		// Perform the check and return if OK
        		if (allowedBounds.contains(map.getCenter())) {
          			return;
        		}
        		// It`s not OK, so find the nearest allowed point and move there
        		var C = map.getCenter();
        		var X = C.lng();
        		var Y = C.lat();

        		var AmaxX = allowedBounds.getNorthEast().lng();
        		var AmaxY = allowedBounds.getNorthEast().lat();
        		var AminX = allowedBounds.getSouthWest().lng();
        		var AminY = allowedBounds.getSouthWest().lat();

        		if (X < AminX) {X = AminX;}
        		if (X > AmaxX) {X = AmaxX;}
        		if (Y < AminY) {Y = AminY;}
        		if (Y > AmaxY) {Y = AmaxY;}
        		//alert ("Restricting "+Y+" "+X);
        		map.setCenter(new GLatLng(Y,X));
      		}
      // === END RESTRICT BOUNDS ===== 
	  
	  
	  
	//==== Create an EWindow ===
	if(show_this!="nh"){
		ew = new EWindow(map, E_STYLE_7); 
		// Add EWindow
		map.addOverlay(ew);
	}
      
	// This function just makes a pretty table for the EWindow contents
	function pretty(a,b) {
        return '<div class="ewwrap" ><table border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff"><tr><td xclass="EWTitle"><div class="ewhead" >' + a + '</div></td></tr>' +
               '<tr><td ><div class="ewinterior" >' + b + '</div></td></tr></table></div>';
      }

    
	//==== Set up markers with EWindows  ===
	if(show_this!="nh"){
		showAddress(show_this,plotPoint,  rest_full_address,rest_name, rest_details);
	}
	  
	// close the EWindow if theres a map click 
	//GEvent.addListener(map, "click", function(marker,point) {
		//if (point) {
          //ew.hide();
        //}
      //});
  	}
    
// display a warning if the browser was not compatible
else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
}
	
	
	
	
    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/   
    // http://www.econym.demon.co.uk/googlemaps/
