var appState = new applicationState()
var allmenu = new Array()
var orgX,orgY,menuId;
function applicationState() {
  this.contextMenu = null
}

function loadContextMenu(path,file) {

}

function setContextMenu(x,y,id) {
    var contextMenu
    orgX = x;
    orgY = y;
    menuId = id;
    contextMenu = document.getElementById(id);
	if(contextMenu.style!=null)
	{
    contextMenu.style.left = x
    contextMenu.style.top = y
    contextMenu.style.display = "block"
    appState.contextMenu = contextMenu
    showOverlay(contextMenu)
    ReleaseMouse()
	}
}

function loadContextMenuSub(obj) {
  var contextMenu
  var parentMenu
  parentMenu = returnContainer(obj)
  contextMenu = document.all[obj.id + "Sub"]
  contextMenu.style.display = "block"
  var correctX=0,correctY=0;
  if(parentMenu.id==menuId)
  {
    correctX = orgX;
    correctY = orgY;
  }
  contextMenu.style.top = obj.offsetTop + parentMenu.style.pixelTop - correctY;
  contextMenu.style.left = obj.offsetWidth + parentMenu.style.pixelLeft - correctX;
  parentMenu.subMenu = contextMenu
  showOverlay(contextMenu)
}

function showOverlay(group)
{
  var overid = group.id + "_over";
  if (!document.all[overid])
  {
    document.body.insertAdjacentHTML("beforeEnd","<iframe id='" +overid+ "' src='javascript:void 0;' style='position:absolute;left:0px;top:0x;z-index:990;display:none' scrolling='no' frameborder='0'></iframe>");
    allmenu[allmenu.length] = group;
  }
  if (document.all[overid])
  {
    var overs = document.all[overid].style;
    overs.top = aspnm_winY(group);
    overs.left = aspnm_winX(group);
    overs.width = group.offsetWidth;
    overs.height = group.offsetHeight;
    overs.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
    overs.display = 'block';
	//window.defaultStatus = 'show:'+overid+' left='+overs.left+' top='+overs.top+' width='+overs.width+' height='+overs.height;
  }
}

// Hides a windowed control overlay for the given group
function hideOverlay(group)
{
  var overid = group.id + "_over";
  if (document.all[overid])
  {
	//window.defaultStatus = 'hide:'+overid;
    document.all[overid].style.display = 'none';
  }
}

// Calculates the absolute page x coordinate of given element for Windows clients
function aspnm_winX(o)
{
  var x = 0;
  while (o != document.body)
  {
    x += o.offsetLeft;
    o = o.offsetParent;
  }
  return x;
}

// Calculates the absolute page y coordinate of given element for Windows clients
function aspnm_winY(o)
{
  var y = 0;
  while (o != document.body)
  {
    y += o.offsetTop;
    o = o.offsetParent;
  }
  return y;
}

var InMenu=false;
var TimeOutID=0;

function CaptureMouse()
{
	if(InMenu)
	{
		//停止定时器
		window.clearTimeout(TimeOutID);
		TimeOutID = 0;
	}
}

function ReleaseMouse()
{
	if(!InMenu&&TimeOutID==0)
	{
		//启动定时器
		TimeOutID = window.setTimeout('clean();',1000);
	}
}

function contextHighlightRow(obj) {
  var parentMenu
  var subMenu
  var i
  
  parentMenu = returnContainer(obj)
  if(obj.selected == "false") {
	obj.setCapture(false);
	InMenu = true;
    for(i=0; i < obj.childNodes.length; i++) {

      obj.childNodes(i).style.backgroundColor="#3F8AB1"
      if(obj.childNodes(i).cellIndex == 0) {
        obj.childNodes(i).style.borderLeft = "1px solid "
      }
      else if (obj.childNodes(i).cellIndex == obj.cells.length-1) {
        obj.childNodes(i).style.borderRight = "1px solid "
      }
    }
    if(parentMenu.subMenu != null && parentMenu != parentMenu.subMenu) {
      subMenu = parentMenu.subMenu
      while(subMenu != null) {
        subMenu.style.display = "none"
		hideOverlay(subMenu)
        subMenu = subMenu.subMenu
      }
    }
    obj.selected = "true"
  }
  else {
	obj.releaseCapture();
	InMenu = false;
    for(i=0; i < obj.childNodes.length; i++) {
      if(i == 0) {
        obj.childNodes(0).style.backgroundColor="#54A3CC"
    
      }
      else {
        obj.childNodes(i).style.backgroundColor=obj.background      
      }
      
      if(obj.childNodes(i).cellIndex == 0) {
        obj.childNodes(i).style.borderLeft = "1px solid " + obj.titlebar
      }
      else if (obj.childNodes(i).cellIndex == obj.cells.length-1) {
        obj.childNodes(i).style.borderRight = "1px solid " + obj.background
      }
    }
    obj.selected = "false"
  }
}

function clean() {
  window.clearTimeout(TimeOutID);
  TimeOutID = 0;
  for(var i=allmenu.length-1;i>=0;i--)
  {
	var amenu = allmenu[i];
	amenu.style.display = "none";
	document.all[amenu.id+"_over"].style.display = "none";
  }
  appState.contextMenu.style.display = "none";
}

function returnContainer(container) {
  while(container.tagName != "DIV") {
    container = container.parentNode  
  }
  return container
}	 