/*
 * Copyright 2006 Christian Classics Ethereal Library
 * 
 * This file is part of CCEL-Desktop.
 *
 * CCEL-Desktop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *   
 * CCEL-Desktop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CCEL-Desktop; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//Handles highlighting of search text and scripture links based off of parameters

function highlightScripture(book, fch,fv, tch,tv){
	if(isNaN(fch)) fch=0;
	if(isNaN(fv)) fv=0;
	if(isNaN(tch)) tch=fch;
	if(isNaN(tv)) tv=fv;
	//alert("calling highlightScripture("+book+","+fch+","+fv+","+tch+","+tv+")");
	var arr=document.links;//first highlight the SCRIPREF anchors
	var needToPlaceAnchor=true;
	for(var i=0; i<arr.length; i++){
		if(arr[i].name){
			var parsed=arr[i].name.split(';');
			for(var j=0; j<parsed.length; j++){
				if(parsed[j].length<1)
					break;
				var scr=parsed[j].toLowerCase().split('_');
				for(var k=2; k<scr.length; k++)
					scr[k]=parseInt(scr[k]);
				//0=trans, 1=bk, 2=fc, 3=fv, 4=tc, 5=tv
				if(!scr[4] || scr[4]==0)
					scr[4]=scr[2];
				if(!scr[5] || scr[5]==0)
					scr[5]=scr[3];
				if(book.toLowerCase()==scr[1] && 
					(fch==0 || (fch>=scr[2] && tch<=scr[4])) && 
					(fv==0 || ((fch!=scr[2] || fv<=scr[3]) && (tch!=scr[4] || tv>=scr[5])))){
						arr[i].className="scrRefHighlight";
						arr[i].style.backgroundColor="red";
						arr[i].style.color="white";
						if(needToPlaceAnchor){
							insertAnchorAt(arr[i]);
							needToPlaceAnchor=false;
						}
				}
			}
		}
	}
	book = book.replace(/1/,"i").replace(/2/,"ii").replace(/3/,"iii").replace(/4/,"iv");
	var scrStart=book+"."+fch+"."+fv;
	var scrEnd=book+"."+tch+"."+(tv+1);
	var start=document.getElementById(scrStart);
	if(start){
		insertAnchorAt(start);
		var end=document.getElementById(scrEnd);
		if(!end){
			end=document.getElementById("noteBottom");
			if(!end)
				end=document.getElementById("navbottom");
		}
		//alert("scripture: "+scrStart+" - "+scrEnd);
		highlightUp(start.parentNode, start, end, null); //highlights the SCRIPTURE tag range
	}
}
function highlightUp(current, startingPlace, end, searchText){ //return true if find end
	if(!current) return false;
	if(current==end) return true;
	var i;
	for(i=0; current.childNodes[i]!=startingPlace; i++);
	for(i++; i<current.childNodes.length; i++){
		if(highlightDown(current.childNodes[i],end, searchText)==-1)
			return true;
	}
	return highlightUp(current.parentNode, current, end, searchText); //continue higher
}
function highlightDown(current, end, searchText){
	if(!current) return false;
	if(current.nodeValue){
		if(searchText)
			return textHighlightNode(current, searchText);
		return scriptureHighlightNode(current);
	}
	if(current==end) return -1;
	var temp;
	for(var i=0; i<current.childNodes.length; i++){
		temp = highlightDown(current.childNodes[i], end, searchText);
		i+=temp;
		if(temp==-1)
			return -1;
	}
	return 0;
}

function scriptureHighlightNode(current){
	var wrapper = document.createElement('span');
	var text=document.createTextNode(current.nodeValue);
	wrapper.appendChild(text);
	wrapper.style.backgroundColor='red';
	wrapper.style.color='white';
	current.parentNode.replaceChild(wrapper,current);
	return 0;
}

//returns offset from current to continue
function textHighlightNode(current, text){
	var nodeText = current.nodeValue;
	var i = nodeText.toLowerCase().indexOf(text);
	if(i!=-1 && (i==0 || " \t\b\"'\n\r".indexOf(nodeText.charAt(i-1))!=-1)){
		//alert("SearchText="+text+"\nTextNode: "+nodeText);
		var before = document.createTextNode(nodeText.substring(0,i));
		var highlight = document.createElement("span");
		highlight.id = "searchHighlight";
		highlight.style.backgroundColor='yellow';
		var highlightText = document.createTextNode(nodeText.substring(i,i+text.length));
		highlight.appendChild(highlightText);
		var after = document.createTextNode(nodeText.substring(i+text.length));
		current.parentNode.insertBefore(before, current);
		current.parentNode.insertBefore(highlight, current);
		current.parentNode.replaceChild(after,current);
		return 1;
	}
	return 0;
	
}

function debug(lcBodyText){
	var x;
	for(x=0; x+1000<lcBodyText.length; x+=1000)
  	  alert(lcBodyText.substring(x,x+1000));
    alert(lcBodyText.substring(x));
}

function highlightSearch(){
//alert("calling highlightSearch");
	var url = document.location.href;
	var index = url.indexOf('?');
	var searchText="";
	var book="",ch="",v="";
	if(index!=-1){
		url=url.substring(index+1);
		index=url.indexOf('#');
		if(index!=-1)
			url=url.substring(0,index);
		url+='&';
		index=url.indexOf("highlight=");
		if(index!=-1){
			var index2=url.indexOf('&',index);
			searchText=url.substring(index+10,index2);
		}
		index=url.indexOf("scrBook=");
		if(index!=-1){
			var index2=url.indexOf('&',index);
			book=url.substring(index+8,index2);
			index=url.indexOf("scrCh=");
			if(index!=-1){
				index2=url.indexOf('&',index);
				ch=url.substring(index+6,index2);
				index=url.indexOf("scrV=");
				if(index!=-1){
					index2=url.indexOf('&',index);
					v=url.substring(index+5,index2);
				}
			}
		}
	}
	if(!book && window.location.hash.length>0){
		var scr = window.location.hash.split(/\./);
		if(scr.length==3){
			book=scr[0].substring(1);
			ch=scr[1];
			v=scr[2];
		}
	}
	return highlightSearchTerms(searchText, book, ch, v);
}

function highlightSearchTerms(searchText, scrBk,scrCh,scrV)
{	
	if (!searchText && !scrBk && !scrCh && !scrV)
    return true;
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    return false;
  }
  if(scrBk){
  	var books=scrBk.split(",");
  	var chs=scrCh.split(",");
  	var vs=scrV.split(",");
  	for(var i=0; i<books.length; i++){
  		var ch = chs[i].split("-");
  		var v = vs[i].split("-");
  		highlightScripture(books[i],parseInt(ch[0]),parseInt(v[0]),parseInt(ch[1]),parseInt(v[1]));
  	}
  	//debug(bodyText);
  }
  if(searchText){
  	searchText = decodeURI(searchText);
	  searchArray = searchText.split(",");
	  var start = document.getElementById("navtop");
	  var end = document.getElementById("navbottom");
	  var color = "yellow";
	  for (var i = 0; i < searchArray.length; i++) {
	    //switch(i){
		//    case 0: color="yellow"; break;
		//    case 1: color="cyan"; break;
		//    case 2: color="palegreen";break;
		//    case 3: color="pink"; break;
	    //}
	    if(searchArray[i].length > 0)
	    	highlightUp(start.parentNode, start, end, searchArray[i]);
	  }
	  var insertNode=document.getElementById("searchHighlight");
	  if(insertNode)
		insertAnchorAt(insertNode);
  }
  
  //jump to the highlight anchor
  if(!window.location.hash || window.location.hash.length <1){
  	window.location.hash='#highlight';
  }else{
    window.location.hash=window.location.hash;
  }
  return true;
}

function insertAnchorAt(node){
//alert("calling insertAnchorAt");
	var highlightAnchor=document.createElement('A');
	highlightAnchor.href="#highlight";
	highlightAnchor.id="highlight";
	highlightAnchor.name='highlight';
	var insertNode=getLinkNodeIsIn(node);
	if(insertNode)
		node=insertNode;
	node.parentNode.insertBefore(highlightAnchor, node);
}
function getLinkNodeIsIn(node){
	while(node && (node.className!='a' && node.className!='A'))
		node=node.parentNode;
	return node;
}
