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

// Specific functionality for the store-selection page. Replaces the
// stuff in app.js and dispatch.js for that page.

// Limited dispatch.
function dispatch(fragment) {
  clearPage();
  if (!(siteInfo.anonymous || user))
    Mold.cast(getPage(), template("site/noanon"));
  else if (!fragment)
    showStoreList();
  else if (fragment == "users")
    userList();
  else
    showMessage("Resource '" + fragment + "' not found.", true, true);
}

connect(window, "load", function() {
  defineMoldInstructions();
  initUser(function () {
    loadInfo(function() {dhtmlHistory.initialize(doDispatch);});
  });
});

function loadInfo(c) {
  fetchJSON("site/info", [], success, failure);
  function success(info) {
    siteInfo = info;
    siteInfo.showStoreList = true;
    initLayout();
    c();
  }
  function failure(message, xhr) {
    initLayout();
    showDialog("Could not connect to server (" + message + "). Retry?",
               refresh, null, true, true);
  }
}

function showStoreList() {
  fetchJSON("site/stores", [], success, failure);
  Mold.cast(getPage(), template("message/loading"));
  function success(stores) {
    Mold.cast(getPage(), template("site/stores"), stores);
  }
  function failure(message) {
    clearPage();
    failMessage("Loading store list", message);
  }
}

// Site configuration

function setGoogleKey(key) {
  activityPost("site/googlekey", [key], "Saving key", function() {siteInfo.googleKey = key;}, false);
}

function setPrologEnabled(enabled) {
  activityPost("site/prolog", [enabled], "Changing Prolog setting", function() {siteInfo.prolog = enabled;}, false);
}

function setAnonymousAccess(enabled) {
  activityPost("site/access", [enabled, siteInfo.newAccounts], "Changing anonymous setting",
               function() {siteInfo.anonymous = enabled;}, false);
}

function setNewAccounts(enabled) {
  activityPost("site/access", [siteInfo.anonymous, enabled], "Changing new account setting",
               function() {siteInfo.newAccounts = enabled;}, false);
}

function setSiteTimeout(amount) {
  var number = Number(amount);
  if (isNaN(number) || number <= 0)
    showMessage("'" + amount + "' is not a valid amount of seconds.", false, true);
  else
    activityPost("site/timeout", [number], "Saving timeout", function(){siteInfo.timeout = number;}, false);
}
