/*
 * Disable/Enable Anchor scripts help in complimenting the Effect.Fade functions.
 * Problem: When a layer is faded and another layer is popped up, the layer that was
 * faded will be active with hyper links still active and traversal possible.
 * The replaceAnchors function accepts a section most likely a div, if not
 * the root HTML element as parameter. It gets all the anchor elements, stores them
 * in a global array for enabling again when needed. It then replaces these anchors
 * with a text node. getBackLinks restores the section to the original form.
 *
 * WARNING: These functions work on a happy path, exception and error handling
 * is not complete.
 *
 * Assumption: Styles for text node cannot be assigned explicitely. It is assumed
 * parent element has the necessary style defined for that section.  If not
 * the text replaced for the anchor will be black. Dont be surprised!
 *
 * +-----------+-------------------------+--------------------------------------------+
 * | Date      | Author                  | Comments                                   |
 * +-----------+-------------------------+--------------------------------------------+
 * | 15 Mar 06 | R Ramesh                | Anklet created, as part of Suggestica.     |
 * |           |                         | Could be reused wherever needed            |
 * +-----------+-------------------------+--------------------------------------------+
 *
 * Copyright: Compassites Software Solutions P Ltd - 2006-2007
 */

// Array - stores all the anchor elements
var links;

// Array - stores the parent element of each link
var parentOfLinks;

// Array - stores the text nodes that has to be replaced for anchor and vice versa
var linkTexts;

/*
 * This function replaces all anchors with text nodes
 */
function replaceAnchors(section) {
	// initialize the Arrays
	links = new Array();
	linkTexts = new Array();
	parentOfLinks = new Array();
	
	// Get the section element
	divElement = document.getElementById(section);
	
	// Get all the Anchor elements
	anchors = divElement.getElementsByTagName("a");
	
	noOfAnchors = anchors.length;
	
	//Loop through the anchors
	for (index=0;index < noOfAnchors; index ++) {
		/*
		 * OK here some explanation is needed.  Whenever an anchor is replaced with a text
		 * the DOM structure is changed, so the next available anchor will be in the zero'th
		 * position, thats why you see anchors[0] being used. Got it?
		 */
		
		// Store the link element for later replacement. Clone and get it so
		// it is not a reference but a copy
		links[index] = anchors[0].cloneNode(true);
		
		// Store the parent element of the link for restoring
		parentOfLinks[index] = anchors[0].parentNode;
		
		// Take a copy of the child element of anchor
		linkTexts[index] = anchors[0].childNodes[0];
				
		//Replace the anchor with a text node
		parentOfLinks[index].replaceChild(linkTexts[index], anchors[0]);
	}
	
}

/*
 * This function restores back all the anchors using the stored information
 */
function getBackLinks() {
	for(index = 0; index < links.length; index++) {
		parentOfLinks[index].replaceChild(links[index], linkTexts[index]);
	}

}