<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="/usr/bin/fortune"
             title_url="http://www.siafoo.net/help/fortune?src=ggf"
             directory_title="/usr/bin/fortune"
             description="Get random quips, quotes, idioms, and jokes from the fortune libraries of Unix and Linux.  Save your favorites.  Waste some time. :-)" 
             screenshot="http://www.siafoo.net/img/fortune/screenshot.png" 
             thumbnail="http://www.siafoo.net/img/fortune/thumbnail.png" 
             author="David @ Siafoo"
             author_email="feedback@siafoo.net"
             author_affiliation="Siafoo.net"
             author_location="Davis, CA, USA" 
             author_aboutme="http://www.siafoo.net/user/david" 
             author_link="http://www.siafoo.net/help/about?src=gga" 
             author_quote="You can only live once, but if you do it right, once is enough." 
             author_photo="http://www.siafoo.net/img/fortune/author.png" 
             height="120" 
             width="550">
    <Require feature="com.google.gadgets.analytics"/>
    <Require feature="dynamic-height"/>
    <Require feature="settitle"/>
    <Require feature="setprefs"/>
    <Require feature="tabs"/>
</ModulePrefs>

<UserPref name="favorites" datatype="hidden" default_value=""/>
<UserPref name="simple" datatype="bool" default_value="false" display_name="Simple mode (hide the cruft)"/>

<Content type="html"><![CDATA[
  
<style type='text/css'>

  a{
    text-decoration: none;
  }
  
    a:hover{
        text-decoration: underline;
    }
  
    a img{
      border: none;
    }
  
  img{
     vertical-align: bottom;
  }
  
  div.clear{
    float: none!important;
    clear: both!important;
  }
  
  /* Google Tabs */
  .tablib_table {
    display: none;
  }
  
  /* Classes */
  .fortune{
    font-family: courier,courier new,arial,helvetica,sans-serif;
    font-size: 80%;
  }
  
  .tool{
    font-size: 70%;
  }
  
  .tool, .tool a{ /* FF3 Hack: Otherwise the toolbar doesn't work right */
    font-family: arial,helvetica,sans-serif;
  }
  
  div#content{
    margin: 0.5em;
  }
  
  div#toolbar{
    margin: 1.2em 0.6em 0em 0.6em; /* 0.7 = 85/70 * 0.5 */
  }
  
      div#toolbar div.left{
        float: left;
      }

        div#toolbar div.left a{
          float: left;
          margin-right: 1.2em;
        }
      
      div#toolbar div.right{
        float: right;
      }
  
        div#toolbar div.right a{
          float: left;
          margin-left: 1.2em;
        }

  div#favorites{
    overflow: auto;
    max-height: 250px;
    _height: 250px; /* IE 6 Hack */
    margin: 0.5em 0.5em 0 0.5em;
  }
  
    div#tab_to_new{
      margin: 0.6em;
    }
    
    div#no_faves{
      margin: 1.2em 0.6em 0 0.6em;
    }
  
    div.favorite{
      clear: both;
      margin-right: 0.25em;
      padding: 0.5em 0.5em; /* Padding not margin */
    }
  
      div.favorite.even{
        background-color: #E5ECF9; /* Google Blue */
      }
  
      div.favorite a{
        float: right;
        margin: 0 0 0.2em 0.5em;
      }

</style>

<div id='fortune'>
   <div id='content' class='fortune'></div>
   <div id='toolbar' class='tool'>
     <div class='left'>
        <a id='loading' href='javascript:newFortune()'></a>
        <span id='save' class='cruft'></span>
    </div>
    <div class='right'>
        <a id='tab_to_fav' href='javascript:tabs.setSelectedTab(1)'>
          <img src='http://www.siafoo.net/icons/heart_r8.png' alt=''></img> Favorites
        </a>
        <a id='about' href='http://www.siafoo.net/help/fortune?src=ggf' target='_blank'>
          <img src='http://www.siafoo.net/icons/info_16_r8.png' alt='About the Fortunes' title='About the Fortunes'></img>
        </a>
    </div>
    <div class='clear'></div>
</div>  

<div id='favorites'>
    <div id='tab_to_new' class='tool'>
       <a href='javascript:tabs.setSelectedTab(0)'>&#x2190; Back to the Random Fortune</a>
    </div>
    <div id='favorites_list'></div>
</div>

<script type='text/javascript'>

  // Functions

  function newFortune(){
      loadingEle.innerHTML = "<img src='http://www.siafoo.net/img/loading_inline_r6.gif' alt='Loading...' title='Loading...'/>"
      _IG_FetchContent('http://www.siafoo.net/tools/fortune', postFetch, {refreshInterval: 0})
  }

  function postFetch(responseText){
        if (responseText.substr(0,9) == '<!DOCTYPE' || responseText.substr(0,6) == '<html>') { // Probably a 404 or 500
        var valid = false
        currentFortune = "<img src='http://www.siafoo.net/icons/exclamation_16_r8.png' alt=''>" +
                         "Whoops, it looks like you've found a bug.  "+
                         "If you have a second, please <a href='http://www.siafoo.net/feedback'> report this to us</a>.  Thanks."

        }
      
      else{
        var valid = true
        currentFortune = responseText.replace(/([^\s:]) ?(\r\n|\r|\n)[\t ]*(\w[\w\s])/g, '$1 $3') // Replace pointless (?) carriage returns with a space
                                        // The \w[\w\s] at the end is there to exclude things like 'A:' 
                                     .replace(/\r\n|\r|\n/g, '<br />') // Replace carrige returns with line breaks
                                     .replace(/(\t| {3,})/g, '&nbsp;&nbsp;&nbsp;&nbsp;') // Replace tabs, multiple spaces with forced spaces
      }
      
      contentEle.innerHTML = currentFortune
      _IG_AdjustIFrameHeight()

      loadingEle.innerHTML = "<img src='http://www.siafoo.net/img/loading_inline_static_r8.png' alt='New Fortune' title='New Fortune'/>"
      saveEle.innerHTML = valid ? "<a href='javascript:addFavorite()'>" +
                                  "   <img src='http://www.siafoo.net/icons/heart_add_r8.png' alt=''></img> Save" +
                                  "</a>" : ""

      if (tracker && initDone) tracker.reportPageview(basePage + '/random')
  }
  
  function loadFaves(){
      str = prefs.getString('favorites')
      if (str == '') 
          return []
      else
          return str.split('|')
  }
  
  function saveFaves(faves){
      str = faves.join('|')
      prefs.set('favorites', str)
  }
  
  function addFavorite(){

      if (!currentFortune) return
      
      var faves = loadFaves()
      faves.push(currentFortune)
      saveFaves(faves)

      showFavorites()
      tabToFavEle.style.display = ''
      save.innerHTML = 'Saved'
      if (tracker) tracker.reportPageview(basePage + '/favorites/add')
  }

  function removeFavorite(index){
      var faves = loadFaves()
      faves.splice(index,1)
      saveFaves(faves)

      showFavorites()
      if (tracker) tracker.reportPageview(basePage + '/favorites/remove')
  }

  function showFavorites(){
      var faves = loadFaves()
      var contents = ''
      for (var i = 0; i < faves.length; i++){
          contents += "<div class='favorite " + (i % 2 ? "odd" : "even") + "'>" +
                      "    <a class='tool' href='javascript:removeFavorite(" + i + ")'>" +
                      "        <img src='http://www.siafoo.net/icons/heart_rm_r8.png' alt=''></img> Delete" +
                      "    </a>" +
                      "    <span class='fortune'>" + faves[i] + "</span>" +
                      "    <div class='clear'></div>" +
                      "</div>"
      }
      if (!faves.length){
        contents += "<div id='no_faves' class='tool'>You have no favorite fortunes.</div>"
      }
      
      favoritesEle.innerHTML = contents
      _IG_AdjustIFrameHeight()
  }
  
  function onTabSwitch(tab_id){
      if (!loadFaves().length){
         tabs.setSelectedTab(0)
         tabToFavEle.style.display = 'none'
      }
      
      if (!initDone) return // There's no sense in setting the height multiple times, or recording an extra pageview on startup
      _IG_AdjustIFrameHeight()
      if (tracker) tracker.reportPageview(basePage + '/tab/' + tab_id)
  }
  
  var initDone = false
  
  // Constants
  var basePage = '/gadgets/fortune'

  // Google Elements
  var prefs = new _IG_Prefs()
  var tabs = new _IG_Tabs(__MODULE_ID__, 'New Fortune')

  // Dom Elements
  var contentEle =  _gel('content')
  var favoritesEle = _gel('favorites_list')
  var loadingEle = _gel('loading')
  var saveEle = _gel('save')
  var tabToFavEle = _gel('tab_to_fav')

  // Current Fortune
  var currentFortune
  
  // Initialize Tabs & Favorites (we don't do callbacks as the tabs shouldn't get updated every time)
  var tabFortune = tabs.addTab('New Fortune', 'fortune', onTabSwitch)
  var tabFavorites = tabs.addTab('Favorites', 'favorites', onTabSwitch)
  
  // Hide Cruft
  if (prefs.getBool('simple')){
    saveEle.style.display = 'none'
    tabToFavEle.style.display = 'none'
    _gel('about').style.display = 'none'
  }
    
  // Initialize Fortune & Favorites
  newFortune()
  showFavorites()
  
  // Initialize tracker, report page view
  var tracker = new _IG_GA('UA-555778-6')
  tracker.reportPageview(basePage)
  initDone = true
  
</script>
  
]]>
</Content>

</Module>