//===================================================================
//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.getElementById)
  {
    if( typeof( window.pageYOffset ) == 'number' ) 
    {
      scrOfY = window.pageYOffset;
      scrOfX = window.pageXOffset;
    } 
    else 
    {
      scrOfY = document.documentElement.scrollTop;
      scrOfX = document.documentElement.scrollLeft;
    }
  x = event.clientX + scrOfX;
//  alert("taille:"+event.clientX+" et scroll:"+ scrOfX);
  y = event.clientY + scrOfY;
  }
  else if (document.all)
  {
    x = event.clientX + document.body.scrollLeft;
    y = event.clientY + document.body.scrollTop;
  }
  else if (document.layers)
  {
    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 +"px";
  context.style.top = y +"px";
  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(ob)
{
  target = document.getElementById(ob);
  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=\"contextline\" id=\"contextLine" + contextRows + "\"></div>";
  }
  else 
  {
    code = "<div class=\"contextRow\" align=\"left\" onmouseover=\"SwitchMenu('contextLine"+contextRows+"');\" onmouseout=\"SwitchMenu('contextLine"+contextRows+"');\" 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 = '';
}
