//======================================
//Functions used within the context menu
//======================================
var action = Array(); //array where key are the id of the lines, and where values are the functions to call
var context = null; //pointer to the current context menu object
function ContextMenuIE()
{
  return ContextMenu(event);
}
var oldOnClick = null; //save the original onclick event
function ContextMenu(event)
{
  if (context != null) //a context menu is already open, perhaps in an other frame ?
    {
      document.onclick = oldOnClick;
      oldOnClick = null;
      context.style.display = "none";
      context = null;
    }
  else
    {
      if (window.parent.contextFrame != '')
	{
	  window.parent.frames[ window.parent.contextFrame ].document.onclick = oldOnClick;
	  window.parent.frames[ window.parent.contextFrame ].oldOnClick = null;
	  window.parent.frames[ window.parent.contextFrame ].context.style.display = 'none';
	  window.parent.frames[ window.parent.contextFrame ].context = null;
	}
    }

  //register the opened context menu in the parent frame
  window.parent.contextFrame = window.frames.name;

  context = document.getElementById("context");
  if (event.type != "contextmenu" && event.button != 2) //other event that contextmenu occurs
    {
      document.onclick = oldOnClick;
      oldOnClick = null;
      //unregister the contextmenu
      window.parent.contextFrame = '';
      return false;
    }
  if (event.ctrlKey)
    {
      document.onclick = oldOnClick;
      oldOnClick = null;
      context.style.display="none";
      context = null;
      //unregister the contextmenu
      window.parent.contextFrame = '';
      return true;
    }
  context.style.display="block";
  var x = 0;
  var y = 0;
  if (document.all)
    {
      x = event.clientX + document.body.scrollLeft;
      y = event.clientY + document.body.scrollTop;
    }
  else
    {
      x = event.layerX;
      y = event.layerY;
    }

  //test if the menu does not goes too much on the right or the bottom
  if (y + context.clientHeight > document.body.clientHeight + document.body.scrollTop)
    y = y - context.offsetHeight;
  if (x + context.clientWidth > document.body.clientWidth + document.body.scrollLeft)
    x = x - context.offsetWidth;

  //and test that now it does not goes too much on the top
  if (y < document.body.scrollTop) y = document.body.scrollTop;
  if (x < document.body.scrollLeft) x = document.body.scrollLeft;
  context.style.left = x;
  context.style.top = y;
  context.style.zIndex = 100;
  document.onclick = document.oncontextmenu;
  return false;
}
function GetLine(target)
{
  while (target.className!='contextRow'
	 && target.className!='contextRowOver'
	 && target.className != 'context')
    {
      target = target.parentElement;
    }
  return target;
}
function SwitchMenu(event)
{
  if (!document.all)
    event.srcElement = event.target;
  
  if (event.srcElement.className + "" == "undefined")
    {
      event.cancelBubble = true;
      return;
    }
  target = GetLine(event.srcElement);

  if (target.className=="contextRow")
    target.className="contextRowOver";
  else if (target.className=="contextRowOver")
    target.className="contextRow";
}
function ContextClick(event)
{
  context.style.display="none";
  for (var id in action)
    {
      if (target.id == id)
	eval(action[id]);
    }
  context = null;
  window.parent.contextFrame = '';  
}

//=======================================
//constructions functions of context menu
//=======================================
var contextRows = 0;
function InitContext()
{
  var code;
  code = "<DIV id=context class=context style=\"display:none;position:absolute;\" onClick=\"ContextClick(event);\">";
  document.write(code);
}
function AddContextLine(html, lineAction, separator)
{
  var code;
  if (separator)
    code = "<DIV class=contextRow style=\"padding-left=0pt;padding-right:0pt;\" id=contextLine" + contextRows + " onmouseover=\"SwitchMenu(event);\" onmouseout=\"SwitchMenu(event);\"><HR width=\"80%\"></DIV>";
  else code = "<DIV class=contextRow align=left  onmouseover=\"SwitchMenu(event);\" onmouseout=\"SwitchMenu(event);\" id=contextLine" + contextRows + ">" + html + "</DIV>";
  action["contextLine" + contextRows] = lineAction;
  contextRows++;
  document.write(code);
}
function AddSeparator()
{
  AddContextLine("","", true);
}
function FinalizeContext()
{
  document.write("</DIV>");
  if (document.all)
    document.oncontextmenu = ContextMenuIE;
  else document.oncontextmenu = ContextMenu;

  window.parent.contextFrame = '';
}
