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

// Dispatch a URL fragment -- matches it against a bunch of pattern to
// decide which function to call to display the corresponding page.
// Note that the store-selection page on multi-store sites has its own
// dispatch function in multi.js.

function dispatch(fragment) {
  function num(str) {
    return (str == "" || str == null) ? null : Number(str);
  }

  clearPage();
  var match;
  // No dispatching at all on a site that has anonymous access
  // disabled, unless the user is logged in.
  if (!(siteInfo.anonymous || user))
    Mold.cast(getPage(), template("site/noanon"));
  else if (!fragment)
    showOverview();
  else if (match = fragment.match(/^node\/(.+)$/))
    showNode(match[1]);
  else if (match = fragment.match(/^query(?:\/(\d*)?)?$/))
    showQuery(num(match[1]));
  else if (match = fragment.match(/^query\/s\/([^\/]+)(?:\/(.+)?)?$/))
    showUserQuery(match[1], match[2]);
  else if (match = fragment.match(/^query\/db(?:\/(.*)?)?$/))
    showDBQuery(match[1]);
  else if (fragment == "query/recent")
    showRecent();
  else if (match = fragment.match(/^search\/(.*)$/))
    showSearch(match[1]);
  else if (fragment == "namespaces")
    userNamespaces();
  else if (fragment == "users")
    userList();
  else
    showMessage("Resource '" + fragment + "' not found.", true, true);
}
