/**
 * @fileoverview Common functions used in various southprojects Grid demos.
 *
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 */

/* $Id: demo.js 4787 2006-10-16 18:36:36Z alex $ */

/**
 * Passed to grid through callbackCellOnClick config option and called when grid
 * cell is clicked.
 * @private
 */
function onCellClick(objGrid, objCell) {
  alert('Row id is ' + objGrid.getCellRowId(objCell) +
   '\nCell id is ' + objGrid.getCellId(objCell) +
   '\nItem number is ' + objGrid.getCellValueString(objGrid.getCellByRow(
   objGrid.getRowByCell(objCell), 0)));
}

/**
 * Passed to grid through callbackRowOnClick config option and called when grid
 * row is clicked.
 * @private
 */
function onRowClick(objGrid, objRow) {
  alert('Row id is ' + objGrid.getRowId(objRow) + '\nItem number is ' +
   objGrid.getCellValueString(objGrid.getCellByRow(objRow, 0)));
};

/**
 * Range of items slider "onChange" event listener. Called when slider position
 * is changed programmatically.
 * @private
 */
function onRangeOfItemsChange(iMinValue, iMaxValue) {
  iMinValue = Math.round(iMinValue);
  iMaxValue = Math.round(iMaxValue);
  // Display scale
  document.getElementById('rangeOfItems').innerHTML =
   iMinValue + ' - ' + iMaxValue;
}

/**
 * Range of items slider "newPosition" event listener. Called when slider is
 * drag-n-dropped.
 * @private
 */
function onRangeOfItemsDrag(iMinValue, iMaxValue) {
  onRangeOfItemsChange(iMinValue, iMaxValue);
  // Limit range of items
  objGrid.limitRange({
    column: 0,
    min: iMinValue,
    max: iMaxValue
  });
}

/**
 * Range of minutes slider "onChange" event listener. Called when slider
 * position is changed programmatically.
 * @private
 */
function onRangeOfMinutesChange(iMinValue, iMaxValue) {
  iMinValue = Math.round(iMinValue);
  iMaxValue = Math.round(iMaxValue);
  // Display scale
  document.getElementById('rangeOfMinutes').innerHTML =
   iMinValue + ' - ' + iMaxValue;
}

/**
 * Range of minutes slider "newPosition" event listener. Called when slider is
 * drag-n-dropped.
 * @private
 */
function onRangeOfMinutesDrag(iMinValue, iMaxValue) {
  onRangeOfMinutesChange(iMinValue, iMaxValue);
  // Limit range of items
  objGrid.limitRange({
    column: 5,
    min: iMinValue,
    max: iMaxValue
  });
}

/**
 * Holds items slider object.
 * @private
 */
var objItemsSlider;

/**
 * Holds minutes slider object.
 * @private
 */
var objMinutesSlider;

/**
 * Passed to grid through eventListeners config option as 'gridInitialized'
 * event listener and called when grid is initialized. Grid object can be
 * accessed through "this" because function is called in scope of grid object.
 * @private
 */
function onGridInit() {
  var objGrid = this;
  // Get range of items
  var objItemsRange = this.getColumnRange({column: 0});
  if (objItemsRange) {
    document.getElementById('rangeOfItems').innerHTML = 
      objItemsRange.min + ' - ' + objItemsRange.max;
    if (objItemsSlider) {
      // Update slider
      objItemsSlider.reset(objItemsRange.min, objItemsRange.max);
    } else {
      // Draw items slider
      objItemsSlider = new southprojects.Slider({
        div: 'rangeOfItemsScale', 
        length: 99,
        dual: true, 
        orientation: 'H',
        step: 1,
        range : [objItemsRange.min, objItemsRange.max],
        eventListeners: {
          'onChange': onRangeOfItemsChange,
          'newPosition': onRangeOfItemsDrag
        }
      });
    }
  }
  // Get range of minutes
  var objMinutesRange = this.getColumnRange({column: 5});
  if (objMinutesRange) {
    document.getElementById('rangeOfMinutes').innerHTML = 
     objMinutesRange.min + ' - ' + objMinutesRange.max;
    if (objMinutesSlider) {
      // Update slider
      objMinutesSlider.reset(objMinutesRange.min, objMinutesRange.max);
    } else {
      // Draw minutes slider
      objMinutesSlider = new southprojects.Slider({
        div: 'rangeOfMinutesScale', 
        length: 99,
        dual: true, 
        orientation: 'H',
        step: 1,
        range : [objMinutesRange.min, objMinutesRange.max],
        eventListeners: {
          'onChange': onRangeOfMinutesChange,
          'newPosition': onRangeOfMinutesDrag
        }
      });
    }
  }
}

/**
 * Sets filter to the grid.
 * @private
 */
function filter(objForm) {
  if (objGrid) {
    objGrid.setFilter({
      text: objForm.textFilter.value
    });
  }
  return false;
}

/**
 * Removes all filters from the grid.
 * @private
 */
function resetControls(objForm) {
  // Reset form
  objForm.textFilter.value = '';
  // Reset sliders
  for (var iWidget = 0; iWidget < southprojects.Widget.all.length; iWidget++) {
    var objWidget = southprojects.Widget.all[iWidget];
    if (objWidget.constructor == southprojects.Slider) {
      objWidget.setPos(objWidget.config.range[0], objWidget.config.range[1]);
    }
  }
  // Reset filters
  if (objGrid) {
    objGrid.resetFilters();
  }
}
