/*
 * Functions to handle the Index Finder pages on this website
 * 
 * $Id: indexFinder.js,v 1.2 2010/06/03 12:27:26 obo Exp $
 */

// For a given region, we check if the subType has been selected; if so, we proceed to get
// the results, else we switch to the byType view. We loop over the all the subTypes cells
// and read the description from inside the anchor tag. If the region-to-type map for that
// region does not contain the type, we over-write the anchor tag in the cell with simply
// the type description (thus disabling the link) and change the text color.
function selectRegion(superRegion, subRegion) {
  selected.superRegion = superRegion;
  selected.subRegion = subRegion;
  dojo.style(dojo.byId("byRegionOpeningPara"), "display", "none");
  dojo.style(dojo.byId("byRegionChangeType"), "display", "");
  if (selected.superType && selected.subType) {
    getResultData(selected);
  }
  else {
    dojo.query(".subType").forEach(
      function(node) {
        var anchor = node.children[0];
        if (dojo.indexOf(regionToTypeMap[superRegion + "|" + subRegion], anchor.id) < 0) {
          node.innerHTML = anchor.innerHTML;
          dojo.style(node, "color", disabledLinkColor);
        }
      }
    );
    dojo.style(dijit.byId("byRegion").domNode, "display", "none");
    dojo.style(dijit.byId("byType").domNode, "display", "");
    dojo.style(dojo.byId("byTypeChangeRegion"), "display", "");
    dijit.byId("changeRegion").setValue(selected.superRegion + '|' + selected.subRegion);
  }
  return false;
}

// Same as the above, but selecting type after region.
function selectType(superType, subType) {
  selected.superType = superType;
  selected.subType = subType;
  dojo.style(dojo.byId("byTypeOpeningPara"), "display", "none");
  dojo.style(dojo.byId("byTypeChangeRegion"), "display", "");
  if (selected.superRegion && selected.subRegion) {
    getResultData(selected);
  }
  else {
    dojo.query(".subRegion").forEach(
      function(node) {
        var anchor = node.children[0];
        if (dojo.indexOf(typeToRegionMap[superType + "|" + subType], anchor.id) < 0) {          
          node.innerHTML = anchor.innerHTML;
          node.onmouseover = "";
          dojo.style(node, "color", disabledLinkColor);
        }
      }
    );
    dojo.style(dijit.byId("byRegion").domNode, "display", "");
    dojo.style(dijit.byId("byType").domNode, "display", "none");
    dojo.style(dojo.byId("byRegionChangeType"), "display", "");
    dijit.byId("changeType").setValue(selected.superType + '|' + selected.subType);
  }
  return false;
}

// Remove the region map and fetch the result data using the selected type/region data.
function getResultData(s) {
  dojo.style(dojo.byId("byRegionMap"), "display", "none");
  var pane = dijit.byId("indexResultsPane");
  if (pane) {
    pane.href = "/indices/finder_results.html?superRegion=" + encodeURIComponent(s.superRegion)
      + "&subRegion=" + encodeURIComponent(s.subRegion)
      + "&superType=" + encodeURIComponent(s.superType)
      + "&subType=" + encodeURIComponent(s.subType);
    pane.refresh();
  }
}

// Re-do the region selection (i.e. from pulldown selector)
function reSelectRegion(region) {
  if (ignoreOnChange) {
    ignoreOnChange = false;
    return false;
  }
  var parts = region.split("|");
  location = "?superRegion=" + encodeURIComponent(parts[0])
    + "&subRegion=" + encodeURIComponent(parts[1]);
  return false;
}

//Re-do the type selection (i.e. from pulldown selector)
function reSelectType(type) {
  if (ignoreOnChange) {
    ignoreOnChange = false;
    return false;
  }
  var parts = type.split("|");
  location = "?superType=" + encodeURIComponent(parts[0])
    + "&subType=" + encodeURIComponent(parts[1]);
  return false;
}
