/**
 *  object to show a context menu
 *
 *  @author   Tobias Hettinger
 *  @version  $Id: contextmenu.js,v 1.11 2009/05/12 13:19:06 tobias Exp $
 */

 
/**
 *  constructor
 *
 *  @param string name           instance name of the context menu object
 *  @param string imgBaseUrl     image base url with a path separator at the end
 *  @param string baseClassName  base class name of the context menu div. Since the classes of the "inner" elements
 *                               are fixed, the base class has to be used to choose a context menu style
 *  @param int    width          width of the context menu
 *  @param string divId          id of the div for the menu (or null if the name should be automatically created)
 *                               the div with the passed id MUST NOT yet exist in the document
 */
var WATContextMenu = function(name, imgBaseUrl, baseClassName, width, divId)
{
  //  initialize the member variables
  this.name = name;
  this.divId = divId ? divId : name + 'div';
  this.imgBaseUrl = imgBaseUrl;
  this.baseClassName = baseClassName;
  this.width = width;
  this.offsetTop = 0;
  this.offsetLeft = 0;
  
  this.divEffects = null;
  this.menuItems = new Array();
  this.canAutoClose = false;
  this.eventsAdded = false;
  
  this.created = false;
  this.contentValid = false;
  this.initialized = true;
}


//=====================================================================================================================
//  object deletion
//=====================================================================================================================

/**
 *  function to remove the context menu from the document
 */
WATContextMenu.prototype.RemoveObject = function()
{
  //  remove the div
  if (this.divEffects) this.divEffects.RemoveDiv();
  //  invalidate the object
  this.initialized = false;
}


//=====================================================================================================================
//  private member variables
//=====================================================================================================================

/**
 *  function to create the context menu. This function is called once when the context menu is shown for the first time.
 *  The context menu does not expect the user to provide a div element but it creates one itself.
 */
WATContextMenu.prototype.__Create = function()
{
  //  create a new div
  var divElement = document.createElement('div');
  divElement.id = this.divId;
  divElement.className = this.baseClassName;
  divElement.style.position = 'absolute';
  divElement.style.display = 'none';
  divElement.style.zIndex = 9999;
  
  //  add the div to the document
  document.body.appendChild(divElement);
  this.divEffects = new WATDivEffects(this.name + '.divEffects', this.divId);
  this.created = true;
}

/**
 *  function to create the content. This function is automatically called before the context menu is shown
 *  if the current content is not valid
 */
WATContextMenu.prototype.__CreateContent = function()
{
  //  setup the style
  this.divEffects.divElement.style.width = this.width + 'px';
  this.divEffects.divElement.style.backgroundImage = 'url(' + this.imgBaseUrl + 'background.gif)';
  
  //  create the menu items
  var content = '';
  for (var name in this.menuItems)
  {
    //  get the current item
    var item = this.menuItems[name];
    
    //  create the icon
    iconSrc = this.imgBaseUrl + 'noicon.gif';
    if (item['icon'] != null) iconSrc = (item['icon'].indexOf('://') == -1 ? this.imgBaseUrl : '') + item['icon'];

    //  create the content
    content += '<a class="cm" href="' + item['href'] + '" ' + (item['target'] != null ? 'target="' +
               item['target'] + '"' : '') + ' onclick="' + this.name + '.Hide();"><img class="cm" src="' +
               iconSrc + '" />' + item['caption'] + '</a>';
  }
  
  //  apply the content
  watDivContent(this.divId, content);
  this.contentValid = true;
}


//=====================================================================================================================
//  user interface
//=====================================================================================================================

/**
 *  function to add a menu item
 *
 *  @param string name     name of the menu item which can be later used to access the item in order
 *                         to modify/delete the item
 *  @param string caption  caption of the menu item
 *  @param string icon     name of the icon (from the base url of the context menu or the full url)
 *                         or null (NOT '') if no item is set
 *  @param string href     reference of the item link
 *  @param string target   target of the link or null (NOT '') if no target should be used
 */
WATContextMenu.prototype.AddItem = function(name, caption, icon, href, target)
{
  //  do nothing if the object is not initialized
  if (!this.initialized) return false;
  
  //  create the menu item
  var menuItem = new Array();
  menuItem['caption'] = caption;
  menuItem['icon'] = icon;
  menuItem['href'] = href;
  menuItem['target'] = target;
  
  //  add the item to the menu
  this.menuItems[name] = menuItem;
  this.contentValid = false;
}

/**
 *  function to clear all context menu items
 */
WATContextMenu.prototype.ClearItems = function()
{
  //  do nothing if the object is not initialized
  if (!this.initialized) return false;
  
  //  reset the context menu items
  this.menuItems = new Array();
  this.contentValid = false;
}

/**
 *  function to show the context menu. The position depends on the mouse position and is automatically set
 */
WATContextMenu.prototype.Show = function()
{
  //  do nothing if the object is not initialized
  if (!this.initialized) return false;
  
  //  create the menu if it is not valid yet
  if (!this.created) this.__Create();
  if (!this.contentValid) this.__CreateContent();

  //  hide event handler
  var obj = this;
  function __Hide()
  {
    if (obj.canAutoClose)
    {
      obj.Hide();
      obj.eventsAdded = true;
    }
  }
  
  //  create the close event handlers
  if (!this.eventsAdded)
  {
    //  create the event handlers
    if (WATBrowserInfo.IsIE)
    {  
      //  ... for Internet Explorer
      document.attachEvent('onclick', __Hide);
      document.attachEvent('oncontextmenu', __Hide);
    }
    else
    {
      //  ... for other browsers
      document.addEventListener('click', __Hide, true);
      document.addEventListener('contextmenu', __Hide, true);
    }
  }

  //  show the context menu
  watDivPos(this.divId, watMousePosY + 10 + this.offsetTop - watMouseGetScrollTop(),
            watMousePosX + 10 + this.offsetLeft, -1, -1);
  this.canAutoClose = false;
  this.divEffects.FadeIn(100, 200);
  setTimeout(this.name + '.canAutoClose = true;', 1000);
}

/**
 *  function to hide the context menu
 */
WATContextMenu.prototype.Hide = function()
{
  this.divEffects.FadeOut(0, 200);
  this.canAutoClose = false;
}

