// JavaScript Document
//the functions below are used to highlight text within the document after it's loaded:
function linkStrings(array, node, attributes){
	for(i=0;i<array.length;i++){
		linkString(array[i], node, attributes);
	}
}
function linkString(string, node, attributes,appendArray, prependArray){
	if(string==null || string.length==0) return;
	if(node == null || node == '') node = eval('document.body');
	if(!attributes || attributes.constructor.toString().indexOf("Array") == -1) attributes = new Array(); //to be used later to assign onClick, etc. actions.
	//dv=document.defaultView; //I think this is needed for IE.
	if(navigator.userAgent.indexOf('Safari') == -1) linkStringWithinNode(node, string.toUpperCase(), attributes,appendArray,prependArray);
}

function linkStringWithinNode(node, searchstring, attributes,appendArray,prependArray){
	var pos, skip, newlink, middlebit, endbit, middleclone;skip=0, count=0;
	//if(appendArray.constructor.toString().indexOf("Array") == -1) appendArray = new Array();
	//if(prependArray.constructor.toString().indexOf("Array") == -1) prependArray = new Array();
	len = searchstring.length;
	if( node.nodeType==3 ){
		pos=node.data.toUpperCase().indexOf(searchstring);
		if(pos>=0){
			//spannode=document.createElement("SPAN");
			//spannode.style.backgroundColor=eval('"'+color+'"');
			//create the link
			newlink = document.createElement('a');
			for(k in attributes){
		 		newlink.setAttribute(k, attributes[k]);
			}
			middlebit=node.splitText(pos);
			endbit=middlebit.splitText(len);
			middleclone=middlebit.cloneNode(true);
			newlink.appendChild(document.createTextNode(middlebit.nodeValue));
			if(node.parentNode.toString().indexOf('http:') == -1 && node.parentNode.toString().indexOf('mailto:') == -1 && node.parentNode.toString().indexOf('https:') == -1 && node.parentNode.toString().indexOf('ftp:') == -1){ //don't link already linked nodes.	
				//coninue; //for some reason this doesn't work?
				//now append:
				for(k in appendArray){
					append = document.createElement(appendArray[k]['type']);
					for(j in appendArray[k]['attributes']){
						append.setAttribute(j,appendArray[k]['attributes'][j]);
					}
					newlink.appendChild(append);
				}
				//prepend:
				for(k in prependArray){
					prepend = document.createElement(prependArray[k]['type']);
					for(j in prependArray[k]['attributes']){
						prepend.setAttribute(j,prependArray[k]['attributes'][j]);
					}
					prepend.appendChild(newlink);
					newlink = prepend;
				}
				middlebit.parentNode.replaceChild(newlink,middlebit);
			}
			++count;
			skip=1;
		}
	}else if( node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!="SCRIPT" && node.tagName.toUpperCase!="STYLE" && node.parentNode.tagName.toUpperCase!="A"){
		for (var child=0; child < node.childNodes.length; ++child){
			child=child+linkStringWithinNode(node.childNodes[child], searchstring, attributes,appendArray,prependArray);
		}
	}
	return skip;
}
