<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs 
    title="Top Google Gadget Authors" 
    scrolling="true" 
    height="400" 
    width="600" 
    author="Gordy F" 
    author_email="gordyf@gmail.com" 
    description="Displays the top gadget authors across the world on a map!" 
    thumbnail="http://trigse.cx/gmod/gadauthorscrn_120.png" 
    screenshot="http://trigse.cx/gmod/gadauthorscrn_280.png" 
    author_photo="http://www.google.com/ig/gadgets/file/113224972997380417953/gordy_thumbnail.png">
      <Require feature="analytics"/>
  </ModulePrefs> 
  <UserPref name="numAuthors" 
      display_name="Authors to Display"
      datatype="enum"
      default_value="1">
    <EnumValue value="1" display_value="20"/>
    <EnumValue value="2" display_value="40"/>
    <EnumValue value="3" display_value="60"/>
    <EnumValue value="4" display_value="80"/>
  </UserPref>
  <Content type="html">
  <![CDATA[ 

  <script type="text/javascript">
    _IG_Analytics("UA-886172-3", "/topGadgetAuthors");
  </script>
  
  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAGvzBX5lFpYcegbnwLp7NVRTZqGWfQErE9pT-IucjscazSdFnjBTun5QRN1sjILMgpKImNbo0vT0nJg"
            type="text/javascript"></script>
<div id="MAP___MODULE_ID___map" style="width: 100%; height: 100%;"></div>
    <script type="text/javascript"> 
      // set the height
      var height = 200;
      if (document.body.scrollHeight > document.body.offsetHeight) {
          height = document.body.scrollHeight-15;
      } else {
        height = document.body.offsetHeight-15;
      }
      
      var heightString = height+"px";
      document.getElementById("MAP___MODULE_ID___map").style.height = heightString;
      
      var map;
      var geocoder;
      
      function displayFeed(url) {
        _IG_FetchXmlContent(url, function (response) {
          if (response == null || typeof(response) != "object" || response.firstChild == null) {
            return;
          }
          
          var itemList = response.getElementsByTagName("item");
          for (var i = 0; i < itemList.length ; i++) { 
            var nodeList = itemList.item(i).childNodes;
            for (var j = 0; j < nodeList.length ; j++) {
              var node = nodeList.item(j);
              if (node.nodeName == "title") {
                if (node.childNodes.length > 0) var name = node.firstChild.nodeValue;
                else var name = "";
              } else if (node.nodeName == "description") {
                if (node.childNodes.length > 0) var bio = node.firstChild.nodeValue;
                else var bio = "";
              } else if (node.nodeName == "ghapi:author_photo") {
                if (node.childNodes.length > 0) var photo = node.firstChild.nodeValue;
                else var photo = "";
              } else if (node.nodeName == "ghapi:author_location") {
                if (node.childNodes.length > 0) var loc = node.firstChild.nodeValue;
                else var loc = "";
              }
            }
            addToMap(name, bio, photo, loc);
          }
        });
      }

      function randomOffset(num) {
        return num + (Math.random()/20) - (1/20);
      }
    
      function addToMap(name, bio, photo, loc) {
        geocoder.getLocations(loc, function(response) {
          if (response.Status.code == 200) {
            var place = response.Placemark[0];
            var point = new GLatLng(randomOffset(place.Point.coordinates[1]), randomOffset(place.Point.coordinates[0]));
            var marker = new GMarker(point);
            GEvent.addListener(marker, "click", function() {
              var html = '<div style="min-height:110px; width:400px;">';
              html += '<img src="'+photo+'" alt="" style="float:left; padding-right:10px;"/>';
              html += '<div style="margin-left:80px;">';
              html += '<b>'+name+'</b>';
              html += ': '+bio+'<br/>';
              html += '<i>'+loc+'</i>';
              html += '</div></div>';
              marker.openInfoWindowHtml(html);
            });
            map.addOverlay(marker);
          }
        });
      }
      
      function displayFeeds() {
        map = new GMap2(document.getElementById("MAP___MODULE_ID___map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(50, 0), 1);

        var prefs = new _IG_Prefs(__MODULE_ID__);
        var numAuthors = prefs.getInt("numAuthors")
        for (var i = 0; i < numAuthors; i++) {
          var feedurl = "http://google.com/ig/authors?output=rss&start="+(20*i);
          displayFeed(feedurl);
        }
      }

      _IG_RegisterOnloadHandler(displayFeeds);
    </script>
  ]]>
  </Content> 
</Module>

