/////////////////////////////////
//  Componet "Tab menu " v3.0
//  Author: Artiom Vendelev
//  Create: 13 Nov 2006
//  Modified: -
//  Notes: Don't rename this file!
/////////////////////////////////

function tabMenu(container, dataInfo, direction, toolbar) {
  var me     = this;
  me._cont   = container;
  me._direct = direction;
  me._dataA  = dataInfo;
  me._dataI  = {}
  me._curTab = '';
  me._ifr    = null;
  me._path   = '/img/toolbar/small/';
  me._table  = null;
  me._navig  = null;
  me._scroll = 20;
  me._defaultButtonColor     = 'buttonface';
  me._highlightButtonColor   = 'threedhighlight';
  me._threedButtonColor      = 'threedshadow';
  me._highlightSelectedColor = 'buttonface';
  if (typeof(top.getColors)=='function') _setColor(top.getColors());


/////////////////////////////////
  if (me._direct!='Hor') {
    me._init      = _initV;
    me._add       = _addV;
    me._onResize  = _onWindowResizeV;
  } else {
    me._init      = _initH;
    me._add       = _addH;
    me._onResize  = _onWindowResizeH;
  }
  
  me.kill         = _kill;
  me.init         = me._init;
  me.setColor     = _setColor;
  me.addTab       = _addTab;
  me.removeTab    = _removeTab;    // { {} [,{}] }
  me.selectTab    = _selectTab;    // id 
  me.deSelectTab  = _deSelectTab;  // id 

  me._cont.onclick= _onClickTab;

  me.onSelect       = null;
  me.onBeforeSelect = null;    // id 
  me.getDataInfo    = function () {return me._dataI};


/////////////////////////////////
  function _initH() {
    me._cont.innerHTML = [
      '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" class="tab_container tab_h">',
      '<TR valign=top><TD style="height:100%;border-top: 1px solid ',me._threedButtonColor,';background-color:white;"></TD></TR>',
      '<TR><TD style="height:2px;font-size:0px;background-color:', me._highlightSelectedColor, '">&nbsp;</TD></TR>',
      '<TR><TD style="height:30px;border-top: 1px solid ',me._threedButtonColor,
      ';background-color:', me._defaultButtonColor, '" valign=top><DIV class="div_hidden_h"></DIV>',
      '<DIV align=center class=div_navigate_h>',
      '<IMG src="',me._path,'left.gif" type=navigate alt="<" id="lf_">',
      '<IMG src="',me._path,'right.gif" type=navigate alt=">" id="rt_">',
      '</DIV></TD></TR>',
      '</TABLE>'    ].join('');
    me._ifr = me._cont.childNodes[0].childNodes[0].childNodes[0].childNodes[0];
    me._ifr.style.display = 'none';
    var trs = me._cont.childNodes[0].childNodes[0].childNodes;
    me._table = trs[trs.length-1].childNodes[0].childNodes[0].appendChild(document.createElement('TABLE'));
    me._table.cellPadding = 0;
    me._table.cellSpacing = 0;
    me._table.border = 0;
    me._table.style.height = '28px';
    me._menu = me._table.insertRow(me._table.rows.length);
    me._menu.vAlign= 'top';
    
    me._navig = trs[trs.length-1].childNodes[0].childNodes[1];
    me._navig.onclick = _onScroll;
    _setBorderDefault(me._navig);
    _initScrollBarH();
    _addTab(me._dataA);
  }
  function _initV() {
    me._cont.innerHTML = [
      '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" class="tab_container tab_v">',
      '<TR valign=top>',
      '<TD style="width:28px;background-color:', me._defaultButtonColor, '" nowrap>',
      '<DIV align=right class="div_hidden_V"></DIV>',
      '<DIV class=div_navigate_v align=center>',
      '<IMG src="',me._path,'up.gif" type=navigate alt="<" id="tp_"><BR>',
      '<IMG src="',me._path,'down.gif" type=navigate alt=">" id="bt_"></DIV></td>',
      '<TD style="width:2px;border-left:1px solid ',me._highlightButtonColor,';font-size:10px;background-color:', me._highlightSelectedColor, '">&nbsp;</TD>',
      '</TR>',
      '</TABLE>'    ].join('');
    var trs = me._cont.childNodes[0].childNodes[0].childNodes[0].childNodes;
    me._menu= trs[0].childNodes[0].appendChild(document.createElement('TABLE'));
    me._menu.cellPadding = 0;
    me._menu.cellSpacing = 0;
    me._menu.border = 0;
    me._menu.style.width = '28px';
    me._table = me._menu;
    me._navig = trs[0].childNodes[1];
    me._navig.onclick = _onScroll;
    _setBorderDefault(me._navig);
    _initScrollBarV();
    _addTab(me._dataA);
    trs = null;
  }
  function _addTab(addobj, upd) {
    if (!addobj) return null;
    var ii, id = '';
    for (ii in addobj) {
      if (me._dataI==null) me._dataI = {}
      if (me._dataI[ii]) {
        if (upd) _updateTab(addobj[ii]);
        id = me._dataI[ii].id;
      } else {
        me._dataI[ii] = addobj[ii];
        me._add(ii);
        id = addobj[ii].id;
      }
    }
    me._onResize();
    return id;
  }
  function _addV(id) {
    var row = me._menu.insertRow(me._menu.rows.length);
    row.vAlign= 'top';
    var chl = row.insertCell(row.cells.length);
    chl.noWrap = true;
    chl.align = 'right';
    chl = chl.appendChild(document.createElement('DIV'));
    chl.className = 'tab_menu3_v';
    chl.id = id;
    _setBorderDefault(chl);
    var img = '';
    if (me._dataI[id].img) img = '<IMG id="img_'+me._dataI[id].id+'" src="'+me._path+me._dataI[id].img+'" alssign=left>';
    chl.innerHTML = img;
    row = chl = null;
  }
  function _addH(id) {
    var chl = me._menu.insertCell(me._menu.cells.length);
    chl.noWrap = true;
    chl = chl.appendChild(document.createElement('DIV'));
    chl.className = 'tab_menu3';
    chl.id = id;
    _setBorderDefault(chl);
    var img = '';
    if (me._dataI[id].img) img = '&nbsp;<IMG id="img_'+me._dataI[id].id+'" src="'+me._path+me._dataI[id].img+'" align=absmiddle>';
    chl.innerHTML = img+' '+me._dataI[id].txt+'  ';
    chl = null;
  }
  function _removeTab(remobj) {
    var ii, elm, par, id;
    for (ii in remobj) {
      if (!me._dataI[ii]) continue;
      if (me._direct=='Hor') {
        if (elm = document.getElementById(ii)) {
          elm = elm.parentNode;
          /*if (me._curTab == ii) {
            id = elm.previousSibling.childNodes[0].id;
            _selectTab(id)
            document.getElementById('ifr_' + id).focus();
          }*/
          par = elm.parentNode;
          par.deleteCell(elm.cellIndex);
        }
      } else {
        if (elm = document.getElementById(ii)) {
          elm = elm.parentNode.parentNode;
          /*if (me._curTab == ii) {
            id = elm.previousSibling.childNodes[0].childNodes[0].id;
            _selectTab(id)
            document.getElementById('ifr_' + id).focus();
          }*/
          par = elm.parentNode.parentNode;
          par.deleteRow(elm.rowIndex);
        }
      }
      elm = document.getElementById('ifr_' + ii);
      if (elm) {
        var tmp = null;
        if (elm.contentWindow &&
            elm.contentWindow.document &&
            elm.contentWindow.document.body &&
            typeof(elm.contentWindow.document.body.onbeforeunload) == 'function') {
          tmp = elm.contentWindow.document.body.onbeforeunload.toString()
          elm.contentWindow.document.body.onbeforeunload = null;
          tmp = tmp.replace('function anonymous()\n\{\n', '')
          tmp = tmp.replace('\(\)\n\}', '')
          elm.contentWindow[tmp]();
        }
        if (elm.contentWindow &&
            elm.contentWindow.document &&
            elm.contentWindow.document.body &&
            typeof(elm.contentWindow.document.body.onunload) == 'function') {
          tmp = elm.contentWindow.document.body.onunload.toString()
          elm.contentWindow.document.body.onunload = null;
          tmp = tmp.replace('function anonymous()\n\{\n', '')
          tmp = tmp.replace('\(\)\n\}', '')
          elm.contentWindow[tmp]();
        }

        par = elm.parentNode;
        par.removeChild(elm);
      }
      delete me._dataI[ii];
    }
    elm = par = tmp = null;
    //_showScroll();
  }
  function _selectTab(idx) {
    if (me._curTab == idx) return 'ifr_' + me._curTab;
    if (me._curTab!='') _deSelectTab(me._curTab);
    me._curTab = idx;
    var ids  = 'ifr_' + me._curTab;
    
    if (typeof(me.onBeforeSelect)=='function') me.onBeforeSelect(me._dataI[me._curTab])
    if (top.jsdump) top.jsdump.dump(top.PUB_copyObj(me._dataI[me._curTab]), null, 'TabMenu select tab ' + me._curTab);
    
    var el = document.getElementById(me._curTab);
    if (me._direct=='Hor') el.className = 'tab_menu2'; else el.className = 'tab_menu2_v';
    _setBorderSelected(el);
    
    if (typeof(me.onSelect)=='function') me.onSelect(ids);
    //_showScroll();
    el = null;
    return ids
  }
  function _deSelectTab(idx) {
    var el = document.getElementById(me._curTab);
    if (me._direct=='Hor') el.className = 'tab_menu3'; else el.className = 'tab_menu3_v';
    _setBorderDefault(el);
    el = null;
  }
  function _updateTab(obj) {
    if (!obj) return;
    if (typeof(obj.pth) != 'undefined' && me._dataI[obj.id].pth != obj.pth) {
      me._dataI[obj.id].pth = obj.pth;
      if (typeof(me.onBeforeSelect)=='function') me.onBeforeSelect(me._dataI[obj.id])
    }
    if (obj.txt) {
      var ttt = document.getElementById(obj.id);
      ttt.childNodes[ttt.childNodes.length-1].nodeValue = ' '+obj.txt+'  ';
      ttt = null;
    }
  }
  function _onClickTab(e) {
    var res = {ev: null, el: null};
    if (e) {
      res.ev = e;
      res.el = res.ev.target;
    } else {
      if (window.event) {
        res.ev = window.event;
        res.el = res.ev.srcElement;
      }
    }
    if (!res.el.id || res.el.id == me._curTab) return;
    if (res.el.tagName=='IMG') res.el = res.el.parentNode;
    if (res.el.className.indexOf('tab_menu')!=-1) _selectTab(res.el.id);
    res = null;
  }
  function _initScrollBarH() {
    me._resize = window.onresize;
    window.onresize = _onWindowResizeH;
  }
  function _onWindowResizeH() {
    var tmp = me._table.parentNode;
    if (tmp.scrollWidth>tmp.offsetWidth) {
      me._navig.style.display = 'block';
    } else {
      me._navig.style.display = 'none';
    }
    if (typeof(me._resize)=='function') me._resize();
    tmp = null;
  }
  function _initScrollBarV() {
    me._resize = window.onresize;
    window.onresize = _onWindowResizeV;
  }
  function _onWindowResizeV() {
    var tmp = me._table.parentNode;
    if (tmp.scrollHeight>tmp.offsetHeight) {
      me._navig.style.display = 'block';
    } else {
      me._navig.style.display = 'none';
    }
    if (typeof(me._resize)=='function') me._resize();
    tmp = null;
  }
  function _onScroll(e) {
    var res = {ev: null, el: null};
    if (e) {
      res.ev = e;
      res.el = res.ev.target;
    } else {
      if (window.event) {
        res.ev = window.event;
        res.el = res.ev.srcElement;
      }
    }
    if (res.el.tagName!='IMG') return;
    switch (res.el.id) {
      case 'tp_':
        _onScrollB();
        break;
      case 'bt_':
        _onScrollT();
        break;
      case 'lf_':
        _onScrollR();
        break;
      case 'rt_':
        _onScrollL();
        break;
    }
    res = null;
  }
  function _onScrollL() {
    var dv = me._table.parentNode;
    if (dv.scrollLeft == dv.scrollWidth) return;
    if ((dv.scrollLeft+me._scroll) <= dv.scrollWidth) {
      dv.scrollLeft+= me._scroll;
    } else dv.scrollLeft = dv.scrollWidth;
    dv = null;
  }
  function _onScrollR() {
    var dv = me._table.parentNode;
    if (dv.scrollLeft == 0) return;
    if ((dv.scrollLeft-me._scroll) > 0) {
      dv.scrollLeft-= me._scroll;
    } else dv.scrollLeft = 0;
    dv = null;
  }
  function _onScrollT() {
    var dv = me._table.parentNode;
    if (dv.scrollTop == dv.scrollHeight) return;
    if ((dv.scrollTop+me._scroll) <= dv.scrollHeight) {
      dv.scrollTop+= me._scroll;
    } else dv.scrollTop = dv.scrollHeigth;
    dv = null;
  }
  function _onScrollB() {
    var dv = me._table.parentNode;
    if (dv.scrollTop == 0) return;
    if ((dv.scrollTop-me._scroll) > 0) {
      dv.scrollTop-= me._scroll;
    } else dv.scrollTop = 0;
    dv = null;
  }
  function _setBorderDefault(chl) {
    chl.style.borderBottom = "1px solid " + me._threedButtonColor;
    chl.style.borderLeft   = "1px solid " + me._highlightButtonColor;
    chl.style.borderTop    = "1px solid " + ((me._direct=='Hor') ? me._defaultButtonColor: me._highlightButtonColor);
    chl.style.borderRight  = "1px solid " + ((me._direct=='Hor') ? me._threedButtonColor : me._defaultButtonColor);
    chl.style.backgroundColor = me._defaultButtonColor;
  }
  function _setBorderSelected(chl) {
    chl.style.borderBottom    = "2px solid " + me._threedButtonColor;
    chl.style.borderLeft      = "2px solid " + me._highlightButtonColor;
    chl.style.borderTop       = "2px solid " + ((me._direct=='Hor') ? me._highlightSelectedColor : me._highlightButtonColor);
    chl.style.borderRight     = "2px solid " + ((me._direct=='Hor') ? me._threedButtonColor : me._highlightSelectedColor);
    chl.style.backgroundColor = me._highlightSelectedColor;
  }
  function _setColor(clrs) {
    me._defaultButtonColor   = clrs.defaultButtonColor;
    me._highlightButtonColor = clrs.highlightButtonColor;
    me._threedButtonColor    = clrs.threedButtonColor;
    me._highlightSelectedColor = clrs.highlightSelectedColor;
  }
  function _kill() {
    window.onresize = me._resize;
    me._cont.onclick  = null;
    me._navig.onclick = null;
    while (me._cont.firstChild) me._cont.removeChild(me._cont.firstChild);
    for (var ii in me) me[ii] = null;
    me = null;
  }
}