<?xml version="1.0" encoding="UTF-8"?>
<Module>
<!--
Copyright 2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
  <ModulePrefs
    title="Embedded KML Viewer"
    title_url="http://www.gmodules.com/ig/creator?synd=open&amp;url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml"
    directory_title="Embedded KML Viewer"
    thumbnail="http://code.google.com/apis/kml/embed/res/embedkmlgadget-thumb.png"
    screenshot="http://code.google.com/apis/kml/embed/res/embedkmlgadget-screen.png"
    author="Roman Nurik"
    author_email="api.roman.public+embedkmlgadget@gmail.com"
    author_affiliation="Google"
    width="500"
    height="400"
    description="Embed a KML file in your page using Google Earth or Google Maps!">
    <Locale lang="en" country="us"/>
  </ModulePrefs>
  
  <UserPref name="kml_url" display_name="KML or My Maps URL"/>
  <UserPref name="view_mode" display_name="View mode" default_value="earth" datatype="enum">
    <EnumValue value="maps" display_value="2D (Google Maps)"/>
    <EnumValue value="earth" display_value="3D (Google Earth Plugin)"/>
  </UserPref>
  <UserPref name="earth_2d_fallback" display_name="3D - Fall back to 2D on error" datatype="bool"/>
  <UserPref name="earth_fly_from_space" display_name="3D - Fly in from space" default_value="true" datatype="bool"/>
  <UserPref name="earth_show_nav_controls" display_name="3D - Show navigation controls" default_value="1" datatype="bool"/>
  <UserPref name="earth_show_buildings" display_name="3D - Show buildings" default_value="1" datatype="bool"/>
  <UserPref name="earth_show_terrain" display_name="3D - Show 3D terrain" default_value="1" datatype="bool"/>
  <UserPref name="earth_show_roads" display_name="3D - Show roads" default_value="1" datatype="bool"/>
  <UserPref name="earth_show_borders" display_name="3D - Show borders and labels" default_value="1" datatype="bool"/>
  <UserPref name="earth_sphere" display_name="3D - Sphere" default_value="earth" datatype="enum">
    <EnumValue value="earth" display_value="Earth"/>
    <EnumValue value="sky" display_value="Sky"/>
    <EnumValue value="moon" display_value="Moon"/>
    <EnumValue value="mars" display_value="Mars"/>
  </UserPref>
  <UserPref name="maps_zoom_out" display_name="2D - Zoom out on load" datatype="bool"/>
  <UserPref name="maps_default_type" display_name="2D - Default Map Type" default_value="map" datatype="enum">
    <EnumValue value="map" display_value="Map"/>
    <EnumValue value="satellite" display_value="Satellite"/>
    <EnumValue value="hybrid" display_value="Hybrid"/>
    <EnumValue value="terrain" display_value="Terrain"/>
  </UserPref>
  
  <Content type="html"><![CDATA[
<script type="text/javascript" src="http://www.google.com/jsapi?hl=en&key=ABQIAAAAKkfkHb2nXsD0o1OX2TbdkRTZdFmpiU8vv3PBIA-hr88t-5BzzxQjEeEmKaZUy66ADwTlY8x2M14hHg"></script>
<script type="text/javascript">
  google.load('earth', '1');
  google.load('maps', '2');

  var ge = null;
  var map = null;
  var prefs = new _IG_Prefs();
  
  var appPath = 'http://code.google.com/apis/kml/embed/';

  /**
   * Prepare to create the gadget UI.
   */
  function initGadget(viewMode) {
    // Bugfix for IE6.
    var c = document.getElementById('mapcontainer');
    while (c && c.offsetParent != c) {
      c.style.height = '100%';
      c = c.offsetParent;
    }
    
    createGadgetUI(prefs.getString('view_mode'));
  }
  
  /**
   * Create the gadget UI, which consists of either a Google Map or a
   * Google Earth Plugin instance.
   */
  function createGadgetUI(viewMode) {
    var kmlUrl = prefs.getString('kml_url');
    
    // Clean kmlUrl to accept My Maps URLs.
    myMapsMatch = kmlUrl.match(/maps\.google\.[a-z]+.*msid=([0-9a-f.]+)/);
    if (myMapsMatch) {
      kmlUrl = 'http://maps.google.com/maps/ms?msa=0&output=kml&msid=' +
          myMapsMatch[1];
    }
    
    if (viewMode == 'maps') {
      // Create a Google Map.
      map = new google.maps.Map2(document.getElementById('mapcontainer'));
      map.setCenter(new google.maps.LatLng(0, 0), 1);
      map.setUIToDefault();

      var defaultMapType = prefs.getString('maps_default_type');
      if (defaultMapType == 'map')
        map.setMapType(G_NORMAL_MAP);
      else if (defaultMapType == 'satellite')
        map.setMapType(G_SATELLITE_MAP);
      else if (defaultMapType == 'hybrid')
        map.setMapType(G_HYBRID_MAP);
      else if (defaultMapType == 'terrain')
        map.setMapType(G_PHYSICAL_MAP);

      if (kmlUrl) {
        // Apply a cachebuster.
        kmlUrl += (kmlUrl.indexOf('?') >= 0 ? '&' : '?') +
            Number(new Date()).toString();
        
        var geoXml = new google.maps.GeoXml(kmlUrl);
        map.addOverlay(geoXml);
        
        if (!prefs.getBool('maps_zoom_out'))
          geoXml.gotoDefaultViewport(map);
      }
    } else if (viewMode == 'earth') {
      if (!google.earth.isInstalled() &&
          prefs.getBool('earth_2d_fallback')) {
        // Fall back to Maps if the Google Earth Plugin isn't installed.
        createGadgetUI('maps');
        return;
      }
      
      // Determine which sphere to create (earth/sky/moon/mars).
      var createOptions = {};
      var sphere = prefs.getString('earth_sphere') || 'earth';

      if (sphere == 'mars' || sphere == 'moon')
        createOptions = { database: 'http://khmdb.google.com/?db=' + sphere };

      google.earth.createInstance('mapcontainer', function(pluginInstance) {
        ge = pluginInstance;
        
        if (sphere == 'sky')
          ge.getOptions().setMapType(ge.MAP_TYPE_SKY);      
        
        ge.getWindow().setVisibility(true);

        // Set options.
        ge.getNavigationControl().setVisibility(
            prefs.getBool('earth_show_nav_controls') ? ge.VISIBILITY_AUTO : ge.VISIBILITY_HIDE);

        if (sphere == 'earth') {
          ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS,
              prefs.getBool('earth_show_buildings'));

          ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN,
              prefs.getBool('earth_show_terrain'));

          ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS,
              prefs.getBool('earth_show_roads'));

          ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS,
              prefs.getBool('earth_show_borders'));
        }

        if (kmlUrl) {
          // If loading KML, create a KmlNetworkLink with a flyToView=1.
          var link = ge.createLink('');
          link.setHref(kmlUrl);

          var nl = ge.createNetworkLink('');
          nl.setLink(link);
          nl.setFlyToView(true);
          
          var originalFlyToSpeed = ge.getOptions().getFlyToSpeed();
          if (!prefs.getBool('earth_fly_from_space'))
            ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);
          
          ge.getFeatures().appendChild(nl);
          
          window.setTimeout(function() {
            ge.getOptions().setFlyToSpeed(originalFlyToSpeed);
          }, 500);
        }
      }, function(error) {
        if (prefs.getBool('earth_2d_fallback')) {
          // Upon plugin creation failure, use Maps as a fallback.
          createGadgetUI('maps');
        }
      }, createOptions);
    }
  }
  
  google.setOnLoadCallback(initGadget);
</script>
<div id="mapcontainer" style="width: 100%; height: 100%;"></div>
]]></Content>
</Module>

