/**
 * Cause external links to open in a new window
 */
document.observe ("dom:loaded", function () {
  $$('a[href^="http:"]').each (function (link) {
    link.writeAttribute ({
      'title': 'External link: '+link.readAttribute ('href'),
      'target': '_blank'
    });
  });
});

/**
 * Cause links labeled "pop" to open in new window
 */
document.observe ("dom:loaded", function () {
  $$('a.pop').each (function (link) {
    link.writeAttribute ({
      'target': '_blank'
    });
  });
});

/**
 * Replace textified email addresses with mailto: links
 */
document.observe ("dom:loaded", function () {
  $$('.email-text').each (function (email_text) {
    var text = new String (email_text.innerHTML);

    // Extract the title (before colon) if exists
    var arr1 = text.split (':');
    if (typeof (arr1[1]) == 'undefined')
    {
      var arr2 = arr1[0].split (' ');
      var user = arr2[0];
      var domain = arr2[2];
      var ext = arr2[4];

      var display = user+'@'+domain+'.'+ext;
    }
    else
    {
      var arr2 = arr1[1].split (' ');
      var user = arr2[1];
      var domain = arr2[3];
      var ext = arr2[5];

      var display = arr1[0];
    }

    // Update the email address text with a link
    email_text.update ('<a href="mailto:'+user+'@'+domain+'.'+ext+'">'+display+'</a>');
  });
});

/**
 * Highlight table row/column on hover
 */
document.observe ("dom:loaded", function () {
  $$('table.highlight.row tr').each (function (tr) {
    tr.observe ('mouseover', function (event) {
      event.findElement ('tr').addClassName ('hover');
    });
    tr.observe ('mouseout', function (event) {
      event.findElement ('tr').removeClassName ('hover');
    });
  });
});

/**
 * Links that have class of "toggle" will hide/show the element they link to
 */
document.observe ("dom:loaded", function () {
  $$('a.toggle[href^="#"]').each (function (link) {
	  link.observe ('click', function (event) {
			target = $(event.findElement('a.toggle').readAttribute('href'));
			target.toggle ();
			event.stop ();
		});
	});
									
	// Hide the targets of the above links here, so that if the JavaScript fails,
	// the user won't have missing content.
	$$('.hide').invoke ('hide');
});
