﻿//<![CDATA[

// Load our favorite javascript libraries
    //google.load("prototype", "1.6");
    //google.load("maps", "2");

// Image base, for URL generation

    function load(strPath, strFile, mapid) {
        var strImagePath = strPath;
        var strImageFile = strFile;
        var initialZoom=2;

    // Incompatible browser
        if (!GBrowserIsCompatible())
        {
            alert("Sorry, the Google Maps API used for this image zoom is not compatible with this browser.");
            return;
        }

    // Create the map
        var map = new GMap(document.getElementById(mapid));
        map.addControl(new GLargeMapControl());
        map.getMinimumResolution = function() { return 6 };
        map.getMaximumResolution = function() { return 17 };


    // You could enable scroll wheel zooming here.  Unfortunately, I found that
    // interacted strangely with the normal scroll bar on this page, so I have
    // disabled it for this demo.  I would only advise enabling this feature
    // if your viewer page is of fixed dimensions and does not scroll.
        map.enableDoubleClickZoom();
	    map.enableContinuousZoom();
        //map.enableScrollWheelZoom();

    // Custom tile URL
        myTileURL=function(p,z){
            //alert("/images/zoomtile.aspx?path=" + strImagePath + "&file=" + strImageFile + "&zoom=" + z + "&x=" + p.x + "&y=" + p.y);
            return "/images/zoom/tiler.aspx?path=" + strImagePath + "&file=" + strImageFile + "&zoom="+z+"&x="+p.x+"&y="+p.y;
            /*alert("/images/zoom/"+img_base+"/"+z+"-"+p.x+"-"+p.y+".jpg");
            return "/images/zoom/"+img_base+"/"+z+"-"+p.x+"-"+p.y+".jpg";*/
            }

     // Create and load new copyright data for this site
        var copyright = new GCopyrightCollection('');
        copyright.addCopyright(
            new GCopyright(1,
                new GLatLngBounds(
                    new GLatLng(0,0),
                    new GLatLng(0,0)
                    ),
                1,
                ""
                )
            );

    // Create a new projection so we can control the wrap functionality and
    // prevent the map from wrapping the image at the X axis.
    // Code taken from the example here:
    // http://groups.google.com/group/Google-Maps-API/browse_thread/thread/16777e5a58ce37a1
        var proj = new GMercatorProjection(5);
        proj.tileCheckRange = function(p,z,s) {
                if (p.y < 0 || p.x < 0) {
                    return false;
                }
                var max=Math.pow(2,z);
                if (p.y >= max || p.x >= max){
                    return false;
                }
                return true;
            }
        // Custom coordinate handlers force the image to appear within +-50 lat
        // and lng so that we can easily prevent people from dragging the image
        // outside of its boundaries.
        proj.fromPixelToLatLng = function(pixel, zoom, unbounded) {
                var max = Math.pow(2,zoom)*256;
                var lng = -(pixel.x / max) * 100 + 50;
                var lat = (pixel.y / max) * 100 - 50;
                return new GLatLng(lat, lng, unbounded);
            }
        proj.fromLatLngToPixel = function(latlng, zoom) {
                var max = Math.pow(2,zoom)*256;
                var x = -max * ((latlng.lng() - 50) / 100);
                var y = max * ((latlng.lat() + 50) / 100);
                return new GPoint(x, y);
            }

    // Prevent people from scrolling outside the boundaries of our image

        // Add a move listener to restrict the bounds range
        GEvent.addListener(
            map,
            "move",
            function() {
                checkBounds();
            }
            );

        // If the map position is out of range, move it back
        function checkBounds() {

            // Get some info about the current state of the map
            var C   = map.getCenter();
            var lng = C.lng();
            var lat = C.lat();
            var B  = map.getBounds();
            var sw = B.getSouthWest();
            var ne = B.getNorthEast();

            // Figure out if the image is outside of the artificial boundaries
            // created by our custom projection object.
            var new_lat = lat;
            var new_lng = lng;
            if (sw.lat() < -50) {
                new_lat = lat - (sw.lat() + 50);
            }
            else if (ne.lat() > 50) {
                new_lat = lat - (ne.lat() - 50);
            }
            if (sw.lng() < -50) {
                new_lng = lng - (sw.lng() + 50);
            }
            else if (ne.lng() > 50) {
                new_lng = lng - (ne.lng() - 50);
            }
            // If necessary, move the map
            if (new_lat != lat || new_lng != lng) {
                map.setCenter(new GLatLng(new_lat,new_lng));
            }
        }

        // Create the tile later and apply it to the map and set max zoom i.e GTileLayer(copyright, MIN, MAX)
        var tilelayers = [new GTileLayer(copyright,2,4)];
        tilelayers[0].getTileUrl = myTileURL;
        var tileMap = new GMapType(tilelayers, proj, 'Zoom');
        tileMap.getTextColor = function() {return "#333333";};

    // Activate the map with the custom tile layers
        map.addMapType(tileMap);
        map.setCenter(new GLatLng(-50,50), 2, tileMap);
        return false;
    }

    //]]>
