// Flex layout (c) Craig J Copi (craig@copi.org)
// Turn narrow tables into wide tables.  We set a lower limit on the
//   size of tables we will expand. We do not adjust any relative width
//   tables, only fixed width. Background images are also removed.
// ==UserScript==
// @name          Flex Layout
// @namespace     http://craig.copi.org/greasemonkey/
// @description   Replaces fixed widths with variable widths for wide layout elements.
// @include       http://*.bensbargains.net/*
// @include       http://bensbargains.net/*
// @include       http://*.sportsline.com/*/story/*
// @exclude       http://*.sportsline.com/
// @include	  http://news.bbc.co.uk/*/hi/*
// @exclude	  http://news.bbc.co.uk/
// @exclude	  http://news.bbc.co.uk/*default.stm
// ==/UserScript==

/* Minimum size allowed for fixed width tables.  Anything larger
 * gets changed to 100%. */
var minsize = 200;

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}
// Remove background image from the body tag
addGlobalStyle('body { background-image: none ! important;}');

/* Select the tags we want using XPath.  Pull out all tags with
 * absolute widths > minsize. */
var items = document.evaluate(
  "//*[@width > " + minsize + "]",
  document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i=0; i < items.snapshotLength; i++) {
  var item = items.snapshotItem(i);
  //GM_log(item.tagName + ": " + item.width);
  /* Checking for the existence of .indexOf seems to be a good way to
   * insure that this is a tag for which we want to reset the width.
   * I'm not entirely sure why this is the case. */
  if (item.width && item.width.indexOf) { // Can we resize widths? 
    item.setAttribute('width', '100%');
  }
}

