var SHIMGIF = "/ve/res/images/shim.gif";
var VEPopupMenu_DefaultStyler = new Object();
var VEPopupMenu_initialized = false;

//VEPopupMenu_DefaultStyler.menuStyle = 'background-color: #CCCCCC; font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-style: normal; border: 1px solid #000000;';
VEPopupMenu_DefaultStyler.menuStyle = "background-color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-style: normal; border: 1px solid #000000;";
VEPopupMenu_DefaultStyler.itemRowStyle = '';
VEPopupMenu_DefaultStyler.itemLabelStyle = '';
VEPopupMenu_DefaultStyler.itemRollOverStyle = 'background-color: #000088; color: #FFFFFF; cursor: pointer';
VEPopupMenu_DefaultStyler.horzSeparator = '/ve/res/images/hseparator.gif';
VEPopupMenu_DefaultStyler.vertSeparator = '/ve/res/images/vseparator.gif';
VEPopupMenu_DefaultStyler.checked = '<img src="/ve/res/images/checkmark.gif" />';
VEPopupMenu_DefaultStyler.notChecked = '<img width="7" height="7" src="' + SHIMGIF + '" />';
VEPopupMenu_DefaultStyler.subMenu = '<img src="/ve/res/images/submenu.gif" />';


function VEPopupMenuItem( label, action, icon, check )
  {
  this.label = label;
  this.action = action;
  this.icon = icon;
  this.checked = check;

  this.selected = function()
    {
    if( this.checked )
      this.setChecked( !this.checked );

    if( this.action && this.action.indexOf( "javascript:" ) == 0 )
      {
      var item = this;
      eval( this.action.substring( 11 ) );
      }
    else if( this.action )
      document.location.href = this.action;
    }

  this.setChecked = function( checked )
    {
    this.checked = checked;
    return this;
    }
  }

function VEPopupMenu( label, icon, action )
  {
  this.popup = new PopupWindow( "VEPopupMenuDiv" );
  this.popup.offsetY = 22;
  this.invalidated = true;
  this.items = new Array();
  this.label = label;
  this.icon = icon;
  this.action = action;
  this.styler = VEPopupMenu_DefaultStyler;
  this.columnCount = 1;
  this.timer = null;

  this.show = function( anchor )
    {
    if( this.invalidated )
      this.refresh();

    VEPopupMenu_setOpenMenu( this );
    var bounds = vepositions_getBounds( anchor );
    var page = vepositions_getPageSize();

    if( bounds.x + this.menuSize.w > page.w )
      this.popup.offsetX = page.w - this.menuSize.w - bounds.x - this.menuSize.w/2 + 20;

    this.attachEventListeners();

    var allowFilters = browser.isIE55 || browser.isIE6;  
    var popupDiv = getTag( "VEPopupMenuDiv" );
    
  	if( false && allowFilters && popupDiv.filters && popupDiv.filters[0] )
      popupDiv.filters[0].Apply();     this.popup.showPopup( anchor.id );

  	if( false && allowFilters && popupDiv.filters && popupDiv.filters[0] )
      popupDiv.filters[0].Play();

    this.startTimer();
    }

  this.showAsSubmenu = function()
    {
    alert( "pop as submenu" );
    }

  this.showAt = function( x, y )
    {
    }

  this.selectItem = function( item )
    {
    this.selectedItem = this.items[ item ];
    this.hide();

    if( this.selectedItem && this.selectedItem.selected )
      this.selectedItem.selected();
    }

  this.hide = function()
    {
    this.detachEventListeners();
    this.popup.hidePopup();
    this.clearTimer();
    this.invalidated = true;
    VEPopupMenu_hideOpenMenu();
    }

  this.toHTML = function()
    {
    var output = "<table cellpadding='1' cellspacing='0' border='0' width='100%' style='" + this.styler.menuStyle + "' id='myTable' veMenu='true'>";
    var styler = this.styler;

    for( var i = 0; i < this.items.length; i++ )
      {
      var item = this.items[ i ];

      if( item == VEPopupMenu.Separator )
        {
        output += "<tr><td colspan='" + this.columnCount + "' width='100%' ";
        output += "style='background: url(" + styler.horzSeparator + ") repeat-x;'>";
        output += "<img height='2' width='1' src='" + SHIMGIF + "' /></td></tr>";
        }
      else
        {
        output += "<tr item='" + i + "' ";

        if( item.items )
          output += "submenu='true' ";

        output += "onmouseover='VEPopupMenu_rollover( this )' style='" + this.styler.itemRowStyle + "'>";

        if( this.showCheckmarks )
          output += "<td>" + ( item.checked ? styler.checked : styler.notChecked ) + "</td>";

        if( this.showIcons )
          output += "<td><img src='" + ( item.icon ? item.icon : SHIMGIF ) + "' /></td>";

        output += "<td nowrap style='" + styler.itemLabelStyle + "' onmousedown='VEPopupMenu_Click( " + i + " )'>" + item.label + "</td>";

        if( this.showSubItems )
          output += "<td>" + ( item.items ? styler.subMenu : "<img src='" + SHIMGIF + "' />" ) + "</td>";

        output += "</tr>";
        }
      }

    output += "</table>";
    return output;
    }

  this.refresh = function()
    {
    var html = this.toHTML();
    this.menuSize = this.measure( html );
    this.popup.populate( html );
    //this.popup.offsetX = - size.x;
    this.invalidated = this.showCheckmarks;
    }

  this.add = function( item )
    {
    this.items[ this.items.length ] = item;

    if( item.icon )
      {
      this.showIcons = true;
      this.columnCount++;
      }

    if( item.check )
      {
      this.showCheckmarks = true;
      this.columnCount++;
      }

    if( item.items )
      {
      this.showSubItems = true;
      this.columnCount++;
      }

    item.parent = this;
    }

  this.addSeparator = function()
    {
    if( VEPopupMenu.Separator == null )
      VEPopupMenu.Separator = new Object();

    this.items[ this.items.length ] = VEPopupMenu.Separator;    
    }

  this.measure = function( html )
    {
    var d = getTag( "VEPopupMenuDiv" );
    d.innerHTML = html;
    //var size = vepositions_getSize( getTag( 'myTable' ) );
    var size = vepositions_getSize( getTag( 'VEPopupMenuDiv' ) );
    return size;
    }

  this.isMyAncestor = function( anotherMenu )
    {
    var p = this.parent;

    while( p != null && p != anotherMenu )
      p = p.parent;

    return p != null;
    }
    
  this.attachEventListeners = function()
    {
    var popupDiv = getTag( "VEPopupMenuDiv" );
    
    if( document.all )
      {
      document.body.attachEvent( "onmousedown", VEPopupMenu_hideAllOpenMenus );
      popupDiv.attachEvent( "onmouseover", VEPopupMenu.OpenMenu.clearTimer );
      popupDiv.attachEvent( "onmouseout", VEPopupMenu.OpenMenu.startTimer );
      }
    else
      {
      document.body.addEventListener ( "mousedown", VEPopupMenu_hideAllOpenMenus, false );
      popupDiv.addEventListener( "mouseover", VEPopupMenu.OpenMenu.clearTimer, false );
      popupDiv.addEventListener( "mouseout",  VEPopupMenu.OpenMenu.startTimer, false );   
      }
    }
    
  this.detachEventListeners = function()
    {
    var popupDiv = getTag( "VEPopupMenuDiv" );
    
    if( document.all )
      {
      document.detachEvent( "onmousedown", VEPopupMenu_hideAllOpenMenus );
      popupDiv.detachEvent( "onmouseover", VEPopupMenu.OpenMenu.clearTimer );
      popupDiv.detachEvent( "onmouseout", VEPopupMenu.OpenMenu.startTimer );
      }
    else
      {
      document.removeEventListener( "mousedown", VEPopupMenu_hideAllOpenMenus, false );
      popupDiv.removeEventListener( "mouseover", VEPopupMenu.OpenMenu.clearTimer, false );
      popupDiv.removeEventListener( "mouseout", VEPopupMenu.OpenMenu.startTimer, false );   
      }
    }  
    
  this.startTimer = function()
    {
    if( this.timer == null )
		  this.timer = window.setTimeout( "VEPopupMenu_hideAllOpenMenus()",	4000 );
    } 
  
  this.clearTimer = function()
    {
    if( this.timer != null )
      {
		  window.clearTimeout( this.timer );
		  this.timer = null;
		  }
    }    
  }

function VEPopupMenu_rollover( row )
  {
  if( VEPopupMenu_rollover.lastRow != null )
    {
    VEPopupMenu_rollover.lastRow.style.cssText = VEPopupMenu_rollover.lastRowStyle;
 
    if( VEPopupMenu_rollover.lastRow.submenu )
      VEPopupMenu_getMenuItem( VEPopupMenu_rollover.lastRow.item ).hide();
    }

  var openMenu = VEPopupMenu_getOpenMenu();
  
  if( openMenu )
    {
    VEPopupMenu_rollover.lastRowStyle = ( VEPopupMenu_rollover.lastRow = row ).style.cssText;
    row.style.cssText = openMenu.styler.itemRollOverStyle;

    if( row.submenu )
      VEPopupMenu_getMenuItem( row.item ).showAsSubmenu();
      
    openMenu.clearTimer();  
    }
  }

function VEPopupMenu_getOpenMenu()
  {
  return VEPopupMenu.OpenMenu;
  }

function VEPopupMenu_setOpenMenu( menu )
  {
  if( VEPopupMenu.OpenMenu != null )
    if( menu.isMyAncestor( VEPopupMenu_getOpenMenu.menu ) )
      {
      if( ! VEPopupMenu.OpenMenuStack )
        VEPopupMenu.OpenMenuStack = new Array();

      VEPopupMenu.OpenMenuStack[ VEPopupMenu.OpenMenuStack.length ] = VEPopupMenu.OpenMenu;
      }
    else
      VEPopupMenu_hideAllOpenMenus();

  VEPopupMenu.OpenMenu = menu;
  }

function VEPopupMenu_hideOpenMenu()
  {
  if( VEPopupMenu.OpenMenuStack && VEPopupMenu.OpenMenuStack.length > 0 )
    {
    VEPopupMenu.OpenMenu = VEPopupMenu.OpenMenuStack[ VEPopupMenu.OpenMenuStack.length - 1 ];
    VEPopupMenu.OpenMenuStack = VEPopupMenu.OpenMenuStack.slice( 0, VEPopupMenu.OpenMenuStack.length - 1 );
    }
  else
    VEPopupMenu.OpenMenu = null;
  }

function VEPopupMenu_hideAllOpenMenus()
  {
  while( VEPopupMenu.OpenMenu != null )
    {
    VEPopupMenu.OpenMenu.hide();
    }
  }

function VEPopupMenu_getMenuItem( item )
  {
  return VEPopupMenu_getOpenMenu().items[ parseInt( item ) ];
  }

function VEPopupMenu_Click( item )
  {
  VEPopupMenu_getOpenMenu().selectItem( item );
  }

function VEPopupMenuInit()
  {
  //document.writeln( "<div id='VEPopupMenuDiv' style='width:100px;position:absolute; visibility:hidden;filter: progid:DXImageTransform.Microsoft.Fade(Overlap=1.00 duration=1.0 enabled=1) progid:DXImageTransform.Microsoft.Shadow(color='#666666', Direction=135, Strength=3 enabled=0) progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true' enabled=1);'></div>" );
  if( !VEPopupMenu_initialized )
    {
    document.writeln( "<div id='VEPopupMenuDiv' style='width:80px;position:absolute; visibility:hidden;filter: progid:DXImageTransform.Microsoft.Fade(Overlap=0.50 duration=0.5 enabled=1)'></div>" );
    VEPopupMenu_initialized = true;
    }
  }
