/*
JS: MainMenu logic
Constants:
	MainMenuConf (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 mainmenu_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 mainmenu_hide)
	function mainmenu_show - display on mouse over
	function mainmenu_click  - display on mouse click
	function mainmenu_show_aux(parent, child)  - generic function for show, clicl
	function mainmenu_hide() - hide on hidetimeout
	
Functions - simple mouseover logic - NO BLINKING!!!:
	function mainmenu_select_attach(object, className, classNameSelected) - attach logic
	function mainmenu_select_show()	- mouseover
	function mainmenu_select_hide() - mouseout
*/

var MainMenuConf = { 
	classname_item : "MainMenu_Item", 
	classname_item_selected : "MainMenu_Item_Selected", 
	classname_item_current : "MainMenu_Item_Current", 
	classname_subitem : "MainMenu_SubItem", 
	classname_subitem_selected : "MainMenu_SubItem_Selected", 
	classname_subitem_current : "MainMenu_SubItem_Current", 
	hidetimeout: 100,
	simple_hidetimeout: 2,
	leftoffset: 0,
	topoffset: -2
}

function mainmenu_attach(parent, child, showtype, position, isCurrent, bDisplayChild)
{
  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     = mainmenu_click;
      p.onmouseout  = mainmenu_hide;
      c.onmouseover = mainmenu_show;
      c.onmouseout  = mainmenu_hide;
      break;
    case "over":
      p.onmouseover = mainmenu_show;
      p.onmouseout  = mainmenu_hide;
      c.onmouseover = mainmenu_show;
      c.onmouseout  = mainmenu_hide;
      break;
  }
}

function mainmenu_show()
{

  p = $(this["at_parent"]);
  c = $(this["at_child" ]);  
  bDisplayChild =  p["displayChild"];  
  p.className = MainMenuConf.classname_item_selected;
  if (bDisplayChild){
	  c.className = 'MainMenu_Item_Sub';
	  mainmenu_show_aux(p.id, c.id);
  }
  clearTimeout(c["at_timeout"]);
}


function mainmenu_click()
{
  p = $(this["at_parent"]);
  c = $(this["at_child" ]);
  bDisplayChild =  p["displayChild"];  
  if(bDisplayChild){
	  if (c.style.visibility != "visible") mainmenu_show_aux(p.id, c.id);
	  else c.style.visibility = "hidden";
  }
  return false;
}

function mainmenu_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 + MainMenuConf.leftoffset;
  top =  top + MainMenuConf.topoffset;
  
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}


function mainmenu_hide()
{
  p = $(this["at_parent"]);
  c = $(this["at_child"]);
  bDisplayChild =  p["displayChild"];  
  
  if( this["isCurrent"] == true){
  	className = MainMenuConf.classname_item_current;
  } else {
	className = MainMenuConf.classname_item;
  }  
  c["at_timeout"] = setTimeout("$('"+c.id+"').style.visibility = 'hidden';$('"+p.id+"').className='"+className+"';", MainMenuConf.hidetimeout);
}



// ----- Simple mouseover logic----- NO BLINKING!!!!

function mainmenu_select_attach(object, className, classNameSelected)
{  
  p = $(object);
  p["at_id"]     = p.id;  
  p["at_classname"]   = className;  
  p["at_classname_selected"]   = classNameSelected;  
  
  p.onmouseover = mainmenu_select_show;
  p.onmouseout  = mainmenu_select_hide;	
}

function mainmenu_select_show()
{
  p = $(this["at_id"]); 
  p.className = this["at_classname_selected"];  
  clearTimeout(p["at_timeout"]);
}

function mainmenu_select_hide()
{
  p = $(this["at_id"]);
  className = this["at_classname"];  
  p["at_timeout"] = setTimeout("$('"+p.id+"').className='"+className+"';", MainMenuConf.simple_hidetimeout);
}

