// Copyright (C) 2005 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
// [Adapted by GHC]

// ----- Popup Control ---------------------------------------------------------

// ----- Attach -----

// PARAMETERS:
// parent   - id of visible html element
// child    - id of invisible html element that will be dropdowned
// activeLinkColor  - color of link disabled during hover 
// left/top Offset - num pixels to offset dropdown (may be -ve)
// hideElement - id of any element to hide on activation, else null

function at_attach(parent, child, activeLinkColor, leftOffset, topOffset, hideElement )
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  p["at_active_link_color"] = activeLinkColor;
  p["at_left_offset"] = leftOffset;
  p["at_top_offset"] = topOffset;
  p["at_hide_element"] = hideElement;

  c["at_parent"]     = p.id;
  c["at_child"]      = c.id;

  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  c["at_hide_timeout"] = null;

  p.onmouseover = at_show;
  p.onmouseout  = at_hide;
  c.onmouseover = at_show;
  c.onmouseout  = at_hide;
}

// ----- Show -----

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);
  var a = document.getElementById(p.id + "-link");

  p["at_cached_href"] = a.href ;
  a.href = "javascript:void(0)" ;

  p["at_cached_color"] = a.style.color ;
  a.style.color = p["at_active_link_color"] ;

  c["at_show_timeout"] = setTimeout( "at_show_aux('" + p.id + "','" + c.id + "'," + p["at_left_offset"] +  "," + p["at_top_offset"] + ")", 222 ) ;

  clearTimeout(c["at_hide_timeout"]);
}

// ----- Show Aux -----

function at_show_aux(parent, child, leftOffset, topOffset)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );

  var top  = p.offsetHeight;
  var left = 0;

  if( p["at_hide_element"] != null )
  {
  	 var h = document.getElementById(p["at_hide_element"]) ;
 	 h.style.visibility = "hidden" ;
  }

  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;
  }

  left += leftOffset ;
  top += topOffset ;

  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

// ----- Hide -----

function at_hide()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child"]);
  var a = document.getElementById(p.id + "-link");
  a.href = p["at_cached_href"] ;
  a.style.color = p["at_cached_color"] ;

  c["at_hide_timeout"] = setTimeout("at_hide_aux('" + p.id + "','" + c.id + "')", 333);
  clearTimeout( c["at_show_timeout"] ) ;
}

function at_hide_aux( parent, child )
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  var a = document.getElementById(p.id + "-link");

  c.style.visibility = "hidden";

  var h = document.getElementById("dropdown-hide") ;

  if( p["at_hide_element"] != null )
  {
  	 var h = document.getElementById(p["at_hide_element"]) ;
 	 h.style.visibility = "visible" ;
  }
}
