var clickBox = null;

function createProjection(maxZoom) {
    var maxZoom = 17;

    // create a new Mercator projection instance
    var proj = new GMercatorProjection(maxZoom+1);
    
    // add an exposed copy of the tileBounds array
    proj.tileBounds = [];
    var c=1;
    for(var d=0;d<=maxZoom;d++){
        proj.tileBounds.push(c);
        c*=2;
    } 
		
    // == a method that checks if the y value is in range
    // == but *doesnt* wrap the x value ==
    proj.tileCheckRange=function(a,b,c){
        var d=this.tileBounds[b];
        if (a.y<0||a.y>=d) {
            return false;
        }
        if(a.x<0||a.x>=d) {
            return false;
        }
        return true
    }

    // == a method that returns the width of the tilespace ==      
    proj.getWrapWidth=function(zoom) {
        return 99999999999999;
    } 	
    
    return proj;
}

function createLayer(proj, subdir,longName,shortName) {
    // ============================================================
    // == 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 a blank tile
    CustomGetTileUrl=function(a,b){
        if (b < maxZoom+1) {
            return baseURL+subdir+"/" + a.x + "_" + a.y + "_" + b + ".png";
        } else {
            return "blank.png";
        }
    }
    
    var copyright = new GCopyright(1,
        new GLatLngBounds(new GLatLng(0,0),new GLatLng(0,0)),
        1, longName);

    var copyrightCollection = new GCopyrightCollection('Map Data:');
    copyrightCollection.addCopyright(copyright);

    var tilelayers = [new GTileLayer(copyrightCollection,1,maxZoom)];
    tilelayers[0].getTileUrl = CustomGetTileUrl;
    
    var custommap = new GMapType(tilelayers, proj, shortName, {});
    return custommap;
}

function clearMapTypes(map) {
    var layers = map.getMapTypes();
    
    for (var i=0; i < layers.length; i++) {
        map.removeMapType(layers[i]);
    }
}

function addMapTypes(map, types) {
    for (var i=0; i < types.length; i++) {
        map.addMapType(types[i])
    }
}

function setMapTypes(map, types) {
    clearMapTypes(map);
    addMapTypes(map, types);
    
    map.setMapType(types[0]);
    //map.setCenter(new GLatLng(0,0), 1, types[0]);
}

function windowHeight() {
  // Standard browsers (Mozilla, Safari, etc.)
  if (self.innerHeight) {
    return self.innerHeight;
  }
  // IE 6
  if (document.documentElement && document.documentElement.clientHeight) {
   return document.documentElement.clientHeight;
  }
  // IE 5
  if (document.body) {
    return document.body.clientHeight;
  }
  // Just in case. 
  return 0;
}

function createMap(proj,layers,divId,queryScript) {
    // create the map
    map = new GMap2(document.getElementById(divId),{"mapTypes":layers});
    document.getElementById(divId).style.backgroundColor="white"; 
            
    map.addMapType(layers[0]);
            
    for (var i=0; i < layers.length; i++) {
        map.addMapType(layers[i]);
    }
    
    // add some controls
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl(layers),new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(0,0)));
            
    //ov = new GOverviewMapControl(new GSize(200,200));
    //map.addControl(ov,new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(0,0)));
    //GEvent.addListener(ov.getOverviewMap(), "zoomend", function() {
    //    ov.getOverviewMap().setZoom(0);
    //});
    //ov.hide()

    // center the map
    map.setCenter(new GLatLng(0,0), 1, layers[0]);
            
	// for scrollling zoom
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
				
	function wheelZoom(a) {
		if (a.detail) { // Firefox
			if (a.detail < 0 && map.getZoom() < maxZoom) {
                map.zoomIn();
            } else if (a.detail > 0) { 
                map.zoomOut(); 
            }
		} else if (a.wheelDelta) { // IE
			if (a.wheelDelta > 0 && map.getZoom() < maxZoom){ 
                map.zoomIn(); 
			} else if (a.wheelDelta < 0){ 
                map.zoomOut(); 
            }
		}
	}
	
	// these listeners are to capture the mouse scroll wheel activity
	GEvent.addDomListener(document.getElementById(divId), "DOMMouseScroll", wheelZoom); // Firefox
	GEvent.addDomListener(document.getElementById(divId), "mousewheel",     wheelZoom); // IE

    GEvent.addListener(map, "click", function(overlay, point) {
        if (overlay){	// marker clicked
            //overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
        } else if (point) {	// background clicked
            
        }
    });

    GEvent.addListener(map, "zoomend", function(oldLevel,newLevel) {
        if (newLevel > maxZoom) {
             map.setZoom(maxZoom);
        }
    });

    GEvent.addListener(map, "maptypechanged", possiblyShowLegend);

	// code for drawing boxes to select nodes
    var firstClick = null;
    var secondClick = null;
    
    function drawBox(p1,p2) {
        var boxPoints = new Array();
        var p3 = new GLatLng(p1.lat(),p2.lng());
        var p4 = new GLatLng(p2.lat(),p1.lng());
        boxPoints.push(p1);
        boxPoints.push(p3);        
        boxPoints.push(p2);
        boxPoints.push(p4);
        boxPoints.push(p1);
        
        clickBox = new GPolygon(boxPoints,"#000000",1,.8,"#ffffff",.5);
        map.addOverlay(clickBox);
        clickBox.show()
    }

    function infoClick(marker, point)
    {
        if (firstClick == null) 
        {
            if (clickBox != null) {
                map.removeOverlay(clickBox);
                clickBox = null;
                showLegend();
                clearSidebar();
            } else {
                firstClick = point;
                secondClick = point;
            }        
        } else if (secondClick != null) {
            var zoom = map.getZoom();
            var c1 = proj.fromLatLngToPixel(firstClick,zoom);
            var c2 = proj.fromLatLngToPixel(secondClick,zoom);
            
            var tileSize = 256.0*Math.pow(2,zoom);

            var url = queryScript;
            url += "?x1="+(0.0 + ((((c1.x/tileSize) - 0.5))*2.0));
            url += "&y1="+(0.0 - ((((c1.y/tileSize) - 0.5))*2.0));
            url += "&x2="+(0.0 + ((((c2.x/tileSize) - 0.5))*2.0));
            url += "&y2="+(0.0 - ((((c2.y/tileSize) - 0.5))*2.0));
            
            firstClick = null;
            
            getMarkers(url,1000000,false);
        }
    }

    function infoHover(point) {
        if (firstClick != null && point != null) {
            if (clickBox != null) {
                map.removeOverlay(clickBox);
            }
            drawBox(firstClick,point);
            secondClick = point;
        }
    }
    
    function infoEsc() {
    	if (clickBox != null) {
            map.removeOverlay(clickBox);
            clickBox = null;
			showLegend();
			clearSidebar();
			firstClick = null;
        }
    }
    
    GEvent.addListener(map,"click",infoClick);   
    GEvent.addListener(map,"mousemove",infoHover);
    shortcut.add("Esc",infoEsc);	      

    
    // 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/
    // http://gmaps-utility-library.googlecode.com/svn/trunk/labeledmarker/release/examples/manhattan/index.html
    
    return map;
}
