// LookWAYup
// Copyright (c) 2000-2004 RES Inc.
// You may not change nor copy this file, or even write programs "inspired" by it
// Webmasters: you can put LookWAYup on your pages using the methods and subject to the conditions on the LookWAYup site.

// Overrideable parameters, add JS after this file.
var popupWidth=300;
var popupHeight=250;
var LWUintro=9000;
var lwuID=50;  //  site type
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 lwuImgDir ='http://lookWAYup.com/free/';

var lwuScript ='http://lookWAYup.com/lwu.exe/lwu/d';
var slang
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);
Agt = navigator.userAgent.toLowerCase()
Ope = (Agt.indexOf('ope') > -1);
Saf = (Agt.indexOf('safari') > -1);
ns=ns4 || (ns6 && !ie5);
var popIt=null;
var Popup;
var Click2=false;
var pop=false;
var dragging=false;
ClickOne=false;
if (ns6) {var sa= dL.getElementsByTagName('script')
for (var i=0;i<sa.length;i++) if (sa[i].src.match(/lWu\./)) lwuImgDir=sa[i].src.split(/lWu\./)[0]}

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;

var eCnt=0
function imgErr(tag)
{
if (eCnt++<2)tag.src=tag.src.replace(lwuImgDir, 'http://lookWAYup.com/free/')
}

function popTop(width, drag, mode)
{
var dc=(drag)?' style=\"\{cursor:move\}\" onmousedown=\"drag()\"':''
if (mode=='c') {
width=40; divWidth=40
var t='<A HREF=\"JavaScript:rePop()\"><IMG SRC=\"'+lwuImgDir+'eyes.gif\"  onError=\"imgErr(this)\" onAbort=\"imgErr(this)\" BORDER=0><\/A>'
}else{
var t='<SPAN STYLE="color:white; font:8pt Arial,sans-serif;" ID="TITLE">&nbsp;<B><FONT COLOR="yellow">LookWAYup</FONT></B> Dictionary, More Info.<\/SPAN>'
}
return '<TABLE WIDTH='+width+' CELLSPACING=0 CELLPADDING=0 BORDER=1 STYLE=\"border: gray ridge thin; padding:0px;right:0px;z-index:2\" onSelectStart="return false"><TR><TD WIDTH='+width+' BGCOLOR=\"darkblue\"'+dc+'>'+t+'<\/TD><TD><A HREF=\"JavaScript:closePop()\"><IMG SRC=\"'+lwuImgDir+'close.gif\" onError=\"imgErr(this)\" onAbort=\"imgErr(this)\" BORDER=0 alt="x"><\/A><\/TD><\/TR>';
}

var intro='<TR><TD colspan=2 BGCOLOR=FLORALWHITE><a href="http://lookWAYup.com/free/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;z-index:2}</STYLE>');

// cookie?
var cmode=''
if (dL.cookie.indexOf('LWUp=c')!= -1) {intro='';cmode='c'}
dL.write('<DIV Name=\"Popup\" ID=\"Popup\">'+popTop(divWidth,false,cmode)+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 (Ope)dL.onmouseup=MouseUpHandler;
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||Ope||Saf){
	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 when dragging.
}

function KeyDownHandler(e)
{
var key=ns?e.which:event.keyCode
if (key==13)// CR. Make sure no form has the focus
{fromBottom=ns? (window.outerHeight-e.screenY) : (dL.body.clientHeight-event.clientY);
lookUpWord();} 
if (key==27) unPop()// Esc
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 || Ope){
	if (ClickOne){Click2=true}
	else {ClickOne=true; setTimeout("ClickOne=false;",600);}
}
// User doubleclicked on nothing before, but now term is selected.
if (Click2) { lookUpWord() }
else {if (pop) dragging=false}
return true;
} 

function lookUpWord()
{
Click2=false;
var word=GetSel();
if (Ope && !word) {window.open('http://lookwayup.com/free/installOP.htm','inst', 'scrollbars,resizable'); return}
var cleanWord=CleanString(word);
if (!cleanWord || !cleanWord.length) return;

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;
var l=(slang)?';slang='+slang:'';slang=null
LookWAYupURL=lwuScript+l+"?s=p&w=" + escape(word)+"&cw=" + escape(cleanWord)+"&t=" + escape(CleanString(dtitle)) + "&h=" + location.host + '&b=' + bString + '&I='+lwuID;
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
	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) if (Saf) str=getSelection(); // Safari
if (!str) return "";
if (str.length > 45) return "";
str=str.replace(/^[^A-Za-z]+|[^A-Za-z]+$/g,'');// trim
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
// bags -> bag, lashes -> lash ,marbles -> marble
word=Fix(Fix(Fix(Fix(word, /(le)s$/), /([aeiou][^aeiou]e)s$/), /([cg]e)s$/), /([^aeioucs])e?s$/);
re = /ies$/;
return word.replace (re, "y");
}

function Fix (word, re)
{
re.exec (word);
return word.replace (re, RegExp.$1);
}
