var mouse_x = 0;
var mouse_y = 0;
var distance_to_right_edge = 0;
var distance_to_bottom = 0;
var contextmenu = null;
var contextmenu_sel = null;

addLoadEvent(onstart);

function onstart() {
  contextmenu = document.getElementById('thecontextmenu');
  addGenericEvent(document,'mousemove',mousemove);
  addGenericEvent(document,'click',hide_contextmenu);
}

function cm_oc(element, subid, target) { // context menu item on click

 var cmzip = document.getElementById("cm_zip");
 var cmroom = document.getElementById("cm_room");

 if(cmzip) cmzip=cmzip.value;
 if(cmroom) cmroom=cmroom.value;

 if(!contextmenu_sel)
    return true;

 if(subid)
 {
  cmlink = document.getElementById(subid);

  if(cmlink)
  {
    if(cmlink.className == "cmdisabled") return false;

    if(!target) return true;

    var extra = "zip="+cmzip + "&room=" + cmroom + "&sel=" + contextmenu_sel;

    if(cmlink.cmaction)
    {
     if(element.cmaction == "select") { cmlink.href = "home.php?"+ extra; }
     else if(cmlink.cmaction == "url") { cmlink.href = target+ extra; }
     else if(cmlink.cmaction == "jumper") { cmlink.href = "home.php?"+ extra; }
    }
    else cmlink.href = target+ extra;
  }

  return true;
 }

 return false;
}

function cm_mo(element, color) {    // context menu item mouse over
  element.className = 'highlight';
}

function cm_mout(element) {    // context menu item mouse out
  element.className = '';
}

function getFirstIdItem(node)
{
  if(!node) return null;

  var tr = node.firstChild;

  while(tr)
  {
    if(tr.id)
        return tr;

    var ret = getFirstIdItem(tr);

    if(ret) return ret;

    tr = tr.nextSibling;
  }

  return null;
}

function cm_isVisible(node, cantgrab, hashref, itemclass, itemtype)
{
    if(node.id.indexOf("islink") != -1) // 'follow weblink'
    {
        // 'follow weblink' option only if not widget, jumper and has href
        if(itemclass == 'jumper' || itemclass == 'widget') return false;
        return hashref;
    }
    if(node.id.indexOf("isselect") != -1) // 'Select'
    {
        if(itemclass == 'jumper') return false;
        if(itemclass == 'widget') return true;
        return !hashref;
    }
    if(node.id.indexOf("isenter") != -1) // 'Enter'
    {
        if(itemclass != 'jumper') return false;
        return hashref;
    }

    return true;
}

function cm_isDisabled(node, cantgrab, hashref)
{
  var owner = get("cm_owner", false);
  var canedit = get("cm_canedit", false);
  var loggedin = get("cm_loggedin", false);
  var zip = get("cm_zip");
  var room = get("cm_room");

  if(!node) return null;
  if(!node.id) return null;

  if(node.id.indexOf("islink") != -1)
  {
      return false;
  }

  if(node.id.indexOf("cangrab") != -1)
  {
      if(!loggedin) return true;
      if(!cantgrab) return false;

      if(!canedit) return true;

      return false;
  }
  if(node.id.indexOf("candel") != -1)
  {
      if(!canedit || !room)
          return true;
  }

  if(node.id.indexOf("canedit") != -1 && !canedit)
      return true;
  if(node.id.indexOf("owner") != -1 && !owner)
      return true;

  return false;
}

function setHref(cmitem, sel)
{
    if(!cmitem) return false;
    if(!sel) return false;

    var parentid = "wlink_" + sel.replace(".wcn", "");

    var parent = document.getElementById(parentid);

    if(!parent) return false;

    cmitem.href = parent.href;

    return true;
}

function show_contextmenu(event, id, sel, cantgrab, hashref, itemclass, itemtype) 
{
  contextmenu = document.getElementById(id);
  contextmenu_sel = sel;

  if(contextmenu == null) return false;

  var tr = contextmenu.firstChild;

  while(tr)
  {
    var cmitem = getFirstIdItem(tr);

    if(cmitem)
    {
        if(cm_isVisible(cmitem, cantgrab, hashref, itemclass, itemtype))
        {
            if(cm_isDisabled(cmitem, cantgrab, hashref))
            {
                cmitem.className = 'cmdisabled';
            }
            else cmitem.className = 'cmtext';

            setHref(cmitem, sel);

            tr.style.display='block';
        }
        else tr.style.display='none';
    }

    tr = tr.nextSibling;
  }

  get_page_boundaries();
  contextmenu.style.left = mouse_x+"px";
  contextmenu.style.top  = mouse_y+"px";
  contextmenu.style.visibility = "visible";

  // adjust menu if near window edge
  if (distance_to_right_edge < contextmenu.offsetWidth)
    contextmenu.style.left = 2+mouse_x - contextmenu.offsetWidth+"px";  // The 2+ is not some dumb kludge - 
  if (distance_to_bottom < contextmenu.offsetHeight)                    // it places the menu just under the pointer,
    contextmenu.style.top = 2+mouse_y - contextmenu.offsetHeight+"px";  // instead of just outside
  
  try {
    window.getSelection().collapseToStart();  // try to compensate for tendency to treat right-clicking as text selection
  } catch (e) {} // do nothing
  
  // prevent the event from bubbling up and causing the regular browser context menu to appear.
  event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation(); 
  if (event.preventDefault) event.preventDefault();
  
  return false;
}

function hide_contextmenu() {
  if(contextmenu)
     contextmenu.style.visibility = "hidden";

  return true;
}


function addLoadEvent(func) {
  if (window.addEventListener)
    window.addEventListener("load",func,false);
  else if (document.addEventListener)
    document.addEventListener("load",func,false);
  else if (window.attachEvent)
    window.attachEvent("onload",func);
  else if (document.attachEvent)
    document.attachEvent("onload",func);
}

function addGenericEvent(source, trigger, func) {
  if (source.addEventListener)
    source.addEventListener(trigger,func,false);
  else if (source.attachEvent)
    source.attachEvent("on"+trigger,func);
}

function window_x() {
  if (window.screenX)
    return window.screenX
  else if (window.screenLeft)
    return window.screenLeft;
}

function window_y() {
  if (window.screenY)
    return window.screenY
  else if (window.screenTop)
    return window.screenTop;
}

function mousemove(e) { 
  if (e && e.clientX && typeof(window.scrollY) == 'number') { // Moz
    mouse_x = e.clientX + window.scrollX;
    mouse_y = e.clientY + window.scrollY;
    event_target = e.target;
  }
  else if (window.event) { // IE
    if (document.documentElement)   // Explorer 6 Strict
    {
      mouse_x = window.event.clientX + document.documentElement.scrollLeft - 4;
      mouse_y = window.event.clientY + document.documentElement.scrollTop - 4;
    }
    else if (document.body) // all other Explorers
    {
      mouse_x=window.event.clientX+document.body.scrollLeft-4;
      mouse_y=window.event.clientY+document.body.scrollTop-4;
    }
 
    mouse_window_x = window.event.clientX;
    mouse_window_y = window.event.clientY;
  }
}

function get_page_boundaries()
{
  if (window.innerWidth) {
    distance_to_right_edge = window.innerWidth-(mouse_x - window.scrollX)
    distance_to_bottom = window.innerHeight-(mouse_y - window.scrollY);
  } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    distance_to_right_edge = document.documentElement.clientWidth-mouse_x;
    distance_to_bottom = document.documentElement.clientHeight-mouse_y;
  }else if (document.body.clientWidth) {
    distance_to_right_edge = document.body.clientWidth-mouse_x;
    distance_to_bottom = document.body.clientHeight-mouse_y;
  }
}
