var infowin = null;
var graphpanel = null;
var timer = null;
var curtime = new Date();

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
	  while (obj.offsetParent) {
	      curleft += obj.offsetLeft;
	      obj = obj.offsetParent;
	  }
    }
    else if (obj.x)
	  curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
	  while (obj.offsetParent) {
	      curtop += obj.offsetTop;
	      obj = obj.offsetParent;
	  }
    }
    else if (obj.y)
	  curtop += obj.y;
    return curtop;
}

function togglegraphs() {
    if(graphpanel == null) { 
	  graphpanel = document.getElementById('usagegraphs');
    }
    if(infowin == null) {
	  infowin = document.getElementById('infowin');
    }
    if(graphpanel.style.display == "none") {
	  graphpanel.style.display = "block";
	  infowin.style.width = "45ex";
	  infowin.style.minWidth = "250px";
    } else {
	  graphpanel.style.display = "none";
	  infowin.style.width = "auto";
	  infowin.style.minWidth = "";
    }
}

function show(name, maxmem, text, relto) {
    canceltimer();
    if(infowin == null) {
	  infowin = document.getElementById('infowin');
	  infowin.style.width = "auto";
	  infowin.style.height = "auto";
	  infowin.style.position = "absolute";
	  infowin.style.background = "lightyellow";
	  infowin.style.border = "1px solid black";
	  infowin.style.margin = "5px";
	  infowin.style.fontFamily = "sans-serif";
	  infowin.style.fontSize = "75%";
    }
    table = document.createElement('p');
    table.style.margin = "5px";
    lines = text.split(/	/);
    for(i = 0; i < lines.length; ++i) {
	  table.appendChild(document.createTextNode(lines[i]));
	  if(i < lines.length - 1) {
	      table.appendChild(document.createElement('br'));
	  }
    }
    document[ 'cpuhist' ].src = '/maestro/cgi-bin/rosetta-graph.cgi?' + name + '&cpu&' + curtime.getTime();
    document[ 'memhist' ].src = '/maestro/cgi-bin/rosetta-graph.cgi?' + name + '&mem&' + maxmem + '&' + curtime.getTime();
    if(infowin.hasChildNodes()) {
	  infowin.replaceChild(table, infowin.firstChild);
    } else {
	  infowin.appendChild(table);
    }
    infowin.style.left = findPosX(relto);
    infowin.style.top = findPosY(relto) + 30;
    infowin.style.display = "inline";
}

function hide() {
    if(infowin != null) { timer = setTimeout('close()', 250); }
}

function close() {
    if(infowin != null) { 
	  infowin.style.display = "none";
	  if(graphpanel != null) {
	      graphpanel.style.display = "none";
	  }
	  infowin.style.minWidth = "";
	  infowin.style.width = "auto";
    }
}

function canceltimer() {
    if(timer != null) { clearTimeout(timer); timer = null; }
}
