
/* Relative display toggling [06/29/05]
 * Written by Josh Mast (josh@hivehaus.com)
 *
 * butchered up, by Richard Harman ;)
 */

function toggle_expand(obj) {
  // hack for undef display on init
  if (obj.style.display == "") { obj.style.display = "none"; return }
  if (obj.style.display != 'inline') {
          obj.style.display = 'inline';
          return;
  } else {
          obj.style.display = 'none';
          return;
  } 
}

function getElementsByClass(search_class)
{
  var Elements = new Array();
  var els = document.getElementsByTagName("*");
  for (i = 0, j = 0; i < els.length; i++)
  {
    if ( els[i].className == search_class)
    {
      Elements[j] = els[i];
      j++;
    }
  }
  return Elements;
}

function toggle_expand_by_class(search_class,source)
{
  var objs = getElementsByClass(search_class);

  // toggle underline/strikethrough
  if ( source.style.textDecoration == "underline" || source.style.textDecoration == "" )
  { source.style.textDecoration="line-through" }
  else 
  { source.style.textDecoration="underline" }

  // toggle 'em  
  for (i = 0; i < objs.length; i++)
  { toggle_expand(objs[i]); }
}

function find_sibling(obj,name)
{
  var parent_obj = obj.parentNode.parentNode;
  var target;
  for (var i = 0; i < parent_obj.childNodes.length; i++)
  {
    if (parent_obj.childNodes[i].className == name)
    {
      target = parent_obj.childNodes[i];
    }
  }
  return target;
}

// this creates the popup
function create_popup(obj,what,id)
{
  // create_popup(this,'string','popup_src_str_informational__statistics')

  var val = obj.innerHTML;
  // first off, check to see if the popup div we created exists already.
  var existing_obj = document.getElementById(id);
  if (existing_obj != null)
  {
    // it does, relocate the already existing DIV.
    var newpos = getElementPosition(obj);
    existing_obj.style.left=newpos.curleft + "px";
    existing_obj.style.top=newpos.curtop + "px";
    return;
  }

  // create the new object
  var child = document.createElement('DIV');

  // CSS class and id
  child.className = "popup_box";
  child.id = id;

  // position it
  var newpos = getElementPosition(obj);
  child.style.left=newpos.curleft + "px";
  child.style.top=newpos.curtop + "px";


  // variable for the div's content
  var text;

  // HOLY SHIT.  AJAX. FO REEL.
  child.innerHTML = "please wait.. <span class='popup_button'"+" onClick='kill_obj(this.parentNode);'>[X]</span>";
  obj.parentNode.appendChild(child);

  // set zaxis
  child.style.zIndex = 100;

  // we seem to have picked up our parent's onclick handler.
  child.onclick=null;
  child.onmousedown=null;
  xmlhttp.open("GET", "popup.mhtml?type="+what+"&value="+val,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {

      // set it
      var xsl;
      // var XML = xmlhttp.responseXML;
      var TEXT = xmlhttp.responseText;
      // var val = XML.documentElement.getElementsByTagName("note").item(0).textContent;
      var val = TEXT;
      child.innerHTML = val;
    }
  }
  xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')
  xmlhttp.send(null)
}

function kill_obj(what)
{
  what.parentNode.removeChild(what);
}

function getElementPosition(obj)
{
  var curleft = findPosX(obj); 
  var curtop = findPosY(obj); 
  return {curleft:curleft, curtop:curtop};
}

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;
}
