<?php
/**
 * @package Rjmprogramming_Multi_Lookup
 * @version 1.6
 */
/*
Plugin Name: Rjmprogramming Multi Lookup
Description: Float various lookup button links to Wikipedia, Google, Free Dictionary for highlighted text of a blog webpage.
Author: Robert James Metcalfe
Version: 1.6
Author URI: http://www.rjmprogramming.com.au/
*/


// We need some Javascript to respond to highlights
function rjmprogramming_multi_js() {

	echo "<div id='phfloater'></div>
	
	<script type='text/javascript'>
var phight=' ';	
var cbackc='';
var cookcnt=0;
var cookblank=' ';
var zoharr=[];

function plfhlater() {
   var aswq=document.getElementsByTagName(\"a\");
   for (var iaswq=0; iaswq<aswq.length; iaswq++) {
     if (aswq[iaswq].innerHTML == 'Long hover help available.') {
       aswq[iaswq].innerHTML='Highlighting and long hover help.';
       aswq[iaswq].title+=' ... also note that Highlighting Webpage Contents allows for Wikipedia, Google Translate, Free Dictionary and Google Search functionalities to appear';
     }
   }
}

function cookiePutHVal(indv) {
    var expireDate = new Date();
    var opis='';
    expireDate.setMonth(expireDate.getMonth()+6);
    expireDate.setDate(expireDate.getDate()-1);
    if (indv == '') {
      cookcnt=0;
      cbackc='';
      document.cookie = 'linktoothers' + '=' + cbackc + ';expires=' + expireDate.toGMTString();
    } else {
    var pvalis=prompt('Enter absolute URL prefix#Title ... where two blank Title prefix replaces value blanks for - and one blank Title prefix replaces value blanks for +', '');
    if (pvalis == null) { pvalis=''; }
    if (pvalis.indexOf('//') != -1) {
          var otit='Other Link';
          if (pvalis.indexOf('#') != -1) {
            otit=pvalis.split('#')[eval(-1 + pvalis.split('#').length)].trim();
          }
          if (pvalis.split('#')[eval(-1 + pvalis.split('#').length)].indexOf('  ') == 0) {
            opis=phight.replace(/\ /g,' + \"'-'\" + ');
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" title=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" target=_blank onmouseover=\"this.href=this.title + hopis(String.fromCharCode(45));\" ontouchstart=\"this.href=this.title + hopis(String.fromCharCode(45));\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';
          } else if (pvalis.split('#')[eval(-1 + pvalis.split('#').length)].indexOf(' ') == 0) {
            opis=phight.replace(/\ /g,' + \"'+'\" + ');
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" title=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" target=_blank onmouseover=\"this.href=this.title + hopis(String.fromCharCode(43));\" ontouchstart=\"this.href=this.title + hopis(String.fromCharCode(43));\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';
          } else {
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" title=\"' + pvalis.replace('#' + pvalis.split('#')[eval(-1 + pvalis.split('#').length)],'').trim() + '\" target=_blank onmouseover=\"this.href=this.title + encodeURIComponent(phight);\" ontouchstart=\"this.href=this.title + encodeURIComponent(phight);\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';         
          }
     if (cbackc != '') {
      document.cookie = 'linktoothers' + '=' + cbackc + '|' + encodeURIComponent(pvalis) + ';expires=' + expireDate.toGMTString();
     } else {
      cbackc=encodeURIComponent(pvalis);
      document.cookie = 'linktoothers' + '=' + encodeURIComponent(pvalis) + ';expires=' + expireDate.toGMTString();
     }
     cookcnt++;
    }
    }
}

function cookieHVal(cName) {
  if (document.cookie != '') {
   var tCookie=document.cookie.split('; ');
   for (var j=0; j<tCookie.length; j++) {
    if (cName == tCookie[j].split('=')[0]) {
      return decodeURIComponent(tCookie[j].split('=')[1]);
    }
   }
  }
  return '';
}

function hopis(inop) {
  return hight.replace(/\ /g, inop);
}

function plookforhighlight() {
  var usualdelay=2000, oRange=null, oRect=null, s_top='0px', s_left='5px', opis='', fourhundred=300, isfx='0', otit='';
  if (phight == ' ') {
    setTimeout(plfhlater, 5000);
    phight='';
    cbackc=cookieHVal('linktoothers');
    if (cbackc != '') {
      zoharr=cbackc.split('|');
    }
  }
  setTimeout(plookforhighlight, usualdelay);
  var xsel=window.getSelection().toString(); 
  if (xsel.length == 0) {
    try {
    xsel=document.selection.createRange().htmlText;
    xsel=xsel.replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ');
    xsel=xsel.replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ');
    } catch(ertw) {
    xsel='';
    }
  } else {
    xsel=xsel.replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ').replace(String.fromCharCode(10),' ');
    xsel=xsel.replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ').replace(String.fromCharCode(13),' ');
    if (xsel != phight && xsel != '') {
     oRange = window.getSelection().getRangeAt(0); //get the text range
     oRect = oRange.getBoundingClientRect();
     s_top = ('' + oRect.top).replace('px','') + 'px';
     s_left = ('' + oRect.left).replace('px','') + 'px';
     if (s_top != '0px') { 
        fourhundred=eval('' + s_top.replace('px',''));
        s_top='0px';
        var dse='', dde='';
        if (document.scrollingElement) {
          if (document.scrollingElement.scrollTop) {
            dse=('' + document.scrollingElement.scrollTop);
          }
        }
        if (document.documentElement) {
          if (document.documentElement.scrollTop) {
            dde=('' + document.documentElement.scrollTop);
          }
        }
        if (dse != '') {
          fourhundred+=eval('' + dse.replace('px',''));
          isfx='0';
        } else if (dde != '') {
          fourhundred+=eval('' + dde.replace('px',''));
          isfx='0';
        } else if (('' + window.scrollY) != '') {
          fourhundred+=eval('' + ('' + window.scrollY).replace('px',''));
          isfx='0';
        } else if (('' + window.pageYOffset) != '') {
          fourhundred+=eval('' + ('' + window.pageYOffset).replace('px',''));
          isfx='0';
        } else {
          isfx=('' + document.body.scrollTop).replace('px','');
          if (isfx.replace('0','') == '') {
            isfx=('' + document.scrollingElement.scrollTop).replace('px','');
            if (isfx.replace('0','') == '') {
               isfx=('' + window.scrollY).replace('px','');
               if (isfx.replace('0','') == '') {
                  isfx=('' + window.pageYOffset).replace('px','');
               }
            }
          }
        }
        fourhundred+=120;
     }
    }
  }
  if (xsel != phight && xsel != '') {
    phight=xsel;
    if (s_top == '0px') { s_top = ('' + eval(fourhundred + eval('' + isfx))) + 'px'; }
    if (document.getElementById('phfloater')) {  
      if (document.getElementById('phfloater').innerHTML == '') {
        document.getElementById('phfloater').style.position='absolute';
        document.getElementById('phfloater').style.top=s_top;
        document.getElementById('phfloater').style.left=s_left;
        document.getElementById('phfloater').style.zIndex='85';
        document.getElementById('phfloater').style.backgroundColor='rgba(0,255,165,0.9)'; 
        document.getElementById('phfloater').style.border='1px solid purple'; 
        document.getElementById('phfloater').style.padding='2px 2px 2px 2px'; 
        document.getElementById('phfloater').style.display='block'; 
        document.getElementById('phfloater').innerHTML='<b>Wikipedia <a style=\"inline-block;\" href=\"//wikipedia.org/wiki/Main_Page\" title=\"//wikipedia.org/wiki/\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'_'\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'_'\" + ');\" id=b_phfloater_wikipedia>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Google Translate <a style=\"inline-block;\" href=\"//translate.google.com\" title=\"//translate.google.com/#view=home&op=translate&sl=auto&tl=en&text=\" target=_blank onmouseover=\"this.href=this.title + encodeURIComponent(phight);\" ontouchstart=\"this.href=this.title + encodeURIComponent(phight);\" id=b_phfloater_google_translate>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Free Dictionary <a style=\"inline-block;\" href=\"//www.thefreedictionary.com/\" title=\"//www.thefreedictionary.com/\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'+'\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'+'\" + ');\" id=b_phfloater_free_dictionary>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Google Search <a style=\"inline-block;\" href=\"//www.google.com/\" title=\"//www.google.com/search?q=\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'+'\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'+'\" + ');\" id=b_phfloater_google_search>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Tag Search <a style=\"inline-block;\" href=\"//www.rjmprogramming.com.au/ITblog/\" title=\"//www.rjmprogramming.com.au/ITblog/tag/\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ');\" id=b_phfloater_tag_search>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Category Search <a style=\"inline-block;\" href=\"//www.rjmprogramming.com.au/ITblog/\" title=\"//www.rjmprogramming.com.au/ITblog/category/\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ');\" id=b_phfloater_category_search>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Tag First Word Search <a style=\"inline-block;\" href=\"//www.rjmprogramming.com.au/ITblog/\" title=\"//www.rjmprogramming.com.au/ITblog/tag/\" target=_blank onmouseover=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ').replace(' + \"'-','/?wopenall='\" + ');\" ontouchstart=\"this.href=this.title + phight.replace(/\ /g,' + \"'-'\" + ').replace(' + \"'-','/?wopenall='\" + ');\" id=b_phfloater_tag_first_word_search>' + phight + '</a></b><br>';
        document.getElementById('phfloater').innerHTML+='<b>Posting Title Search <a style=\"inline-block;\" href=\"//www.rjmprogramming.com.au/itblog/match/mapper.php\" title=\"//www.rjmprogramming.com.au/itblog/match/mapper.php?pm=\" target=_blank onmouseover=\"this.href=this.title + encodeURIComponent(phight);\" ontouchstart=\"this.href=this.title + encodeURIComponent(phight);\" id=b_phfloater_posting_title_search>' + phight + '</a></b><br>';
        if (zoharr.length >= 1) {
         for (var ioharr=0; ioharr<zoharr.length; ioharr++) {
          otit='Other Link ' + eval(1 + ioharr);
          if (zoharr[ioharr].indexOf('#') != -1) {
            otit=zoharr[ioharr].split('#')[1].trim();
          }
          if (zoharr[ioharr].split('#')[1].indexOf('  ') == 0) {
            opis=phight.replace(/\ /g,' + \"'-'\" + ');
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + zoharr[ioharr].split('#')[0].trim() + '\" title=\"' + zoharr[ioharr].split('#')[0].trim() + '\" target=_blank onmouseover=\"this.href=this.title + hopis(String.fromCharCode(45));\" ontouchstart=\"this.href=this.title + hopis(String.fromCharCode(45));\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';
          } else if (zoharr[ioharr].split('#')[1].indexOf(' ') == 0) {
            opis=phight.replace(/\ /g,' + \"'+'\" + ');
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + zoharr[ioharr].split('#')[0].trim() + '\" title=\"' + zoharr[ioharr].split('#')[0].trim() + '\" target=_blank onmouseover=\"this.href=this.title + hopis(String.fromCharCode(43));\" ontouchstart=\"this.href=this.title + hopis(String.fromCharCode(45));\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';
          } else {
            document.getElementById('phfloater').innerHTML+='<b>' + otit + ' <a style=\"inline-block;\" href=\"' + zoharr[ioharr].split('#')[0].trim() + '\" title=\"' + zoharr[ioharr].split('#')[0].trim() + '\" target=_blank onmouseover=\"this.href=this.title + encodeURIComponent(phight);\" ontouchstart=\"this.href=this.title + encodeURIComponent(phight);\" id=b_phfloater_' + cookcnt + '>' + phight + '</a></b><br>';         
          }
          cookcnt++;
         }
        }
        document.getElementById('phfloater').innerHTML+='<b><input title=\"Add your own into HTTP Cookies\" type=button onclick=cookiePutHVal(cookblank); value=+></input>&nbsp;<input title=\"Remove all HTTP Cookie URLs\" type=button onclick=cookiePutHVal(cookblank.trim()); value=-></input></b><br>';
      } else {
        document.getElementById('b_phfloater_wikipedia').innerHTML=phight;
        document.getElementById('b_phfloater_google_translate').innerHTML=phight;
        document.getElementById('b_phfloater_free_dictionary').innerHTML=phight;
        document.getElementById('b_phfloater_google_search').innerHTML=phight;
        document.getElementById('b_phfloater_tag_search').innerHTML=phight;
        document.getElementById('b_phfloater_category_search').innerHTML=phight;
        document.getElementById('b_phfloater_tag_first_word_search').innerHTML=phight;
        document.getElementById('b_phfloater_posting_title_search').innerHTML=phight;
        if (cookcnt > 0) {
          for (var icook=0; icook<cookcnt; icook++) {
            document.getElementById('b_phfloater_' + icook).innerHTML=phight;
          }
        }
        document.getElementById('phfloater').style.top=s_top;
        document.getElementById('phfloater').style.left=s_left;
        document.getElementById('phfloater').style.display='block'; 
      }  
    } 
  } else if (xsel == '') {
    if (document.getElementById('phfloater')) {  
      document.getElementById('phfloater').style.display='none';   
    }
    phight='';
  }
}

plookforhighlight();

	</script>
	";
}

add_action( 'wp_footer', 'rjmprogramming_multi_js' );

?>
