/*
 * Richmond Pharmacology
 * Script: behaviour.js
 * Default behaviour layer
 *
 * By Andy Smith, Preview Graphics
 * www.preview.co.uk
 */


/*
 * Link behaviour
 * --------------------------------------------------------------------------
 */

/*
 * Add an event handler to all links within the node whose 'rel'
 * attributes contain the specified relation. The 'rel' attribute is
 * treated as a space-separated list. If relation is null, all links
 * will match. If relation is the empty string, only links with no
 * relation specified (or an empty rel attribute) will match.
 *
 * The event handler is added to the click and DOMActivate events.
 * This is meant to capture any time the link is followed. The click
 * event is also fired by key presses in all browsers as far as I
 * know, so there's no need to handle the keypress event too.
 */

function setLinkRelHandler(node, relation, handler)
{
  if (!node.getElementsByTagName) return;
  var links = node.getElementsByTagName('a');
  
  /*
   * Reduce list to links including the desired relation. If relation
   * is null all links will be passed through the filter.
   */
  pred = function (link) {
      if (link.getAttribute && (typeof relation == 'string'))
        {
	  var rel = link.getAttribute('rel');
	  if (relation == '')
	    return ((rel == '') || (rel == null))
	  else
            return getSpaceListItem(rel, relation);
	}
      else
        return false;
    }
  links = filter(pred, links);
  
  /*
   * Add the handler to all matching links.
   */
  for (var i = 0; i < links.length; i++)
    {
      addEvent(links[i], 'click', handler);
      addEvent(links[i], 'DOMActivate', handler);
    }
}

/*
 * Make 'enlargement' links open in a new window. If the 'rel' attribute
 * also contains a word like 'enlargement-x-y' it will open in an X by
 * Y window.
 */
 
function popupLink(e)
{  if (!e) var e = window.event;
  var target = getEventLinkTarget(e);
  
  if (target && target.href && window.open)
    {
      var features = 'toolbar=no,menubar=no,directories=no,status=no';
      
      if (target.getAttribute && RegExp)
        {
          var rel = target.getAttribute('rel');
          var relRe = new RegExp('\\benlargement-([0-9]+)-([0-9]+)\\b');
          var reResult = rel.match(relRe);
          if (reResult)
            {
              var x = reResult[1];
              var y = reResult[2];
              if (x && y)
                {
                  features += ',width='+x+',height='+y;
                }
            }
        }
      
      window.open(target.href, '', features);
      return stopEventDefault(e);
    }
  else
    {
      return true;
    }
}

/*
 * If links are marked as 'external' but not 'family' make them pop up
 * in a new window. 'Family' links are to other Richmond sites.
 */

function externalLinkPopup(e)
{
  if (!e) var e = window.event;
  var target = getEventLinkTarget(e);
  
  if (target && target.href && target.getAttribute && window.open)
    {
      var rel = target.getAttribute('rel');
      if (!getSpaceListItem(rel, 'family'))
        {
          window.open(target.href);
          return stopEventDefault(e);
        }
      else
        {
          return true;
        }
    }
  else
    {
      return true;
    }
}

/*
 * Initialise link handler. Make enlargement links pop up as described
 * above, and make external non-family links open in a new window (sorry!)
 */

addEvent(window, 'load', function () {
    setLinkRelHandler(document, 'enlargement', popupLink);
    setLinkRelHandler(document, 'external', externalLinkPopup);
  });
