/** Constructor of fly menu - navigation bar with set of popup menu 
 *  navig[i][j] - two-dimension array:  i - index pop menu in bar menu, j - index of item in pop menu.
 *  This is a configuration array.  
 * */
var FlyMenu =
function( navig )
{
  this.navmenu = navig;
  this.numberSingleMenus = 0;
  this.isSingle = false;
  this.offdiv = null;
  
  this.active = null;
  this.menuInstance = new Array();
  this.bar_e = new Array();  // array of barElement objects. They are members of bar menu
}

FlyMenu.prototype =
{
  activate: function( object )
  {
    if( this.active == null ) // && active_bar_menu != this )
    {
	  this.active = object;
      this.offdiv.show();
      object.openfly();
	}
	else
	  if( this.active != object ) 
	  {
 		this.active.closefly();
 		this.active = object;
   	    object.openfly();
	  }  
  },
  
  closefly: function()
  {
	  this.active.closefly();
	  this.active = null;
	  this.offdiv.hide();
  },
  
  setParentId: function( parentId )
  {
    this.numberSingleMenus = 1;
    this.menuInstance[0] = new Array( parentId, null );
    this.isSingle = false;
  },
  
  setParentIdForSingle: function( parentId, parameter )
  {
    var id = this.numberSingleMenus;
    this.menuInstance[id] = new Array( parentId, parameter );
    this.numberSingleMenus = ++id;
    this.isSingle = true;
  },
  
  printbarmenu: function()
  {
    var menuInst = this.menuInstance[0];
    if( !menuInst )
    {
      alert( 'Do not set parent element for this menu' ); return;
    }
	var parent = $(menuInst[0]);
//    var list = parent.childNodes
//    for (i = 0; i < list.length; i++){
//    	alert( i + ': ' + list[i].nodeName + ', ' + list[i].nodeType + ', ' + list[i].nodeValue );
//    	if (list[i].nodeType == 3)
//    		list[i].nodeValue = '';
//    }
    
    var doc = parent.ownerDocument;
    var barMenuDIV = doc.createElement('div');
    parent.insertBefore( barMenuDIV, parent.firstChild );
    barMenuDIV.id = 'barMenu'; // ??? if we have a few bar menu that it is wrong !!!
    barMenuDIV.className = 'barMenu';
    for( var x = 1; x < this.navmenu.length; x++ )
    {
	  var barElement = this.bar_e[x] = new BarElement( x, this );
	  barElement.create();
	  barElement.pop = new PopFlyMenu( x, this );
      
	  var be = barElement.shadow();
      barMenuDIV.appendChild( be );
    }    
  },
  
  printsinglebarmenu: function()  // id eleminate !!!
  {
	barCount = ( this.isSingle )? this.numberSingleMenus + 1 : this.navmenu.length;

	for( var id = 1; id < barCount; id++ )
    { 
	  var menuInst = this.menuInstance[id-1];
      if( !menuInst )
      {
        alert( 'Do not set parent element for the menu #' + id ); continue;
      }
      var parameter = menuInst[1];
      var x = 1;
    
      this.bar_e[id] = new BarElement( x, this );
      this.bar_e[id].parameter = parameter;
      this.bar_e[id].pop = ((id == 1)? new PopFlyMenu( x, this ) : this.bar_e[1].pop );
      this.bar_e[id].create();

      var be = this.bar_e[id].shadow();

      var parent = $(menuInst[0]);
      var doc = parent.ownerDocument;
      var sbm = doc.createElement('div');
      sbm.className = 'singleBarMenu';
      sbm.appendChild( be );

      parent.appendChild( sbm );
    }  
  },
  
//  createBarElements: function( menuRecord )
//  {
//	var barElemetsCount = ( this.isSingle )? this.numberSingleMenus + 1 : this.navmenu.length;
//	for( var x = 1; x < barElemetsCount; x++ )
//	{
//		var id = ( this.isSingle )? 1 : x;
//		this.bar_e[x] = new BarElement( id, this );
//		this.bar_e[x].parameter = menuRecord[1];
//	    this.bar_e[x].create();
//		if ( this.isSingle )
//			this.bar_e[x].pop = ((x == 1)? new PopFlyMenu( id, this ) : this.bar_e[1].pop );
//		else	
//			this.bar_e[x].pop = new PopFlyMenu( id, this );
//	}
//	  
//  },
//  
//  createDOCInstances: function( menuRecord )
//  {
//	var barElemetsCount = ( this.isSingle )? this.numberSingleMenus + 1 : this.navmenu.length;
//	  
//  },
  
  prepmenus: function()
  {
    var barCount;
	barCount = ( this.isSingle )? this.numberSingleMenus + 1 : this.navmenu.length;

	for( var x = 1; x < barCount; x++ )
	{
      this.bar_e[x].prepmenus();
	}
  },
  
  printPopMenus: function()
  {
	if( this.isSingle )
	{ 
		if ( this.numberSingleMenus > 0 )
			this.bar_e[1].pop.create();
	}
	else
	{
		for( var x = 1; x < this.navmenu.length; x++ )
			this.bar_e[x].pop.create();
	}  

    var parent = $(this.menuInstance[0][0]);
    var doc = parent.ownerDocument;
    var span = doc.createElement('span');
    Element.extend(span);
	span.style.width = '100%'
	span.style.height = '100%'
	span.style.position = 'absolute'
	span.style.left = '0'
	span.style.top = '0'
	span.style.zIndex = '25'
	this.offdiv = span;
	doc.body.appendChild( span );
    Event.observe( span, 'mouseover', this.closefly.bindAsEventListener(this) );
    span.hide();
  }
}
