﻿
var map;
var infoWindow = new google.maps.InfoWindow();
var GMapTileDataMap = "/MapData/niv1/";
var GMapClearTile = "clear.jpg";
var GMapZoomOffset = 3;

var bazPdfMapType = new google.maps.ImageMapType({
    getTileUrl: function (coord, zoom) {
        return GMapTileDataMap+GetBazaarTile(coord.x,coord.y,zoom);
    },
    tileSize: new google.maps.Size(256, 256),
    isPng: false,
    maxZoom: 7,
    minZoom: 3,
    name: "Bazaar"
});

//Generates the custom tile url used to diplay the correct tiles.
function GetBazaarTile(x,y,zoom) {

    var fakeZoom = zoom - GMapZoomOffset;
    var displayZoom = fakeZoom + 1;
    var offset = (fakeZoom < 0) ? 0 : (GMapZoomOffset << fakeZoom);
    var tileLimit = 1 << displayZoom;

    x -= offset;
    y -= offset;

    return (x < 0 || y < 0 || x >= tileLimit || y >= tileLimit) ? GMapClearTile : displayZoom + "_" + x + "_" + y + ".jpg";
}


//polygon extension
if (!google.maps.Polygon.prototype.getBounds) {

﻿  google.maps.Polygon.prototype.getBounds = function(latLng) {

﻿  ﻿  var bounds = new google.maps.LatLngBounds();
﻿  ﻿  var paths = this.getPaths();
﻿  ﻿  var path;
﻿  ﻿  
﻿  ﻿  for (var p = 0; p < paths.getLength(); p++) {
﻿  ﻿  ﻿  path = paths.getAt(p);
﻿  ﻿  ﻿  for (var i = 0; i < path.getLength(); i++) {
﻿  ﻿  ﻿  ﻿  bounds.extend(path.getAt(i));
﻿  ﻿  ﻿  }
﻿  ﻿  }

﻿  ﻿  return bounds;
﻿  }
}

function initMap() {
    var myLatlng = new google.maps.LatLng(0, 0);
    var myOptions = {
        //disableDefaultUI: true,
        panControl: true,
        zoomControl: true,
        scaleControl: false,
        streetViewControl: false,
        center: myLatlng,
        zoom: 2,
        backgroundColor: "#fff",
        mapTypeControlOptions: {
            mapTypeIds: ["bazPdf"]
        }
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    map.mapTypes.set("bazPdf", bazPdfMapType);
    map.setMapTypeId("bazPdf");
    
    var Bound1 = new google.maps.LatLng(-50, -80);
    var Bound2 = new google.maps.LatLng(50, 80);
    
    var strictBounds = new google.maps.LatLngBounds(Bound1,Bound2);

    google.maps.event.addListener(map, 'idle', function () {
        if (strictBounds.contains(map.getCenter())) return;

        var c = map.getCenter(),
         x = c.lng(),
         y = c.lat(),
         maxX = strictBounds.getNorthEast().lng(),
         maxY = strictBounds.getNorthEast().lat(),
         minX = strictBounds.getSouthWest().lng(),
         minY = strictBounds.getSouthWest().lat();

        if (x < minX) x = minX;
        if (x > maxX) x = maxX;
        if (y < minY) y = minY;
        if (y > maxY) y = maxY;

        map.panTo(new google.maps.LatLng(y, x));

    });
    
}

