/* DOM Stuff (c) 2007 by David Gauer

   Contains goodies
*/



var curX, curY, keycode;

function centerOnCursor(obj){
  obj.style.left = ""+(curX - Math.round(obj.offsetWidth/2))+"px";
  obj.style.top  = ""+(curY - Math.round(obj.offsetHeight/2))+"px";
  if(parseInt(obj.style.left)<5){obj.style.left = "5px";}
  if(parseInt(obj.style.top)<5){ obj.style.top  = "5px";}
}

function processEvent(e){
  if(e==null){ e = window.event; }

  // update cursor
	if (e.pageX || e.pageY) 	{
		curX = e.pageX;
		curY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		curX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		curY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}

  // update key
	if (e.keyCode) keycode = e.keyCode;
	else if (e.which) keycode = e.which;

  // prevent other items from getting this action
  e.cancelBubble = true;
  if(e.stopPropagation) e.stopPropagation();
}

function callOnEnter(callThis){
  // usage: ...onkeypress = callOnEnter(makeClosure(funcToCall, params...));
  return function(e){
    processEvent(e);
    if(keycode!=13) return;
    return callThis(e);
  }
}

function makeClosure(){
  var  a=arguments;
  return function(e){
    processEvent(e)
    a[0](a[1], a[2], a[3], a[4]);
    return false;
  };
}

function clearTag(tag){
  while(tag.hasChildNodes()){
  	tag.removeChild(tag.lastChild);
  }
}

function setClassName(obj, c){
  obj.className = c;
}

function hide(id){ hideObject(get(id)); }
function show(id){ showObject(get(id)); }

function hideObject(o){
  o.style.display = "none";
}

function showObject(o){
  o.style.display = '';
  // the below is what I originally had, and is probably more correct
  // sadly, IE SUCKS ASS AS USUAL and does not understand the table
  // display types the above fix is a hack
	/* switch(o.nodeName.toLowerCase()){
		case "span":  o.style.display = "inline";     break;
		case "tr":    o.style.display = "table-row";  break;
		case "td":    o.style.display = "table-cell"; break;
		case "table": o.style.display = "table";      break;
		default:      o.style.display = "block";      break;
  }
  */
}

function toggle(id){
  if(get(id).style.display == "none"){  show(id); }
  else{ hide(id); }
}

function get(id){ return document.getElementById(id); }


function newFromTemplate(templateName){
  var n = get(templateName).cloneNode(true);
  n.id = null;
  showObject(n);
  return n;
}

function makeText(string){
  return document.createTextNode(string);
}

function putTextInTag(id, string){
  var t = get(id);
  clearTag(t);
  t.appendChild(makeText(string));
}

function appendTextToTag(id, string){
  var t = get(id);
  t.appendChild(makeText(string));
}

function getTag(node, tag, num){
  return node.getElementsByTagName(tag)[num];
}

