<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="Digg Roundup" author="Michael Mahemoff" author_email="michael@mahemoff.com">
    <Require feature="dynamic-height"/>
  </ModulePrefs>
   <UserPref name="bgColor" display_name="Background Color:" default_value="White" datatype="enum" >
        <EnumValue value="White" />
        <EnumValue value="Pink" />
        <EnumValue value="Aqua" />
        <EnumValue value="Lime" />
        <EnumValue value="Yellow" /> 
        <EnumValue value="Orange" />
        <EnumValue value="Black" />
    </UserPref>
   <UserPref name="container" display_name="Story Type:" default_value="" datatype="enum" >
        <EnumValue value="" display_value="All" />
        <EnumValue value="technology" display_value="Technology" />
        <EnumValue value="science" display_value="Science" />
        <EnumValue value="world_business" display_value="World Business" />
        <EnumValue value="sports" display_value="Sports" />
        <EnumValue value="entertainment" display_value="Entertainment" /> 
        <EnumValue value="gaming" display_value="Gaming" />
    </UserPref>
   <UserPref name="storyAmount" display_name="No. of Stories:" default_value="5" datatype="string" >
   </UserPref>
   <UserPref name="popularOnly" display_name="Popular Only?" default_value="true" datatype="bool" >
   </UserPref>
  <Content type="html">
    <![CDATA[
      <style type="text/css">
        a { text-decoration: none; }
        a.diggAmount { border: 1px solid #dd7; background-color: #ff9; color: #777; width: 100%; display: block; padding-right: 0; padding-left: 3px; font-size: 70%; }
        a.diggAmount:hover { border: 1px solid #ff9; background-color: #dd7; }
        .diggsCell { width: 1px; }
        .storyCell { width: 100%; }
        table { font-size: 90%; }
        td { vertical-align: top; padding: 0 3px; }
      </style>
      <div id="message" style="width:100%; height: 100%; padding: 8px 4px;"></div>
      <div id="scriptHolder"></div>
      <script type="text/javascript">

        ////////////////////////////////////////////////////////////////////
        // INITIALISE
        ////////////////////////////////////////////////////////////////////

        var prefs = new _IG_Prefs(__MODULE_ID__);
        setupUI();
        callDigg();
        setInterval(callDigg, 30*1000);

        ////////////////////////////////////////////////////////////////////
        // KEY FUNCTIONALITY
        ////////////////////////////////////////////////////////////////////

        function setupUI() {
          message = document.getElementById("message");
          message.style.backgroundColor = prefs.getString("bgColor");
          message.style.color = prefs.getString("fgColor");
        }

        function callDigg() {
          var containerSuffix = prefs.getString("container")!="" ?
               "/container/"+prefs.getString("container") : "";
          var popularSuffix = prefs.getBool("popularOnly") ? "/popular" : "";
          url = "http://services.digg.com/stories" + containerSuffix + popularSuffix
                + "/?appkey=http%3A%2F%2Fmahemoff.com&type=json";
          var thirtyDaysAgo = (new Date()).getTime()/1000 - 30*24*60*60;
          var randomSubmitDate = Math.floor(thirtyDaysAgo + 24*60*60*Math.random());
          url+= "&min_submit_date=" + randomSubmitDate;
          _IG_FetchContent(url, showStories);
        }

        function showStories(json) {
          eval("stories="+json);
          var message = document.getElementById("message");
          h = "<table cellspacing='5'>";
          for (var i=0; i<min(10,prefs.getInt("storyAmount")); i++) {
            var story = stories.stories[i];
            h += "<tr><td class='diggsCell'>"+link(story.href,pluralize("digg", story.diggs), "diggAmount")+ "</td><td class='storyCell'>" + link(story.link, story.title, "storyTitle") +"</td></tr>";
          }
          h += "</table>";
          message.innerHTML = h;
          // _IG_AdjustIFrameHeight();
        }

        ////////////////////////////////////////////////////////////////////
        // UTILS
        ////////////////////////////////////////////////////////////////////

        function min(a,b) { return a<b ? a : b; }

        function pluralize(word, amount) {
          if (amount==0) amount="No";
          if (amount!=1) word+="s";
          return amount+"&nbsp;"+word;
        }

        function link(url,message,className) {
          return "<a target='_top' class='"+className+"' href='"+url+"'>"+message+"</a>";
        }

      </script>
      <script type="text/javascript" src=""></script>
    ]]>
  </Content>
</Module>
