<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <ModulePrefs
    title="Recent Bird Sightings"
    title_url="http://ebird.org"
    screenshot="http://ebird.org/wstools1.1/google/loc-recent/images/ebird_loc_recent.jpg"
    thumbnail ="http://ebird.org/wstools1.1/google/loc-recent/images/ebird-logo.gif"
    scrolling="true"
    height="600"
    width="400"
    author="Tim Lenz"
    author_email="clois-api@cornell.edu"
    description="Summary of sightings from up to 10 eBird locations over the past month"
    author_affiliation="Cornell Lab of Ornithology"
    author_location="Ithaca, NY, USA"
    >
    <Require feature="views" />    
  </ModulePrefs>
  <UserPref name="locIDs" display_name="Location IDs (comma-separated)" default_value="L99381" datatype="string" required="true"/>
  <UserPref name="daysBack" display_name="Look back " default_value="7" required="true" datatype="enum">
    <EnumValue value="1" display_value="1 day"/>
    <EnumValue value="2" display_value="2 days"/>
    <EnumValue value="3" display_value="3 days"/>
    <EnumValue value="4" display_value="4 days"/>
    <EnumValue value="5" display_value="5 days"/>
    <EnumValue value="6" display_value="6 days"/>
    <EnumValue value="7" display_value="1 week"/>
    <EnumValue value="14" display_value="2 weeks"/>
    <EnumValue value="21" display_value="3 weeks"/>
    <EnumValue value="30" display_value="1 month"/>
  </UserPref>
  <UserPref name="locName" display_name="Location name (overrides default)" default_value="Stewart Park Ithaca" datatype="string" required="false"/>
  <Content type="html" view="home">
    <![CDATA[

<style type="text/css">
table {
font-size: 75%
}
td {
border-width: 0;
margin: 0
}
.row1 { background: #eee;}
.row2 { }
.sppcol { width: 50%;}
.numcol { width: 10px; text-align: right }
.datecol { width: 125px; text-align: center}
</style >

<div style="float: left"><a href="http://ebird.org/" target="_blank"><img src="http://ebird.org/wstools1.1/google/loc-recent/images/ebird-logo.gif" border="0"/></a></div>
<div id="location_div" style="text-align: center; font-weight: bold; font-size: 100%">...</div>
<div style="clear: both" id="content_div"><i>Loading.</i></div>
<script type="text/javascript">

function displaySummary() {

var prefs = new _IG_Prefs(__MODULE_ID__);

var targetLocID = prefs.getString("locIDs");

var daysBack = prefs.getInt("daysBack");

var url = "http://ebird.org/ws1.1/data/obs/loc/recent?detail=full&r=" + targetLocID + "&back=" + daysBack;

_IG_FetchXmlContent(url, callback, { refreshInterval: (60) });

}


function callback(response) {
if (response == null || typeof(response) != "object" || response.firstChild == null) {
_gel("content_div").innerHTML = "<i>Invalid data.</i>";
return;
}

var prefs = new _IG_Prefs(__MODULE_ID__);

//Title Html
var title = prefs.getString("locName");
var locIDs = prefs.getString("locIDs");
var html = "";
html += "<a target='_blank' href='http://ebird.org/ebird/GuideMe?cmd=decisionPage&getLocations=hotspots&reportType=location&hotspots=" + locIDs + "'>";
html += title;
html += "</a>";

_gel('location_div').innerHTML = html;

html = "";
html += "<div>";
html += "<span style='font-size:9px;'>Sightings over the last " + prefs.getInt("daysBack") + " days</span>";
html += "<table width='100%'>";
html += "<tr>";
html +="<th>Species</th>";
html += "<th>Count</th>";
html += "<th>Date</th>";
html += "</tr>";

var itemList = response.getElementsByTagName("sighting");

for (var i = 0; i < itemList.length ; i++) {

// For each <sighting> node, get child nodes.
// var nodeList = itemList.item(i).childNodes;
var d = getData(itemList.item(i));

if (i % 2 == 0) {
html += "<tr class='row1'>";
}
else {
html += "<tr class='row2'>";
}
html += "<td class='sppcol'>";
html += d["com-name"];
var obsName = "Unknown Observer";
if  (d["last-name"])
    obsName = d["first-name"] + "  " + d["last-name"];
html += " (<a target='_blank' href='http://maps.google.com/?ie=UTF8&t=p&z=13&q=" + d["lat"] + "," + d["lng"] +  "(" +  d["com-name"].replace("'","")  + ", " + d["loc-name"] +  ", " + obsName + ")&ll=" + d["lat"] + "," + d["lng"] + "'>map</a>)";
html += "</td>";

html += "<td class='numcol'>";
if (d["how-many"] > 0)
  html += d["how-many"];
else
  html += "Present";
html += "</td>";

html += "<td class='datecol''>" + d["obs-dt"] + "</td>";
html += "</tr>";

}

html += "</table>";
html += "</div>";

_gel('content_div').innerHTML = html;

}

function getData(n) {
var d = new Object();
var nodeList = n.childNodes;
for (var j = 0; j < nodeList.length ; j++) {
var node = nodeList.item(j);
d[node.nodeName] = node.firstChild.nodeValue;
} return d;
}

_IG_RegisterOnloadHandler(displaySummary);

</script>
]]>

  </Content>
  <Content type="html" view="canvas">
    <![CDATA[

<style type="text/css">
table {
font-size: 75%
}
td {
border-width: 0;
margin: 0
}
.row1 { background: #eee;}
.row2 { }
.sppcol { width: 25%;}
.numcol { width: 10px; text-align: right }
.obscol { width: 50px; text-align: center}
.loccol { width: 25%; text-align: center}
.datecol { width: 100px; text-align: center}
</style >

<div style="float: left"><a href="http://ebird.org/" target="_blank"><img src="http://ebird.org/wstools1.1/google/loc-recent/images/ebird-logo.gif" border="0"/></a></div>
<div id="location_div" style="text-align: center; font-weight: bold; font-size: 100%">...</div>
<div style="clear: both" id="content_div"><i>Loading.</i></div>
<script type="text/javascript">

function displaySummary() {

var prefs = new _IG_Prefs(__MODULE_ID__);

var targetLocID = prefs.getString("locIDs");

var daysBack = prefs.getInt("daysBack");

var url = "http://ebird.org/ws1.1/data/obs/loc/recent?detail=full&r=" + targetLocID + "&back=" + daysBack;

_IG_FetchXmlContent(url, callback, { refreshInterval: (60) });

}


function callback(response) {
if (response == null || typeof(response) != "object" || response.firstChild == null) {
_gel("content_div").innerHTML = "<i>Invalid data.</i>";
return;
}

var prefs = new _IG_Prefs(__MODULE_ID__);

//Title Html
var title = prefs.getString("locName");
var locIDs = prefs.getString("locIDs");
var html = "";
html += "<a target='_blank' href='http://ebird.org/ebird/GuideMe?cmd=decisionPage&getLocations=hotspots&reportType=location&hotspots=" + locIDs + "'>";
html += title;
html += "</a>";

_gel('location_div').innerHTML = html;

html = "";
html += "<div>";
html += "<table width='100%'>";
html += "<tr>";
html +="<th>Species</th>";
html += "<th>Count</th>";
html += "<th>Observer</th>";
html += "<th>Location</th>";
html += "<th>Date</th>";
html += "</tr>";

var itemList = response.getElementsByTagName("sighting");

for (var i = 0; i < itemList.length ; i++) {

// For each <sighting> node, get child nodes.
// var nodeList = itemList.item(i).childNodes;
var d = getData(itemList.item(i));

if (i % 2 == 0) {
html += "<tr class='row1'>";
}
else {
html += "<tr class='row2'>";
}
html += "<td class='sppcol'>";
html += d["com-name"] ;
html +="<br/>";
html += "</td>";

html += "<td class='numcol'>";
if (d["how-many"] > 0)
  html += d["how-many"];
else
  html += "Present";
html += "</td>";

html += "<td class='obscol'>";
var obsName = "Unknown Observer";
if  (d["last-name"])
    obsName = d["first-name"] + "  " + d["last-name"];
  
html += obsName;
html += "</td>";

html += "<td class='loccol'>";
html += d["loc-name"];
html += " (<a target='_blank' href='http://maps.google.com/?ie=UTF8&t=p&z=13&q=" + d["lat"] + "," + d["lng"] +  "(" +  d["com-name"].replace("'","")  + ", " + d["loc-name"] +  ", " + obsName + ")&ll=" + d["lat"] + "," + d["lng"] + "'>map</a>)";
html += "</td>";

html += "<td class='datecol''>" + d["obs-dt"] + "</td>";
html += "</tr>";

}

html += "</table>";
html += "</div>";

_gel('content_div').innerHTML = html;

}

function getData(n) {
var d = new Object();
var nodeList = n.childNodes;
for (var j = 0; j < nodeList.length ; j++) {
var node = nodeList.item(j);
d[node.nodeName] = node.firstChild.nodeValue;
} return d;
}

_IG_RegisterOnloadHandler(displaySummary);

</script>
]]>

  </Content>
  
</Module>
