//alert("");
// LookWAYup
// Copyright (c) 2000, 2001 Online Direct Inc.

// Overrideable parameters, add JS after this file.
var popupWidth = 200;
var popupHeight = 65;
var LWUintro=0;
var lwuID=50;  // health site
var stopwordsA = ["a","an","and","are","as","at","for","in","is","of","one","our","or","the","that","you"];
var LWUxD=-5;
var LWUyD=-5;

var lwuScript ='http://lookWAYup.com/lwu.exe/lwu/d';
var lwuImgDir ='http://lookWAYup.com/free/';

var dL=document;
ns4 = (dL.layers)? true:false;
ns6 = (dL.getElementById)? true:false; // DOM: Gecko or ie5
ie4 = (dL.all)? true:false; // ie4 or up
ie5 = (ie4 && ns6) ? true:false;
var vL = navigator.appVersion.toLowerCase(); 
X11 = (vL.indexOf('x11') > -1);
Mac = (vL.indexOf('mac') > -1);
ns = ns4 || (ns6 && !ie5);
var popIt=null;
var Popup;
var Click2=false;
var pop=false;
var dragging=false;
ClickOne=false;

var x=0;
var y=0;
var leftMax=0;
var fromBottom=800;
var timerID;
var tod = new Date();
var exp = new Date(tod.getTime() + 31449600000); // 52 weeks
exp= ';expires=' + exp.toGMTString();

divWidth=220;

function popTop(width, drag, mode)
{
var dc=(drag)?' onmousedown=\"drag()\"':''
if (mode=='c') {
width=40; divWidth=40
var t='<A HREF=\"JavaScript:rePop()\"><IMG SRC=\"'+lwuImgDir+'eyes.gif\" BORDER=0><\/A>'
}else{
var t='<SPAN STYLE="color:white; font:8pt Arial,sans-serif;" ID="TITLE">&nbsp;<B><FONT COLOR="#f4a460">E-cologis</FONT></B> Euro Convertisseur.<\/SPAN>'
}
return '<TABLE WIDTH='+width+' CELLSPACING=0 CELLPADDING=0 BORDER=1 STYLE=\"border: gray ridge thin; padding:0px;right:0px\"><TR><TD WIDTH='+width+' BGCOLOR=\"#695d51\"'+dc+'>'+t+'<\/TD><TD><A HREF=\"JavaScript:closePop()\"><IMG SRC=\"'+lwuImgDir+'close.gif\" BORDER=0 alt="x"><\/A><\/TD><\/TR>';
}
var intro='<TR><TD colspan=2 BGCOLOR=FLORALWHITE><a href="'+lwuImgDir+'helpintro.htm"><SPAN STYLE=\"font:normal 12px Arial,sans-serif\">';
intro+= (X11 || Mac)?'Double-click then highlight any word. ':'Double-click any word. ';
intro+= 'Click for details<\/SPAN><\/A>';

dL.write('<STYLE TYPE="text/css">#Popup {position:absolute;border:0px solid gray;left:10px;top:10px}</STYLE>');

// cookie?
if (dL.cookie.indexOf('LWUp=c')!= -1) {dL.write('<DIV ID=\"Popup\">'+popTop(divWidth,false,'c')+'<\/TABLE><\/DIV>')}
else dL.write('<DIV Name=\"Popup\" ID=\"Popup\">'+popTop(divWidth,false,'') +intro+'<\/TABLE><\/DIV>');

if (ns||ie4) InstallEventHandlers();

function FindWidth()
{
leftMax = ns? (window.innerWidth-divWidth-16) : (dL.body.clientWidth-divWidth-16)
}
if (ie4) Popup=document.all.Popup;
setTimeout('if (ns6)Popup=dL.getElementById(\'Popup\');popIt=ns4?dL.Popup:Popup.style;unPop();if (LWUintro){popIt.visibility=ns4?\'show\':\'visible\';pageOffset()}',300); // call soon
var LWUinc=1

function pageOffset() {
if (!pop && (LWUintro!=0)){
	if (!timerID)timerID=setTimeout("if(!pop)unPop();LWUintro=0",LWUintro);
	var w=parseInt(popIt.border,10)
	if (isNaN(w))w=0
	if (w==2)LWUinc=-1
	if (w==0)LWUinc=1
	w+=LWUinc;
	var t=ns?(window.pageYOffset):(dL.body.scrollTop);
	t-=(LWUyD+w);
	var l=ns?(window.pageXOffset):(dL.body.scrollLeft);
	l+=(leftMax-LWUxD-w);
	popIt.top= px(t);
	popIt.left=px(l);
	popIt.border= px(w)+' solid';
	setTimeout('pageOffset()',125);
  }
}

function InstallEventHandlers()
{
FindWidth();
if (ns4){
	window.captureEvents (Event.DBLCLICK | Event.MOUSEUP | Event.KEYDOWN | Event.MOUSEMOVE);
	window.onDblClick = DblClickHandler;
	window.onMouseUp = MouseUpHandler;
	window.onKeyDown = KeyDownHandler;
	window.onMouseMove = mouseMove;
}else{
if (ie4){
	dL.ondblclick = DblClickHandler;
	dL.onmouseup = MouseUpHandler;
	dL.onkeydown = KeyDownHandler;
	dL.onmousemove = mouseMove;
	}else{
		// gecko
		dL.addEventListener("dblclick", DblClickHandler, false); // ok?
		dL.addEventListener("mouseup", MouseUpHandler, false);
		dL.addEventListener("keydown", KeyDownHandler, false);
		dL.addEventListener("mousemove", mouseMove, false);
	}
}
window.onresize = FindWidth;
}

function mouseMove(e) {
if (ns) {x=e.pageX; y=e.pageY}
if (ie4) {x=event.x; y=event.y}
if (ie5) {x+=dL.body.scrollLeft; y+=dL.body.scrollTop}
if (ie4 && dragging && !event.button) dragging=false;  // we missed the mouseup event
if (dragging) {
	popIt.top=y-LWUyD;
	popIt.left= (x-LWUxD < leftMax)? ((x>LWUxD)?(x-LWUxD):0):leftMax;
}
return !dragging;  // don't bubble to selection when dragging.
}

function KeyDownHandler(e)
{
var key=ns?e.which:event.keyCode
if (key == 13)
{
// User hit carriage return. Make sure no form has the focus
fromBottom=ns? (window.outerHeight-e.screenY) : (dL.body.clientHeight-event.clientY);
	lookUpWord();
} 

// User hit escape.
if (key == 27) unPop()
// Continue normal processing.
return true;
} 

function DblClickHandler(e)
{
Click2 = true;
// If a term is selected now, pop up the LookWAYup window.
fromBottom=ns? (window.outerHeight-e.screenY) : (dL.body.clientHeight-event.clientY);
lookUpWord();
return true;
}

function MouseUpHandler(e)
{
// for X11, no DoubleClick event
if (X11 || Mac){
	if (ClickOne){Click2=true}
	else {ClickOne=true; setTimeout("ClickOne=false;",500);}
}
// User doubleclicked on nothing before, but now term is selected.
if (Click2) { lookUpWord() }
else {if (pop) dragging = false}
return true;
} 

function lookUpWord()
{
var word = GetSel();
var cleanWord = CleanString(word);
if (!cleanWord || !cleanWord.length) return;

Click2 = false;                                                   
pop=true;
dragging=false;
popIt.visibility= ns4? 'hide':'hidden';
popIt.border= '0px solid'
if (fromBottom<popupHeight) window.scrollBy(0, popupHeight-fromBottom+25);
bString = ns?(ns4?'ns4':'ns4'):(ie5?'ie5':'ie4');
dtitle = dL.title;
if (ie4)
	for (var i=0;i < dL.all.length; i++)
		if ((dL.all(i).tagName.toLowerCase() == 'meta') && (dL.all(i).name.toLowerCase() == 'keywords'))
			dtitle+=' '+dL.all(i).content;

LookWAYupURL = "e-cologisEuro.asp?s=p&p=" + escape(word)+"&cw=" + escape(cleanWord)+"&t=" + escape(CleanString(dtitle)) + "&h=" + location.host + '&b=' + bString + '&I='+lwuID;
//LookWAYupURL ="e-cologisEuro.asp"; 
if (divWidth-popupWidth){divWidth=popupWidth;FindWidth()}
popIt.top=px(y+8);
x=(x < leftMax) ? x:leftMax;
popIt.left = px(x);

titleBar= popTop(popupWidth,true)+'<TR><TD COLSPAN=2>'
if (ns4) { // Layers always crash    
alert(LookWAYupURL);
	lwuWin=window.open(LookWAYupURL, 'lwu', 'dependent=1,scrollbars=1,height='+popupHeight+',width='+popupWidth+',screenX='+(popIt.left+window.screenX+6-window.pageXOffset)+',screenY='+(popIt.top+window.screenY+window.outerHeight-window.innerHeight-24-window.pageYOffset));// adjust for statusbar
	lwuWin.focus();
}
else { Popup.innerHTML = titleBar+'<IFRAME SRC=\"'+LookWAYupURL+'\" MARGINHEIGHT=0 MARGINWIDTH=0 VSPACE=0 FRAMEBORDER=0 ALIGN=TOP WIDTH='+(popupWidth-2)+' HEIGHT='+(popupHeight-2)+'><\/IFRAME><\/TD><\/TR><\/TABLE>' };

popIt.visibility= ns4? 'hide':'visible'
}

function px(n){
return ns4?n:(n.toString() +'px');
}

function unPop() {
popIt.visibility= ns4? 'hide':'hidden';
pop=false;
}

function closePop(){
dL.cookie = 'LWUp=c'+exp
unPop();
}

function rePop(){
dL.cookie = 'LWUp=f'+exp
divWidth=220;
FindWidth();
if (ns4) {location.reload()} else {Popup.innerHTML= popTop(divWidth,false) +intro+'<\/TABLE>'};
clearTimeout(timerID)
timerID=setTimeout("if(!pop)unPop();LWUintro=0",LWUintro);
}

function drag() {
dragging=true;
if (ie4) {LWUxD=event.offsetX+15;LWUyD=event.offsetY;}
if (ns) {LWUxD=x-parseInt(popIt.left,10)}
}

function GetSel () 
{
var str="";
if (dL.getSelection) 
{ 
	str = dL.getSelection(); 
} else if (dL.selection && dL.selection.createRange) 
{ 
	var range = dL.selection.createRange(); 
	if (range) str = range.text; 
} 
if (!str) return "";

// Surely you jest
if (str.length > 45) return "";
var iEnd = str.length-1;
// trim
while (str.charAt (iEnd) == ' ') {iEnd--}
return str;
}

function CleanString (str) 
{
// LOWERCASE
str = str.toLowerCase();
// REMOVE PUNCTUATION, keeping whitespace
re = /[^A-Za-z0-9_\-\s]/g;
str = str.replace (re, " ");

// STOPWORDS
var strA=str.split(/ /); // JavaScript1.2, splits on any run of white space.
var	str2='';
var d='';
var fStop;

for (var i in strA)
{
	fStop=0;
	if (strA[i].length)
	{
		for (var j in stopwordsA)	if (strA[i] == stopwordsA[j]) {fStop=1;	break;};
		if (!fStop)	{str2 += d+Stem(strA[i]);d=' '}
	}
}
return str2;
}

function Stem (word)
{
// morphology: oversimplify Porter
// remove s from marbles faces farces barges
word=Fix (word, /(le)s$/);
word=Fix (word, /([aeiou][^aeiou]e)s$/);
word=Fix (word, /([cg]e)s$/);
// flies -> fly
re = /ies$/;
word=word.replace (re, "y");
// bags -> bag, lashes -> lash 
word=Fix (word, /([^aeioucs])e?s$/);
return word;
}

function Fix (word, re)
{
re.exec (word);
return word.replace (re, RegExp.$1);
}