<?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"
             height="250">
    <Preload href="http://www.tarot.com/rss/generate.php?code=google-ig&amp;feed=daily_horoscope&amp;sign=__UP_sign__" />
</ModulePrefs>
<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:7px; 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;
  }

  // If the local date is the same as the date in PST, return null.  
  // Else return the local date as a string YYYYMMDD.
  function generateDateString__MODULE_ID__() {
    var local_date = new Date();
    var local_days = local_date.getDate();

    var pst_date = new Date();
    var gmt_minus_pst_minutes = 8 * 60;    // TODO: account for daylight savings
    var gmt_minus_local_minutes = local_date.getTimezoneOffset();
    var pst_minus_local_minutes = gmt_minus_local_minutes - gmt_minus_pst_minutes;
    var pst_minus_local_millis = pst_minus_local_minutes * 60 * 1000;
    pst_date.setTime(local_date.getTime() + pst_minus_local_millis);

    var pst_days = pst_date.getDate();
    if (local_days == pst_days) {       // no need to check months or year
      return null;
    }

    var local_year = local_date.getFullYear();
    var local_month = local_date.getMonth() + 1;

    var str = local_year.toString();
    str += (local_month < 10) ? "0" + local_month : local_month;
    str += (local_days < 10) ? "0" + local_days : local_days;
    return str;
  }

  function logclick__MODULE_ID__(action) {
     var url = _args()["url"];
     var prefs = new _IG_Prefs(__MODULE_ID__);
     var sign = prefs.getString("sign");
     _gel('t__MODULE_ID__').innerHTML += "<img width=1 height=1 src='/ig/nop?url="+_esc(url)+"&action="+_esc(action)+"&sign="+_esc(sign)+"'>";
  }

  function tarot__MODULE_ID__() {
     var prefs = new _IG_Prefs(__MODULE_ID__);
     var sign = prefs.getString("sign");
     // People might have "" stored in their prefs from older versions of
     // the gadget.  Historically we treated this as Gemini, so we continue the
     // same behavior.  (But it breaks preload.)
     if (sign == null || sign == "") { sign = "Gemini"; }

     var url = "http://www.tarot.com/rss/generate.php?code=google-ig&feed=daily_horoscope&sign=" + _esc(sign);

     // Only append date if necessary (it defeats prefetching).  Tarot.com
     // assumes PST if no date is specified, so we check if the user's date is
     // the same as the date in PST.  (This is true at least half the time.)
     var date = generateDateString__MODULE_ID__();
     if (date) {
       url += "&date=" + date; 
     }

     _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("All Signs", "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;

	// Historically we use "everyone.gif", not "all signs.js".  This is
        // probably good, as browsers use several ways of encoding whitespace in
        // URLs and it's not clear how our server will map it to static files.
	if (sign == "All Signs") {
          sign = "everyone";
        }

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