// Functions to use the dom-drag.js scripts to reorder items for the Avenue CMS

function doDragStart2(x,y) {
  // Show dragging outline
  for(var i=0; i<this.childNodes.length; i++) {
    if (this.childNodes[i].nodeName=='DIV') {
      this.childNodes[i].style.visibility='visible';
    }
  }
  this.style.cursor='move';
}
function doDragEnd2(x,y) {
  getPosition();
  var i, dragcell;
  var currentIndex=this.id.replace(/.*Dragcell/, '');
  var itemType=this.id.replace(/Dragcell.*/, '');
  var currentOffset=this.offsetTop;
  var draggroup='';
  if (this.getAttribute && this.getAttribute('draggroup')) {
    draggroup=this.getAttribute('draggroup');
  } else if (this.hasAttribute && this.hasAttribute('draggroup')) {
    draggroup=this.hasAttribute('draggroup');
  }
  // Hide dragging outline
  for(i=0; i<this.childNodes.length; i++) {
    if (this.childNodes[i].nodeName=='DIV') {
      this.childNodes[i].style.visibility='hidden';
    }
  }
  this.style.cursor='auto';
  // Find dragcell with offset closest to drop-point of drag operation
  this.style.top="0px";
  var bestIndex=0;
  var bestDistance=999999;
  i=1;
  dragcell = findObject(itemType+'Dragcell'+i);
  var ok;
  while(dragcell) {
    if (draggroup) {
      var draggroup2;
      if (dragcell.getAttribute && dragcell.getAttribute('draggroup')) {
        draggroup2=dragcell.getAttribute('draggroup');
      } else if (dragcell.hasAttribute && dragcell.hasAttribute('draggroup')) {
        draggroup2=dragcell.hasAttribute('draggroup');
      }
      ok = (draggroup==draggroup2);
    } else {
      ok = true;
    }
    if (ok && Math.abs(dragcell.offsetTop-currentOffset)<bestDistance) {
      bestIndex=i;
      bestDistance=Math.abs(dragcell.offsetTop-currentOffset);
    }
    i++;
    dragcell = findObject(itemType+'Dragcell'+i);
  }
  // Get TBODY of current item, and item occupying target location
  /*
  var currentRow = this;
  while(currentRow && !(currentRow.id
      && currentRow.id.indexOf(itemType+'Row')==0)) {
    currentRow=currentRow.parentNode;
  }
  var bestRow = findObject(itemType+'Dragcell'+bestIndex);
  while(bestRow && !(bestRow.id
      && bestRow.id.indexOf(itemType+'Row')==0)) {
    bestRow = bestRow.parentNode;
  }
  var tableNode=bestRow.parentNode;
  if (!bestRow || !currentRow || currentIndex==bestIndex) {
    return;
  }
  registerChange();
  // Place current item in list at target location. Move up other items
  if (bestRow.offsetTop>currentRow.offsetTop) {
    tableNode.insertBefore(currentRow, bestRow.nextSibling);
  } else  {
    tableNode.insertBefore(currentRow, bestRow);
  }
  */
  // Obtain current order and store in hidden form vars
  var orderField=findObject(itemType+'IdOrder');
  if (!orderField) {
    return;
  }
  var orderIds=new Array;
  for(i=0; i<tableNode.childNodes.length; i++) {
    currentRow=tableNode.childNodes[i];
    if (currentRow.id && currentRow.id.indexOf(itemType+'Row')==0) {
      orderIds.push(currentRow.id.substr(itemType.length+3));
    }
  }
  orderField.value=orderIds.join(',');
}

function initDragSort2(itemType) {
  var i=1;
  var dragcell = findObject(itemType+'Dragcell'+i);
  while(dragcell) {
    var handle=document.getElementById(itemType+"Draghandle"+i);
    Drag.init(handle, dragcell, (i == 1 ? 0 : -17), (i == 1 ? 920 : 903), 0, 0, false, false);
    dragcell.onDragStart = doDragStart2;
    dragcell.onDragEnd = doDragEnd2;
    i++;
    dragcell = findObject(itemType+'Dragcell'+i);
  }
}
