<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs
    title="Sea Level Rise"
    description="Show regions affected by arbitrary rise in sea level"
    author="Michael Kosowsky"
    author_email="sealevel_mapplet+nospam@heywhatsthat.com"
    author_location="Lincolnville, Maine, USA"
    author_affiliation="heywhatsthat.com"
    screenshot="http://www.heywhatsthat.com/mapplets/sealevel-screenshot.png"
    thumbnail="http://www.heywhatsthat.com/mapplets/sealevel-thumbnail.png"
    scrolling="true"
    >
  <Require feature="sharedmap"/>
  <Require feature="setprefs" />
  <Require feature="analytics"/>
  <Require feature="dynamic-height"/>
</ModulePrefs>
<UserPref name="use_metric"  datatype="hidden" default_value="0" />
<UserPref name="redlevel"    datatype="hidden" default_value="1" />
<UserPref name="orangelevel" datatype="hidden" default_value=""  />
<UserPref name="yellowlevel" datatype="hidden" default_value="3" />

<Content type="html"><![CDATA[

<style type="text/css">
  .rbutton { text-align: right; color: blue; cursor: pointer; text-decoration: underline; -moz-user-select: none }
  .rbutton:hover { color: black }
  .rbutton:visited { color: blue }
</style>

<small>

<div style="text-align: right">
<span class="rbutton"><a class="rbutton" target="_blank" href="http://www.heywhatsthat.com">HeyWhatsThat</a></span>
&nbsp;
<span class="rbutton"><a class="rbutton" target="_blank" href="http://www.heywhatsthat.com/mappletfaq.html">FAQ / Sitemap</a></span>
<br>
<span class="rbutton" title="Sign up for HeyWhatsThat email updates" onclick="signup()">Sign up</span>
&nbsp;
<script>
document.write('<span class="rbutton"><a class="rbutton" target="_blank" href="mailto:comments-sealevel-mapplet@hey' + '' + 'what' + 'sthat.com">Comments?</a></span>');
</script>
<br>
<!-- <span class="rbutton"><a class="rbutton" target="_blank" href="http://www.heywhatsthat.com/alaska.html">Can you see Russia from Alaska?</a></span> -->
<span class="rbutton"><a class="rbutton" target="_blank" href="http://wisp.heywhatsthat.com">New: HWT Fixed Wireless Site</a></span>
</div>
<!--
<p>
<i>
We're currently seeing heavy demand on our servers,
so for now you'll have to hit <b>Go</b> whenever
you move or zoom the map to load the overlay images.
<p>
Visit <a href="http://www.heywhatsthat.com" target="_blank">HeyWhatsThat.com</a>
where we tell you the mountains you can see when you're standing almost anywhere in the world.
</i>
<p>
-->

<form name="q" onsubmit="return false">
Threshholds:<br>

<table style="font-size: small">
<tr>
  <td width=10 style="background-color: red"></td>
  <td>Red</td>
  <td><input size=5 name="redlevel" onchange="set_level('redlevel', this.value)"></input></td>
  <td id=u1></td>
</tr>
<tr>
  <td></td>
  <td colspan=3><input type="checkbox" name="include0">include sea level (0)</input></td>
</tr>
<tr>
  <td width=10 style="background-color: orange"></td>
  <td>Orange</td><td><input size=5 name="orangelevel" onchange="set_level('orangelevel', this.value)"></input></td>
  <td id=u2></td>
</tr>
<tr>
  <td width=10 style="background-color: yellow"></td>
  <td>Yellow</td><td><input size=5 name="yellowlevel" onchange="set_level('yellowlevel', this.value)"></input></td>
  <td id=u3></td>
</tr>
</table>

<center>
<input type="radio" name="units" value="0" onclick="set_units(0)">English</input>
<input type="radio" name="units" value="1" onclick="set_units(1)">Metric</input>
<br>
<input type="submit" value="Go" onclick="go()"></input>
</center>
</form>

<p>
Notes:<br>
According to the IPCC's 2007 predictions, potential sea level rise
by 2100 is less than 1 meter, but it's a controversial subject and estimates
of 5m and even higher have support in the scientific community.
<p>
This mapplet's analysis suffers from the shortcomings
described in a series of posts at Free Geography Tools
(<a target="_blank" href="http://freegeographytools.com/?p=97">1</a>
 <a target="_blank" href="http://freegeographytools.com/?p=178">2</a>
 <a target="_blank" href="http://freegeographytools.com/?p=188">3</a>
 <a target="_blank" href="http://freegeographytools.com/?p=193">4</a>
 <a target="_blank" href="http://freegeographytools.com/?p=199">5</a>
 <a target="_blank" href="http://freegeographytools.com/?p=198">6</a>
 <a target="_blank" href="http://freegeographytools.com/2007/sea-level-rise-google-mapplet">7</a>),
specifically that the data's resolution is too low 
for accurate small-scale predictions, and that we are not taking into account
whether ocean water can actually reach a particular location.
<p>
For simulations that don't suffer the same shortcomings, visit the
Free Geography Tools URLs listed above, which also show flooded buildings,
and <a target="_blank" href="http://www.architecture2030.org/current_situation/coastal_impact.html">Architecture2030</a>,
which shows the results of accurate predictions for over a hundred US coastal cities.
<p>
The data's vertical resolution is one meter, so threshholds
you enter will be rounded to the nearest meter.
In particular, if using English units you'll find your
entry rounded to 0, 3, 7, 10, 13, ...
<p>
A similar non-mapplet app can be found at <a href="http://flood.firetree.net" target="_blank">Flood Maps</a>.
<p>
<!--
<i>
This mapplet runs most reliably on <a href="http://www.mozilla.com/firefox" target="_blank">Firefox 1.5 or later</a> at this time.
</i>
-->
</small>

<script>
_IG_Analytics("UA-2222064-1", "/mapplets/sealevel");
_IG_AdjustIFrameHeight();  // need this and dynamic-height feature as of about 10/20/07

var AUTO_UPDATE = 1;

function encodeURI_more(s) {
  s = encodeURI(s);
  s = s.replace(/\&/g, '%26');
  s = s.replace(/\=/g, '%3D');
  s = s.replace(/\+/g, '%2B');
  return s;
}

function signup() {
  var email = prompt("Give us an email address and\nwe'll send you news about HeyWhatsThat", '');
  if (email)
    _IG_FetchContent('http://www.heywhatsthat.com/bin/subscribe.cgi?src=sealevelmapplet&email=' + encodeURI_more(email),
      function(s) {
        var a = s.replace(/\n/g,'');
	if (a)
          alert(a + ' subscribed.  Thanks.');
        else
	  alert('Subscription failed for ' + email);
      }
    );
}

function radio_set(r, v) {
  for (var i = 0; i < r.length; i++) if (r[i].value == v) r[i].checked = 1;
}
function radio_get(r) {
  for (var i = 0; i < r.length; i++) if (r[i].checked) return r[i].value;
}
var FEET_PER_METER = 1 / .3048;
function units_to_meters(x) {
  if (x == '')
    return x;
  if (use_metric)
    return Math.round(x);
  else
    return Math.round(x / FEET_PER_METER);
}
function meters_to_units(x) {
  if (x == '' || use_metric)
    return x;
  return Math.round(x * FEET_PER_METER);
}

function set_level(s, x) {
  var xx = units_to_meters(isNaN(x - 0)? '' : x);
  prefs.set(s, xx);
  document.q[s].value = meters_to_units(xx);
}
function set_units(b) {
  use_metric = b;
  prefs.set('use_metric', use_metric);
  radio_set(document.q.units, use_metric);
  document.q.redlevel.value    = meters_to_units(prefs.getString("redlevel"));
  document.q.orangelevel.value = meters_to_units(prefs.getString("orangelevel"));
  document.q.yellowlevel.value = meters_to_units(prefs.getString("yellowlevel"));
  document.getElementById("u1").innerHTML = use_metric? 'm' : 'ft';
  document.getElementById("u2").innerHTML = use_metric? 'm' : 'ft';
  document.getElementById("u3").innerHTML = use_metric? 'm' : 'ft';
}


function go() {
  map.clearOverlays();

	// sort the levels
  var a = [];
  if (prefs.getString("redlevel") != '')
    a.push({high: prefs.getString("redlevel") - 0,    color: 'ff000040'});
  if (prefs.getString("orangelevel") != '')
    a.push({high: prefs.getString("orangelevel") - 0, color: 'ffa50040'});
  if (prefs.getString("yellowlevel") != '')
    a.push({high: prefs.getString("yellowlevel") - 0, color: 'ffff0040'});
  a.sort(function(a, b) { return a.high - b.high; });

  if (a.length <= 0)
    return;

  for (var i = 0; i < a.length - 1; i++)
    if (a[i].high == a[i + 1].high) {
      alert("Can't have the same height in more than one box.\n(Threshholds are rounded to the nearest meter; if you're using English units, you'll find your entries rounded to 0, 3, 7, 10, 13, ...)");
      return;
    }

  var limits = '';
  var j = 0;
  var lasthighplus1 = '';
  for (var i = 0; i < a.length; i++) {
  		// if we don't want elev 0 colored, create an extra level
    if (lasthighplus1 <= 0 && a[i].high > 0 && !document.q.include0.checked) {
      limits +=   '&low'   + j + '=' + lasthighplus1
  	        + '&high'  + j + '=-1'
  	        + '&color' + j + '=' + a[i].color;
      lasthighplus1 = 1;
      j++;
    }

    limits +=   '&low'   + j + '=' + lasthighplus1
  	      + '&high'  + j + '=' + a[i].high
  	      + '&color' + j + '=' + a[i].color;
    lasthighplus1 = a[i].high + 1;
    j++;
  }

		// sealevel0 .. sealevel1
  //var host = 'sealevel' + Math.floor(2 * Math.random()) + '.heywhatsthat.com';
  var host = 'sealevel.heywhatsthat.com';

  tile_overlay = new GTileLayerOverlay(new GTileLayer(copyright_collection, copyright_min_zoom, copyright_max_zoom, {
    tileUrlTemplate: 'http://' + host + '/bin/sealevel_tiles.cgi'
               + '?src=sealevelmapplet'
  	       + limits
	       + '&zoom={Z}&x={X}&y={Y}' }));
  map.addOverlay(tile_overlay);  
}


var map = new GMap2();
  //  9/9/2008 reduce server load a bit by requring explicit clicks ...
  //  5/31/2011 un-reduced
  //  BUG: This doesn't work if you do it AFTER the set_units call??
if (!AUTO_UPDATE) {
  GEvent.addListener(map, "movestart", function() { map.clearOverlays(); });
  GEvent.addListener(map, "zoomend", function() { map.clearOverlays(); });
}

var prefs = new _IG_Prefs(__MODULE_ID__);
var tile_overlay;
var use_metric;

//var copyright_min_zoom = 0;
//var copyright_max_zoom = 17;
//var copyright_collection = new GCopyrightCollection('Contours (C) 2007');
//copyright_collection.addCopyright(new GCopyright(2, new GLatLngBounds(new GLatLng(-54,-180), new GLatLng(60,180)), copyright_min_zoom, 'Michael Kosowsky'));
var copyright_min_zoom = null;
var copyright_max_zoom = null;
var copyright_collection = null;

set_units(prefs.getInt("use_metric"));  // this also sets initial levels

if (AUTO_UPDATE)
  go();

</script>
]]></Content>
</Module>
