/**
 * This set of functions allow us to selectively remove anchors from links that
 * don't do anything. There are two sets of links were primarily concerned with:
 * those that refer to the page the browser's currently on and those that have
 * the key phrase 'linknotset' inside of them.
 */

$j(document).ready(function() {
  // fade and deactive incomplete links
  $j('a[@href*="linknotset"]').each(
	function () {
	  $j(this).removeAttr('href');
	  $j(this).fadeTo(0, 0.35);
	  $j(this).setStyle({textDecoration:'none'});
	}
  );
  
  // bold self-referencing links
  $j('a.navLink').each(function(){
	var aURL = $j(this).attr('href');
	if( window.location.href.indexOf( aURL ) >= 0 && aURL.length > 1 ) {
	  $j(this).css('text-decoration', 'none');
	  $j(this).css({color: '#444', fontWeight: 'bold'});
	  $j(this).removeAttr('title');
	}
  });
})

/* When the page loads, scrub all the anchors */
/*
Event.onDOMReady(function(){
	$$('a').each(function(anchor) {
		scrubAnchor(this);
	});
});
*/  

/* Removes anchors from links that don't go anywhere */
function scrubAnchor(anchor) {

	// Determine the URL without the relative link
	var currentURL = document.location.toString().split('#')[0];
	var targetURL = anchor.toString().split('#')[0];

	// Incomplete links will be faded to 30% and deactivated
	if (targetURL.indexOf('linknotset') != -1) {
		anchor.setOpacity(.35);
		deactivateAnchor(anchor);
	}
	
	// Self-referencing links will be bolded and deactivated
	else if (currentURL == anchor.toString()) {
		if (anchor.getAttribute('class') != 'rootHeaderLink') {
			deactivateAnchor(anchor);
			anchor.setStyle({fontWeight:'bold'});
			anchor.setStyle({color:'#444'});
			anchor.removeAttribute('title');
		}
	}
}

/* Deactivates an anchor, removing its link and underlining */
function deactivateAnchor(anchor) {
//  anchor.removeAttribute('href');
	anchor.setStyle({textDecoration:'none'});
}

/* Scrubs the descendants of the given node */
function scrubContents(node) {
	node.descendants().each(function(descendant) {
		if (descendant.hasAttribute('href')) {
			scrubAnchor(descendant);
		}   
	});
}
