// 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.

// Initialising the application, keeping some global state.

// Configure CodeMirror
(function() {
  var cssDir = baseURL + "js/lib/codemirror/";
  fill(CodeMirrorConfig, {
    stylesheet: [cssDir + "lispcolors.css", cssDir + "sparqlcolors.css"],
    basefiles: ["editor.js"],
    path: baseURL + "js/lib/codemirror/",
    autoMatchParens: true,
    width: "100%"
  });
})();

connect(window, "load", function() {
  // Warn if CodeMirror (and a bunch of other things) won't work on
  // this browser.
  if (!CodeMirror.isProbablySupported()) {
    if (getCookie("ignoreSupportCheck") || confirm("We are not sure whether your browser will properly handle the AllegroGraph Web View. (Supported are recent versions of Internet Explorer, Firefox, Safari, Chrome, and Opera.) Try anyway?"))
      setCookie("ignoreSupportCheck", "t");
    else
      return;
  }

  defineMoldInstructions();
  initUser(function() {
    loadInfo(function() {
      initQueryStore();
      dhtmlHistory.initialize(doDispatch);
    });
  });
});

// Load site and store info, then call c. ('c' arguments are always continuations).
function loadInfo(c) {
  fetchJSON("site/info", [], setSite, fallback);
  function setSite(data) {
    siteInfo = data;
    if (siteInfo.anonymous || user)
      loadDBInfo(c);
    else {
      setDBInfo(null);
      c();
    }
  }
  function fallback(message, xhr) {
    setDBInfo(null);
    showDialog("Failed to connect to server ('" + message + "'). Retry?",
               partial(loadInfo, c), null, true, true);
  }
}

function loadDBInfo(c) {
  fetchJSON("db/info", [], applyDB, fail);
  function applyDB(data) {
    setDBInfo(data);
    c();
  }
  function giveUp() {
    if (siteInfo && siteInfo.multiple)
      document.location.href = baseURL;
  }
  function fail(message) {
    setDBInfo(null);
    showDialog("Failed to fetch store information ('" + message + "'). Retry?",
               partial(loadDBInfo, c), giveUp, true, true);
  }
}

function showOverview() {
  Mold.cast(getPage(), template("store/overview"), dbInfo);
}

// Whether long parts and graph names are shown. Stored in a cookie
// for easy persistence.
var showGraphNames = getCookie("showGraphNames") == "t",
    showLongParts = getCookie("showLongParts") == "t";

function setBoolCookie(name, val) {
  if (val) setCookie(name, "t");
  else delCookie(name);
}

function toggleGraphNames(state) {
  if (state != showGraphNames) {
    showGraphNames = state;
    setBoolCookie("showGraphNames", showGraphNames);
    refresh();
  }
}

function toggleLongParts(state) {
  if (state != showLongParts) {
    showLongParts = state;
    setBoolCookie("showLongParts", showLongParts);
    clearPartCache();
    refresh();
  }
}

var useReasoning = false;

function toggleReasoning(state) {
  if (state != useReasoning) {
    useReasoning = state;
    refresh();
  }
}
