/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* modified by Mark Wickens */
function highlightWord(node,word,caseSens) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word,caseSens);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		if (caseSens == 0) {
			tempNodeVal = node.nodeValue.toLowerCase();
			tempWordVal = word.toLowerCase();
		}
		else {
			tempNodeVal = node.nodeValue;
			tempWordVal = word;		
		}
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			if (pn.className != "ar_searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "ar_searchword";
				
				asin = '';
				if (docWordVal == 'Atlas Shrugged') {
					asin = '0452011876';
				}
				else if (docWordVal == 'The Fountainhead' || docWordVal =='The Fountainhead\'s') {
					asin = '0452286379';
				}
				else if (docWordVal == 'We The Living' ||  docWordVal == 'We the Living') {
					asin = '0451187849';
				}
				else if (docWordVal == 'Night of January 16th') {
					asin = '0452264863';
				}
				else if (docWordVal == 'Anthem') {
					asin = '0451191137';
				}
				
				if (asin != '') {
				    alink = document.createElement("a");
				    alink.setAttribute("href", "http://www.amazon.com/gp/product/" + asin + "?ie=UTF8&tag=wickensca-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=" + asin);
				    alink.appendChild(hiwordtext);
				    alink.className='ar_searchword';
				    hiword=alink;
				}
				else {
					hiword.appendChild(hiwordtext);
				}
				
				
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function getElementsByName_iefix(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function highlightAll() {
	//var startTime = new Date().valueOf();
	var nodes = getElementsByName_iefix("span", "entrybody");
	for (i=0; i<nodes.length; i++) {
		highlightWord(nodes[i],'Anthem', 1);
		highlightWord(nodes[i],'Ayn Rand\'s', 1);
		highlightWord(nodes[i],'Ayn Rand', 1);
		highlightWord(nodes[i],'Ayn', 1);
		highlightWord(nodes[i],'Randian', 1);
		highlightWord(nodes[i],'Rand\'s', 1);
		highlightWord(nodes[i],'Rand ', 1);
		highlightWord(nodes[i],'Rand.', 1);
		highlightWord(nodes[i],'Rand,', 1);
		highlightWord(nodes[i],'Rand;', 1);
		highlightWord(nodes[i],'Rand?', 1);
		highlightWord(nodes[i],'The Fountainhead\'s', 1);
		highlightWord(nodes[i],'Fountainhead\'s', 1);
		highlightWord(nodes[i],'The Fountainhead', 1);
		highlightWord(nodes[i],'Fountainhead', 1);
		highlightWord(nodes[i],'Atlas Shrugged', 0);
		highlightWord(nodes[i],'Atlas Shrugging', 0);
		highlightWord(nodes[i],'Atlas Shrug', 0);
		highlightWord(nodes[i],'Atlas', 1);
		highlightWord(nodes[i],'Objectivist\'s', 0);
		highlightWord(nodes[i],'Objectivism\'s', 0);
		highlightWord(nodes[i],'Objectivist', 0);
		highlightWord(nodes[i],'Objectivists', 0);
		highlightWord(nodes[i],'Objectivism', 0);
		highlightWord(nodes[i],'Night of January 16th', 0);
		highlightWord(nodes[i],'We the Living', 0);
		highlightWord(nodes[i],'Howard Roark\'s', 1);
		highlightWord(nodes[i],'Howard Roark', 1);
		highlightWord(nodes[i],'John Galt', 1);
	}
	//var endTime = new Date().valueOf();
	//alert(endTime - startTime);
}

