
var CP_UsedWords=[];

function CP_ScanNode(o){
var ChCol, Len, i, Ch, NodeNum;

ChCol=o.childNodes; Len=ChCol.length;
for(i=0; i<Len; i++){
	Ch=ChCol[i];
	if (Ch.nodeType==3){
		NodeNum=CP_FindWords(Ch); i+=NodeNum;	Len+=NodeNum;
		}
	else if (Ch.hasChildNodes()){
		if (oCP.SkipTags.indexOf(','+Ch.nodeName+',')!=-1) continue;
		CP_ScanNode(Ch);
		}
	}
}


function CP_WordCh(Ch){

if (Ch.length==0) false;
if (Ch.search(/\w/)!=-1) return true;

var Code=Ch.charCodeAt(0);
return (Code>=1072 && Code<=1103 || Code==1105);
}


function CP_FindWords(o){
var Txtl=o.data.toLowerCase(), i, Word, WL, aPos=[], Pos, NodeNum=0, Ch, EndPos, StrLen;

//find positions of all words
for(i=0; i<CP_Keywords.length; i++){
	//Only for poker sites: hilight just once
	if (CP_UsedWords[i]) continue;
	
	Word=CP_Keywords[i]; WL=Word.length; Pos=0;
	while(true){
		Pos=Txtl.indexOf(Word, Pos); if (Pos==-1) break;
		//check word boundary
		while(true){
			if (CP_WordCh(Txtl.charAt(Pos-1))) break;
			EndPos=Pos+WL; StrLen=WL;
			Ch=Txtl.charAt(EndPos);
			
			if (CP_WordCh(Ch)){
				if (Ch=='s'){
					if (CP_WordCh(Txtl.charAt(EndPos+1)))	break;
					StrLen++;
					}
				else if (Txtl.substr(EndPos, 2)=='es'){
					if (CP_WordCh(Txtl.charAt(EndPos+2)))	break;
					StrLen+=2;
					}
				else break;
				}
			
			aPos[aPos.length]=[Pos, StrLen, i]; 
			
			break;
			}
		Pos+=WL;
		}
	}

if (aPos.length){
	//sort and filter out overlaps
	if (true){ //aPos.length>1
		
		var Match1, Match2;

		if (aPos.length>1) aPos.sort(CP_PosSort); 
		
		for(i=0; i<aPos.length; i++){
			Match1=aPos[i]; 

			if (!CP_UsedWords[Match1[2]]){
				
				if (i<aPos.length-1){
					Match2=aPos[i+1];
					if (Match1[0]+Match1[1]>Match2[0]){
						aPos.splice(i+1, 1); i--; continue;
						}
					}

				CP_UsedWords[Match1[2]]=1;
				}
			//Remove duplicates (only for poker sites!)
			//I have to do it right in the loop to consider the filter
			else{
				aPos.splice(i, 1); i--;
				}
			}
		}

	if (aPos.length) NodeNum=CP_HiLite(o, aPos);
	}
return NodeNum;
}


function CP_HiLite(o, aPos){
var i, Txt=o.data, LastPos=0, Match, NodeNum=0, Par=o.parentNode, oLink, St;

for(i=0; i<aPos.length; i++){
	Match=aPos[i];
	NodeNum+=CP_AddText(Txt.substring(LastPos, Match[0]), Par, o);
	oLink=oCP.D.createElement('SPAN');
	oLink.className='Tooltip';
	CP_AddText(Txt.substr(Match[0], Match[1]), oLink, null);

	St=oLink.style; 
	//St.color='blue'; //St.cursor=(!oCP.IE5 ? 'pointer' : 'hand');
	//St.textDecoration='underline'; //St.borderBottom='1px dotted blue';

	oLink.onmouseover=CP_OMOver; oLink.onmouseout=CP_OMOut;
	
	oLink.setAttribute('wn', Match[2]);
	Par.insertBefore(oLink, o); NodeNum++;

	LastPos=Match[0]+Match[1];
	}

if (LastPos<Txt.length)	o.data=Txt.substr(LastPos);
else{
	Par.removeChild(o); NodeNum--;
	}

return NodeNum;
}

//-- Events

function CPInitEvent(e, bTo){

oCP.E=(!oCP.Gec ? event : e); oCP.EvSrc=oCP.E[oCP.Gec ? 'target' : 'srcElement'];
if (oCP.Ns) CP_CheckEvNode('EvSrc');
if (bTo){
	oCP.EvTo=(!oCP.Gec ? oCP.E.toElement : oCP.E.relatedTarget);
	if (oCP.Ns) CP_CheckEvNode('EvTo');
	}
}


function CP_CheckEvNode(Prop){
if (oCP[Prop].nodeType==3) oCP[Prop]=oCP[Prop].parentNode;
}

function CP_OMOver(e){

if (oCP.bMD) return;
CPInitEvent(e);

if (oCP.WinVis && oCP.EvSrc==oCP.oLink){
	clearTimeout(oCP.tmHide); return;
	}
oCP.LastSrc=oCP.EvSrc; oCP.tmSearch=setTimeout("CP_Request();", 200);
}

function CP_OMD(e){
var Type, Handler;

CPInitEvent(e); Type=oCP.E.type; oCP.bMD=(Type=='mousedown');
Handler='old'+Type;
if (oCP[Handler] && oCP[Handler]!=CP_OMD.caller) return oCP[Handler](e);
}

function CP_OMOut(e){

if (oCP.tmSearch){
	clearTimeout(oCP.tmSearch); oCP.LastSrc=null; return;
	}
if (!oCP.WinVis){
	oCP.LastSrc=null; 
	if (oCP.Op) setTimeout("CP_HideText()", 100); 
	return;
	}

CPInitEvent(e, true);

if (CP_Contains(oCP.oWin, oCP.EvTo)) return;
if (oCP.EvSrc!=oCP.oWin) oCP.tmHide=setTimeout("CP_HideText()", 750);
else{
	if (oCP.EvTo && oCP.EvTo.onmouseover==CP_OMOver) return;
	CP_HideText();
	}
}

function CP_TextOMOver(){clearTimeout(oCP.tmHide);}

function CP_OKP(e){

CPInitEvent(e);
if (oCP.E.keyCode==27) CP_HideText();
else if (oCP.OldOKP) oCP.OldOKP(e);
}

//-- Utils 

function CP_Contains(o, Ch){
while(Ch && Ch!==o && Ch!==oCP.D) Ch=Ch.parentNode;
return (Ch===o);
}


function CP_AbsPos(O, Parent){
var X=0, Y=0, Next, Root=CP_GetRoot();

if (oCP.IE) O.style.marginLeft='1px';

Next=O; if (Parent==null) Parent=document;
while (Next!=null && Next!==Parent){
	Y+=Next.offsetTop; X+=Next.offsetLeft; 
	Next=Next.offsetParent;
	}

//Subtract scroll
Next=O; if (Parent==null) Parent=document;
while (Next!=null && Next!==Parent){
	if (Next.scrollTop!=0) Y-=Next.scrollTop;
	if (Next.scrollLeft!=0) X-=Next.scrollLeft;
	Next=Next.parentNode;
	if (Next==Root) break; //do not consider page scroll
	}


if (oCP.IE) O.style.marginLeft='0px';

return [X, Y];
}

//--

function CP_PosSort(p1, p2){
var diff=p1[0]-p2[0]; return (diff!=0 ? diff : p2[1]-p1[1]);
}

function CP_GetRoot(){
return (oCP.D.getElementsByTagName((oCP.D.compatMode=='BackCompat' && !oCP.Saf) || oCP.Op ?
		'BODY' : 'HTML')[0]);
}

function CP_GetPos(Link){
var 
	oRoot=CP_GetRoot(), 
	ScrTop=(!oCP.Saf ? oRoot.scrollTop : oCP.D.body.scrollTop),
	Above, Below, LinkH=Link.offsetHeight, X, Y, Diff, DivH=oCP.oWin.offsetHeight, aPos;

aPos=CP_AbsPos(Link); X=aPos[0]; Y=aPos[1];
//Y
Diff=ScrTop+oRoot.clientHeight-Y-LinkH-DivH;

if (Diff<0){ //doesn't fit below
	Y+=3; Above=Y-ScrTop;
	if (Above>=DivH) Y-=DivH;
	else{ //doesn't fit above
		Below=Diff+DivH;
		if (Above<Below) Y+=LinkH+Diff;
		else Y-=Above;
		}
	}
else Y+=LinkH;

//X
Diff=oRoot.scrollLeft+oRoot.clientWidth-X-oCP.oWin.offsetWidth;
if (Diff<0){
	X+=Diff; //X-=oCP.oWin.offsetWidth-Link.offsetWidth; //
	}

return [X, Y];
}


function CP_Request(){
var ScrSrc, w;

oCP.tmSearch=null;

if (oCP.oLink && oCP.LastSrc.getAttribute('wn')==oCP.oLink.getAttribute('wn')){
	//same words - just move the hint
	CP_PosWin(); clearTimeout(oCP.tmHide); return;
	}

w=CP_Keywords[oCP.LastSrc.getAttribute('wn')];
w=(!oCP.IE5 ? encodeURIComponent(w) : escape(w));
ScrSrc=TT_SearchURL+w+(window.CP_PubParams ? CP_PubParams : '')+(oCP.utf ? '&utf=1' : '');

if (0){//oCP.IE5
	var FrDoc;
	FrDoc=window.CPFr.document; FrDoc.open();
	FrDoc.write('<'+'Script Src="'+ScrSrc+'&op=1"><'+'/Script>');
	FrDoc.close();
	}
else{
	oCP.Scr=oCP.D.createElement('SCRIPT');
	oCP.B.appendChild(oCP.Scr); oCP.Scr.src=ScrSrc;
	}
}

function CP_LinkOver(){window.status='Go to '+oCP.Host;}

function CP_MakeHint(Term, Descr){
var i, ChCol, Len, oP, Width, FixWidth;

if (oCP.LastSrc){
	//kill old stuff
	ChCol=oCP.oWin.childNodes; Len=ChCol.length;
	for(i=0; i<Len; i++) oCP.oWin.removeChild(ChCol[0]);
	oCP.oWin.style.width='auto';
	
	Descr=Descr.replace(/\\n/g, "\n");
	oCP.oWin.innerHTML='<H5>'+Term+'</H5>\n'+Descr;
	//CP_AddWinEl('H5', Term); oP=oCP.D.createElement('DIV');
	//oP.innerHTML=Descr; oCP.oWin.appendChild(oP);
	
	Width=oCP.oWin.scrollWidth;
	if (Width>=300) FixWidth='300';
	else if (Width<100) FixWidth='100';
	if (FixWidth) oCP.oWin.style.width=FixWidth+'px';
	
	CP_PosWin();
	
	if (!oCP.WinVis){
		setTimeout("CP_ShowText()", 10);
		oCP.OldOKP=oCP.D.onkeypress; oCP.D.onkeypress=CP_OKP;
		}
	}

if (oCP.Scr){
	oCP.B.removeChild(oCP.Scr); oCP.Scr=null;
	}
else setTimeout("CP_KillFrame()", 1);

clearTimeout(oCP.tmHide);
}

function CP_KillFrame(){
var FrDoc=window.CPFr.document; FrDoc.open(); FrDoc.close();
}


function CP_PosWin(){
var aPos;

aPos=CP_GetPos(oCP.LastSrc);
oCP.oWin.style.left=aPos[0]+'px'; oCP.oWin.style.top=aPos[1]+'px';
oCP.oLink=oCP.LastSrc;
}


function CP_ShowText(){
var bFlt=CP_FilterInit();

oCP.oWin.style.visibility='visible'; oCP.WinVis=true;
if (bFlt) oCP.Flt.Play();
}


function CP_FilterInit(){
var bFlt=false;

if (oCP.Flt && oCP.Flt.status!=2){
	oCP.Flt.Apply(); bFlt=true;
	}
return bFlt;
}

function CP_HideText(){

if (!oCP.WinVis) return;
oCP.D.onkeypress=oCP.OldOKP; oCP.OldOKP=null;

var bFlt=CP_FilterInit();
oCP.oWin.style.visibility='hidden';
oCP.WinVis=false; clearTimeout(oCP.tmHide); oCP.oLink=null;
if (bFlt) oCP.Flt.Play();
else CP_OFC();
}


function CP_OFC(){
if (oCP.WinVis) return;

if (oCP.Flt && oCP.Flt.status==2) return;
oCP.oWin.style.left='0px'; oCP.oWin.style.top='0px';
}


function CP_AddWinEl(Tag, Text, HRef){
var o=oCP.D.createElement(Tag);

if (HRef){
	o.href=HRef; o.target='_blank';
	}
CP_AddText(Text, o, null); oCP.oWin.appendChild(o); return o;
}


function CP_AddText(Txt, Par, o){

if (Txt.length==0) return 0;
Par.insertBefore(oCP.D.createTextNode(Txt), o);
return 1;
}


function CP_HiLiteKeywords(){
var St, oWin;

oWin=oCP.D.createElement('DIV'); oWin.id='CP-Win';
oWin.onmouseover=CP_TextOMOver;	oWin.onmouseout=CP_OMOut;

St=oWin.style;
St.position='absolute';	St.left='0px'; St.top='0px';
St.visibility='hidden'; //St.padding='8px 10px'; //St.width='300px'; 

oCP.B.appendChild(oWin); 

if (oCP.IE && (!oCP.IE5 || oCP.IE55)){
	St.filter='progid:DXImageTransform.Microsoft.Fade(duration=0.3, overlap=0.1) progid:DXImageTransform.Microsoft.dropshadow(OffX=4, OffY=4, color="#808080", positive="true")';
	oCP.Flt=oWin.filters[0]; oWin.onfilterchange=CP_OFC;
	}

oCP.WinVis=false; oCP.tmHide=null; 
oCP.LastSrc=null; oCP.tmSearch=null; oCP.oWin=oWin;
oCP.SkipTags=',A,SCRIPT,FORM,INPUT,SELECT,EMBED,OBJECT,TEXTAREA,FRAME,IFRAME,';

oCP.oldmousedown=oCP.D.onmousedown; oCP.oldmouseup=oCP.D.onmouseup; 
oCP.D.onmousedown=CP_OMD; oCP.D.onmouseup=CP_OMD; oCP.bMD=false;
}


function CP_Init(){
var Prop;

//Copy from UA
for(Prop in oCPUA) this[Prop]=oCPUA[Prop];
delete oCPUA;

if (typeof(CP_Keywords)=='undefined') return;
this.B=this.D.getElementsByTagName('BODY')[0];
	
if (0){ //this.Op || this.IE5 || this.Gec
	document.write('<IFrame Name=CPFr Id=CPFr '+
		'Style="height:1px;visibility:hidden;"></IFrame>');
	}
setTimeout('CP_HiLiteKeywords(); CP_ScanNode(oCP.B);', 1);
}

function CP_InitUA(){
var Nav=window.navigator.userAgent;

//Init
this.Gec=(Nav.indexOf('Gecko')!=-1);
this.Op=(Nav.indexOf('Opera')!=-1); this.IE=(!this.Gec && !this.Op);
this.Ns=(this.Gec && Nav.indexOf('Netscape')!=-1);
this.IE5=(this.IE && Nav.indexOf('MSIE 5')!=-1);
this.IE55=(this.IE5 && Nav.indexOf('MSIE 5.5')!=-1);
this.Saf=(this.Gec && Nav.indexOf('Safari/')!=-1);
this.bWin=(window.location.host.indexOf('.localhost')!=-1);
this.D=document;
}

function CP_LoadTerms(sid){
var Charset;

Charset=(oCPUA.IE ? oCPUA.D.charset : oCPUA.D.characterSet).toLowerCase();
oCPUA.utf=(Charset.indexOf('utf')!=-1);
oCPUA.D.write('<Script Src="/termlist.php?sid='+sid+'&utf='+(oCPUA.utf ? '1' : '0')+'"></Script>');
}

function Dev(s){if (bDebug) alert(s);}

//-----
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-----



oCPUA=new CP_InitUA();
bDebug=(window.location.search.indexOf('test')!=-1);
