(function () {
  "use strict";

  var IS_LOCAL  = /(:\/\/localhost|file:\/\/)/.test(document.location.href);

  require.config({
      waitSeconds :   (IS_LOCAL? 10 : 45)
    , paths: {

      // requireJs
        app:            '../js/app'
      , polyfill:       '../js/polyfill'
      , overrides:      '../js/overrides'
      , almond:         '../js/libs/require/almond'
      , text:           '../js/libs/require/text'
      , css:            '../js/plugins/require-css/require-css'
      , normalize:      '../js/plugins/require-css/normalize'
      , json:           '../js/plugins/requirejs-plugins/json'
      , async:          '../js/plugins/requirejs-plugins/async'

      // plugins/libs
      , jquery:         '../js/libs/jquery/jquery'
      , jqm:            '../js/libs/jquery-mobile/jquery-mobile'
      , i18next:        '../js/plugins/i18next/i18next'
      , renderjs:       '../js/plugins/renderjs/renderjs'
      , modernizr:      '../js/plugins/modernizr'
      , selectivzr:     '../js/plugins/selectivzr'
      , jio:            '../js/libs/jio/jio'
      , md5:            '../js/libs/jio/md5'
      , localstorage:   '../js/libs/jio/localstorage'
      , indexstorage:   '../js/libs/jio/indexstorage'
      , complex_queries:'../js/libs/jio/complex_queries'
      , validval:       '../js/plugins/validval/validval'

      // page gadgets
      , address:          '../modules/address/address'
      , basket:           '../modules/basket/basket'
      , browser:          '../modules/browser/browser'
      , config:           '../modules/config/config'
      , confirm:          '../modules/confirm/confirm'
      , description:      '../modules/description/description'
      , details:          '../modules/details/details'
      , footer:           '../modules/footer/footer'
      , gallery:          '../modules/gallery/gallery'
      , header:           '../modules/header/header'
      , index:            '../modules/index/index'
      , item:             '../modules/item/item'
      , items:            '../modules/items/items'
      , login:            '../modules/login/login'
      , media:            '../modules/media/media'
      , payment:          '../modules/payment/payment'
      , refine:           '../modules/refine/refine'
      , register:         '../modules/register/register'
      , search:           '../modules/search/search'
      , searchbar:        '../modules/searchbar/searchbar'
      , seo:              '../modules/seo/seo'
      , shipment:         '../modules/shipment/shipment'
      , sorting:          '../modules/sorting/sorting'
      , storage:          '../modules/storage/storage'
      , summary:          '../modules/summary/summary'
      , teaser:           '../modules/teaser/teaser'
      , terms:            '../modules/terms/terms'
      , translate:        '../modules/translate/translate'
      , pageheader:       '../modules/pageheader/pageheader'
      , pagefooter:       '../modules/pagefooter/pagefooter'
    }
    , shim: {
        'jqm':              { deps: ['jquery'], exports: 'mobile' }
      , 'jio':              { deps: ['md5'] }
      , 'localstorage':     { deps: ['jio', 'complex_queries'] }
      , 'indexstorage':     { deps: ['jio'] }
      , 'renderjs':         { deps: ['jquery'], exports: 'RenderJs' }
      , 'app':              { deps: ['jio'], exports: 'App' }
      , 'validval':         { deps: ['jquery'] }
    }
    , map: {
      '*': {
        'css': '../js/plugins/require-css/css'
      }
    }
  });


  require(['app', 'config'], function(App, Config) {

    var setup = new $.Deferred();
    var spec = {};
    var i;
    var def;

    spec.app = App;
    spec.config = Config;
    spec.setup = setup;

    // fallback for fast browsers...
    spec.fallbackLoader = {
      deferreds: [],
      args: [],
    };

    // expose App once everything is loaded
    spec.setup.done(function(App, Config) {

      // expose configuration - settings
      App.settings = Config.settings;
      // expose configuration - storages
      App.storage = Config.storage;

      for (i = 0; i < window.fallbackLoader.deferreds.length; i += 1) {
        window.fallbackLoader.deferreds[i]
          .resolve(window.fallbackLoader.args[i]);
      }

      // cleanup
      delete window.fallbackLoader;
    });

    spec.configuration_setter = function (spec) {
      spec.config.set(spec);
    };

    // expose fallback
    window.fallbackLoader = spec.fallbackLoader;

    // initialize
    App.initialize(spec);
  });
}());