/**
* JavaScript source for MenuBar.
*
* Changes to original version:
*  - removed menuInit().
*  - added class Animator.
*  - additions in various functions in order to enable animation.
*
* @package Api
* @subpackage MenuBar
* @author Mike Hall
* @author Pavel "Papi" Jartsev <papi@digitalfruit.ee>
* @copyright 2000-2002 by Mike Hall. See http://www.brainjar.com for terms of use.
*/

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() 
{

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();


//----------------------------------------------------------------------------
// Global variables.
//----------------------------------------------------------------------------

var activeButton = null;

// CSS class of currently active menu, i.e. mouse moves over it.
var activeMenuClass = '';

// Type of active menubar: 
//	'top' - 1.level is horizontal, 2.level opens at top
//	'bottom' - 1.level is horizontal, 2.level opens at bottom
//	'left' - 1.level is vertical, 2.level opens at left
//	'right' - 1.level is vertical, 2.level opens at right
var activeMenuBarType = 'bottom';


//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------

function buttonClick(button, menuId) 
{


  // Blur focus from the link to remove that annoying outline.
  button.blur();

  // Associate the named menu to this button if not already done.
  // Additionally, initialize menu display.
  if ( button.menu == null ) {
    button.menu = document.getElementById( menuId );
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.
  //if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;

  // Exit if this button is the currently active one.
  if (button == activeButton)
    return false;

  // [END MODIFIED]

  // Reset the currently active button, if any.
  if (activeButton != null)
    resetButton(activeButton);

  // Activate this button, unless it was the currently active one.
  if (button != activeButton) {
    depressButton(button);
    activeButton = button;
  }
  else
    activeButton = null;

  return false;
}

var timeout=0;
var tm_ev;
var tm_mid;

function buttonMouseover(event, menuClass, menuId, menuBarType) 
{

  var button;

	// Set CSS class of active menu
	activeMenuClass = menuClass;
	
	// Set type of active menubar
	activeMenuBarType = menuBarType;
	
  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Activates this button's menu if no other is currently active.
  
  // Get the target button element.
  if (browser.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;
   
  if(tm_ev==button && tm_mid==menuId){
  	buttonClick(tm_ev, tm_mid);
  	return;
  }
  else {
	clearTimeout(timeout);
  	tm_ev = button;
  	tm_mid = menuId;
  }
    
  if (activeButton == null) {
  	button.onmouseout = function(){clearTimeout(timeout)};
  	timeout = setTimeout("buttonClick(tm_ev, tm_mid)",200);
    return;
  }


  // If any other button menu is active, make this one active instead.
  if (activeButton != null && activeButton != button){
    	button.onmouseout = function(){clearTimeout(timeout)};
	 timeout = setTimeout("buttonClick(tm_ev, tm_mid)",200);}
}

function buttonMouseoverFake() 
{
  	tm_ev = null;
  	tm_mid = null;
}

function depressButton(button) 
{
  var x, y;

  // Update the button's style class to make it look like it's
  // depressed.
  button.className += " menuButtonActiveArrow";

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.
  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;
  if (button.menu.onmouseout == null)
    button.menu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

	// Horizontal menubar: put menu into top-side
	if ( activeMenuBarType == 'top' ) {
		x = getPageOffsetLeft(button);
		y = getPageOffsetTop(button) + button.offsetHeight;

	// Horizontal menubar: put menu into bottom-side
	} else if ( activeMenuBarType == 'bottom' ) {
		x = getPageOffsetLeft(button);
		
		// bad hack for IE (needed if IE show menus too far below the menubar)
		if ( browser.isIE && MENU_Y ) {
			y = MENU_Y;
		} else {
			y = getPageOffsetTop(button) + button.offsetHeight;
		}
	
	// Vertical menubar: put menu into left-side
	} else if ( activeMenuBarType == 'left' ) {
		x = getPageOffsetLeft(button) - button.menu.offsetWidth;
		y = getPageOffsetTop(button);
	
	// Vertical menubar: put menu into right-side
	} else if ( activeMenuBarType == 'right' ) {
		x = getPageOffsetLeft(button) + button.offsetWidth;
		y = getPageOffsetTop(button);
	}
	
  // For IE, adjust position.
  /* 
  if (browser.isIE) {
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;
  } 
  */
	
  // set position of menu
  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  
  // set shadow of menu
  if ( SHADOW && button.menu.shadow == null )
  	button.menu.shadow = setShadow( button.menu, x, y );
  
	// show menu with "roll out"-effect or...
	if ( ANIMATE ) {
		var a = new Animator( button.menu, 'v', 'out' );
		a.init();
		
	// ...without effects
	} else {
  	showMenu( button.menu );
	}
}

function resetButton(button) 
{	
  // Restore the button's style class.
  removeClassName(button, "menuButtonActiveArrow");

  // Hide the button's menu, first closing any sub menus.
  if (button.menu != null) {
    closeSubMenu( button.menu );
    
    // hide menu with "roll in"-effect or...
		if ( ANIMATE ) {
			var a = new Animator( button.menu, 'v', 'in' );
			a.init();
		
		// ...without effects
		} else {
  		hideMenu( button.menu );
		}
  }
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function buttonOrMenuMouseout(event) 
{
  var el;
  
  // If there is no active button, exit.
  if (activeButton == null)
    return;

  // Find the element the mouse is moving to.
  if (browser.isIE)
    el = window.event.toElement;
  else if (event.relatedTarget != null)
    el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
    
  // If the element is not part of a menu, reset the active button.
  if (getContainerWith(el, "DIV", activeMenuClass) == null) {
    resetButton(activeButton);
    activeButton = null;
  }
	
	// Unset CSS class of active menu
	activeMenuClass = '';
}

// [END MODIFIED]

function closeSubMenu(menu) 
{

  if (menu == null || menu.activeItem == null)
    return;

  // Recursively close any sub menus.
  if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
    
    // hide sub-menu with "roll in"-effect or...
		if ( ANIMATE ) {
			var a = new Animator( menu.activeItem.subMenu, 'h', 'in' );
			a.init();
		
		// ...without effects
		} else {
  		hideMenu( menu.activeItem.subMenu );
		}
		
    menu.activeItem.subMenu = null;
  }
  removeClassName(menu.activeItem.row, "menuItemHighlight");
  menu.activeItem = null;
}

function hideMenu( menu )
{
	if ( typeof menu != 'object' ) return;
	
  if ( typeof menu.shadow == 'object' )
  	menu.shadow.style.visibility = "hidden";
  menu.style.visibility = "hidden";
}

function menuMouseover(event, menuClass) 
{
  var menu;
	
	// Set CSS class of active menu
	activeMenuClass = menuClass;

  // Find the target menu element.
  if (browser.isIE)
    menu = getContainerWith(window.event.srcElement, "DIV", activeMenuClass);
  else
    menu = event.currentTarget;

  // Close any active sub menu.
  if (menu.activeItem != null) {
    closeSubMenu(menu);
  }
}

/*
* Hover-effect for cells of given table row.
*/
function menuItemHover( row, state,itemId )
{
	if ( typeof row != 'object' ) return;
	
	row = document.getElementById('row_'+itemId);
	/*var className = row.className;
	row.className = ( 
			state == true ? 
			( className.indexOf( 'menuItemHover' ) == -1 ? ( className + ' menuItemHover' ) : className ) :
			className.replace( / menuItemHover/, "" )
		);*/
	if(state){
		row.className+="menuItemHover";
	}
	else {
		removeClassName(row,"menuItemHover")
	}
	/*if(state==false) console.debug(row.className);
	row.className=state?'menuItemHover':'';
		/*
	// go through cells of row
	for ( var i = 0; i < row.cells.length; i++ ) {
		var className = row.cells[i].className;
			
		// highlight/unhighlight cell
		row.cells[i].className = ( 
			state == true ? 
			( className.indexOf( 'menuItemHover' ) == -1 ? ( className + ' menuItemHover' ) : className ) :
			className.replace( / menuItemHover/, "" )
		);
	}*/
}

function menuItemMouseover(event, menuClass, menuId,itemId) 
{
  var item, menu, x, y;
  
	// Set CSS class of active menu
	activeMenuClass = menuClass;
	
  // Find the target item element and its parent menu element.
/*  if (browser.isIE)
    item = getContainerWith( window.event.srcElement, "TD", "menuItem" );
  else
    item = event.currentTarget;*/
   
   item = document.getElementById("item_"+itemId);
   row = document.getElementById("row_"+itemId);
   item.row = row;
    
  menu = getContainerWith(item, "DIV", activeMenuClass);

  // Close any active sub menu and mark this one as active.
  if (menu && menu.activeItem != null) {
    closeSubMenu(menu);
  }
  menu.activeItem = item;

  // Highlight the item element.
  row.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.
  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the sub menu, if not already done.
  if (item.subMenu.onmouseout == null)
    item.subMenu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Get position for submenu based on the menu item.
  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);

  // Adjust position to fit in view.
  var maxX, maxY;

  if (browser.isNS) {
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
    
  } else if (browser.isIE) {
    maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
    maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
      (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
  }
  
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  // set position of sub-menu
  item.subMenu.style.left = (x+10) + "px";
  item.subMenu.style.top  = y + "px";
  
  // set shadow of menu
  if ( SHADOW && item.subMenu.shadow == null )
  	item.subMenu.shadow = setShadow( item.subMenu, x, y );
  	
  // show sub-menu with "roll out"-effect or...
	if ( ANIMATE ) {
		var a = new Animator( item.subMenu, 'h', 'out' );
		a.init();
		
	// ...without effects
	} else {
  	showMenu( item.subMenu );
	}

  // Stop the event from bubbling.
  if (browser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}

function setShadow( menu, x, y )
{
	shadow = document.getElementById( menu.id + '_shadow' );

	shadow.style.height = menu.offsetHeight - 2;
  shadow.style.top = parseInt( y + ( browser.isNS ? 2 : 4 ) ) + "px";
  shadow.style.width = menu.offsetWidth - 1;
  shadow.style.left = parseInt( x + ( browser.isNS ? 0 : 2 ) ) + "px";
  
  return shadow;
}

function showMenu( menu )
{
	if ( typeof menu != 'object' ) return;
	
	menu.style.visibility = "visible";
  if ( typeof menu.shadow == 'object' ) {
  	menu.shadow.style.visibility = "visible";
  }
}

//----------------------------------------------------------------------------
// Class Animator.
//----------------------------------------------------------------------------

// currently running animators
var animators = [];

/**
* Creates new Animator object.
*/
function Animator( menu, type, roll )
{
	// index of clip item to use (clip-bottom for vert.roll, clip-right for hor.roll)
	this.clip_index = null;
	
	// max value for clip item (menu.offsetHeight for vert.roll, menu.offsetWidth for hor.roll)
	this.clip_max = null;
	
	// unique id (actually it's index of this animator in animators array)
	this.id = null;
	
	// time interval for animation steps (in msec)
	this.interval = 20;
	
	// animated menu
	this.menu = menu;
	
	// animation step (in pixels)
	this.step = ( type == 'v' ? 10 : 30 );
	
	// timer ID (created by setInterval())
	this.timer = null;
	
	// rolling direction of menu: 'out', 'in'
	this.roll = roll;
	
	// type of rolling: 'h' - horisontal, 'v' - vertical
	this.type = type;
	
	// methods
	this.animate = Animator_animate;
	this.finish = Animator_finish;
	this.init = Animator_init;
}

/**
* Animates menu by changing "clip"-property of "this.menu.style".
*/
function Animator_animate()
{
	// current clip
	var clip = splitClip( this.menu.style.clip );
	if ( typeof clip != 'object' ) return;
	
	// compute new value
	clip[this.clip_index] = parseInt( clip[this.clip_index] ) + this.step;
	
	// set new clip
	this.menu.style.clip = joinClip( clip[0], clip[1], clip[2], clip[3] );
	
	// animate shadow
	if ( typeof this.menu.shadow == 'object' ) {
		var sclip = splitClip( this.menu.shadow.style.clip );
		sclip[this.clip_index] = parseInt( clip[this.clip_index] ) + this.step;
		this.menu.shadow.style.clip = joinClip( clip[0], clip[1], clip[2], clip[3] );
	}
	
	// finish this, if menu is rolled out/in
	if (( this.roll == 'out' && clip[this.clip_index] >= this.clip_max ) || 
		( this.roll == 'in' && clip[this.clip_index] <= 0 )) {
		
		this.finish();
	}
}

/**
* Finishes execution of Animator.
*/
function Animator_finish()
{
	// clear timer
	clearInterval( this.timer );
	this.timer = null;
		
	// clear this in animators array
	animators[this.id] = null;
}

/**
* Initializes Animator.
*/
function Animator_init()
{
	if ( ! this.menu ) return;
	
	// set negative step for roll-in
	if ( this.roll == 'in' )
		this.step = -this.step;
	
	// vertical roll
	if ( this.type == 'v' ) {
		this.menu.style.clip = ( this.roll == 'out' ? 
			joinClip( 0, this.menu.offsetWidth, 0, 0 ) :
			joinClip( 0, this.menu.offsetWidth, this.menu.offsetHeight, 0 )
		);
		if ( typeof this.menu.shadow == 'object' ) {
			this.menu.shadow.style.clip = ( this.roll == 'out' ? 
				joinClip( 0, this.menu.shadow.offsetWidth, 0, 0 ) :
				joinClip( 0, this.menu.shadow.offsetWidth, this.menu.shadow.offsetHeight, 0 )
			);
		}
		
		this.clip_index = 2;
		this.clip_max = this.menu.offsetHeight;
	
	// horisontal roll
	} else {
		this.menu.style.clip = ( this.roll == 'out' ? 
			joinClip( 0, 0, this.menu.offsetHeight, 0 ) :
			joinClip( 0, this.menu.offsetWidth, this.menu.offsetHeight, 0 )
		);
		if ( typeof this.menu.shadow == 'object' ) {
			this.menu.shadow.style.clip = ( this.roll == 'out' ? 
				joinClip( 0, 0, this.menu.shadow.offsetHeight, 0 ) :
				joinClip( 0, this.menu.shadow.offsetWidth, this.menu.shadow.offsetHeight, 0 )
			);
		}
		
		this.clip_index = 1;
		this.clip_max = this.menu.offsetWidth;
	}
	
	showMenu( this.menu );
	
	// add this into animators array
	this.id = animators.length;
	animators[this.id] = this;
	
	// animate
	this.timer = setInterval( "animators[" + this.id + "].animate()", this.interval );
}

/**
* Splits given 'clip'-string into array.
* Eg: splitClip( 'rect(0px 0px 0px 0px)' ) => Array( 0, 0, 0, 0 ).
*/
function splitClip( clip )
{
	clip = clip.split( browser.isIE ? 'px ' : 'px, ' );
	if ( typeof clip == 'object' ) {
		clip[0] = clip[0].replace( /rect\(/, '' );
		clip[3] = clip[3].replace( /px\)/, '' );
	}
	
	return clip;
}

/**
* Joins given parameters into 'clip'-string.
* Eg: joinClip( 0, 0, 0, 0 ) => 'rect(0px 0px 0px 0px)'.
*/
function joinClip( top, right, bottom, left )
{
	var clip = new Array( top, right, bottom, left );
	
	return ( 'rect(' + clip.join( 'px ' ) + 'px)' );
}


//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith(node, tagName, className) 
{

  // Starting with the given node, find the nearest containing element
  // with the specified tag name and style class.
  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
        hasClassName(node, className))
      return node;
    node = node.parentNode;
  }

  return node;
}

function hasClassName(el, name) 
{

  var i, list;

  // Return true if the given element currently has the given class name.
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function removeClassName(el, name) 
{

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.
  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function getPageOffsetLeft(el) 
{

  var x;

  // Return the x coordinate of an element relative to the page.
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) 
{

  var y;

  // Return the y coordinate of an element relative to the page.
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

