function formatExternalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "external")
      anchor.target = "_blank";
    }
}

Event.observe( window, 'load', function(){ 
  // Format external links
  formatExternalLinks(); 

  // Label first and last list items as appropriate
  navs = $$('ul');
  for (var i=0; i<navs.length; i++) {
    nav = navs[i];
    if(nav.immediateDescendants().length > 0) {
      nav.immediateDescendants().first().addClassName('first');
      nav.immediateDescendants().last().addClassName('last');
    }
  }

  $$('div > p:last-child').each(function(p) {
    p.addClassName('last');
  });

  $$('div > p:first-child').each(function(p) {
    p.addClassName('first');
  });

  // Create lightbox popups for any links that require authentication
  authLinks = $$('.link_requires_authentication');
  authLinks.each(function(link, index) {
    // Grab the original link so we have the redirect to
    redirectTo = link.getAttribute('href');

    // Turn off link - TODO: replace this with event cancellation
    link.setAttribute('href','#');

    // When link is clicked, it should pop up login box
    link.observe("click", function(event) {
      Dialog.alert({url: '/login?redirect_to=' + redirectTo, options: {method: 'get'}}, {className: "webistWindow", width:575, closable: true});
    });
  });

  // Assign odd and even class names to alternating table rows
	$$('table tbody > tr:nth-child(odd)').each(function(s) {
	  s.addClassName('odd');
	});

	$$('table tbody > tr:nth-child(even)').each(function(s) {
	  s.addClassName('even');
	});

  // Disable oneclick submit buttons on click
	$$('form .oneclick').each(function(button) {
          button.observe("click", function(event) {
                  button.form.submit();
                  button.setAttribute('disabled', 'disabled');
              });
	});

});
