var list={"Articles of Faith":["3"],"abraham":["3"],"Matthew":["3"],"D&C":["3"],"1. Kings":["3"],"1. Nephi":["3"]}; 
var over=0;
var total=0;
var total_max = 30;
var key_max = 5;

 /* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
function highlightWord(node,word,type) {

	// 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,type);
		}
	}

	// And do this node itself
	if (node.nodeType == 3 && total < total_max && key < key_max) { // text node
		
		tempNodeVal = node.nodeValue.toLowerCase();
		
		var ref = '';
		
		tempWordVal = word.toLowerCase();
//		alert(tempWordVal);
		if (tempNodeVal.indexOf(tempWordVal) != -1 && total < total_max && key < key_max) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 && 
			checkn.nodeName.toLowerCase() != 'body') { 
			// 9 = top of doc
				if (checkn.nodeName.toLowerCase()=='a') { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword" && total < total_max && key < key_max) {
				// 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));
					
					add = 0
					
					if(type == 3 ) {
					 	endCounter = 1;
					 	startCounter = ni+word.length;
					 	var checking = true;
						var allowed = new Array();
					 	allowed[0] = '0';
					 	allowed[1] = '1';
					 	allowed[2] = '2';
					 	allowed[3] = '3';
					 	allowed[4] = '4';
					 	allowed[5] = '5';
					 	allowed[6] = '6';
					 	allowed[7] = '7';
					 	allowed[8] = '8';
					 	allowed[9] = '9';
					 	allowed[10] = ' ';					 	
					 	allowed[11] = ',';					 	
					 	allowed[12] = '-';
					 	allowed[13] = ':';	
					 	allowed[14] = ';';	
					 		
						
						while (checking == true) {
							
							var search = false;
						
							for (var ic=0; ic<=13 && search == false; ic++) {
							 //	alert(nv.charAt(startCounter+endCounter) +":"+ allowed[ic]);
								if (nv.charAt(startCounter+endCounter) == allowed[ic]) {
									search = true;
								//	alert('MATCH FOUND');
								}	
							}
							if (search == true) {
							 //	alert(endCounter);
								endCounter++;
							}
							else {
								checking = false;
							}
							
								
						}
						
						if (nv.charAt(startCounter+endCounter)==' ') {
						 
							endCounter--;
						}
						add = endCounter;

						if (endCounter > 0) {
							ref = nv.substr(startCounter,endCounter);
						
						}						 						 						 						 						 						 						 					 	
					}

					docWordVal = nv.substr(ni,(word.length+add));
					
					after = document.createTextNode(nv.substr(ni+word.length+add));
					hiwordtext = document.createTextNode(docWordVal);
					hiword = document.createElement("a");

					hiword.className = "searchword";
					
					hiword.setAttribute("style","border-bottom: 1px solid; color: #0000FF;line-height: 20px;");
					hiword.appendChild(hiwordtext);
					hiword.style.borderBottom='1px solid';
										hiword.style.color='#0000FF';					
					hiword.style.lineHeight='20px';					
				
					
					hiword.onmouseover=function (event){showBox(word,ref,event);};
					hiword.onmouseout=function (){startDelay();};

					pn.insertBefore(before,node);
					pn.insertBefore(hiword,node);
					pn.insertBefore(after,node);
					pn.removeChild(node);
				
					total++;
					key++;
			}
		}
	}
}


function hideDelay() {
	if (over == 1)	{
		over=0;
		forceRemoveElement();
	}
}

function startDelay() {
 	over=1;
	setTimeout('hideDelay()',1000);
}

function stopDelay() {
 	over = 0;
}

function searchWords() {

	if (total < total_max) {
		var as = document.getElementsByTagName('a');

		for (var asc = as.length-1; asc>=0; asc--) {
			as[asc].className = as[asc].className+" nosearchi ";
		}
	

		for(var word in list) {
	 		if (total < total_max) {
	 		 	key=0;
			 	var type=list[word][0];

				highlightWord(document.getElementsByTagName("body")[0],word,type);
			}
		}
	}
}

window.onLoad = searchWords();


//
//   CODE FOR BOXES
//

function zxcCkEventObj(zxce,zxcobj){
 	if (!zxce) {
  		var zxce=window.event;
	}
 	zxce.cancelBubble=true;
 	if (zxce.stopPropagation) {	
		zxce.stopPropagation();
	}
 	if (zxce.target){
		zxceobj=zxce.target;
	}
 	else if (zxce.srcElement) {
		zxceobj=zxce.srcElement;
	}
 	if (zxceobj.nodeType==3) {
		zxceobj=zxceobj.parentNode;
	}
 	var zxceobj=(zxce.relatedTarget)?zxce.relatedTarget:(zxce.type=='mouseout')?zxce.toElement:zxce.fromElement;
 	if (zxceobj==zxcobj) {
		return false;
	}
	while (zxceobj.parentNode){
  		if (zxceobj==zxcobj) {
			return false;
		}
  		zxceobj=zxceobj.parentNode;
 	}
 	return true;
}

function showBox(word,ref,e) {
 
 

	// GET POSITION OF MOUSE
	if (!e)
		e = window.event;
	if (e.pageX || e.pageY){
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop+ document.documentElement.scrollTop;
	}
	
	// GET SCREEN WIDTH
	
	 var viewportwidth;
	 var viewportheight;
 
 	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 	if (typeof window.innerWidth != 'undefined')  {
		viewportwidth = window.innerWidth,
      	viewportheight = window.innerHeight
 	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 	else if (typeof document.documentElement != 'undefined'
     			&& typeof document.documentElement.clientWidth != 'undefined' 
				&& document.documentElement.clientWidth != 0) {
       	viewportwidth = document.documentElement.clientWidth,
       	viewportheight = document.documentElement.clientHeight
 	}
 
 	// older versions of IE
 
 	else {
       	viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       	viewportheight = document.getElementsByTagName('body')[0].clientHeight
 	}

	// GET SCROLL POSITION
	
	var s_left = 0;
	var s_top = 0;
	
	if (navigator.appName == "Microsoft Internet Explorer"){
	 	s_top = document.body.scrollTop;
	 	s_left = document.body.scrollLeft;
	}
	else { 
	 	s_top = window.pageYOffset;
	 	s_left = window.pageXOffset;
	}

	
	
	var bwidth=322;
	var bheight=270;
	
	
	
	if (ldsbox = document.getElementById('ldsbox')) {
	 	forceRemoveElement();
	}	 
	 
	var boxort = '';
	var boxx = posx;
	var boxy = posy;
	 
	if (bheight > posy-s_top) {
		
		if (posx+bwidth > viewportwidth) {
			boxort = 'br';			
			boxx = boxx-bwidth+25;
			boxy = boxy+5;
		}
		else {
			boxort = 'bl';	
			boxx = boxx-15;	
			boxy = boxy+5;
		}
	}
	else {
	 	if (posx+bwidth > viewportwidth) {
			boxort = 'tr';			
			boxx = boxx-bwidth+10;
			boxy = boxy-bheight;
		}
		else {
			boxort = 'tl';
			boxy=boxy-bheight;		
		}
	}
	
	if (boxx<0){boxx=0;}
	if (boxy<0){boxy=0;}

	var ni = document.getElementsByTagName('body')[0];
   	var newdiv = document.createElement('div');
	newdiv.style.display = "none";  
	newdiv.setAttribute('onMouseOver','stopDelay();');	
	newdiv.setAttribute('style','float: left; position: absolute;top:'+boxy+';left:'+boxx+';width:'+bwidth+'px;height:'+bheight+'px;');
  	newdiv.setAttribute('id','ldsbox');
  	newdiv.onmouseout = function (){removeElement()};
	newdiv.style.position="absolute";	
	newdiv.style.top=boxy;	
	newdiv.style.left=boxx;	
	newdiv.style.width=bwidth;
	newdiv.style.margin='0px';
	newdiv.style.padding='0px';	
	newdiv.style.height=bheight;
	newdiv.onmouseover=function (){stopDelay();};			


	newdiv.innerHTML ='<iframe src="http://www.ldswebdeveloper.com/blogger/info.php?word='+URLEncode(word)+'&or='+boxort+'&ref='+URLEncode(ref)+'" width="'+bwidth+'" height="'+bheight+'" frameborder="0" border="0" allowtransparency="true" scrolling="no" style="padding: 0px; margin: 0px;"></iframe>';	
	
  	ni.appendChild(newdiv);
  	document.getElementById('ldsbox').style.top=boxy+'px';
  	document.getElementById('ldsbox').style.left=boxx+'px';  	
  	document.getElementById('ldsbox').style.position="absolute";  
  	document.getElementById('ldsbox').style.display = "block";  	
  	
}

function removeElement() {
  	if (ldsbox = document.getElementById('ldsbox')) {
 	 	var zxcevt=window.event||arguments.callee.caller.arguments[0];
 	 	
 	 	if( zxcCkEventObj(zxcevt,ldsbox) ) {
			var d = document.getElementsByTagName('body')[0];
  			var olddiv = document.getElementById('ldsbox');
  			d.removeChild(olddiv);
  		}
  	}
}


function forceRemoveElement() {
 	if (ldsbox = document.getElementById('ldsbox') != 'null') {
 			var d = document.getElementsByTagName('body')[0];
  			var olddiv = document.getElementById('ldsbox');
  			try {
               d.removeChild(olddiv);
            }
            catch (err) {
                
            }
   	}
}

function URLEncode(url) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" + // Numeric
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
	"abcdefghijklmnopqrstuvwxyz" +
	"-_.!~*'()"; // RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	
	var plaintext = url;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	
	return encoded;
}; 
