// menu kontekstowe dla ui:ctx-menu

// Copright (c) 2004 Efigence Sp. z o.o.
// Jarosław Zimon

// dynamicznie tworzy z tablicy MenuItem elementy do wyswietlenia
// klasa PopupMenu daje 1 menu - moze istniec wiele menu
// nie sa implementowane podmenu

// name - String - nazwa wyswietlana
// active - boolean - czy akcja ma byc dostepna (czy wyszarzona)
// barBelow - boolean - czy menuBar pod akcją

function MenuItem(name, active, link) {
  this.name = name;  
  this.active = active;
  this.link = link;
}

// wstawianie menu jeden raz przez write() do <div id="name" ktory musi juz istniec w dokumencie
// menu dynamicznie zmienia wyszarzanie oraz odpala akcje
// name - nazwa menu - potzreba w kilku miejscach
// className - predefiniowana nazwa styli (dodawane sa rozne suffixy) dla elementow - wszystkie podswietlania robione sa przez zmiane klasy
// width - dlugosc jest niezbedna do zrobienia divow
// items - tablica elementow do wyswietlenia

PopupMenu.prototype = new CustomLayer();

function PopupMenu(name, className, width, items) {

  CustomLayer.apply(this, [name + "Outer"]);
  
  this.name = name;
  this.className = className;
  this.width = width;
  this.inner = new CustomLayer(name + 'Inner');
  
  this.items = {};
  this.menuElements = new Object();
  
  var html = '<table border="1" onmouseout="PopupMenu.list[\''+name+'\'].setTimer()" onmouseover="PopupMenu.list[\''+name+'\'].clearTimer()" class="'+className+'Table">'; // dodaj poczatek
  
  for (var i = 0; i < items.length; i ++) {
    var item = items[i];
    this.menuElements[item.name] = item;
    var id ='_sl_'+PopupMenu.seq++;
    this.items[id] = item.name;
    this.items[item.name] = id;
    var menuName = 'PopupMenu.list[\''+ name +'\']';
    var handlers = 'onmouseover="'+menuName+'.mouseover(this, true)" onmouseout="'+menuName+'.mouseover(this, false)" onclick="'+menuName+'.click(this.parentNode)"';
    html += '<tr><td><div id="'+ id +'" style="width: '+ width +'"><a href="#" '+ handlers +'>'+ item.name +'</a></div></td></tr>'; 
  }
  
  html += '</table>'; //dodaj zakonczenie
  this.setContent(html);
  //efi_layerWrite(name + 'Inner', null, html);
  PopupMenu.list[name] = this;

  this.mouseover = function (obj, over) { 
    var name = this.items[obj.parentNode.id];  
    divObj = document.getElementById(obj.parentNode.id);
    divObj.className = this.className + 'Item' + (this.currentItems[name] ? '' : 'Dim') + (over ? 'Over' : '') ;
  }

  this.click = function (obj) { 
    var name = this.items[obj.id];  
    if (this.currentItems[name]) {
      this.current = name;
      if (this.menuElements[name]) {
        document.location = this.menuElements[name].link;
      } else {
        alert('no item');
      }
    }
  }

  this.clearTimer = function () { 
    if (this.timer != null) {
      clearTimeout(this.timer);
      this.timer = null;
    }
  }

  this.setTimer = function () { 
    if (this.timer != null) {
      clearTimeout(this.timer);
    }
    this.timer = setTimeout('PopupMenu.list[\''+this.name+'\'].hide(); PopupMenu.list[\''+this.name+'\'].timer = null', 300);
  }

  this.prepareActions = function(items) { 

    this.currentItems = {}; // sprawdzanie czy nie wyszarzony

    for(var i = 0; i < items.length; i ++) {
      var item = items[i];
      this.currentItems[item.name] = item.active;
      divId = this.items[item.name];
      divObj = document.getElementById(divId);  
      divObj.className = this.className + 'Item' + (item.active ? '' : 'Dim');
    }
  }

}

if (!PopupMenu.seq) PopupMenu.seq = 0;
PopupMenu.list = []; // lista wszystkich utworzonych menu na stronie 
