var map;
var markerData = [];
var count = 0;
var LF = String.fromCharCode(10);
var mapX = -135;
var mapY = 79.5;
function startUp() {

	var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-180, -180), new GLatLng(180, 180)), 3, "TERAINK");
		var copyrightCollection = new GCopyrightCollection("Map Data: ");
		copyrightCollection.addCopyright(copyright);

		var myLayer = [new GTileLayer(copyrightCollection,3,8)];
		myLayer[0].getTileUrl = function(tile, zoom) {

        if (zoom==3 && tile.x>=0 && tile.x<=1 && tile.y>=0 && tile.y<= 1) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        } 
	if (zoom==4 && tile.x>=0 && tile.x<=3 && tile.y>=0 && tile.y<= 3) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        }
	if (zoom==5 && tile.x>=0 && tile.x<=7 && tile.y>=0 && tile.y<= 7) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        }
	if (zoom==6 && tile.x>=0 && tile.x<=15 && tile.y>=0 && tile.y<= 15) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        }
	if (zoom==7 && tile.x>=0 && tile.x<=31 && tile.y>=0 && tile.y<= 31) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        }
	if (zoom==8 && tile.x>=0 && tile.x<=63 && tile.y>=0 && tile.y<= 63) {
          return "http://nyseikatsu.com/api/191/"+ zoom +"/"+tile.x+","+tile.y+".jpg";
        }
	else {
          return "blank.jpg";
        }
		};
	
	var album = new GMapType(myLayer, new GMercatorProjection(9), "TERAINK");
	
  map = new GMap2(document.getElementById("gmap"));
  map.addMapType(album);
  map.setCenter(new GLatLng(mapY, mapX), 3);
  bounds = map.getBounds();
  map.setMapType(album);
  map.enableDoubleClickZoom();
  


//インフロート設定
var pos1 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5,5));
      pos1.apply(document.getElementById("nav"));
      map.getContainer().appendChild(document.getElementById("nav"));


//ページ移動イベント設定
var selObj = document.getElementById("gotoPage");
selObj.onchange = function() {
var gp = this.value;
if (gp == "") return;

var x = gp.split(",")[1];
var y = gp.split(",")[0];

if (x==-101.25 && y==83)
	{
	map.setCenter(new GLatLng(y, x), 4);
	}
else{
map.setCenter(new GLatLng(y, x), 5);
}
}



//移動範囲設定
      GEvent.addListener(map, "move", function() {
        checkBounds();
      });

      // The allowed region which the whole map must be within
      var allowedBounds = new GLatLngBounds(new GLatLng(66.5, -179), new GLatLng(85, -90));
      
      // 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));
      }  
  
  

}

window.onresize = function() {
map.checkResize();
var zoom = map.getBoundsZoomLevel(bounds);
map.setCenter(new GLatLng(mapY, mapX), zoom);
}

function MoveN() {
map.panDirection(0, 0.5);
}
function MoveS() {
map.panDirection(0, -0.5);
}
function MoveE() {
map.panDirection(-0.5, 0);
}
function MoveW() {
map.panDirection(0.5, 0);
}
function MoveR() {
map.setCenter(new GLatLng(79.5, -135), 3 );
}
onload   = startUp;
onunload = GUnload;
