/*
*  Plugin adjust ZIndex and performs 'always on top' checks
* 
*  Distributed under the terms of the GNU Library General Public License
*
*  @package ddi (Drag'n'Drop Interface)
*  @version 1.1
*  @title ZIndex adjuster
*  @author Ilya Lebedev (ilya@lebedev.net), (c) 2005
*
*  @depends ArrayExtensions.js
*
*  Plugin performs check of 'alwaysOnTop' element property.
*
*  Revision history
*  v1.1
*  % execution moved to dragstart.after
*  + plugin group DDI_SYSTEM
*
*  v1.0
*  + first public release
*/
__DDI__.plugin.adjustZIndex = new function () {
  this.name = 'Adjust ZIndex';
  this.description = 'Plugin adjust ZIndex and performs \'always on top\' checks';
  this.group = 'DDI_SYSTEM';
  var elStack = [];
  var elTopStack = [];
  var topStart = 500;
  this.dragstart = {
    'after' : function (e) {
      if (!e.__currentTarget.urid) e.__currentTarget.urid = new Date().getMilliseconds();
      var urid = e.__currentTarget.urid;
      var sl = elStack.length;
      for (var i=0;i<sl;i++) { if (elStack[i] && elStack[i].urid==urid) break;}
      if (elStack[i] && elStack[i].urid == urid) {
        elStack.splice(i,1);
      }
      if (!e.__currentTarget.alwaysOnTop) elStack[elStack.length] = e.__currentTarget;
      for (i=0;i<=sl;i++) if (elStack[i]) elStack[i].style.zIndex = i+(elStack[i].alwaysOnTop?topStart:1);
      var sl = elTopStack.length;
      for (i=0;i<sl;i++) { if (elTopStack[i] && elTopStack[i].urid==urid) break;}
      if (elTopStack[i] && elTopStack[i].urid == urid) {
        elTopStack.splice(i,1);
      }
      if (e.__currentTarget.alwaysOnTop) elTopStack[elTopStack.length] = e.__currentTarget;
      for (i=0;i<=sl;i++) if (elTopStack[i]) elTopStack[i].style.zIndex = i+(elTopStack[i].alwaysOnTop?topStart:1);
    }
  }
};
