
/*** CONFIGURA ***/
var _widthmenu = 180; /*** LARGHEZZA DI UN SIGOLO MENU ORIG 150***/
var _topoffset = 8; /*** DISTANZA SUPERIORE DEL POPUP DALLA VOCE DEL MENU ***/
var _alignerr = 5; 
//var _menuerr = 120; /*** Per la liquidità del menu: più è piccolo e più si allarga ***/
//var _menuerr = 160; /*** Per la liquidità del menu: più è piccolo e più si allarga ***/
var _menuerr = 130; /*** Per la liquidità del menu: più è piccolo e più si allarga ***/





var _items = new Array();
var _currentMenu = null;

if( ! document.getElementById ) document.getElementById = function() { return null; }

function _winsize_() {
 var _wsize = null;

 if( parseInt( navigator.appVersion ) > 3 ) {
  if( navigator.appName === "Netscape" ) {
   _wsize = window.innerWidth;
  }

  if( navigator.appName.indexOf( "Microsoft" ) !== -1 ) {
   _wsize = document.body.offsetWidth;
  }
 }

 return _wsize;
}

function _isLeftAlign_() {
 return ( Math.abs( document.getElementById( _items[0] ).offsetLeft - document.getElementById( _items[_items.length - 1] ).offsetLeft ) < _alignerr )? true : false;
}

function _isTopAlign_() {
 return ( Math.abs( document.getElementById( _items[0] ).offsetTop - document.getElementById( _items[_items.length - 1] ).offsetTop ) < _alignerr )? true : false;
}

function _initializeMenu_( _menuId, _actuatorId ) {
 var _menu = document.getElementById( _menuId );
 var _actuator = document.getElementById( _actuatorId );

 if ( ( null === _menu ) || ( null === _actuator ) ) return;

 _actuator.showMenu = function() {

                       _menu.style.width = _widthmenu + "px";
                       _menu.style.left = ( ( _menu.id.indexOf( _items[_items.length - 1] ) !== -1 ) && ( ( this.offsetLeft + _widthmenu + _menuerr ) > _winsize_() ) )? ( ( this.offsetLeft + this.offsetWidth - _widthmenu ) + "px" ) : ( this.offsetLeft + "px" );
                       _menu.style.top = ( _topoffset + this.offsetTop + this.offsetHeight ) + "px";
                       _menu.style.visibility = "visible";
                       _menu.style.display = "block";
                       _currentMenu = _menu;
                       return;

                      }

 _actuator.onfocus = function() {

                      if( _isTopAlign_() ) {
                       if ( null !== _currentMenu ) {
                        if( _currentMenu.id === _menu.id ) {
                         _currentMenu.style.visibility = "hidden";
                         _currentMenu.style.display = "none";
                         _currentMenu = null;
                         return;
                        }
                        else {
                         _currentMenu.style.visibility = "hidden";
                         _currentMenu.style.display = "none";
                         _currentMenu = null;
                        }
                       }

                       this.showMenu();
                      }
                      else if( ( ! _isLeftAlign_() ) && ( null !== _currentMenu ) ) {
                       _currentMenu.style.visibility = "hidden";
                       _currentMenu.style.display = "none";
                       _currentMenu = null;
                      }

                      return;
                     }

 _actuator.onclick = function() {
                      if( navigator.userAgent.indexOf( "Safari" ) !== -1 ) {
                       this.blur();

                       if( _isTopAlign_() ) {
                        _actuator.onfocus();
                        return false;
                       }
                       else
                        return;
                      }

                      else if( navigator.userAgent.indexOf( "Chrome" ) !== -1 ) {
                       _actuator.onfocus();
                       return _isTopAlign_() ? false : true;
                      }

                      else {
                       this.blur();
                       return ( _isTopAlign_() )? false : true;
                      }
                     }

 return;
}

function _applystyle_( _obj, _str ) {
 if( document.all && ( ! window.opera ) )
  _obj.style.setAttribute( "cssText", _str );
 else
  _obj.setAttribute( "style", _str );

 return;
}

function _menuResize_() {
 var _busyspace = 0;

 for( var _index in _items ) {
  _applystyle_( document.getElementById( _items[_index] ), "padding: 0px;" );
  _busyspace += document.getElementById( _items[_index] ).offsetWidth;
 }

 var _padd = Math.floor( ( ( document.getElementById( "mainMenu" ).offsetWidth - _busyspace - _menuerr ) / _items.length ) / 2 );

 for( var _index in _items )
  _applystyle_( document.getElementById( _items[_index] ), "padding: 0px " + _padd + "px;" );

 return;
}
