<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Daily Horoscope for __UP_sign__" 
             title_url="http://www.tarot.com/go/google-ig/rss-horo-dailyhoro/?sign=__UP_sign__"
             directory_title="Daily Horoscopes"
             description="Daily horoscopes courtesy of tarot.com"
             render_inline="optional"
             author="Adam S."
             author_email="adam.feedback+horoscope@gmail.com"
             author_affiliation="Google Inc."
             author_location="Santa Cruz, CA"
             screenshot="/ig/modules/horoscope.png"
             thumbnail="/ig/modules/horoscope-thm.png"
             category="lifestyle" category2="funandgames"
/>
<UserPref name="sign" datatype="enum" default_value="Gemini">
  <EnumValue value="All Signs"   display_value="General Horoscope" />
  <EnumValue value="Aries"       display_value="Aries (March 21 - April 19)" />
  <EnumValue value="Taurus"      display_value="Taurus (April 20 - May 20)" />
  <EnumValue value="Gemini"      display_value="Gemini (May 21 - June 20)" />
  <EnumValue value="Cancer"      display_value="Cancer (June 21 - July 22)" />
  <EnumValue value="Leo"         display_value="Leo (July 23 - August 22)" />
  <EnumValue value="Virgo"       display_value="Virgo (August 23 - September 22)" />
  <EnumValue value="Libra"       display_value="Libra (September 23 - October 22)" />
  <EnumValue value="Scorpio"     display_value="Scorpio (October 23 - November 21)" />
  <EnumValue value="Sagittarius" display_value="Sagittarius (November 22 - December 21)" />
  <EnumValue value="Capricorn"   display_value="Capricorn (December 22 - January 19)" />
  <EnumValue value="Aquarius"    display_value="Aquarius (January 20 - February 18)" />
  <EnumValue value="Pisces"      display_value="Pisces (February 19 - March 20)" />
</UserPref>
<Content type="html">
<![CDATA[
  <div style="display:none;" id=hidden__MODULE_ID__></div>
  <div style="padding:5px; display:inline; vertical-align:top; font-size:12px;"
       id=t__MODULE_ID__></div>
  <script>
  function showMsg__MODULE_ID__(msg) {
     var htmlmsg = '<div style="text-align:left; padding-top:5px;">' + msg + '</div>';
     _gel('t__MODULE_ID__').innerHTML = htmlmsg;
  }

  function tarot__MODULE_ID__() {
     var prefs = new _IG_Prefs(__MODULE_ID__);
     var sign = prefs.getString("sign");
     if (sign == "All Signs") { sign = "everyone"; }
     var dt = new Date();
     var hrsSinceEpoch = Math.round(dt.getTime() / 1000 / 3600);
     var url = "http://www.tarot.com/rss/generate.php?code=google-ig&feed=daily_horoscope&sign=" + encodeURIComponent(sign.toLowerCase()) + "&cache="+hrsSinceEpoch;
     _IG_FetchXmlContent(url, function (response) {
        if (response == null || typeof(response) != "object" || response.firstChild == null) {
           showMsg__MODULE_ID__("Horoscope information is currently not available.  Please try again later.");
           return;
        }

        var htmlobj = _gel("hidden__MODULE_ID__");
        var foundData = false;
        // get the child nodes of "item"
        var nodeList = response.getElementsByTagName("item").item(0).childNodes;
        // look for description/#cdata-section.  This will contain the goods
        for (var i = 0; i <  nodeList.length; i++) {
           var node = nodeList.item(i);
           if (node.nodeName == "description") {
              var childNodes = node.childNodes;
              for (var j = 0; j <  childNodes.length; j++) {
                 var childNode = childNodes.item(j);
                 if (childNode.nodeName == "#cdata-section") {
                    foundData = true;
                    htmlobj.innerHTML = childNode.nodeValue;
                 }
              }
           }
        }
        if (!foundData) {
           showMsg__MODULE_ID__("internal error: could not find horoscope description.");
           return;
        }

        // convert the sign name into its numeric ID
        var sign_number = -1;
        var signs = new Array("everyone", "Aries", "Taurus", "Gemini", "Cancer", "Leo",
           "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces");
        for (var i = 0; i < signs.length; i++) {
           if (sign == signs[i]) {
             sign_number = i;
             break;
           }
        }
        if (sign_number == -1) {
           showMsg__MODULE_ID__("internal error: unknown horoscope sign.");
           return;
        }

        // use the browser to parse the HTML, which lives in a CDATA section
        // in the description of this feed.  This is safe to do because browsers
        // will not execute JS code when you set .innerHTML
        var reading = htmlobj.getElementsByTagName("div")[2].innerHTML;
        var reading_date = htmlobj.getElementsByTagName("div")[3].innerHTML;

        // format the reading into HTML
        var html = '<br><a target=_blank href="http://www.tarot.com/go/google-ig/rss-horo-dailyhoroitem/?sign='+encodeURIComponent(sign.toLowerCase())+'">';
        html += '<img src=/ig/modules/horoscope_content/'+_hesc(sign.toLowerCase())+'.gif title="Tarot.com "+_hesc(sign)+" Horoscope" style="vertical-align: top; float: left; position: relative; padding-right: 20px;" border="0" height="75" width="75"></a>';
        html += _hesc(reading);
        html += '<div style="padding-right: 5px; text-align:right;"><font size=-1>';
        html += _hesc(reading_date) + "<br>";
        html += '<a target=_blank href="http://www.tarot.com/go/google-ig/rss-horo-more/?sign='+sign_number+'">';
        html += 'more from 1tarot.com &raquo;</a>';
        html += '</div>';
        _gel('t__MODULE_ID__').innerHTML = html;
     }); 
  }
  _IG_RegisterOnloadHandler(tarot__MODULE_ID__);
  </script>
]]>
</Content>
</Module>

