var rc__mouse_x = 0;
var rc__mouse_y = 0;
var rc__distance_to_right_edge = 0;
var rc__distance_to_bottom = 0;
var rc__contextmenu = null;
var current_day = null;

addLoadEvent(onstart);

function onstart() {
  rc__contextmenu = document.getElementById('thecontextmenu');
  addGenericEvent(document,'mousemove',mousemove);
  addGenericEvent(document,'click',hide_contextmenu);
}

function context_menuitem_highlight(element, color) {
  element.className = 'highlight';
}

function context_menuitem_unhighlight(element) {
  element.className = '';
}

function show_contextmenu(event) {
  get_page_boundaries();
  rc__contextmenu.style.left = rc__mouse_x+"px";
  rc__contextmenu.style.top = rc__mouse_y+"px";
  rc__contextmenu.style.visibility = "visible";
  // adjust menu if near window edge
  if (rc__distance_to_right_edge < rc__contextmenu.offsetWidth) {
    rc__contextmenu.style.left = 2+rc__mouse_x - rc__contextmenu.offsetWidth+"px";  // The 2+ is not some dumb kludge -
  }
  if (rc__distance_to_bottom < rc__contextmenu.offsetHeight) {                   // it places the menu just under the pointer,
    rc__contextmenu.style.top = 2+rc__mouse_y - rc__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() {
  rc__contextmenu.style.visibility = "hidden";
}

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
    rc__mouse_x = e.clientX + window.scrollX;
    rc__mouse_y = e.clientY + window.scrollY;
    event_target = e.target;
  }
  else if (window.event) { // IE
    if (document.documentElement)   // Explorer 6 Strict
    {
      rc__mouse_x = window.event.clientX + document.documentElement.scrollLeft - 4;
      rc__mouse_y = window.event.clientY + document.documentElement.scrollTop - 4;
    }
    else if (document.body) // all other Explorers
    {
      rc__mouse_x=window.event.clientX+document.body.scrollLeft-4;
      rc__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) {
    rc__distance_to_right_edge = window.innerWidth-(rc__mouse_x - window.scrollX)
    rc__distance_to_bottom = window.innerHeight-(rc__mouse_y - window.scrollY);
    
    //alert(window.innerHeight+' '+rc__mouse_y);
  } else if (document.body.clientWidth) {
    rc__distance_to_right_edge = document.body.clientWidth-rc__mouse_x;
    rc__distance_to_bottom = document.body.clientHeight-rc__mouse_y;
  }
}




