/*
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 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: 0,
	topoffsetShowOnRight:  0 ,
	leftoffsetShowOnBottom: jQuery.browser.msie ? -198 : -200,
	topoffsetShowOnBottom:  -37
}


function mainmenu_attach(parent, child,  className, classNameSelected)
{
  p = document.getElementById(parent);
  c = document.getElementById(child);

  p["at_parent"]     = p.id;  
  p["at_child"]      = c.id;
  p["at_class"]      = className; 
  p["at_class_sel"]  = classNameSelected;  
  
  c["at_parent"]     = p.id;
  c["at_child"]      = c.id;  
  c["at_class"]      = className; 
  c["at_class_sel"]  = classNameSelected; 

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

  p.onmouseover = mainmenu_show;
  p.onmouseout  = mainmenu_hide;
  c.onmouseover = mainmenu_show;
  c.onmouseout  = mainmenu_hide;
}

function mainmenu_show(){

  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child" ]);  
  p.className = this["at_class_sel"];
  mainmenu_show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);
}


function mainmenu_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  //var pos = $(p).offset;
  //alert(pos.left);
  //var paa = $("#" + parent);
  //var offset = p.offset();
  //alert(paa.left);

	//alert($(p).offset().left);
	
  var c = document.getElementById(child);
  var pos = $(p).position();
  var left = pos.left - 5;/*-14*/;
  var top = pos.top + $(p).outerHeight() + 4 ;

  //left = $(p).position().left;
  //top  =  $(p).position().top + $(p).outerHeight();
  
  
	/*
  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"]);
  className = this["at_class"];   
  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);
}


// ----------- CATALOGUE ITEM  -----------

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

  p["at_parent"]     = p.id;  
  p["at_child"]      = c.id;
  
  c["at_parent"]     = p.id;
  c["at_child"]      = c.id;  
 
  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  p.onmouseover = catalogueitem_show;
  p.onmouseout  = catalogueitem_hide;
  c.onmouseover = catalogueitem_show;
  c.onmouseout  = catalogueitem_hide;
}

function catalogueitem_show(){
  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child" ]);  
  catalogueitem_show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);
}

function catalogueitem_show_aux(parent, child){
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  var pos = $(p).position();
  var left = pos.left + 54;/*-14*/;
  var top = pos.top + $(p).outerHeight() - 118;

  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

function catalogueitem_hide(e){
  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child"]);
  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden';", 100);
  e.stopPropagation();
}

