/**------------------------------------------------------------------------
 * Set of function and classes which deal configurable tables 
 * 
 * @version $Id: tableConfiguration.js,v 1.1 2010/05/07 12:36:31 obo Exp $
 *-----------------------------------------------------------------------*/

var _currentConfig = null;

/**
 * Refresh the content pane containing the table-grid by passing all configuration data
 * as request parameters.
 *
 * @param gridId The id of the nml:table-grid
 * @param chunk The page/chunk number to display
 */
function displayChunk(gridId, chunk) {
  var node = dojo.byId(gridId);
  while (dijit.byNode(node)==null) {
    node = node.parentNode;
  }
  
  var pane = dijit.byNode(node); 
  dojo.forEach( pane.getDescendants(), function (node) {node.destroyRecursive();}); 
  
  var i = pane.href.indexOf("page=");
  var href = (i==-1)? pane.href : pane.href.substring(0,i-1);
  href += (href.indexOf("?")==-1)? "?page=" : "&page=";
  href += chunk;

  for (var key in _currentConfig[gridId]) {
    href += "&" + key + "=" + _currentConfig[gridId][key]; 
  }

  pane.href = href;
  pane.refresh();
}

/**
 * Show or hide a column
 *
 * @param gridId The id of the nml:table-grid
 * @param colPos The nml:column position()
 * @param visible true or false;
 */
function displayColumn(gridId, colPos, visible) {
  var menuItemId = gridId + "_column" + colPos;
  var menuAllId = gridId + "_allColumnItems";
  var columnClass = "class_" + menuItemId;
        
  dojo.forEach( dojo.query("." + columnClass), function (node) {
    if (visible) {
      dojo.style(node, "display", "");
    } else {
      dojo.style(node, "display", "none");
    }
  });

  // change style to force firefox to re-render page content width correctly 
  if (navigator.appName == "Netscape") {
    var node = "configButtonHiddenImg_" + gridId;
    if (dojo.style(node, "display") == "none") {
      dojo.style(node, "display", "inline");
    } else {
      dojo.style(node, "display", "none");
    }
  }
    
  dijit.byId(menuItemId).setChecked(visible);
  // Uncheck "All" config menu item if a column is set unchecked 
  if (!visible) {
    var allItem = dijit.byId(menuAllId);
    if (allItem!=null) allItem.setChecked(false);
  }
}

/**
 * Gets the persistent configuration of a column
 *
 * @param gridId The id of the nml:table-grid
 * @param colId A column id
 * @return The column's visibility; true or false
 */
function getPersistentValue(gridId, colId) {
  return _getCookieValues(gridId).search(colId)!=-1;
}

/**
 * Update the current client-side table configuration
 *
 * @param gridId The id of the nml:table-grid
 * @param key A configuration key, e.g. nml:column id, chunkSize, sortgridId, sortColumn
 * @param value The value of the configuration
 * @param persistent Set to true in order to save column configuration persistently
 */
function updateConfiguration(gridId, key, value, persistent) {
  if (_currentConfig == null) _currentConfig = new Object();
  if (_currentConfig[gridId] == null) _currentConfig[gridId] = new Object();
  _currentConfig[gridId][key] = value;
  if (persistent) _writeCookieValue(gridId, key, value);
}

/**
 * Write the current client-side table configuration into a cookie, e.g.
 * tableConfig_<gridId> = "colId1,colId4,colId7,;" (only the visible colums are stored)
 *
 * @param gridId The id of the nml:table-grid
 * @param colId A column id
 * @param value The column visibility; true or false;
 */
function _writeCookieValue(gridId, colId, value) {
  var expireDate = new Date(new Date().getTime() + 1000*60*60*24*30).toGMTString();
  var cookieValues = _getCookieValues(gridId);

  if (value) {
    if (cookieValues.search(colId)==-1) {
      cookieValues = cookieValues + colId + ",";
      document.cookie = _getCookieKey(gridId) + "=" + cookieValues + "; path=/; expires=" + expireDate + ";";
    }
  } else {
    cookieValues = cookieValues.replace(colId+",", "");
    document.cookie = _getCookieKey(gridId) + "=" + cookieValues + "; path=/; expires=" + expireDate + ";";
  }
}

/**
 * Get the client-side table configuration from the cookie
 *
 * @param gridId The id of the nml:table-grid
 * @return All visible colums, e.g "colId1,colId4,colId7,;"
 */
function _getCookieValues(gridId) {
  var cookieValues = document.cookie;
  var start = cookieValues.search(_getCookieKey(gridId));

  if (start!=-1) {
    cookieValues = cookieValues.substring(start) + ";";
    cookieValues = cookieValues.substring(cookieValues.search("=")+1, cookieValues.search(";")); 
    return cookieValues;
  }
  return "";
}

/**
 * Returns the cookie key for the configurable table with id <gridId>
 *
 * @param gridId The id of the nml:table-grid
 * @return "tableConfig_<gridId>"
 */
function _getCookieKey(gridId) {
  return "tableConfig_" + gridId;
}
   
