var pMContrStyles = new Array();
pMContrStyles[0] = loadStyle( "popMenu.css", ".mitemdivs" );
pMContrStyles[1] = loadStyle( "popMenu.css", ".mitemdiv2" );

var minWidth = 50;
var maxWidth = 150;
var PopFlyMenu = function( x, parent )
{
//  alert( '>>>popflymenu constructor pr:' + prefix );
  this.navmenu = parent.navmenu[x];
  this.ID = x;

  this.mwidth = minWidth;  // default
  this.flym = null;          // view of the menu : <div>
  this.mitm = new Array();   // array of items of pop menu
  this.mlnk = new Array();   // array of <a> elements
  this.oitem = -1;           // index of an active item
  this.ready = false;
  
  this.parameter = ''     // parameter for url for all items
  this.controlStyle = pMContrStyles;
}

PopFlyMenu.prototype = 
{
  popfly: function()
  {
	  this.flym.show();
  },
  
  closefly: function()
  {
		this.flym.hide();
		this.colormenuitem( -1 );
  },
  
  colormenuitem: function( xx )
  {
    if( arguments.length > 1 )
      xx = arguments[1];
//    alert( xx + '; L=' + arguments.length + '; arg[0]=' + arguments[0]+ '; arg[1]=' + arguments[1] + '; arg[2]=' + arguments[2] )
    var xxp = this.oitem; // index of previous item
	  if( xxp > 0 )  // clean previous
	  {
		  if( xxp != 1) // first and the rest items are different
		  { }
		  else
		  { }
		  this.lightItem( xxp, 0 );
	  }
    if( xx > 0 )
    {
		  this.lightItem( xx, 1 );
    }
	  this.oitem = xx;
  },
  
  lightItem: function( x, i )
  {
    this.mitm[x].style.backgroundColor =  this.controlStyle[i].backgroundColor; // mitembgD;
	  this.mlnk[x].style.textDecoration = this.controlStyle[i].textDecoration; //"none";
  },
  
  create: function()
  {
	  var thismenu =  "";
    var parDoc = document; // TO DO: get real parent's document (it can be iframe for example)

    var fm = this.flym = new Element('div', {'class':'flymenus'});
    parDoc.body.appendChild( fm );
    var container = new Element('div');
    fm.appendChild( container );
    
    var maxW = 0; // for IE6 5.5 5
    for( var xx = 1; xx < this.navmenu.length; xx++ )
	{ 
	  var itemnlink = this.navmenu[xx];
	  var ml = (xx != 1)? "s": "1";
		  
	  var a = this.mlnk[xx] = new Element('a', {href:'javascript:void(0)'});
      a.title = itemnlink[1];
      a.innerHTML = itemnlink[0];
	  a.style.textDecoration = this.controlStyle[0].textDecoration;
      
	  var div = this.mitm[xx] = new Element('div', {'class':'mitemdiv' + ml});
//	  div.className = 'mitemdiv' + ml;

      Event.observe( div, 'mouseover', this.colormenuitem.bindAsEventListener(this, xx ) );
      Event.observe( div, 'click', this.executeTarget.bindAsEventListener(this, xx ) );

	  div.style.backgroundColor =  this.controlStyle[0].backgroundColor;

	  div.appendChild( a );
	  container.appendChild( div );
	  if( maxW < a.offsetWidth )
		  maxW = a.offsetWidth;
	  }
/*
    var divBottom = parDoc.createElement('div');
    divBottom.style.float = 'left';
    divBottom.style.height = '8px';
    fm.appendChild( divBottom );
    
    var divChild = parDoc.createElement('div');
    divChild.className = 'cornerBL';
    divBottom.appendChild( divChild );
    
    divChild = parDoc.createElement('div');
    divChild.className = 'menucorners';
    divChild.innerHTML = '&nbsp;';
	  divChild.style.width = this.flym.offsetWidth - 16 + 'px';
	  this.menuBottom = divChild;
    divBottom.appendChild( divChild );
    
    divChild = parDoc.createElement('div');
    divChild.className = 'cornerBR';
    divBottom.appendChild( divChild );
*/

    var divBottom = new Element('div');
    divBottom.style.height = '8px';
    fm.appendChild( divBottom );
    
    var divChild = new Element('div');
    divChild.className = 'cornerBL';
    divBottom.appendChild( divChild );
    
    divChild = new Element('div');
    divChild.className = 'menucorners';
    this.menuBottom = divChild;
//    divChild.innerHTML = '&nbsp;';
    divBottom.appendChild( divChild );

    divChild = new Element('div');
    divChild.className = 'cornerBR';
    divBottom.appendChild( divChild );

//    var divChild2 = parDoc.createElement('div');
//    divChild2.className = 'menucorners';
//    divChild2.innerHTML = '&nbsp;';
//    divChild1.appendChild( divChild2 );
        
//    if(document.all && document.getElementById) // IE ?
    {  
    	var tStyle = this.controlStyle[0];
    	this.flym.style.width = (maxW 
    	 + parseInt(tStyle.paddingLeft) + parseInt(tStyle.paddingRight)) + 'px';
    }	
//	this.menuBottom.style.width = maxW - 8 + 'px';
    
    this.menuBottom.style.width = this.flym.offsetWidth - 16 + 'px';   // 8px is a width of the corner picture
/*
	alert( 'container= ' + container.offsetWidth 
    		+ ', flym= ' + this.flym.offsetWidth 
    		+ ', a[1]= ' + this.mlnk[1].offsetWidth
    		+ ', div[1]= ' + this.mitm[1].offsetWidth    
			+ ', divBottom= ' + divBottom.offsetWidth);    
*/
    fm.hide();
    
	this.ready = true;
  },
  
  executeTarget: function( event, xx )
  {
		var itemnlink = this.navmenu[xx];
// itemnlink[0] - menu item title
//          [1] - tip/pop title
//          [2] - control : url, urlpar, js, jsm
//                  url - transition by url 
//                  urlpar - transition by url with constant parameters
//                  js  - transition by global javascript function with constant parameters
//                  jsm - transition by global javascript function with one modified parameter   
//          [3]	- url || js function || js function with $ tag ( will be replaced by parameter)
		if( itemnlink[2] == 'url' )
		{
      window.location.href= itemnlink[3];
    }
		 
		if( itemnlink[2] == 'urlpar' )
		{
      window.location.href= itemnlink[3] + '?param=' + this.parameter;
		}

		if( itemnlink[2] == 'js' )
		{
		  eval( itemnlink[3] );
		}
		
		if( itemnlink[2] == 'jsm' )
    {
      var str = new String( itemnlink[3] );
      var subStr = str.split( "$" );
      eval( subStr[0] + this.parameter + subStr[1] );
    }
  },

  setWidth: function( w )
  {
    if( w <= minWidth )
      w = minWidth;
	this.flym.show();
	var wf = this.flym.offsetWidth;
	if( wf < w )
	{
	  wf = w;
	}  
//	  if( wf > maxWidth )
//	  {
//	    wf = maxWidth;
//	  }  
    this.flym.style.width = wf + 'px';
	this.menuBottom.style.width = (wf - 16) + 'px';
//	alert(w + ', ' + wf + ', ' + this.flym.offsetWidth);
	this.flym.hide();
  }
  
}
