// ******* ********* ********* ********* ********* ********* ********* *********
//
// 2008/05 by jap1968 - http://netpatia.blogspot.com
//
// ******* ********* ********* ********* ********* ********* ********* *********

var map;
var previousMaptypeName;

// ******* ********* ********* ********* ********* ********* ********* *********

function mapsLoaded() {
  previousMaptypeName = "";
  map = new google.maps.Map2(document.getElementById("map"));
  map.addControl(new google.maps.LargeMapControl());
  map.addControl(new google.maps.MapTypeControl());

  map.removeMapType(G_HYBRID_MAP);
  // map.removeMapType(G_NORMAL_MAP);
  map.enableDoubleClickZoom();
  map.enableScrollWheelZoom();
  map.enableContinuousZoom();

  var latLngIni = new google.maps.LatLng(39.99142, 116.39022); // National Stadium
//  var latLngIni = new google.maps.LatLng(39.90565, 116.39105);
  map.setCenter(latLngIni, 13);
  map.setMapType(G_SATELLITE_MAP);

  // ============================================================
  // ====== Custom maptype (http://ditu.google.cn) ==============
  // Creation of a new GProjection. Based on:
  // http://groups.google.com/group/Google-Maps-API-China/browse_thread/thread/af392a78338a462a#

  var dituProj = new google.maps.MercatorProjection(20);

  dituProj.fromLatLngToPixel = function(latlng, zoom) {
    newLatLng = new google.maps.LatLng(latlng.lat() + 0.00143, latlng.lng() + 0.00613);
    return (G_NORMAL_MAP.getProjection()).fromLatLngToPixel(newLatLng, zoom);
  }

  dituProj.fromPixelToLatLng = function(pixel, zoom, unbounded) {
    if (isNaN(pixel.x) || isNaN(pixel.x)) {
      // En caso de llamada con parametros incorrectos!
      alert("Pixel: " + pixel + ", zoom: " + zoom + ", unbounded: " + unbounded);
      return map.getCenter();
    }
    var dituLatLng;
    if (unbounded === undefined) {
      dituLatLng = (G_NORMAL_MAP.getProjection()).fromPixelToLatLng(pixel, zoom);
    }
    else {
      dituLatLng = (G_NORMAL_MAP.getProjection()).fromPixelToLatLng(pixel, zoom, unbounded);
    }
    newLatLng = new google.maps.LatLng(dituLatLng.lat() - 0.00143, dituLatLng.lng() - 0.00613);
    return newLatLng;
  }

  dituGetTileUrl = function(a, b){
    b = this.maxResolution() - b;
    var server = (a.x + a.y) % 4;
    return "http://mt" + server + ".google.cn/mt?v=cn1.0&hl=zh-CN&x=" + a.x + "&y=" + a.y + "&zoom=" + b;
  } 
  
  var tilelayers = [new google.maps.TileLayer(new google.maps.CopyrightCollection("Map: http://ditu.google.cn"), 8, 17)];
  tilelayers[0].getTileUrl = dituGetTileUrl;
  tilelayers[0].getCopyright = function(a,b) {
    return {prefix:"Map: ", copyrightTexts:["http://ditu.google.cn"]};
  }
  
  var dituMap = new google.maps.MapType(tilelayers, dituProj, "DITU", {errorMessage:"Error loading images from Google DITU"});
  map.addMapType(dituMap);

  marker = new google.maps.Marker(latLngIni);
  var html ='<div><a href="http://en.beijing2008.cn/cptvenues/venues/nst/" target="_blank">National Stadium</a><br>Opening Ceremony<br>Beijing 2008</div>';
  google.maps.Event.addListener(marker, "click", function() {
    marker.openInfoWindow(html);
  });
  map.addOverlay(marker);

  // ============================================================
}

// ******* ********* ********* ********* ********* ********* ********* *********

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

// ******* ********* ********* ********* ********* ********* ********* *********