/**
 * @fileoverview Patch for southprojects Grid to provide limited backward
 * compatibility with v1.2.
 *
 * <pre>
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 * </pre>
 */

/* $Id: spgrid-compatibility.js 4624 2006-10-03 23:51:13Z alex $ */

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origLoadData = southprojects.Grid.prototype.loadData;

/**
 * @ignore Replacement for original method.
 *
 * @param {object} objArgs Arguments object
 */
southprojects.Grid.prototype.loadData = function(objArgs) {
  // Patch callbacks
  if (typeof this.config.convert == 'function') {
    var funcOrigConvert = this.config.convert;
    this.config.convert = function() {
      return funcOrigConvert(arguments[1]);
    }
  }
  if (typeof this.config.funcStyle == 'function') {
    var funcOrigFuncStyle = this.config.funcStyle;
    this.config.funcStyle = function() {
      return funcOrigFuncStyle(arguments[1]);
    }
  }
  if (this.config.show_asis &&
   typeof this.config.show_asis.funcShow == 'function') {
    var funcOrigFuncShow = this.config.show_asis.funcShow;
    this.config.show_asis.funcShow = function() {
      return funcOrigFuncShow(arguments[1]);
    }
  }
  if (typeof this.config.callbackRowDisplay == 'function') {
    var funcOrigCallbackRowDisplay = this.config.callbackRowDisplay;
    this.config.callbackRowDisplay = function() {
      funcOrigCallbackRowDisplay(arguments[1]);
    }
  }
  if (typeof this.config.callbackDataDisplay == 'function') {
    var funcOrigCallbackDataDisplay = this.config.callbackDataDisplay;
    this.config.callbackDataDisplay = function() {
      funcOrigCallbackDataDisplay(arguments[1]);
    }
  }
  if (typeof this.config.callbackRowOnClick == 'function') {
    var funcOrigCallbackRowOnClick = this.config.callbackRowOnClick;
    this.config.callbackRowOnClick = function() {
      funcOrigCallbackRowOnClick(arguments[1]);
    }
  }
  if (typeof this.config.callbackCellOnClick == 'function') {
    var funcOrigCallbackCellOnClick = this.config.callbackCellOnClick;
    this.config.callbackCellOnClick = function() {
      funcOrigCallbackCellOnClick(arguments[1]);
    }
  }
  if (typeof this.config.callbackRowOnDblClick == 'function') {
    var funcOrigCallbackRowOnDblClick = this.config.callbackRowOnDblClick;
    this.config.callbackRowOnDblClick = function() {
      funcOrigCallbackRowOnDblClick(arguments[1]);
    }
  }
  if (typeof this.config.callbackCellOnDblClick == 'function') {
    var funcOrigCallbackCellOnDblClick = this.config.callbackCellOnDblClick;
    this.config.callbackCellOnDblClick = function() {
      funcOrigCallbackCellOnDblClick(arguments[1]);
    }
  }
  if (typeof this.config.callbackRowSelect == 'function') {
    var funcOrigCallbackRowSelect = this.config.callbackRowSelect;
    this.config.callbackRowSelect = function() {
      funcOrigCallbackRowSelect(arguments[1]);
    }
  }
  if (typeof this.config.callbackCellSelect == 'function') {
    var funcOrigCallbackCellSelect = this.config.callbackCellSelect;
    this.config.callbackCellSelect = function() {
      funcOrigCallbackCellSelect(arguments[1]);
    }
  }
  if (typeof this.config.callbackRowUnselect == 'function') {
    var funcOrigCallbackRowUnselect = this.config.callbackRowUnselect;
    this.config.callbackRowUnselect = function() {
      funcOrigCallbackRowUnselect(arguments[1]);
    }
  }
  if (typeof this.config.callbackCellUnselect == 'function') {
    var funcOrigCallbackCellUnselect = this.config.callbackCellUnselect;
    this.config.callbackCellUnselect = function() {
      funcOrigCallbackCellUnselect(arguments[1]);
    }
  }
  // Call original method
  this.origLoadData(objArgs);
};

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origPrepareData = southprojects.Grid.prototype.prepareData;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.prepareData = function() {
  // Add header
  this.header = {
    fields: this.fields
  };
  if (this.data.style) {
    this.header.userTableStyle = this.data.style;
  }
  if (this.data.headerStyle) {
    this.header.userStyle = this.data.userStyle;
  }
  // Call original method
  this.origPrepareData();
};

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origPrepareField = southprojects.Grid.prototype.prepareField;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.prepareField = function(objField) {
  // Call original method
  objField = this.origPrepareField(objField);
  // Patch field
  objField.id = objField.i;
  if (!objField.dataType) {
    objField.dataType = 'string';
  }
  if (objField.style) {
    objField.userStyle = objField.style;
  }
  if (!objField.nosort) {
    objField.onclick = "southprojects.Grid.sort('" + this.id + "','" + objField.i +
     "')";
  }
  return objField;
};

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origPrepareRow = southprojects.Grid.prototype.prepareRow;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.prepareRow = function(objRow) {
  // Call original method
  objRow = this.origPrepareRow(objRow);
  // Patch row
  objRow.id = objRow.i;
  objRow.grid = {
    id: this.id
  };
  if (objRow.style) {
    objRow.userStyle = objRow.style;
  }
  return objRow;
};

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origConvertCell = southprojects.Grid.prototype.convertCell;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.convertCell = function(objCell) {
  if (!(objCell instanceof Object)) {
    objCell = {};
  }
  // Patch cell
  if (typeof objCell.v == 'undefined' && typeof objCell.data != 'undefined') {
    objCell.v = objCell.data;
  }
  objCell.id = objCell.i;
  objCell.row = {
    id: objCell.r,
    grid: {
      id: this.id
    }
  };
  // Call original method
  return this.origConvertCell(objCell);
};

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origConvertCellCallback =
 southprojects.Grid.prototype.convertCellCallback;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.convertCellCallback = function(objCell) {
  if (!(objCell instanceof Object)) {
    objCell = {};
  }
  // Patch cell
  objCell.data = {
    mixedValue: objCell.v,
    compareValue: objCell.c,
    origValue: objCell.o,
    toString: function() {
      return this.mixedValue + '';
    }
  };
  if (objCell.style) {
    objCell.userStyle = objCell.style;
  }
  // Call original method
  return this.origConvertCellCallback(objCell);
};

// Check if spgrid-html.js is loaded
if (southprojects.Grid.prototype.newDataHtml) {

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewDataHtml = southprojects.Grid.prototype.newDataHtml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newDataHtml = function(objTable, objHeaderRow) {
    // Call original method
    objData = this.origNewDataHtml(objTable, objHeaderRow);
    // Add header
    this.header = {
      fields: this.fields
    };
    if (objData.style) {
      this.header.userTableStyle = objData.style;
    }
    if (objData.headerStyle) {
      this.header.userStyle = objData.userStyle;
    }
    return objData;
  };

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewFieldHtml = southprojects.Grid.prototype.newFieldHtml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newFieldHtml = function(objTableCell) {
    // Call original method
    objField = this.origNewFieldHtml(objTableCell);
    // Patch field
    objField.id = objField.i;
    if (!objField.dataType) {
      objField.dataType = 'string';
    }
    if (objField.style) {
      objField.userStyle = objField.style;
    }
    objField.onclick = "southprojects.Grid.sort('" + this.id + "','" + objField.i +
     "')";
    return objField;
  };

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewRowHtml = southprojects.Grid.prototype.newRowHtml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newRowHtml = function(objTableRow) {
    // Call original method
    objRow = this.origNewRowHtml(objTableRow);
    // Patch row
    objRow.id = objRow.i;
    objRow.grid = {
      id: this.id
    };
    if (objRow.style) {
      objRow.userStyle = objRow.style;
    }
    return objRow;
  };

}

// Check if spgrid-xml.js is loaded
if (southprojects.Grid.prototype.newDataXml) {

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewDataXml = southprojects.Grid.prototype.newDataXml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newDataXml = function(objTable) {
    // Call original method
    objData = this.origNewDataXml(objTable);
    // Add header
    this.header = {
      fields: this.fields
    };
    if (objData.style) {
      this.header.userTableStyle = objData.style;
    }
    if (objData.headerStyle) {
      this.header.userStyle = objData.userStyle;
    }
    return objData;
  };

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewFieldXml = southprojects.Grid.prototype.newFieldXml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newFieldXml = function(objTableCell) {
    // Call original method
    objField = this.origNewFieldXml(objTableCell);
    // Patch field
    objField.id = objField.i;
    if (!objField.dataType) {
      objField.dataType = 'string';
    }
    if (objField.style) {
      objField.userStyle = objField.style;
    }
    objField.onclick = "southprojects.Grid.sort('" + this.id + "','" + objField.i +
     "')";
    return objField;
  };

  /**
   * Reference to original method.
   * @private
   */
  southprojects.Grid.prototype.origNewRowXml = southprojects.Grid.prototype.newRowXml;

  /**
   * @ignore Replacement for original method.
   */
  southprojects.Grid.prototype.newRowXml = function(objTableRow) {
    // Call original method
    objRow = this.origNewRowXml(objTableRow);
    // Patch row
    objRow.id = objRow.i;
    objRow.grid = {
      id: this.id
    };
    if (objRow.style) {
      objRow.userStyle = objRow.style;
    }
    return objRow;
  };

}

/**
 * Reference to original method.
 * @private
 */
southprojects.Grid.prototype.origGetCellValue = southprojects.Grid.prototype.getCellValue;

/**
 * @ignore Replacement for original method.
 */
southprojects.Grid.prototype.getCellValue = function(objCell) {
  if (!objCell) {
    return;
  }
  var value = southprojects.Grid.prototype.origGetCellValue(objCell);
  if (typeof value == 'undefined' && typeof objCell.data != 'undefined') {
    value = objCell.data;
  }
  return value;
};
