<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
<ModulePrefs 
scrolling="true" 
title="Notable US eBird Sightings" 
title_url="http://ebird.org"
description="Shows notable eBird sightings for a state which the user selects.  User can view details of each sighting and see them plotted on Google Maps"
author="Jeff Gerbracht"
author_email="jeffgerbracht+gadg@gmail.com"
author_location="Ithaca, NY"
author_affiliation="Cornell Lab of Ornithology"
screenshot="http://ebird.org/tools/google/eBirdGadget.png"
thumbnail="http://ebird.org/tools/google/eBirdGadgetThumb.png"
directory_title="Notable eBird Sightings"
>
 <Require feature="setprefs" />
<Require feature="dynamic-height"/>
<Locale lang="en" />
</ModulePrefs>
<UserPref name="state" datatype="hidden" default_value="US-NY"/> 
<UserPref name="heightOption" display_name="Gadget Height" datatype="enum" default_value="0"> 
<EnumValue display_value="Auto Fit to data" value="0"/>
<EnumValue display_value="Small" value="200"/>
<EnumValue display_value="Medium" value="400"/>
<EnumValue display_value="Large" value="600"/>
</UserPref>
<Content type="html">
<![CDATA[ 
 <div id=content_div></div>
 <style>
 .fmaxbox {background-image:url('/ig/images/max_dark_blue.gif');}
 .fminbox {background-image:url('/ig/images/min_dark_blue.gif');}
 a.fmaxbox:hover {background-image:url('/ig/images/max_dark_blue_highlight.gif');}
 a.fminbox:hover {background-image:url('/ig/images/min_dark_blue_highlight.gif');}
 .uftl {border:1px solid white;}
</style>
<link rel="stylesheet" type="text/css" href="/ig/f/U2SQqm5neGg/ig.css"/>
 <script type="text/javascript">
 // Get userprefs
 var prefs = new _IG_Prefs(__MODULE_ID__);
 var url = "http://ebird.org/ws1.1/data/notable/region/recent?rtype=subnational1&detail=full&back=7&r=" + prefs.getString("state");
  var gHeight = prefs.getInt("heightOption");
 function toggleMenu(val){
 	var divObj = document.getElementById("summary_"+val);
 	var divTog = document.getElementById("tog_"+val);
 	if (divObj.style.display == 'none'){
 		divObj.style.display = 'block';
 		divTog.className = 'fminbox';
 	}else{
 		divObj.style.display = 'none';
 		divTog.className = 'fmaxbox';
	}
 	if (gHeight > 0) {  _IG_AdjustIFrameHeight(gHeight); } else {  _IG_AdjustIFrameHeight(); } 
}

function toggleDesc(items, showAll)  {
    	var disp = 'none';
    	var cName = 'fmaxbox';
    	if (showAll == 'show') {
    		disp = 'block';
 		cName = 'fminbox'; 
 		document.getElementById("show_all").style.display = 'none';
 		document.getElementById("hide_all").style.display = 'inline';
    	}
    	else {
 		document.getElementById("show_all").style.display = 'inline';
 		document.getElementById("hide_all").style.display = 'none';
    	}
	for (var i = 0; i < items ; i++)  {
	 	document.getElementById("summary_"+i).style.display = disp;
	 	document.getElementById("tog_"+i).className = cName;
	}
	if (gHeight > 0) {  _IG_AdjustIFrameHeight(gHeight); } else {  _IG_AdjustIFrameHeight(); } 
}

function toggleState() {
	var divObj = document.getElementById("selectAState");
	var divTitle = document.getElementById("title");
 	if (divObj.style.display == 'none'){
 		divObj.style.display = 'block';
 		divTitle.style.display = 'none';
 	}else{
 		divObj.style.display = 'none';
 		divTitle.style.display = 'block';
	}
	if (gHeight > 0) {  _IG_AdjustIFrameHeight(gHeight); } else {  _IG_AdjustIFrameHeight(); } 

}

function changeState(stateCode) {
prefs.set("state", stateCode);
 url = "http://ebird.org/ws1.1/data/notable/region/recent?rtype=subnational1&detail=full&back=7&r=" + stateCode;
displayBirdData();

}
function displayBirdData() { 
// XML Bird data
_IG_FetchContent("http://ebird.org/tools/google/states.txt", function (resp) {
 if (resp == null ) {
_gel("content_div").innerHTML = "<i>Invalid data.</i>";
return;
 }
_IG_FetchXmlContent(url, function (response) {
 if (response == null || typeof(response) != "object" || 
response.firstChild == null) {
_gel("content_div").innerHTML = "<a href=\"http://ebird.org/\" target=\"_parent\" ><img src=\"http://ebird.org/eBirdCommon/images/ebird_logo.gif\" /></a><br>No Data Available at this time for this state, please try again soon by refreshing the browser or select a different state.<br><br><div id='selectAState' style='text-align:left;display:block'><form><select name=\"state\" onChange='changeState(this.options[this.selectedIndex].value)'>"+resp+"</select></form></div>";
return;
 }
 // Start building HTML string that will be displayed in <div>. 
 // Set the style for the <div>.		
 var html = "<div font-family:Arial, Helvetica;" + 
		"text-align:left;font-size:90%'>"; 
					
 // Set style for title.
 html +="<div id='title' style='text-align:center; font-size: 150%; " +
		"font-weight: 700;line-height:40px;'>"; 

 // Display channel title. Use getElementsByTagName() to retrieve the <channel> element.
 // Since there is only one channel element in the file,
 // you can get to it by accessing the item at index "0". 
 if (response.getElementsByTagName("result") == null  || typeof(response.getElementsByTagName("result")) != "object"|| 
response.getElementsByTagName("result").firstChild == null)
 _gel("content_div").innerHTML = "<a href=\"http://ebird.org/\" target=\"_parent\" ><img src=\"http://ebird.org/eBirdCommon/images/ebird_logo.gif\" /></a><br>No Data Available at this time for this state, please try again soon by refreshing the browser or select a different state.<br><br><div id='selectAState' style='text-align:left;display:block'><form><select name=\"state\" onChange='changeState(this.options[this.selectedIndex].value)'>"+resp+"</select></form></div>";

var title = response.getElementsByTagName("subnational1-name").item(0).firstChild.nodeValue;

 // Append the title and description to the HTML string.
 html += "<a href=\"http://ebird.org/\" style=\"float:left;\" target=\"_parent\" ><img src=\"http://ebird.org/eBirdCommon/images/ebird_logo.gif\" /></a>" + title + " <a style='text-align:center; font-size: 70%; font-weight: 100' href=\"javascript:void(0)\" onclick=\"javascript:toggleState();\">(change)</a></div><div id='selectAState' style='text-align:center;display:none;line-height:40px;'><a href=\"http://ebird.org/\" style=\"float:left;\" target=\"_parent\" ><img src=\"http://ebird.org/eBirdCommon/images/ebird_logo.gif\" /></a><form>&nbsp;<select name=\"state\" onChange='changeState(this.options[this.selectedIndex].value)'>"+resp+"</select></form></div>";
 // Get a list of the <item> element nodes in the file
 var itemList = response.getElementsByTagName("sighting");
 if (itemList.length == 0) {
  html = "<div id='title' style='text-align:center; font-size: 150%; font-weight: 700;line-height:40px;'>"; 
  html += "<a href=\"http://ebird.org/\" style=\"float:left;\" target=\"_parent\" ><img src=\"http://ebird.org/eBirdCommon/images/ebird_logo.gif\" /></a><form>" + title + " <select name=\"state\" onChange='changeState(this.options[this.selectedIndex].value)'>"+resp+"</select></form></div><br><div font-family:Arial, Helvetica; text-align:left;font-size:90%'>No Notable Sightings have been reported for "+title+ "</div>";
  _gel("content_div").innerHTML = html;
  return;
 }
html += "<div class=\"uftl\" style=\"text-align:right;\"><a style=\"float:left;display:none;\" href=\"javascript:void(0)\" id='hide_all'  onclick=\"javascript:toggleDesc("+itemList.length+",'hide');\">hide details</a><a style=\"float:left;\" href=\"javascript:void(0)\" id='show_all' onclick=\"javascript:toggleDesc("+itemList.length+",'show');\">show details</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='http://ebird.org/eBirdCommon/images/checkmark.gif'  width='14' height='14'/> = Sighting confirmed by an eBird reviewer.</div>";

 // Loop through all <item> nodes
 for (var i = 0; i < itemList.length ; i++) { 
 // For each <item> node, get child nodes.
 	var nodeList = itemList.item(i).childNodes;
	 var itemCounty = "";
	 var firstName = "";
	 var lastName = "";
	 var howMany = "";
	 for (var j = 0; j < nodeList.length ; j++) {
		var node = nodeList.item(j);
		if (node.nodeName == "com-name") {
		 var itemSpecies = node.firstChild.nodeValue;
		}
		if (node.nodeName == "obs-dt") {
		 var itemDate = node.firstChild.nodeValue;
		}
		if (node.nodeName == "subnational2-name" && node.firstChild) {
		 itemCounty = node.firstChild.nodeValue + " Co.";
		}
		if (node.nodeName == "obs-reviewed") {
		 var itemReviewed = node.firstChild.nodeValue;
		}
		if (node.nodeName == "obs-valid") {
		 var itemValid = node.firstChild.nodeValue;
		}
		if (node.nodeName == "how-many" && node.firstChild) {
		 howMany = node.firstChild.nodeValue;
		}
		if (node.nodeName == "location-private") {
		 var locPrivate = node.firstChild.nodeValue;
		}
		if (node.nodeName == "loc-name") {
		 var locName = node.firstChild.nodeValue;
		}
		if (node.nodeName == "first-name" && node.firstChild) {
		 firstName = node.firstChild.nodeValue;
		}
		if (node.nodeName == "last-name" && node.firstChild) {
		 lastName = node.firstChild.nodeValue;
		}
		if (node.nodeName == "lat" && node.firstChild) {
		 lat = node.firstChild.nodeValue;
		}
		if (node.nodeName == "lng" && node.firstChild) {
		 lng = node.firstChild.nodeValue;
		}
	}  
re = new RegExp("(\\(|\\))","g");
var mapSpp = itemSpecies.replace(re,"");
var mapLoc = locName.replace(re," ");
var mapText = "("+mapSpp+" - "+mapLoc+")";
if (locPrivate == "true") {
	locName = "a personal location";
	mapText = "("+mapSpp+")";
}

 itemDate = itemDate.replace(" ","-");
 itemDate = itemDate.replace(":","-");
 var dArray = itemDate.split("-");
 var d = new Date(dArray[0],dArray[1]-1,dArray[2]);
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
itemDate = curr_date + " " + m_names[curr_month];

 var itemLink = "http://maps.google.com/?q="+lat+","+lng+" "+mapText;
  itemLink = itemLink.replace("&amp;","%26");
  itemLink = itemLink.replace("'"," ");

 var itemDescription = howMany+" "+itemSpecies+" was observed at "+locName+" on "+itemDate+", "+curr_year+" by "+firstName+" "+lastName+".";
if (itemValid == "false")
	itemDescription += "  This sighting has not yet been reviewed by an eBird reviewer.";

 var reviewedIMG = "<img src='http://ebird.org/eBirdCommon/images/spacer_trans.gif' width='14' />";
 var bkgColor = "FFF4C2";
 if (itemReviewed == "true")
 {
     reviewedIMG = "<img src='http://ebird.org/eBirdCommon/images/checkmark.gif' width='14' height='14'/>";
 }
 if (itemValid == "true")
 {
     bkgColor = "ccf3b4";
 }
 // Append extracted data to the HTML string.
 html += "<div class=\"uftl\"><a href=\"javascript:void(0)\" id='tog_"+i+"' class='fmaxbox' onclick=\"toggleMenu('"+i+"');\" ></a>&nbsp;&nbsp;"+reviewedIMG+itemSpecies+" - "+itemCounty+" - "+itemDate+" <a href='"+itemLink+"' target='_blank'>map</a></div>"; 
 html += "<div id=\"summary_"+i+"\" style=\"display:none; background-color:#"+ bkgColor + "\">" + itemDescription
 if (itemValid == "0" && itemReviewed == "0")
    html += "This sighting has not yet been reviewed by an eBird reviewer. ";
    
 html += "</div>";
 } 

 // Close up div
 html += "</div>";
 
// Display HTML string in <div>
_gel('content_div').innerHTML = html; 
// Tells gadget to resize itself
if (gHeight > 0) {  _IG_AdjustIFrameHeight(gHeight); } else {  _IG_AdjustIFrameHeight(); } 

  }, { refreshInterval: (60 * 10) });}, { refreshInterval: 60 * 15 });
}
_IG_RegisterOnloadHandler(displayBirdData);

</script>
]]> 
</Content>
</Module>

