/*
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
		leftoffsetShowOnBottom     - child left offset
		topoffsetShowOnBottom      - child top offset
		leftoffsetShowOnRight      - child left offset
		ttopoffsetShowOnRigh       - 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 MainMenuItems = Array();

var MainMenuConf = { 
	classname_item : "MainMenu_Item", 
	classname_item_selected : "MainMenu_Item_Selected", 
	classname_item_current : "MainMenu_Item_Current", 
	classname_subitem : "TopMenu_Item_Sub", 
	classname_subitem_selected : "TopMenu_Item_Sub", 
	classname_subitem_current : "TopMenu_Item_Sub", 
	hidetimeout: 100,
	simple_hidetimeout: 2,
	leftoffsetShowOnRight: 10,
	topoffsetShowOnRight:  0 ,
	leftoffsetShowOnBottom: 0,
	topoffsetShowOnBottom:  100
}


function mainmenu_add(parent, child, showtype, position, isCurrent, bDisplayChild){
	MainMenuItems[MainMenuItems.length] = Array(parent, child, showtype, position, isCurrent, bDisplayChild);
	//alert(MainMenuItems.length);
}

function mainmenu_load(){
	for(i=0; i<MainMenuItems.length ; i++){
			menuitem = MainMenuItems[i];
			//alert(menuitem[0]);
			mainmenu_attach(menuitem[0],menuitem[1],menuitem[2],menuitem[3],menuitem[4],menuitem[5]);
		}
	
	}

function mainmenu_attach(parent, child, showtype, position, isCurrent, bDisplayChild)
{
  p = document.getElementById(parent);
  c = document.getElementById(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 = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child" ]);  
  bDisplayChild =  p["displayChild"];    
  p.className = MainMenuConf.classname_item_selected;
  if (bDisplayChild){
	  c.className = 'TopMenu_Item_Sub';
	  mainmenu_show_aux(p.id, c.id);
  }
  clearTimeout(c["at_timeout"]);
}


function mainmenu_click()
{
  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(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 = document.getElementById(parent);
  var c = document.getElementById(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;
  }
	if((c["at_position"] == "y")){
		if(BrowserDetect.browser == "Explorer"){
			top = 130;
			left = left - 0;
		} else {		
			top = 130;
			left = left - 0;
		}
	}
	
  if((c["at_position"] == "x")){
  	left =  left + MainMenuConf.leftoffsetShowOnRight;
  	top  =  top + MainMenuConf.topoffsetShowOnRight;
  } else{
 	left =  left + MainMenuConf.leftoffsetShowOnBottom;
  	top  =  top + MainMenuConf.topoffsetShowOnBottom;
  }
  	
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}


function mainmenu_hide()
{
  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child"]);
  bDisplayChild =  p["displayChild"];  
  
  if( this["isCurrent"] == true){
  	className = MainMenuConf.classname_item_current;
  } else {
	className = MainMenuConf.classname_item;
  }  
  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden';document.getElementById('"+p.id+"').className='"+className+"';", MainMenuConf.hidetimeout);
}



// ----- Simple mouseover logic----- NO BLINKING!!!!

function mainmenu_select_attach(object, className, classNameSelected)
{  
  p = document.getElementById(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 = document.getElementById(this["at_id"]); 
  p.className = this["at_classname_selected"];  
  clearTimeout(p["at_timeout"]);
}

function mainmenu_select_hide()
{
  p = document.getElementById(this["at_id"]);
  className = this["at_classname"];  
  p["at_timeout"] = setTimeout("document.getElementById('"+p.id+"').className='"+className+"';", MainMenuConf.simple_hidetimeout);
}

