<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs 
    height="400"
    scrolling="false"
    title="The Onion on Google"
    title_url="http://www.theonion.com/"
    directory_title="The Onion Gadget"
    description="The Onion just got smaller and harder to read."
    screenshot="/static/onion/img/gadget/scr-onion.png"
    thumbnail="/static/onion/img/gadget/onion_gadget_logo.gif"
    author="The Onion"
    author_email="webtech@theonion.com"
  >
<Require feature="analytics" />
<Require feature="tabs" />
<Require feature="setprefs"/>
<Require feature="flash"/>
<Require feature="views" />
</ModulePrefs>
<UserPref name="user_tabs" datatype="hidden" default_value="1_1_1_1_1"/>
<Content type="html" view="home"> 
<![CDATA[ 
<head>
<link rel="stylesheet" href="http://www.gmodules.com/ig/proxy?url=http://www.theonion.com/assets/static/onion/css/gadget.css" type="text/css">
<script src="http://www.gmodules.com/ig/proxy?url=http://www.theonion.com/assets/static/onion/js/gadget.js" type="text/javascript"></script>
<script>
var afns_base_url = 'http://www.theonion.com/';

// there is a point in the iGoogle TOS about not showing ads in the home view
// but it is permissible to show ads in the canvas view. 
var bHomeViewAd = false; // changing this to true to will show home view ads

var bStretchVideo = true; // changing this to false will show the video in a fixed size box

var msg_err_loading_tabs = 'The Onion Gadget is temporarily unavailable. Please try again later';
var msg_prompt_select_tabs = 'Select up to 5 tabs to show:';

// no easily configured preferences below this point
var IE = (document.all ? true : false);
var prefs = null;
var IAB = null;
var ga = null;
var tabs = null;
var arTabs = null;
var arChosenTabs = null;
var tab_was = 0;
var selected_tab = 0;

var arRadio = null;
var arVideo = null;
var timerID = 0;

function Tab(index, title, selection, feed, type, link, image, ruler, advert_url, advert_img) {
  this.index = index;
  this.title = title;
  this.selection = selection;
  this.feed = feed;
  this.type = type;
  this.chosen = false;
  this.link = link;
  this.image = image;
  this.ruler = (ruler ? ruler : 'cccccc');
  this.advert_url = advert_url;
  this.advert_img = advert_img;
}

function handle_Save() {
  var s = '', was = '';
  for (var i = 0, n = arTabs.length; i < n; i++) {
    was += '_' + (arChosenTabs[i] > 0 ? '1' : '0');
  }
  was = was.substr(1);

  for (var i = 0, n = arTabs.length; i < n; i++) {
    var it = _gel('sel_tab_' + i);
    if (it) {
      s += '_' + (it.checked ? '1' : '0');
      arChosenTabs[i] = (it.checked ? 1 : 0);
    }
  }
  s = s.substr(1);

  if (_args()['synd'] == 'ig') {
    prefs.set('user_tabs', s);
  }
  selected_tab = 0;
  _IG_Analytics(ga_acct, '/onion/google/gadget/saved');

  // the iGoogle library does not cleanly support adding/removing tabs
  // so we are just going to reload a new instance with our latest preferences
  var r = new RegExp(was);
  var url = location.href.replace(r, s);
  location.href = url;
}

function tabIndex(n_tab) {
  var rtn = 0;
  for (var i = 0, n = arTabs.length; i < n; i++) {
    if (arTabs[i].index == n_tab) {
      rtn = i;
      break;
    }
  }
  return rtn;
}

function selectTab(n) {
  if (tabs) {
    if (n > tabs.getTabs().length) {
      n = tabs.getTabs().length - 1;
    }
    if (n < 0) {
      n = 0;
    }
    tabs.setSelectedTab(n);
  }
}

function setAdvert(n) {
  var it = _gel('advert');
  if (bHomeViewAd && it) {
    if (arTabs[n].advert_img && arTabs[n].advert_url) {
      var html = new Array();
      html.push('<a target="_blank" href="', arTabs[n].advert_url, '"');
      html.push(' onfocus="this.blur()"');
      html.push(' onclick="return goAdvert()">');
      html.push('<img border="0" src="', _IG_GetCachedUrl(arTabs[n].advert_img), '"></a>');
      it.innerHTML = html.join('');
    } else { 
      it.innerHTML = '';
    }
  }
}

function handle_menuTab(feed, n) {
  stackTab(tab_was);
  var it = _gel('content');
  if (it) {
    var html = new Array();
    html.push('<form onsubmit="handle_Save();return false;" style="padding-top:10px;">');
    html.push('<table><tr><td width="90" valign="top">');
    html.push('<div class="prompt_select_tabs">', msg_prompt_select_tabs, '</div>');
    html.push('</td><td valign="top"><div class="tab_selections">');
    for (var i = 0, n = arTabs.length; i < n; i++) {
      html.push('<input type="checkbox" id="sel_tab_', i, '" ', (arTabs[i].chosen ? 'checked' : '') ,'>');
      html.push(arTabs[i].selection, '<br />');
    }
    html.push('</div></td></tr><tr><td colspan="2" align="right">');
    html.push('<br><input type="submit" value="Save">');
    html.push('&nbsp;<input type="button" onclick="selectTab(', tab_was, ')" value="Cancel">');
    html.push('</table></form>');
    it.innerHTML = html.join('');
  }
  _IG_Analytics(ga_acct, '/onion/google/gadget/tab_' + tabIndex(n));
}

function handle_fetchText(xml, tab_div) {
  if (!tab_div) {
    return;
  }
  var n_tab = parseInt(tab_div.id.substr(3));

  if (!xml || typeof(xml) != 'object' || xml.firstChild == null) {
    tab_div.className = 'msg_err';
    tab_div.innerHTML = msg_err_loading_feed;
    handle_textTab(null, n_tab);
    return;
  }
  
  var idx = tabIndex(n_tab);
  if (_args()['synd'] == 'ig') {
    prefs.set('selected_tab', idx);
  }

  var html = new Array();
  if (arTabs[idx].image && arTabs[idx].link) {
    html.push('<div class="section"><a target="_blank" href="', arTabs[idx].link, '"');
    html.push(' onfocus="this.blur()"');
    html.push(' onclick="return goSection(\'', _esc(arTabs[idx].link), '\')">');
    html.push('<img border="0" src="', _IG_GetCachedUrl(arTabs[idx].image), '"></a></div>');
  }

  var n_items = 0;
  var ar = xml.getElementsByTagName('item');
  for (var i = 0, n = Math.min(ar.length, 3); i < n; i++) {
    var title = getXMLValue(ar[i], 'title');
    var link = getXMLValue(ar[i], 'link');
    var desc = getXMLValue(ar[i], 'description');
    var pubDate = getXMLValue(ar[i], 'pubDate');
    if (title && link && desc && pubDate) {
      pubDate = formatDate(pubDate);
      html.push('<div class="item_title"><a class="item_title" target="_blank" href="', link, '"');
      html.push(' onfocus="this.blur();"');
      html.push(' onclick="return goItem(\'', _esc(link), '\');">', title, '</a></div>');
      html.push('<div class="item_date">', pubDate, '</div>');
      html.push('<div class="item_desc">', desc, '</div>');
      html.push('<div class="item_ruler"');
      if (arTabs[idx].ruler) {
        html.push(' style="border-bottom: solid 1px #', arTabs[idx].ruler, ';"');
      }
      html.push('>&nbsp;</div>');
      n_items++;
      title = null; link = null; pubDate = null; desc = null;
    }
  }

  if (n_items < 1) {
    handle_fetchText(null, tab_div);
  } else {
    tab_div.loaded = true;
    tab_div.innerHTML = html.join('');
    handle_textTab(tab_div.feed, n_tab);
  }
}

function handle_textTab(feed, n) {
  tab_was = stackTab(tab_was);
  var tab_div = _gel('tab' + n);
  if (tab_div) {
    var it = _gel('content');
    if (it) {
      if (tab_div.loaded) {
        setAdvert(tabIndex(n));
        it.innerHTML = '';
        it.appendChild(tab_div);
        _IG_Analytics(ga_acct, '/onion/google/gadget/tab_' + tabIndex(n));
      } else {
        if (feed) {
          it.innerHTML = 'Loading...';
          tab_div.feed = feed;
          _IG_FetchXmlContent(feed, _IG_Callback(handle_fetchText, tab_div));
        } else {
          it.innerHTML = '';
          it.appendChild(tab_div);
          _IG_Analytics(ga_acct, '/onion/google/gadget/tab_err_' + tabIndex(n));
        }
      }
    }
  }
}

function setOpacity(it, n) {
  if (it) {
    if (IE) {
      if (n == 100) {
        it.style.filter = '';
      } else {
        it.style.filter = 'alpha(opacity=' + n + ')';
      }
    }
    it.style.opacity = n/100;
    it.style.MozOpacity = n/100;
  }
}

function getTabFx(type, feed, n) {
  var rtn;
  if (type == 'video') {
    rtn = function () {
      handle_videoTab(feed, n);
    };
  } else if (type == 'radio') {
    rtn = function () {
      handle_radioTab(feed, n);
    };
  } else if (type == 'menu') {
    rtn = function () {
      handle_menuTab(feed, n);
    };
  } else {
    rtn = function () {
      handle_textTab(feed, n);
    };
  } 
  return rtn;
}

function stackTab(n) {
  var rtn = 0;

  if (tabs) {
    try {
      rtn = tabs.getSelectedTab().getIndex();
    } catch(err) {
      rtn = 0;
    }
  }

  var it = _gel('tab_stack');
  if (it) {
    var tab_div = _gel('tab' + n);
    if (tab_div) {
      it.appendChild(tab_div);
    }
  }

  return rtn;
}

function handle_Tabs() {
  var it = _gel('tabs_stack');
  if (it) {
    it.innerHTML = '';
  }
  var n_tabs = 0;
  tabs = new _IG_Tabs();
  for (var i = 0, n = Math.max(5, arTabs.length); i < n; i++) {
    if (arChosenTabs[i] > 0) {
      tabs.addTab(arTabs[i].title, {contentContainer: _gel('tab' + n_tabs), 
          callback: getTabFx(arTabs[i].type, arTabs[i].feed, n_tabs), 
          tooltip: arTabs[i].selection}); 
      arTabs[i].chosen = true;
      stackTab(n_tabs++);
    } else {
      arTabs[i].chosen = false;
    }
  }
  tabs.addTab('&nbsp;+&nbsp;', {contentContainer: _gel('tab' + n_tabs),
      callback: getTabFx('menu', null, n_tabs), 
      tooltip: 'select features'}); 
  stackTab(n_tabs++);
  if (IE) {
    selectTab(n_tabs - 1);
  }
  selectTab(selected_tab);
}

function addTabDiv(n_tabs) {
  var div = _gel('tab' + n_tabs);
  if (!div) {
    div = document.createElement('div');
    div.id = 'tab' + n_tabs;
    document.body.appendChild(div);
  }
  div.loaded = false;
}

function handle_fetchTabs(xml) {
  if (!xml || typeof(xml) != 'object' || xml.firstChild == null) {
    var it = _gel('content');
    if (it) {
      it.className = 'msg_err';
      it.innerHTML = msg_err_loading_tabs;
      return;
    }
  }

  var adv = xml.getElementsByTagName('advert');
  var default_advert_url = null;
  var default_advert_img = null;
  if (adv.length) {
    default_advert_url = getXMLValue(adv[0], 'advert_url');
    default_advert_img = getXMLValue(adv[0], 'advert_img');
  }

  arTabs = new Array();
  var n_tabs = 0;
  var ar = xml.getElementsByTagName('tab');
  for (var i = 0, n = ar.length; i < n; i++) {
    var title = getXMLValue(ar[i], 'title');
    var selection = getXMLValue(ar[i], 'selection');
    var feed = getXMLValue(ar[i], 'feed');
    var type = getXMLValue(ar[i], 'type');
    if (title && selection && feed && type) {
      var link = getXMLValue(ar[i], 'link');
      var image = getXMLValue(ar[i], 'image');
      var ruler = getXMLValue(ar[i], 'ruler');
      var advert_url = getXMLValue(ar[i], 'advert_url');
      if (!advert_url) {
        advert_url = default_advert_url;
      }
      var advert_img = getXMLValue(ar[i], 'advert_img');
      if (!advert_img) {
        advert_img = default_advert_img;
      }
      addTabDiv(n_tabs);

      arTabs[n_tabs] = new Tab(n_tabs, title, selection, feed, type, link, image, ruler, advert_url, advert_img);
      n_tabs++;
      title = null; selection = null; feed = null; type = null;
    }
  }

  if (n_tabs < 1) {
    handle_fetchTabs();
  } else {
    addTabDiv(n_tabs); // for the menu
    handle_Tabs();
  }
}

function getSize() {
  // find out what size iframe we are being displayed in
  if (self.innerWidth) {
    width = self.innerWidth;
    height = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientWidth) {
    width = document.documentElement.clientWidth;
    height = document.documentElement.clientHeight;
  } else if (document.body) {
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }
  var rtn = new Object();
  rtn.width = width;
  rtn.height = height;
  return rtn;
}

function init() {
  IAB = getSize();
  prefs = new _IG_Prefs();
  selected_tab = parseInt(getStringPref('selected_tab', '0'));
  if (isNaN(selected_tab)) {
    selected_tab = 0;
  }

  var s = getStringPref('user_tabs', '1_1_1_1_1_1');
  if (_args()['synd'] == 'ig') {
    // we may have come here from a reload so
    // save our settings
    prefs.set('user_tabs', s);
  }
  arChosenTabs = s.split('_');

  var it = _gel('logo');
  if (it) {
    var html = new Array();
    html.push('<a target="_blank" href="http://www.theonion.com/" onclick="return goLogo();">');
    html.push('<img border="0" src="', 
      _IG_GetCachedUrl(assets + 'onion_gadget_logo.gif'), '" title="The Onion" alt="The Onion"></a>');
    it.innerHTML = html.join('');
  }

  if (IE) {
    it = _gel('content');
    if (it) {
      it.style.width = '100%';
    }
  }

  try {
    var vp = gadgets.views.getParams();
    if (vp) {
      if (vp.selected_tab) {
        selected_tab = vp.selected_tab;
      }
    }
  } catch(err) {
    selected_tab = 0;
  }

  _IG_FetchXmlContent(service + 'gadget_tabs.xml', handle_fetchTabs);
  // record an impression
  _IG_Analytics(ga_acct, '/onion/google/gadget/init');
}
</script>
<style>
.canvas_advert {position:absolute;right:0;top:16px;}
</style>
</head>
<body onload="init()">
<div id="wrapper">
<div id="logo" class="logo"></div>
<div id="advert" class="canvas_advert"></div>
</div>
<div id="content" class="content">Loading...</div>
<div id="tab_stack" style="display:none;width:0;overflow:hidden;"></div>
</div>
<p style="height:600px;"></p>
</body>
]]> 
</Content> 
<Content type="html" view="canvas">
<![CDATA[
<head>
<link rel="stylesheet" href="http://www.google.com/ig/proxy?url=http://www.theonion.com/assets/static/onion/css/gadget.css" type="text/css">
<script src="http://www.google.com/ig/proxy?url=http://www.theonion.com/assets/static/onion/js/gadget.js" type="text/javascript"></script>
<script>
function handleAdvert(xml) {
  var it = _gel('advert');
  if (!it) {
    return;
  }
  if (!xml || typeof(xml) != 'object' || xml.firstChild == null) {
    return;
  }

  var adv = xml.getElementsByTagName('advert');
  if (adv.length) {
    var url = getXMLValue(adv[0], 'advert_url');
    var img = getXMLValue(adv[0], 'advert_img');
    var html = new Array();
    html.push('<a target="_blank" href="', url, '"');
    html.push(' onfocus="this.blur()"');
    html.push(' onclick="return goAdvert()">');
    html.push('<img border="0" src="', _IG_GetCachedUrl(img), '"></a>');
    it.innerHTML = html.join('');
  }
}

function handleDaily(xml) {
  var it = _gel('daily');
  if (!it) {
    return;
  }
  
  if (!xml || typeof(xml) != 'object' || xml.firstChild == null) {
    it.className = 'msg_err';
    it.innerHTML = msg_err_loading_feed;
    return;
  }
 
  var html = new Array();
  var n_items = 0;
  var ar = xml.getElementsByTagName('item');
  for (var i = 0, n = Math.min(9, ar.length); i < n; i++) {
    var title = getXMLValue(ar[i], 'title');
    var link = getXMLValue(ar[i], 'link');
    var desc = getXMLValue(ar[i], 'description');
    var pubDate = getXMLValue(ar[i], 'pubDate');
    if (title && link && desc && pubDate) {
      pubDate = formatDate(pubDate);
      html.push('<div class="item_title"><a class="item_title" target="_blank" href="', link, '"');
      html.push(' style="font-size:14px;"');
      html.push(' onfocus="this.blur();"');
      html.push(' onclick="return goItem(\'', _esc(link), '\');">', title, '</a></div>');
      html.push('<div class="item_date">', pubDate, '</div>');
      html.push('<div class="item_desc" style="font-size:12px;padding-top:4px;">', desc, '</div>');
      html.push('<div class="item_ruler"></div>');
      n_items++;
      title = null; link = null; pubDate = null; desc = null;
    }
  }
  it.innerHTML = html.join('');
  _IG_FetchXmlContent(service + 'gadget_tabs.xml', handleAdvert);
}

function init() {
  var url = 'http://www.theonion.com/feeds/list/rss/';
  _IG_FetchXmlContent(url, handleDaily);
  _IG_Analytics(ga_acct, '/onion/google/gadget/canvas_init');
}
_IG_RegisterOnloadHandler(init);
</script>
</head>
<body>
<a target="_blank" href="http://www.theonion.com"><img border="0"
alt="America's Finest News Source" title="America's Finest News Source"
src="/ig/proxy?url=/static/onion/img/gadget/onion_canvas_logo.gif"></a>
<div id="ruler" style="border-top:solid 4px #006600;"></div>
<div id="wrapper" style="padding:32px;padding-top:12px;height:800px;overflow:auto;">
<div id="daily">Loading...</div></div>
<div id="advert" class="advert"></div>
</body>
]]>
</Content>
</Module>
