/*
 * vegUI is copyright (c) 2005 Stefan Pratter
 *
 * This file is part of vegUI
 *
 * vegUI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * vegUI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with vegUI; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*****************************************************************************
 * G L O B A L S 
 ****************************************************************************/

VEGUIOBJ[VUI_MENU] = VegUIMenu;
VEGUIOBJ[VUI_MENU_ITEM] = VegUIMenuItem;

/*****************************************************************************
 * V E G U I  M E N U 
 ****************************************************************************/

/************************************************/
/** Constructor */

function VegUIMenu(refName, Parent, Manager) {
  this.constructor = VegUINode;
  this.constructor(refName, Parent, Manager);
  
  this.type = VUI_MENU;
  this.itemIdx = 0;
  this.noAutoShow = 1;
  this.Items = this.I = [];
  this.Table = this.add_child('Table', VUI_NODE, 'TABLE');
  this.Tbody = this.Table.add_child('Tbody', VUI_NODE, 'TBODY');
  this.TRow = this.add_child('TRow', VUI_MENU_ITEM); 
  this.Table.T.pos = this.TRow.T.pos = this.Tbody.T.pos = 'static';
  this.T.closeTime = 2500;  

  this.RootMenu = this;
  this.ParentMenu = null;
  this.ChildMenu = [];
  this.FriendMenu = [];

  /* Flag TRow as template so it doesnt get buildt */
  this.TRow.flags |= VUI_TEMPLATE;
  this.flags |=  VUI_HMOUSE_OUT | VUI_HMOUSE_OVER;

  this.States[VUI_MOUSE_OVER].Scripts.add(
    function(argArr) { argArr[0].isActive = true; }
  );

  this.States[VUI_MOUSE_OUT].Scripts.add(
    function(argArr) {
      var Obj = argArr[0];
      var toE = Obj.toE; 
      if(!has_parent(toE, Obj.Base)) {
        Obj.isActive = false;
        Obj.RootMenu.init_close();
      }
    }
  );

  this.build_menu = vuimenu_build;
  this.set_menu = vuimenu_set;
  this.add_item = vuimenu_add_item;
  this.popup = vuimenu_popup;
  this.link_menu = vuimenu_link_menu;
  this.init_close = vuimenu_init_close;
  this.close = vuimenu_close;
  this.get_open = vuimenu_get_open;
  this.get_tail = vuimenu_get_tail;
  this.befriend = vuimenu_befriend;
  this.close_friends = vuimenu_close_friends;
  
  this.build = this.build_menu;
  this.set = this.set_menu; 
}
VegUIMenu.prototype = VegUINode;

/************************************************/
/** VegUIMenu.set(): Sets menu template
  * properties 
  */

function vuimenu_set(x, y, w, closeTime, cN, arrowImg) {
  this.set_node(null, w, 100, x, y);
  if(closeTime)
    this.T.closeTime = closeTime;
  if(cN)
    this.T.className = cN;
  if(arrowImg)
    this.T.arrowImg = arrowImg;
}

/************************************************/
/** VegUIMenu.build(): Build menu */

function vuimenu_build(toNode) {
  if(!this.build_node())
    return null;
  
  this.Table.Base.setAttribute('cellSpacing', 0);
  this.Table.Base.setAttribute('cellPadding', 0);
  this.Table.Base.setAttribute('width', '100%');
  this.closeTime = this.T.closeTime;
  this.arrowImg = this.T.arrowImg;
  this.hide(1);
  this.resize(null, this.TRow.T.h * this.itemIdx);
  this.dock(toNode);
  return 1;
}

/************************************************/
/** VegUIMenu.add_item(): adds item */

function vuimenu_add_item(text, script, lIcon) {
  var _idx = this.itemIdx++;
  var item = this.Tbody.add_child(('Item' + _idx), VUI_MENU_ITEM);
  item.clone(this.TRow);
  item.set(text, script, lIcon);
  item.flags ^= VUI_TEMPLATE;
  this.I[_idx] = item;
}

/************************************************/
/** VegUIMenu.link_menu(): links a child menu
  * to an item, making the menu popup when the
  * user mouses over the item 
  */

function vuimenu_link_menu(itemIdx, Menu) {
  this.I[itemIdx].States[VUI_MOUSE_OVER].Scripts.add(
    function(argArr) { Menu.popup(); }
  );
  if(this.arrowImg) {
    var img = imgnode(this.arrowImg);
    this.I[itemIdx].RCell.clear(img);
  }
  Menu.ParentMenu = this;
  Menu.RootMenu = this.RootMenu;
  Menu.ParentItem = this.I[itemIdx];
  this.ChildMenu.push(Menu);
}

/************************************************/
/** VegUIMenu.popup(): shows the menu */

function vuimenu_popup() {
  if(this.ParentMenu) {
    this.move(this.ParentMenu.x2() + 3, this.ParentMenu.y() + this.ParentItem.Base.offsetTop);
  }
  this.RootMenu.close_friends();
  this.hide(0);
  //this.CloseTimer = setTimeout(this.refName + ".close();", 5000); 
}

/************************************************/
/** VegUIMenu.init_close(): starts a timer that 
  * closes the menu after the time ran out
  */

function vuimenu_init_close() {
  if(this.CloseTimer) {
    clearTimeout(this.CloseTimer)
    this.CloseTimer = null;
  }
  
  this.CloseTimer = setTimeout(this.refName + ".close();", this.closeTime); 
}

/************************************************/
/** VegUIMenu.close(): closes the menu and all 
  * open child menus
  * aborts at an active menu (mouse is over it)
  */

function vuimenu_close() {
  var p = this.RootMenu.get_tail();
  while(p) {
    if(p.isActive)
      return;
    p.hide(1);
    p = p.ParentMenu;
  }
}

/************************************************/
/** VegUIMenu.get_open(): returns
  * the child menu that is currently open
  */

function vuimenu_get_open() {
  var c;
  for(c in this.ChildMenu) {
    if(!this.ChildMenu[c].is_hidden()) {
      return this.ChildMenu[c];
    }
  }
  return null;
}


/************************************************/
/** VegUIMenu.get_tail(): returns the menu
  * on the end of the open child menu chain 
  */

function vuimenu_get_tail() {
  var c;
  if((c = this.get_open())) {
    return c.get_tail();
  }
  return this;
}

/************************************************/
/** VegUIMenu.befriend(): befriends several
  * menus with each other making sure you 
  * can only have one of them open at the
  * same time
  */

function vuimenu_befriend(FMArr, noSpread) {
  var i;
  this.FriendMenu.length = 0;
  for(i in FMArr) {
    if(FMArr[i] != this) {
      this.FriendMenu.push(FMArr[i]);
      if(!noSpread)
        FMArr[i].befriend(FMArr, true);
    }
  }
}

/************************************************/
/** VegUIMenu.close_friends(): closes friend
  * menus
  */

function vuimenu_close_friends() {
  var i;
  for(i in this.FriendMenu)
    this.FriendMenu[i].close();
}

/*****************************************************************************
 * V E G U I  M E N U  I T E M
 ****************************************************************************/

/************************************************/
/** VegUIMenuItem Constructor */

function VegUIMenuItem(refName, Parent, Manager) {
  this.constructor = VegUIButton;
  this.constructor(refName, Parent, Manager);
  
  this.LCell = this.add_child('LCell', VUI_NODE, 'TD', 'static');
  this.MCell = this.add_child('MCell', VUI_NODE, 'TD', 'static');
  this.RCell = this.add_child('RCell', VUI_NODE, 'TD', 'static');

  this.T.pos = 'static';
  this.type = VUI_MENU_ITEM; 
  this.flags |= VUI_HMOUSE_OVER | VUI_HMOUSE_OUT;

  this.build_mnuitem = vuimnuitem_build;
  this.set_mnuitem = vuimnuitem_set;
  this.build = this.build_mnuitem;
  this.set = this.set_mnuitem;
}
VegUIMenuItem.prototype = VegUIButton;

/************************************************/
/** VegUIMenuItem.set_mnuitem() */

function vuimnuitem_set(text, script, lIcon, rIcon, h, lIconW, rIconW, nClass, hClass) {
  this.set_node('TR', null, h);
  if(lIconW != null) this.T.lIconW = lIconW;
  if(rIconW != null) this.T.rIconW = rIconW;
  if(hClass != undefined) this.States[VUI_MOUSE_OVER].P.className = hClass;
  if(nClass != undefined) this.States[VUI_MOUSE_OUT].P.className = nClass;
  if(script) this.States[VUI_MOUSE_DOWN].Scripts.add(script);
  if(lIcon != undefined) this.T.lIconImg= lIcon;
  if(rIcon != undefined) this.T.rIconImg = rIcon;
  if(text) this.T.label = text;
}

/************************************************/
/** VegUIMenuItem.build_mnuitem() */

function vuimnuitem_build(toNode) {
  if(!this.build_node())
    return null;

  if(this.T.lIconImg)
    this.LCell.clear(imgnode(this.T.lIconImg));

  this.MCell.clear(document.createTextNode(this.T.label));  

  if(this.T.rIconImg)
    this.RCell.clear(imgnode(this.T.rIconImg));

  this.LCell.resize(this.T.lIconW);
  this.RCell.resize(this.T.rIconW);


  this.dock(toNode);
}
