/**
 * PEAR Manual - Windows Help Edition
 * Set of javascript functions for the context menu
 *
 * Copyright (c) 2004 Dieter Raber <d.raber@dwhsoft.de>
 * 
 * This source code is licensed to the PEAR Group <pear.php.net>
 * for the use in the Windows Help Edition of their manuals.
 *
 * If you wish to use the code in a different project,
 * please contact the author.
 * 
 */

 
// global variables
var colOn   = '#C8DEBD';
var colOff  = '#F0F0F0';
var selText = '';

// support for both document.all and DOM, you never know what M$ comes up with...
function get_element(sender)
{
  my_element = !document.getElementById
             ? document.all.sender
             : document.getElementById(sender);
  return my_element;
}

// link highlighting
function hover(sender, col)
{
  sender.style.backgroundColor = col;
}

// display menu
function display_context_menu()
{
  hide();
  var menuWidth            = 210;
  var menuHeight           = 280;
  var windowWidthVisible   = document.body.offsetWidth -20;
  var windowHeightVisible  = document.body.offsetHeight -20;
  var eventLeft            = event.clientX + document.body.scrollLeft;
  var eventTop             = event.clientY + document.body.scrollTop;
  var theMenu              = get_element('context_menu');
  
  //zwischen Fensterkante und Fensterrand nicht genugend Platz fuer Menu
  if ((event.clientX < menuWidth) && (eventLeft + menuWidth > windowWidthVisible))
  {
  	 //Menu wird an rechter Kante ausgerichtet, da Event eher in linker Haelfte des Fensters
  	 if (event.clientX < windowWidthVisible - event.clientX)
  	 {
  	 	theMenu.style.left = windowWidthVisible + document.body.scrollLeft - menuWidth;
  	 }
  	 //linke Kante
  	 else
  	 { 
      theMenu.style.left = document.body.scrollLeft;
    }  	 
  }
  //Menu geht nach links auf 
  else if (event.clientX + menuWidth > windowWidthVisible)
  {
    theMenu.style.left = eventLeft - menuWidth; 
  }
  //Menu geht nach rechts auf
  else 
  {
    theMenu.style.left = eventLeft;
  }  
   
  if ((event.clientY < menuHeight) && (eventTop + menuHeight > windowHeightVisible))
  {
  	 if (event.clientY < windowHeightVisible - event.clientY)
  	 {
  	 	theMenu.style.top = windowHeightVisible + document.body.scrollTop - menuHeight;
  	 }
  	 else
  	 { 
      theMenu.style.top = document.body.scrollTop;
    } 
    //theMenu.style.top = eventTop - menuHeight / 2; 
  }
  else if (event.clientY + menuHeight > windowHeightVisible)
  {
    theMenu.style.top = eventTop - menuHeight;
  }
  else 
  {
    theMenu.style.top = eventTop;
  }
  
  theMenu.style.left       = theMenu.style.left;
  theMenu.style.top        = theMenu.style.top; 
  theMenu.style.visibility = 'visible';
  return false;
}
var ieVers = parseInt(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE ') + 5, 3));
if (ieVers >= 5.5)
{
  document.oncontextmenu = display_context_menu;
}

// hide menu
function hide()
{
  get_element('context_menu').style.visibility = 'hidden';
}

function hide_delay()
{
  if (event.button !=2)
  {
    setTimeout('hide()', 200);
  }
}

var ieVers = parseInt(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE ') + 5, 3));
if (ieVers >= 5.5)
{
  document.oncontextmenu = display_context_menu;
  document.onclick       = hide_delay;
  window.onblur          = hide_delay;
}
// internet searches
function find(site)
{
  switch(site)
  {
    case 'google':
                  url = selText 
                      ? 'http://www.google.com/search?q=' + selText 
                      : 'http://www.google.com';
      break;
    case 'pear':
                  url = selText 
                      ? 'http://pear.php.net/package-search.php?pkg_name=' 
                      + selText + '&bool=AND&submit=Search' 
                      : 'http://pear.php.net/';
      break;
    case 'dev':
                  url = selText 
                      ? 'http://marc.theaimsgroup.com/?l=pear-dev&w=2&r=1&q=b&s=' 
                      + selText 
                      : 'http://marc.theaimsgroup.com/?l=pear-dev';
      break;
    case 'general':
                  url = selText 
                      ? 'http://marc.theaimsgroup.com/?l=pear-general&w=2&r=1&q=b&s=' 
                      + selText 
                      : 'http://marc.theaimsgroup.com/?l=pear-general';
      break;
    case 'cvs':
      url = selText 
                      ? 'http://marc.theaimsgroup.com/?l=pear-cvs&w=2&r=1&q=b&s=' 
                      + selText 
                      : 'http://marc.theaimsgroup.com/?l=pear-cvs';
      break;
    case 'alltheweb':
                  url = selText 
                      ? 'http://alltheweb.com/search?avkw=fogg&cat=web&cs=utf-8&q=' 
                      + selText + '&_sb_lang=pref' 
                      : 'http://alltheweb.com';
      break;
    case 'php':
                  url = selText 
                      ? 'http://www.php.net/' + selText 
                      : 'http://www.php.net';
      break;
      
  }
  var ext_window = window.open(url,'pear_chm_help');
  ext_window.focus()
}

function copy_sel()
{
  toClpbrd = tmpText.createTextRange();
  toClpbrd.execCommand("Copy");
}

function capture_text()
{ 
  if(document.selection.createRange().text)
  {
    if (event.button == 1)
    {
      selText       = document.selection.createRange().text;
      tmpText.value = selText;
    }
  }
  return selText;
}

document.onmouseup = capture_text;
