/*
JS: PopUp logic
Constants:
	PopUpConf (Object)
		classname_item             - parent clasname
		classname_item_selected    - parent clasname SELECTED
		classname_item_current     - parent clasname CURRENT (On what page are you)
		classname_subitem          - child clasname
		classname_subitem_selected - child clasname SELECTED
		classname_subitem_current  - child clasname CURRENT (On what page are you)
		hidetimeout                - hide in miliseconds
		simple_hidetimeout         - mouseout hide in miliseconds
		leftoffset                 - child left offset
		topoffset                  - child top offset
		
Functions:
	function popup_attach - array child to parent
		parent    - parentid
		child     - childid - dropdownid
		showtype  - "click" = you should click the parent to show/hide the child
		            "over" = 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
		isCurrent - on hide use current class not default (look PopUp_hide)
	function PopUp_show - display on mouse over
	function PopUp_click  - display on mouse click
	function PopUp_show_aux(parent, child)  - generic function for show, clicl
	function PopUp_hide() - hide on hidetimeout
	
Functions - simple mouseover logic - NO BLINKING!!!:
	function PopUp_select_attach(object, className, classNameSelected) - attach logic
	function PopUp_select_show()	- mouseover
	function PopUp_select_hide() - mouseout
*/

var PopUpConf = { 

	classname_subitem_selected : "PopUp_SubItem_Selected", 
	hidetimeout: 100,
	leftoffset: 16,
	topoffset: 6
}

function popup_attach(parent, child, showtype, position, isCurrent, bDisplayChild, index)
{
  p = $(parent);
  c = $(child);

  p["at_parent"]     = p.id;  
  p["at_child"]      = c.id;
  p["at_position"]   = position; 
  p["isCurrent"]     = isCurrent;
  p["displayChild"]  = bDisplayChild;  

  
  c["at_parent"]     = p.id;
  c["at_child"]      = c.id;  
  c["at_position"]   = position;  
  c["isCurrent"]     = isCurrent;
  c["displayChild"]  = bDisplayChild;

  c.style.position   = "absolute";
  c.style.visibility = "hidden";


  switch (showtype)
  {
    case "click":
      p.onclick     = PopUp_click;
      p.onmouseout  = PopUp_hide;
      c.onmouseover = PopUp_show;
      c.onmouseout  = PopUp_hide;
      break;
    case "over":
      p.onmouseover = PopUp_show;
      p.onmouseout  = PopUp_hide;
      c.onmouseover = PopUp_show;
      c.onmouseout  = PopUp_hide;
      break;
  }
}

function PopUp_show()
{

  p = $(this["at_parent"]);
  c = $(this["at_child" ]); 


  
  bDisplayChild =  p["displayChild"];    
  
  if (bDisplayChild){
	  c.className = 'PopUp_SubItem_Selected';
	  PopUp_show_aux(p.id, c.id);
  }
  clearTimeout(c["at_timeout"]);
}


function PopUp_click()
{
  p = $(this["at_parent"]);
  c = $(this["at_child" ]);
  bDisplayChild =  p["displayChild"];  
  if(bDisplayChild){
	  if (c.style.visibility != "visible") PopUp_show_aux(p.id, c.id);
	  else c.style.visibility = "hidden";
  }
  return false;
}

function PopUp_show_aux(parent, child)
{
  var p = $(parent);
  var c = $(child);
  
  var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;
  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;
  }
  left =  left + PopUpConf.leftoffset;
  top =  top + PopUpConf.topoffset;
  
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}


function PopUp_hide()
{
  p = $(this["at_parent"]);
  c = $(this["at_child"]);
  bDisplayChild =  p["displayChild"]; 

  
if( this["isCurrent"] == true){ 	
	className = PopUpConf.classname_item_current;
  } else {
	  
	className = PopUpConf.classname_item;
  }
  
 

	c["at_timeout"] = setTimeout("$('"+c.id+"').style.visibility = 'hidden';$('"+p.id+"').className='"+className+"';", PopUpConf.hidetimeout);

}