/*
* Thesaurus
*
* @package thesaurus
* @author $Author: sheiko $  
* @since v.2.0.1 
* @copyright (c) Dmitry Sheiko http://www.cmsdevelopment.com 
*/ 
var THConfig = { 
		Copyrights: '<div class="header"><a href="http://www.phpclasses.org/browse/package/3505.html" target="_blank">Thesaurus</a><div class="hr"></div></div>',
		divName: 'tooltip',
		MemoryCache: 'on',
		parseElements: ['PageContent']
};

var Cache = [];
var AcceptMessage=true;
var SubstitutionTable = new Array();

function getDefinition(term, evt){
	adjustTooltip(evt);
	if(Cache[term]!=undefined) { return showTooltip(term, Cache[term]);  }
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"thesaurus/index.ctrl.php", justalert_callback, "ctrl_action=getDataAndFitlering&term="+encodeURIComponent(term));
}

function calcClick(term){
	if(THConfig.MemoryCache!="on") return true;
	for(var term in Cache) {}
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"thesaurus/index.ctrl.php", justalert_callback, "ctrl_action=calcClick&term="+encodeURIComponent(term));
}

function parseContent(){
	var request = YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"thesaurus/index.ctrl.php", justalert_callback, 'ctrl_action=getDataAndParsing');
}

function applyTextParsing(termlist) {
		SubstitutionTable = eval( '(' + termlist + ')' );
		for(var Inx in THConfig.parseElements) {		
			parseChildNodes($(THConfig.parseElements[Inx]));
			re = /\{th\}(.*?)\{\/th\}/gi;
			text_str = $(THConfig.parseElements[Inx]).innerHTML + '';
			$(THConfig.parseElements[Inx]).innerHTML = text_str.replace(re, "<a class=\"thesaurus\" onmouseover=\"getDefinition('$1', event);\" onmouseout=\"setTimer();\">$1</a>");
		}
		
}

function parseChildNodes(obj) {
	for (var i=0; i<obj.childNodes.length; i++) {
		if(	obj.childNodes[i].childNodes.length>0 && 
			obj.childNodes[i].nodeName!="SCRIPT" && 
			obj.childNodes[i].nodeName!="A" && 
			obj.childNodes[i].nodeName!="H1" && 
			obj.childNodes[i].nodeName!="H2" && 
			obj.childNodes[i].nodeName!="H3")
				parseChildNodes( obj.childNodes[i] );
		else parseText( obj.childNodes[i] ) ;
	}
}

function parseText(obj) {
	var text_str = obj.nodeValue + '';
	if(text_str.length<2 || text_str=="null") return false;
	for(var i=0;i<SubstitutionTable.length;i++) {
		re = new RegExp(SubstitutionTable[i],(THConfig.CaseSensitive=="on"?"g":"gi")); 
		if(re.test(text_str)) {
			obj.nodeValue = applyTerm(SubstitutionTable[i], text_str);
			text_str = obj.nodeValue;
		}
	}
}
function applyTerm(term, text_str) {
	if(term+''==text_str+'') return "{th}"+text_str+"{/th}";
	re = new RegExp("("+term+")([ \r\n\.!;,%\"'])",(THConfig.CaseSensitive=="on"?"g":"gi")); 
	text_str = text_str.replace(re, "{th}$1{/th}$2");
	re = new RegExp("([ \r\n\.!;,%\"'])("+term+")$",(THConfig.CaseSensitive=="on"?"g":"gi")); 
	text_str = text_str.replace(re, "$1{th}$2{/th}");
	re = new RegExp("([ \r\n\.!;,%\"'])("+term+")([ \r\n\.!;,%\"'])",(THConfig.CaseSensitive=="on"?"g":"gi")); 
	text_str = text_str.replace(re, "$1{th}$2{/th}$3");
	return text_str;
}
