var searchNodes = [];

function tSearchNode(id,type)
{
	var elNode = document.getElementById(id);
	if (!elNode)
		return false;
	this.elNode = elNode;
	this.setSearchType(type);
	this.getNodes();
}

tSearchNode.prototype.setSearchType = function (t)
{
	var sTypes = ['hl','hide','hidehl'];
	for (var i = 0; i < sTypes.length; i++)
	{
		if (t == sTypes[i])
		{
			this.searchType = t;
			return true;
		}
	}
	this.searchType = sTypes[0];
}

tSearchNode.prototype.getNodes = function ()
{
	if (!this.elNode)
		return false;
	this.textNodes = [];
	this.browseNode(this.elNode);
}

tSearchNode.prototype.browseNode = function (cNode)
{
	var debugDiv = document.getElementById('debugDiv');
	var temp;
	if (cNode.nodeType == 3)
	{
		temp = new tTextNode(cNode,this.searchType);
		if (temp.proper !== false)
		{
			this.textNodes.push(temp);
			//debugDiv.innerHTML += '<span style="color: red">' + this.textNodes[this.textNodes.length - 1].pName + '</span><br />';
		}
	}
	else if (cNode.nodeType == 1 && !matchClass(cNode,'nosearch'))
	{
		for (var i = 0; i < cNode.childNodes.length; i++)
		{
			this.browseNode(cNode.childNodes[i]);
		}
	}
}

tSearchNode.prototype.performSearch = function (str)
{
	for (var i = 0; i < this.textNodes.length; i++)
	{
		this.textNodes[i].Reset();
	}
	for (var i = 0; i < this.textNodes.length; i++)
	{
		this.textNodes[i].performSearch(str,this.searchType);
	}
}


function tTextNode(oNode,sType)
{
	this.proper = true;
	this.searchType = sType;
	var dd = document.getElementById('debugDiv');
	if (oNode.nodeType != 3)
		this.proper = false;
	else
	{
		this.Contents = oNode.innerText ? oNode.innerText : oNode.nodeValue ? oNode.nodeValue : '';
		//dd.innerHTML += this.pName + ' ' + String(this.Contents).length + ' ' + String(trim(this.Contents)).length + '<br />';
		this.Contents = trim(this.Contents);
		if (this.Contents == '' && oNode.parentNode.nodeName.toLowerCase() != 'tr')
			this.proper = false;
		this.elNode = oNode;
		this.pNode = oNode.parentNode;
		if (!this.pNode || this.pNode == undefined)
			this.proper = false;
		this.pName = oNode.parentNode.nodeName.toLowerCase();
		this.oldInnerHTML = this.pNode.innerHTML;
		this.pRefreshable = (this.pName == 'li' || this.pName == 'p' || this.pName == 'span' || this.pName == 'a' || this.pName == 'b' || this.pName == 'strong' || this.pName == 'i' || this.pName == 'td') ? true : false;
		//dd.innerHTML += '<b>' + this.pName + '</b> ' + this.Contents + '<br /><br />';
	}
}

tTextNode.prototype.Reset = function ()
{
	if (!matchClass(this.pNode,'nohide'))
	{
		addClass(this.pNode,this.searchType == 'hl' ? 'hidden' : 'invisible');
		removeClass(this.pNode,'matched');
	}
	this.pNode.innerHTML = this.oldInnerHTML;
}

tTextNode.prototype.Show = function()
{
	removeClass(this.pNode,this.searchType == 'hl' ? 'hidden' : 'invisible');
	this.pNode.innerHTML = this.pNode.innerHTML.replace(/\<span class="hl"\>([^<]+)\<\/span\>/ig,'$1');
}

tTextNode.prototype.match = function (str)
{
	var rE = new RegExp('(' + str + ')','ig');
	//return (this.pNode.innerHTML.replace(/<[^<]+>/ig,'').match(rE) ? true : false);
	return (this.oldInnerHTML.replace(/<[^<]+>/ig,'').toLowerCase().indexOf(str.toLowerCase()) == -1 ? false : true);
}

tTextNode.prototype.performSearch = function (str,searchType)
{
	if (str.length < 2)
	{
		removeClass(this.pNode,'matched');
		removeClass(this.pNode,this.searchType == 'hl' ? 'hidden' : 'invisible');
		this.pNode.innerHTML = this.oldInnerHTML;
		return;
	}
	if (this.match(str))
	{
		addClass(this.pNode,'matched');
		removeClass(this.pNode,this.searchType == 'hl' ? 'hidden' : 'invisible');
		if (this.searchType == 'hl' || this.searchType == 'hidehl')
		{
			var rE = new RegExp('(((?:<[^>]+>)*)(' + str + '))','ig');
			this.pNode.innerHTML = this.oldInnerHTML.replace(rE,'$2<span class="hl">$3</span>');
		}
		if (this.pName == 'li')
		{
			this.checkH2(this.pNode.parentNode);
		}
	}
	else
	{


	}
}

tTextNode.prototype.checkH2 = function (elNode)
{
	while (elNode = elNode.previousSibling)
	{
		if (elNode.nodeType == 1)
		{
			if (elNode.nodeName.toLowerCase() == 'h2')
			{
				addClass(elNode,'matched');
				removeClass(elNode,this.searchType == 'hl' ? 'hidden' : 'invisible');
				return true;
			}
			else
				return false;
		}
	}
}














function InitSearch(nodeId,type)
{
	if (document.getElementById(nodeId))
		searchNodes.push(new tSearchNode(nodeId,type));
}

function doSearch(str)
{
	str = str.replace(/[^a-z0-9à-ÿ,?! ;\-]/ig,'');
	for (var i = 0; i < searchNodes.length; i++)
  {
    searchNodes[i].performSearch(str);
  }
}



function startSearch(str,type)
{
  document.getElementById('searchField').value = str;
  document.getElementById('searchField').focus();
  doSearch(str,type);
}