define([
    'app'
  , 'storage'
  , 'utilities'
  , 'async'
  ],
  function (App, storage) {
    "use strict";

    var priv = {};
    var that = {};

    // set return object
    that.settings = {};
    that.storage = storage;

    priv.setScreenDimensions = function () {
      var spec = {};

      // breakpoints should be optionable?
      spec.lowerTresh = 320;
      spec.upperThresh = 720;
      spec.framed = "medium";

      if ($.mobile.media("screen and (max-width:20em)") ||
        ($.mobile.browser.oldIE && $(window).width() < o.lowerThresh)) {
          spec.framed = "small";
      } else if ($.mobile.media("screen and (min-width:45em)") ||
        ($.mobile.browser.oldIE && $(window).width() >= o.upperThresh )) {
          spec.framed = "large";
      }
      return spec.framed;
    };

    priv.setApplicationSetters = function (spec) {
      if (spec.response === undefined) {
        // TODO: this defaults could be set somewhere else as well
        spec.config.settings.language_default = "en-EN";
        spec.config.settings.language_current = spec.config.settings.language_default;
        spec.config.settings.language_selector = "translate";
        spec.config.settings.language_set = "false";
        spec.config.settings.item_selector = "item_id";
      } else {
        // create application settings
        spec.config.settings.language_default = spec.response.language_default || "en-EN";
        spec.config.settings.language_current = spec.response.language_current || spec.config.settings.language_default;
        spec.config.settings.language_selector = spec.response.selector || "translate";
        spec.config.settings.language_set = spec.response.language_set || "false";
        spec.config.settings.item_selector = spec.response.item_selector || "item_id";
      }
      spec.config.settings.screen_format = priv.setScreenDimensions();

      // finally...
      spec.setup.resolve(spec.app, spec.config);
    };

    priv.JSONPfallback = function (spec) {
      // bah...
      window.config_call = priv.config_call = function (data) {
        // store settings
        if (data !== undefined) {
          // add to localstorage
          data["_id"] = "config";

          storage.queryStorage({
            "storage": "settings",
            "method": "put",
            "doc": data
          });
        }

        spec.response = data || {};
        // finish him...
        priv.setApplicationSetters(spec);
      };
      // fallback: collect configuration from file
      $.getJSON(
        "http://www.franckreich.de/members/export/x/configuration.json?callback=?",
        function(data) {
          priv.config_call(data);
        }
      );
//       require(
//         ["async!http://www.franckreich.de/members/export/x/configuration.json"],
//         config_call
//       );
    };

    that.set = function (param) {
      var spec = {};

      spec.storage = "settings";
      spec.method = "get";
      spec.doc = {"_id":"config"};

      spec.callback = function(err, response) {
        if (err) {
          priv.JSONPfallback(param);
        }
        param.response = response;
        priv.setApplicationSetters(param);
      }
      storage.queryStorage(spec);
    }

    // return config
    return that;
  }
);
