var __aspxTableColumnResizing = null;
var __aspxScrollerHelpers = null;
ASPxClientTableHelper = _aspxCreateClass(null, {
 constructor: function(control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, isHorizontalScrolling){
  this.control = control;
  this.contentTableIDSuffix = contentTableIDSuffix;
  this.headerTableIDSuffix = headerTableIDSuffix;
  this.footerTableIDSuffix = footerTableIDSuffix;
  this.isHorizontalScrolling = isHorizontalScrolling;
  this.colGroupCols = null;
 },
 GetTable: function(tableIDSuffix) {
  return this.control.GetChildElementById(tableIDSuffix);
 },
 GetContentTable: function() {
  return this.GetTable(this.contentTableIDSuffix);
 },
 GetHeaderTable: function() {
  return this.GetTable(this.headerTableIDSuffix);
 },
 GetFooterTable: function() {
  return this.GetTable(this.footerTableIDSuffix);
 },
 GetResizableTable: function() {
  var headerTable = this.GetHeaderTable();
  return headerTable ? headerTable : this.GetContentTable();
 },
 GetVerticalScrollBarWidth: function(table) {
  return table.offsetParent.offsetHeight > table.offsetHeight ? 0 : _aspxGetVerticalScrollBarWidth();
 },
 GetTableLastColGroupCols: function(table) {
  if(!table)
   return null;
  var colGroups = _aspxGetElementsByTagName(table, "COLGROUP");
  for(var i = colGroups.length - 1; i >= 0; i--) {
   var colGroup = colGroups[i];
   if(this.control.IsElementBelongsToGrid(colGroup.parentNode.id))
    return _aspxGetElementsByTagName(colGroup, "COL");
  }
  return null;
 },
 ConvertWidthToInt: function(width) {
  if(!_aspxIsExists(width) || width == "" || width.indexOf("%") > -1)
   return -1;
  else
   return width.indexOf("px") > -1 ? _aspxPxToInt(width) : parseInt(width);
 },
 GetOriginalCellWidth: function(cell) {
  var col = this.GetCellCorrespondingCol(cell, this.colGroupCols);
  var width = -1;
  if(col != null)
   width = this.ConvertWidthToInt(col.width);
  else if(_aspxIsExists(cell.style))
   width = this.ConvertWidthToInt(cell.style.width);
  return width > 0 ? width : cell.offsetWidth - _aspxGetLeftRightBordersAndPaddingsSummaryValue(cell);
 },
 GetCellCorrespondingCol: function(cell, colGroupCols) {
  return colGroupCols != null ? colGroupCols[cell.cellIndex] : null;
 }
});
ASPxClientTableColumnResizing = _aspxCreateClass(ASPxClientTableHelper, {
 constructor: function(control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, isHorizontalScrolling){
  this.constructor.prototype.constructor.call(this, control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, isHorizontalScrolling);
  this.contentWidthRowIndex = -2;
  this.resizableCell = null;
  this.scrollerHelper = null;
  this.maximumOffset = 3;
  this.minimumWidth = 6;
  this.prevX = 0;
  this.resizedInput = null;
  this.resizableControl = null;
  this.editorsToBeResized = null;
 },
 SetResizedInput: function(input) {
  this.resizedInput = input;
 },
 SetResizableControl: function(resizableControl) {
  this.resizableControl = resizableControl;
 },
 SetEditorsToBeResized: function(list) {
  this.editorsToBeResized = list;
 },
 IsResizing: function() {
  return __aspxTableColumnResizing == this;
 },
 OnMouseUp: function(evt) {
  this.EndResizing();
  this.CancelResizing(evt);
 },
 OnMouseMove: function(evt) {
  if(this.resizableCell.cellIndex < 0) {
   this.CancelResizing(evt);
   return;
  }
  var newX = _aspxGetEventX(evt);
  var delta = newX - this.prevX;
  var newColWidth = this.GetOriginalCellWidth(this.resizableCell) + delta;
  if(newColWidth < this.minimumWidth) return;
  if(!this.CanApplyResizableElementsWidth(-delta)) return;
  if(this.ChangeColumnWidth(this.resizableCell, newColWidth)) {
   this.ApplyResizableElementsWidth(-delta);
   this.prevX = newX;
  }
 },
 StartResizing: function(resizableCell, evt, indentColumnCount) {
  this.colGroupCols = this.GetTableLastColGroupCols(this.GetResizableTable());
  if(_aspxGetEventX(evt) - _aspxGetAbsoluteX(resizableCell) < this.maximumOffset)
   resizableCell = resizableCell.parentNode.cells[resizableCell.cellIndex - 1];
  this.resizableCell = resizableCell;
  __aspxTableColumnResizing = this;
  if(this.GetHeaderTable() != null)
   this.scrollerHelper = new ASPxClientTableScrollerHelper(this.control, this.contentTableIDSuffix, this.headerTableIDSuffix, this.footerTableIDSuffix, this.isHorizontalScrolling);
 },
 EndResizing: function() {
  if(!_aspxIsExists(this.resizedInput) || !_aspxIsExists(this.resizableCell)) return;
  var cells = this.resizableCell.parentNode.cells;
  var res = "";
  for(var i = 0; i < cells.length; i ++) {
   width = this.GetOriginalCellWidth(cells[i]);
   res += width + "|";
  }
  this.resizedInput.value = res;
  this.CorrectEditorsBounds();
 },
 CancelResizing: function(evt) {
  __aspxTableColumnResizing = null;
  this.contentWidthRowIndex = -2;
  this.colGroupCols = null;
  this.resizableCell = null;
  this.prevX = 0;
  this.resizedInput = null;
  this.resizableControl = null;
  this.editorsToBeResized = null;
  if(this.scrollerHelper != null)
   this.scrollerHelper.CorrectHeaderFooterColsWidth();
 },
 CorrectEditorsBounds: function() {
  if(this.editorsToBeResized == null) return;
  for(var i = 0; i < this.editorsToBeResized.length; i ++)
   this.editorsToBeResized[i].AdjustControl(false);
 },
 UpdateCursor: function(element, evt, indentColumnCount) {
  var table = this.GetResizableTable();
  var counter = 3;
  var temp = element;
  while(temp != null && temp != table) {
   temp = temp.parentNode;
   if(--counter < 0)
    element = element.parentNode;
  }
  if(_aspxIsExists(temp) && (this.IsResizing() || this.CanStartResizing(element, evt, indentColumnCount)))
   _aspxChangeStyleAttribute(element, "cursor", "w-resize");
  else
   _aspxRestoreStyleAttribute(element, "cursor");
 },
 CanStartResizing: function(resizableCell, evt, indentColumnCount) {
  var table = this.GetResizableTable();
  if(!_aspxIsExists(table))
   return false;
  var cellInfo = this.GetTableCellInfo(table, resizableCell, indentColumnCount);
  if(!cellInfo.exists)
   return false;
  this.prevX = _aspxGetEventX(evt);
  var left = _aspxGetAbsoluteX(resizableCell);
  var right = left + resizableCell.offsetWidth - 1;
  if(this.prevX - left < this.maximumOffset)
   return resizableCell.cellIndex > indentColumnCount;
  if(right - this.prevX <= this.maximumOffset)
   return !cellInfo.isLast;
  return false;
 },
 CanApplyResizableElementsWidth: function(delta) {
  if(_aspxIsExists(this.resizableControl))
   return true;
  var changedCell = this.GetNextChangedCell(this.resizableCell, delta);
  if(changedCell == null)
   return false;
  var oldWidth = this.GetOriginalCellWidth(changedCell);
  return oldWidth + delta >= this.minimumWidth;
 },
 ApplyResizableElementsWidth: function(delta) {
  if(_aspxIsExists(this.resizableControl)) {
   if(this.isHorizontalScrolling)
    return;
   var curWidth = this.GetOriginalCellWidth(this.resizableControl);
   this.resizableControl.style.width = (curWidth - delta) + "px";
   if(this.scrollerHelper != null)
    this.scrollerHelper.SetHeaderFooterContainersWidth();
  } else {
   var changedCell = this.GetNextChangedCell(this.resizableCell, delta);
   if(changedCell == null)
    return;
   var oldWidth = this.GetOriginalCellWidth(changedCell);
   this.ChangeColumnWidth(changedCell, oldWidth + delta);
  }
 },
 ChangeColumnWidth: function(resizableCell, newColWidth) {
  var oldColWidth = this.GetOriginalCellWidth(resizableCell);
  this.ChangeColumnWidthCore(this.GetResizableTable(), newColWidth, this.colGroupCols, resizableCell.cellIndex, 0);
  var result = oldColWidth != this.GetOriginalCellWidth(resizableCell);
  if(result && this.GetHeaderTable() != null) {
   this.ChangeColumnWidthAnotherTable(this.GetContentTable(), resizableCell.cellIndex, newColWidth, this.GetContentWidthRow());
   if(!this.control.hasFooterRowTemplate)
    this.ChangeColumnWidthAnotherTable(this.GetFooterTable(), resizableCell.cellIndex, newColWidth, 0);
  }
  return result;
 },
 ChangeColumnWidthAnotherTable: function(table, columnIndex, newColWidth, rowIndex){
  if(table)
   this.ChangeColumnWidthCore(table, newColWidth, this.GetTableLastColGroupCols(table), columnIndex, rowIndex);
 },
 ChangeColumnWidthCore: function(table, newColWidth, colGroupCols, colIndex, rowIndex){
  if(newColWidth < 0)
   return;
  if(!colGroupCols)
   return;
  var col =  colGroupCols[colIndex];
  col.width = newColWidth + "px";
  if(rowIndex > -1) {
   var row = table.rows[rowIndex];
   row.cells[colIndex].style.width = newColWidth + "px";
   if(__aspxWebKitFamily && rowIndex > 0) {
    var cell = table.rows[rowIndex - 1].cells[0];
    if(cell) {
     cell.colSpan++;
     cell.colSpan--;
    }
   }   
  }
 },
 GetNextChangedCell: function(cell) {
  if(cell.cellIndex == 0 && cell.parentNode.cells.length == 1)
   return null;
  var cellIndex = cell.cellIndex + 1 < cell.parentNode.cells.length ?
   cell.cellIndex + 1 : cell.cellIndex - 1;
  return cell.parentNode.cells[cellIndex];
 },
 GetContentWidthRow: function() {
  if(this.contentWidthRowIndex > -2)
   return this.contentWidthRowIndex;
  this.contentWidthRowIndex = -1;
  var table = this.GetContentTable();
  for(var i = 0; i < table.rows.length; i ++) {
   var index = this.control.getRowIndex(table.rows[i].id);
   if(this.control.IsDataRow(index)) {
    this.contentWidthRowIndex = i;
    break;
   }
  }
  return this.contentWidthRowIndex;
 },
 GetTableCellInfo: function(table, cell, indentColumnCount) {
  var info = {
   exists: false,
   isLast: null
  };
  for(var i = 0; i < table.rows.length; i++) {
   var row = table.rows[i];
   for(var j = 0; j < row.cells.length; j++) {
    if(row.cells[j] != cell)
     continue;
    else if (cell.cellIndex < indentColumnCount)
     return info;
    else {
     info.exists = true;
     info.isLast = j == row.cells.length - 1;
     return info;
    }
   }
  }
  return info;
 }
});
ASPxClientTableScrollerHelper = _aspxCreateClass(ASPxClientTableHelper, {
 constructor: function(control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, isHorizontalScrolling){
  this.constructor.prototype.constructor.call(this, control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, isHorizontalScrolling);
  this.verticalScrollBarWidth = -1;
  this.IsWidthTypePercent = false;
  this.isScrolling = false;
  this.SetOnScroll();
  if(__aspxScrollerHelpers == null) {
   __aspxScrollerHelpers = new Array();
  }
  __aspxScrollerHelpers.push(this);
 },
 CorrectHeaderFooterColsWidth : function() {
  this.SetHeaderFooterContainersWidth();
 },
 AfterWindowResize: function() {
  this.CorrectHeaderFooterColsWidth();
 },
 OnWindowResize: function(index) {
  if(!this.IsWidthTypePercent)
   return;
  _aspxSetTimeout(function() {
   if(__aspxScrollerHelpers != null && index < __aspxScrollerHelpers.length)
    __aspxScrollerHelpers[index].AfterWindowResize();
  }, 0);
 },
 SetOnScroll: function() {
  if(!this.isHorizontalScrolling) return;
  this.SetOnScrollCore(this.GetContentTable(), this.OnContentScroll);
  this.SetOnScrollCore(this.GetHeaderTable(), this.OnHeaderScroll);
 },
 SetOnScrollCore: function(table, proc) {
  var parent = this.GetScrollDiv(table);
  if(parent != null) {
   _aspxAttachEventToElement(parent, "scroll", proc); 
   parent.scrollHelper = this;
  }
 },
 OnContentScroll: function(event) {
  var src = _aspxGetEventSource(event);
  var scrollHelper = src.scrollHelper;
  if(!_aspxIsExists(scrollHelper)) return;
  scrollHelper.OnDivScrollCore(src, scrollHelper.GetHeaderTable(), scrollHelper.GetFooterTable());
 },
 OnHeaderScroll: function(event) {
  var src = _aspxGetEventSource(event);
  var scrollHelper = src.scrollHelper;
  if(!_aspxIsExists(scrollHelper)) return;
  scrollHelper.OnDivScrollCore(src, scrollHelper.GetContentTable(), scrollHelper.GetFooterTable());
 },
 OnDivScrollCore: function(src, table1, table2) {
  if(this.isScrolling) return;
  this.isScrolling = true;
  this.SetScrollLeft(src, table1);
  this.SetScrollLeft(src, table2);
  this.isScrolling = false;
 },
 GetScrollDiv: function(table) {
  if(!_aspxIsExists(table)) return null;
  return table.parentNode;
 },
 SetScrollLeft: function(src, table) {
  var div = this.GetScrollDiv(table);
  if(div != null) {
   div.scrollLeft = src.scrollLeft;
  }
 },
 GetColsWidth: function(table) {
  if(table.rows.length < 2) return null;
  var widths = new Array();
  var row = table.rows[table.rows.length - 1];
  for(var i = 0; i < row.cells.length; i ++) {
   var width = row.cells[i].clientWidth;
   if(__aspxNetscapeFamily && (i == row.cells.length - 1)) {
    width += this.GetVerticalScrollBarWidth(table);
   }
   widths.push(width);
  }
  return widths;
 },
 SetHeaderFooterContainersWidth: function() {
  if(!this.control.IsVisible()) {
   this.control.sizeCorrectedOnce = false;
   return;
  }
  var mainEl = this.control.GetMainElement();
  if(!mainEl) return;
  var headerTable = this.GetHeaderTable();
  this.ResetContainerWidth(headerTable);
  this.ResetContainerWidth(this.GetContentTable());
  this.ResetContainerWidth(this.GetFooterTable());
  ASPxClientGridView.ResetElementWidth(mainEl);
  var mainElWidth = mainEl.offsetWidth;
  if(!this.isHorizontalScrolling && headerTable && !__aspxIE) {
   var headerWidth = headerTable.offsetWidth;
   if(headerWidth > mainElWidth)
    mainElWidth = 2 + headerWidth + _aspxGetVerticalScrollBarWidth();
  }
  var width = mainElWidth -
   _aspxGetLeftRightBordersAndPaddingsSummaryValue(mainEl) -
   _aspxGetLeftRightBordersAndPaddingsSummaryValue(mainEl.rows[0].cells[0]);
  this.SetContainerWidth(headerTable, width, _aspxGetVerticalScrollBarWidth());
  this.SetContainerWidth(this.GetContentTable(), width, 0);
  this.SetContainerWidth(this.GetFooterTable(), width, _aspxGetVerticalScrollBarWidth());
  if(!this.isHorizontalScrolling || this.control.fixedColumnCount < 1) {
   this.SetElementWidth(this.GetContentTable(), width, _aspxGetVerticalScrollBarWidth(), 0);
  }  
  mainEl.style.width = mainElWidth + "px";
 },
 SetContainerWidth: function(table, width, margin) {
  if(table == null) return;
  var parent = table.parentNode;
  if(!_aspxIsExists(parent)) return;
  parent.align = "left";
  this.SetElementWidth(parent, width, margin, margin);
 },
 ResetContainerWidth: function(table) {
  if(table == null) return;
  var parent = table.parentNode;
  if(!_aspxIsExists(parent)) return;
  parent.style.width = "1px";
 },
 SetElementWidth: function(element, width, margin, marginRight) {
  if(!_aspxIsExists(element)) return;
  if(width <= _aspxGetVerticalScrollBarWidth()) return;
  if(marginRight > 0) {
   if(_aspxIsElementRigthToLeft(element) && !(__aspxOpera || __aspxWebKitFamily)) {
    element.style.marginLeft = marginRight + "px";
    element.style.marginRight = "";
   }
   else {
   element.style.marginRight = marginRight + "px";
    element.style.marginLeft = "";
   }
  }
  element.style.width = (width - margin) + "px";
 },
 MakeRowVisible: function(row) {
  var div = this.GetScrollDiv(this.GetContentTable());
  if(div == null) return;
  var top = row.offsetTop;
  if(top + row.offsetHeight < div.offsetHeight) {
   top = 0;
  }
  div.scrollTop = top;
 }
});
ASPxClientTableScrollerHelper.prevBodyWidth = -1;
ASPxClientTableFixedColumnsHelper = _aspxCreateClass(ASPxClientTableHelper, {
 constructor: function(control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, fixedDivName, fixedContentDivName, fixedColumnCount, areaWidth){
  this.constructor.prototype.constructor.call(this, control, contentTableIDSuffix, headerTableIDSuffix, footerTableIDSuffix, true);
  this.FixedDivName = fixedDivName;
  this.FixedContentDivName = fixedContentDivName;
  this.FixedColumnCount = fixedColumnCount; 
  this.hiddenColumnCount = 0;
  this.prevHiddenColumnCount = 0;
  this.doScrolling = false;
  this.areaWidth = areaWidth;
  this.Update();
  this.savedScrollPos = -1;
 },
 GetFixedDiv: function() { return this.control.GetChildElementById(this.FixedDivName); },
 GetFixedContentDiv: function() { return this.control.GetChildElementById(this.FixedContentDivName); },
 Update: function() {
  _aspxDetachEventFromElement(this.GetFixedDiv(), "scroll", this.OnScroll);
  this.GetFixedDiv().helper = this;
  this.UpdateRowsHeight();
  this.colGroupCols = this.GetTableLastColGroupCols(this.GetResizableTable());
  this.scrollableWidth = this.GetScrollableWidth();
  this.GetFixedContentDiv().style.width = this.GetContentWidth() + "px";
  var scrollHeigth = _aspxGetVerticalScrollBarWidth() + "px";
  this.GetFixedDiv().style.height = scrollHeigth;
  this.GetFixedContentDiv().style.height = scrollHeigth;
  _aspxAttachEventToElement(this.GetFixedDiv(), "scroll", this.OnScroll); 
 },
 OnScroll: function(event) {
  var src = _aspxGetEventSource(event);
  if(src.doScrolling) return;
  src.doScrolling = true;
  var helper = src.helper;
  var columnsToHide = helper.GetColumnsToHide(src.scrollLeft);
  if(helper.hiddenColumnCount != columnsToHide) {
   helper.prevHiddenColumnCount = helper.hiddenColumnCount;
   helper.hiddenColumnCount = columnsToHide;
   helper.UpdateCells();
  }
  src.doScrolling = false;
 },
 GetContentWidth: function() {
  return this.GetResizableTable().offsetWidth + _aspxGetVerticalScrollBarWidth();
 },
 GetScrollableWidth: function() {
  var res = this.GetContentWidth();
  var tr = this.GetHeaderRow();
  for(i = 0;  i < this.FixedColumnCount; i ++) {
   res -= this.GetColumnWidth(tr, i);
  }
  return res;
 },
 GetHeaderRow: function() {
  return this.GetResizableTable().rows[0];  
 },
 GetColumnsToHide: function(scrollLeft) {
  var width = 0;
  var tr = this.GetHeaderRow();
  var scrollWidth = this.GetFixedContentDiv().scrollWidth + this.areaWidth;
  var i;
  for(i = this.FixedColumnCount; i < tr.cells.length; i ++) {
   if(scrollLeft * scrollWidth <= width * this.scrollableWidth) break;
   width += this.GetColumnWidth(tr, i);
  }
  if(i == tr.cells.length) i --;
  return i - this.FixedColumnCount;
 },
 GetColumnWidth: function(tr, index) {
  return this.colGroupCols != null ? parseInt(this.colGroupCols[index].width) : this.GetOriginalCellWidth(tr.cells[index])
 },
 UpdateCells: function() {
  this.UpdateTableCells(this.GetHeaderTable());
  this.UpdateTableCells(this.GetContentTable());
  this.UpdateTableCells(this.GetFooterTable());
 },
 UpdateTableCells: function(table) {
  if(!_aspxIsExists(table)) return;
  var startIndex = Math.min(this.prevHiddenColumnCount, this.hiddenColumnCount) + this.FixedColumnCount;
  var endIndex = Math.max(this.prevHiddenColumnCount, this.hiddenColumnCount) + this.FixedColumnCount;
  var display = this.hiddenColumnCount - this.prevHiddenColumnCount > 0 ? "none" : "";
  var cols = this.GetTableLastColGroupCols(table);
  if(cols != null) {
   for(var i = startIndex; i < endIndex; i ++) {
    cols[i].style.display = display;
   }
  }
  if(cols == null || !__aspxIE || __aspxBrowserVersion >= 8) {
   for(var i = 0; i < table.rows.length; i ++) {
    this.UpdateRowCells(table.rows[i], startIndex, endIndex, display);
   }
  }
 },
 UpdateRowCells: function(row, startIndex, endIndex, display) {
  for(var i = startIndex; i < endIndex; i ++) {
   row.cells[i].style.display = display;
  }
 },
 UpdateRowsHeight: function() {
  var table = this.GetContentTable();
  for(var i = 0; i < table.rows.length; i ++) {
   var row = table.rows[i];
   if(row.cells.length == 0) continue;
   var cell = row.cells[0];
   var height = _aspxGetClearClientHeight(cell);
   if(height < 1) return;
   height += "px";
   row.style.height = height;
   cell.style.height = height;
  }
 },
 SaveCallbackSettings: function() {
 },
 RestoreCallbackSettings: function() {
  this.hiddenColumnCount = 0;
  this.prevHiddenColumnCount = 0;
 }
});
_aspxAttachEventToDocument("mouseup", function(e) {
 if(__aspxTableColumnResizing != null) {
  __aspxTableColumnResizing.OnMouseUp(e);
  return true;
 }
});
_aspxAttachEventToDocument("mousemove", function(e) {
 if(__aspxTableColumnResizing != null) {
  __aspxTableColumnResizing.OnMouseMove(e);
  return true;
 }
});
_aspxAttachEventToDocument("selectstart", function(e) {
 if(__aspxTableColumnResizing != null) {
  document.selection.empty();
  return false;
 }
});
_aspxAttachEventToElement(window, "resize", function(e) {
 var width = document.body.offsetWidth;
 if(width == ASPxClientTableScrollerHelper.prevBodyWidth)
  return;
 ASPxClientTableScrollerHelper.prevBodyWidth = width;
 if(__aspxScrollerHelpers != null) {
  for(var i = 0; i < __aspxScrollerHelpers.length; i++)
   __aspxScrollerHelpers[i].OnWindowResize(i);
 }
 for(var name in ASPxClientGridView.instances) {
  var obj = ASPxClientGridView.instances[name];
  if(obj.UpdateHorzScrollDivs)
   obj.UpdateHorzScrollDivs(true);
 }
});
