
var divName = 'dictionary';
var Timer = null;
var dataStore = new Object();
var dictionaryArray = []; //here we load all the terms from the lexique from the database
var intellitext_path = '/jscripts/intellitext/'

if(document.implementation && document.implementation.createDocument) var isMozilla=true;
	else var isMozilla=false;

document.write('<div id="'+divName+'" onmouseover="clearTimer()" onmouseout=\"setTimer();\" class="dictionary">&nbsp;</div>');

function hideMessage(){
	var MessageObj=$(divName);
	MessageObj.style.visibility="hidden";
}

function clearTimer() {
 if (Timer) {
   clearTimeout(Timer);
   Timer = null;
 }
}

function setTimer() {
 	Timer = window.setTimeout('hideMessage()', 250);
}

window.size = function()
{
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else {
			if(document.body.clientWidth) {
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			} else {
				w = window.document.body.offsetWidth;
				h = window.document.body.offsetHeight;
			}
		}
	}
	//w3c
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

function adjustMessage(evt) {
	var left;
	var top;
	clearTimer();
	MessageObj = $(divName);
	if (isMozilla) event=evt;

	wsize = window.size();
	var rightedge = wsize.width-event.clientX;
	var bottomedge = wsize.height-event.clientY;
	
	// Compliance with HTML 4/XHTML
	if(document.documentElement && document.documentElement.scrollTop)
		scrollTop = document.documentElement.scrollTop;
	else
		scrollTop = document.body.scrollTop;
		
	// Compliance with HTML 4/XHTML
	if(document.documentElement && document.documentElement.scrollLeft)
		scrollLeft = document.documentElement.scrollLeft;
	else
		scrollLeft = document.body.scrollLeft;
		
	
	if (rightedge < MessageObj.offsetWidth)
		left = scrollLeft + event.clientX - MessageObj.offsetWidth;
	else
		left = scrollLeft + event.clientX;
		
	if (bottomedge < MessageObj.offsetHeight)
		top = scrollTop + event.clientY - MessageObj.offsetHeight;
	else
		top = scrollTop + event.clientY;

		MessageObj.style.top = (top)+"px";
		MessageObj.style.left = (left)+"px";
		MessageObj.innerHTML = '<div class="body">Voir la d&eacute;finition ...</div>';	
		MessageObj.style.visibility = "visible";
}

/*
GET THE DEFINITION USING AJAX - STORE THE RESULT SO WE DONT NEEED TO RETRIEVE IT ALL THE TIME
*/
function getDefinition(term,evt){
	adjustMessage(evt);

	//check if it exists in the dataStore
	if(dataStore[encodeURIComponent(term)]) {
		MessageObj.innerHTML = '<div id="th_top"><span>'+term+'</span><span class="close" onclick="hideMessage(); return false;"><label>x</label></span></div><div class="body">'+dataStore[encodeURIComponent(term)]+'</div><div id="th_link" onclick="document.location.href=\'http://banner.poker770.com/cgi-bin/SetupPoker.exe\'"><a href="http://banner.poker770.com/cgi-bin/SetupPoker.exe">TELECHARGER GRATUITEMENT</a></div>';
	} else {
		var url = intellitext_path + 'lexique.php?word=' + encodeURIComponent(term);
		new Ajax.Request(url, {method: 'post', onComplete:function(text){
				//store the term and its definition in an array
				dataStore[encodeURIComponent(term)] = text.responseText;
				MessageObj.innerHTML = '<div id="th_top"><span>'+term+'</span><span class="close" onclick="hideMessage(); return false;"><label>x</label></span></a></div><div onclick="return hideMessage();" class="body">'+text.responseText+'</div><div id="th_link" onclick="document.location.href=\'http://banner.poker770.com/cgi-bin/SetupPoker.exe\'"><a href="http://banner.poker770.com/cgi-bin/SetupPoker.exe">TELECHARGER GRATUITEMENT</a></div>';
				}
			}
		);
	}

}

/*
ALL THIS TO MAKE THE CONTEXTUAL TEXT
*/
function parseContent(){
	parseChildNodes($('content'));
	re = /\{th\}(.*?)\{\/th\}/gi;
	text_str = $('content').innerHTML + '';
	$('content').innerHTML = text_str.replace(re, "<a class=\"dictionary_link\" onmouseover=\"getDefinition('$1', event);\" onmouseout=\"setTimer();\">$1</a>");
}

function parseText(obj) {
	var text_str = obj.nodeValue + '';
	if(text_str.length<2 || text_str=="null") return false;
	for(var i=0;i<dictionaryArray.length;i++) {
		re = new RegExp(dictionaryArray[i],"gi"); 
		if(re.test(text_str)) {
			obj.nodeValue = applyTerm(dictionaryArray[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\.!;,%\"'])",("gi")); 
	text_str = text_str.replace(re, "{th}$1{/th}$2");
	re = new RegExp("([ \r\n\.!;,%\"'])("+term+")$",("gi")); 
	text_str = text_str.replace(re, "$1{th}$2{/th}");
	re = new RegExp("([ \r\n\.!;,%\"'])("+term+")([ \r\n\.!;,%\"'])",("gi")); 
	text_str = text_str.replace(re, "$1{th}$2{/th}$3");
	return text_str;
}

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] ) ;
	}
}