// copyright (c) 2006-2008 Franz Inc, Oakland, CA - All rights reserved.
//
// The software, data and information contained herein are proprietary
// to, and comprise valuable trade secrets of, Franz, Inc.  They are
// given in confidence by Franz, Inc. pursuant to a written license
// agreement, and may be stored and used only in accordance with the terms
// of such license.
//
// Restricted Rights Legend
// ------------------------
// Use, duplication, and disclosure of the software, data and information
// contained herein by any agency, department or entity of the U.S.
// Government are subject to restrictions of Restricted Rights for
// Commercial Software developed at private expense as specified in
// DOD FAR Supplement 52.227-7013 (c) (1) (ii), as applicable.

// Functionality for displaying triple-based results -- viewing a node
// or doing a text search.

function showNode(partName) {
  var part = partFromUntrusted(partName);
  if (part == null) {
    showMessage("Invalid node. Could not parse '" + partName + "' as a resource.", true, true);
    return;
  }
  if (part.anon && dbInfo.federated) {
    showMessage("Blank nodes can not be viewed in federated stores. (Technical problem, sorry about that.)", true, true);
    return;
  }

  Mold.cast(getPage(), template("nodeview/loading"), part);
  fetchJSON("node/neighbours", [part.string, null, 0, 100, useReasoning], loaded, failed);

  function loaded(data) {
    data.node = part;
    Mold.cast(getPage(), template("nodeview/view"), data);
  }
  function failed(message) {
    clearPage();
    failMessage("Loading node", message);
  }
}

// Insert more triples into an existing page. (Triples are loaded in
// chunks to prevent building up ridiculously large pages.)
function loadMoreTriples(offset, callApi, interpretResult) {
  function loadMore(cell) {
    Mold.cast(cell, template("instruction/loadingmore"));
    callApi(offset, loaded, failed);

    function loaded(data) {
      var tbody = cell.parentNode.parentNode,
          data = interpretResult ? interpretResult(data) : data;
      Mold.cast(addTBodyBefore(tbody), template("instruction/triplerows"), data);
      if (data.more)
        Mold.cast(cell, template("instruction/more"),
                  loadMoreTriples(data.more, callApi, interpretResult));
      else
        removeNode(tbody);
    }
    function failed(message) {
      failMessage("Loading more triples", message);
      Mold.cast(cell, template("instruction/more"), loadMore);
    }
  }
  return offset ? loadMore : null;
}

function loadMoreNeighbours(node, type, offset) {
  function fetch(offset, ok, fail) {
    fetchJSON("node/neighbours", [node.string, type, offset, 500, useReasoning], ok, fail);
  }
  return loadMoreTriples(offset, fetch, function(data) {data.ignore = type; return data;});
}

function showSearch(query) {
  Mold.cast(getPage(), template("search/loading"));
  fetchJSON("freetext", [query, 0, 500], loaded, failed);

  function loaded(data) {
    data.more = loadMoreSearchResults(query, data.more);
    data.query = query;
    Mold.cast(getPage(), template("search/result"), data);
  }
  function failed(message) {
    clearPage();
    failMessage("Search", message);
  }
}

function loadMoreSearchResults(query, offset) {
  return loadMoreTriples(offset, function(offset, ok, fail) {
    fetchJSON("freetext", [query, offset, 500], ok, fail);
  });
}

function showNodeOnMap(node, place) {
  var geo = node.geo(), latlong = new GLatLng(geo.latitude, geo.longitude), map = showGMap(place, latlong, 13);
  map.addOverlay(new GMarker(latlong));
}
