function at_display(x)
{
  var win = window.open();
  for (var i in x) win.document.write(i+' = '+x[i]+'<br>');
}

// ----- Show Aux -----

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );

  var top  = (c["at_position"] == "y") ? p.offsetHeight+5 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

//  for (; p; p = p.offsetParent)
//  {
//	top  += p.offsetTop;
/*  left += p.offsetLeft;*/
//  }

    top = findPos(p)[1] + 43;

    if (p.id.indexOf("whoareyou") != -1)
        top = findPos(p)[1] + 20;

    //  left = findPos(p)[0];
  // use the parent left offset not the parent of the parent
  left = p.offsetLeft + 9;

  // compensate for the bottomShadow element not allowing anything to overflow
  if (p.offsetLeft > 600)
    left = (left - c.offsetWidth + 4) + p.offsetWidth;

//  alert("p.offsetLeft: " + p.offsetLeft + " | " + c.id + " width: " + c.offsetWidth);
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// ----- Show -----

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  at_show_aux(p.id, c.id);

  clearTimeout(c["at_timeout"]);
}

// ----- Hide -----

function at_hide()
{
  var c = document.getElementById(this["at_child"]);

  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 100);
}

// ----- Click -----

function at_click()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  if (c.style.visibility != "visible")
       at_show_aux(p.id, c.id);
  else c.style.visibility = "hidden";

  return false;
}

// ----- Attach -----

// PARAMETERS:
// parent   - id of visible html element
// child    - id of invisible html element that will be dropdowned
// showtype - "click" = you should click the parent to show/hide the child
//            "hover" = you should place the mouse over the parent to show
//                      the child
// position - "x" = the child is displayed to the right of the parent
//            "y" = the child is displayed below the parent
// cursor   - Omit to use default cursor or check any CSS manual for possible
//            values of this field

function at_attach(parent, child, showtype, position, cursor)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = position;
  c["at_position"]   = position;

  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  if (cursor != undefined) p.style.cursor = cursor;

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
    case "hover":
      p.onmouseover = at_show;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
  }
}

function whichMenu()
{
	userMenu=getCookie('menu')
	if (userMenu!=null && userMenu!="")
  	{
//		alert('Really using menu: '+userMenu+'!');
		document.getElementById(userMenu).style.visibility = 'visible';
	}
	else {
		document.getElementById('parentsMenu').style.visibility = 'visible';
	}

    var sections = {"students":"studstudgroups", "parents":"parents", "teachers":"teachers", "law":"traintechassist", "professional":"intervention"};
    document.getElementById("homeGlobalLink").href = "/"+sections[(userMenu.split("Menu")[0])]+"/home.shtml";
}

function resetMenus()
{
/*	document.getElementById(getCookie("menu")).style.visibility = 'hidden';*/
	setCookie("menu","",-1);
}

function setMenu(userMenu)
{
/*	alert("Existing menu: " + getCookie("menu"));*/
	resetMenus();
/*	alert("Menu after reset: " + getCookie("menu"));*/
/*	alert("Set menu to: " + userMenu);*/
	setCookie("menu", userMenu, null); 
/*	alert("Using menu: " + getCookie("menu"));*/
}

function getCookie(c_name)
{
	var nameEQ = c_name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie(c_name,value,expiredays)
{
	if (expiredays) {
		var date = new Date();
		date.setTime(date.getTime()+(expiredays*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
		document.cookie = c_name+"="+value+expires+"; path=/";
}

function setNavIndicator() {
	var navSections = new Array("bestpractices", "evalassess", "grants", "intervention", "materials", "parents", "reportsresearch",
								"studstudgroups", "teachers", "traintechassist", "treatintervent", "treatment");
	var navItems = new Array ("SM", "PM", "TM", "LM", "ProM");
	var navMenus = new Array ("studentsMenu", "parentsMenu", "teachersMenu", "lawMenu", "professionalMenu");
    var curLoc = location.href;

//    alert("Evaluate current location...");
    for (var section in navSections) {
		if (curLoc.indexOf(navSections[section]) != -1 && curLoc.indexOf("home.shtml") == -1) {
            for (var menu in navMenus)
            {
                var curSection = document.getElementById(navMenus[menu]);
//                alert(curSection.style.visibility);
                if (curSection.style.visibility)
                {
//                    alert("Your current nav location is: " + navSections[section]+navItems[menu]);
                    var navImg = document.getElementById(navSections[section]+navItems[menu]);
                    var left = navImg.offsetLeft;
                    var width = navImg.width;
                    var newPos = left + (width / 2 - 6);

//                    alert("navArrow new pos: " + newPos);
                    document.getElementById("navArrow").style.left = newPos+"px";
                    document.getElementById("navArrow").style.top = "39px";
                    document.getElementById("navArrow").style.zIndex = "999";
                    document.getElementById("navArrow").style.visibility = "visible";
                    break;
                }
            }
		}
	}
}

function showPullDown(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  if (c.style.visibility != "visible")
  {
	  c.style.position   = "relative";
	  //c.style.top        = top +'px';
	  //c.style.left       = left+'px';
	  c.style.visibility = "visible";
  }
  else
  {
	c.style.visibility = "hidden";
	c.style.position = "absolute";
  } 
	

  return false;
}