var DEFAULT_ENV = 'staging';

var API_HOSTS = {
  production: 'https://www.zooniverse.org',
  staging: 'https://panoptes-staging.zooniverse.org',
  development: 'https://panoptes-staging.zooniverse.org',
};

var API_APPLICATION_IDS = {
  production: 'f79cf5ea821bb161d8cbb52d061ab9a2321d7cb169007003af66b43f7b79ce2a',
  staging: '535759b966935c297be11913acee7a9ca17c025f9f15520e7504728e71110a27',
  development: '535759b966935c297be11913acee7a9ca17c025f9f15520e7504728e71110a27',
};

var OAUTH_HOSTS = {
  production: 'https://panoptes.zooniverse.org',
  staging: 'https://panoptes-staging.zooniverse.org',
  development: 'https://panoptes-staging.zooniverse.org',
};

var TALK_HOSTS = {
  production: 'https://talk.zooniverse.org',
  staging: 'https://talk-staging.zooniverse.org',
  development: 'https://talk-staging.zooniverse.org',
};

var SUGAR_HOSTS = {
  production: 'https://notifications.zooniverse.org',
  staging: 'https://notifications-staging.zooniverse.org',
  development: 'https://notifications-staging.zooniverse.org',
};

var STAT_HOSTS = {
  production: 'https://stats.zooniverse.org'
};

var hostFromBrowser = locationMatch(/\W?panoptes-api-host=([^&]+)/);
var appFromBrowser = locationMatch(/\W?panoptes-api-application=([^&]+)/);
var talkFromBrowser = locationMatch(/\W?talk-host=([^&]+)/);
var sugarFromBrowser = locationMatch(/\W?sugar-host=([^&]+)/);
var statFromBrowser = locationMatch(/\W?stat-host=([^&]+)/);

var hostFromShell = process.env.PANOPTES_API_HOST;
var appFromShell = process.env.PANOPTES_API_APPLICATION;
var talkFromShell = process.env.TALK_HOST;
var sugarFromShell = process.env.SUGAR_HOST;
var statFromShell = process.env.STAT_HOST;

var envFromBrowser = locationMatch(/\W?env=(\w+)/);
var envFromShell = process.env.NODE_ENV;

var env = envFromBrowser || envFromShell || DEFAULT_ENV;

if (!env.match(/^(production|staging|development)$/)) {
  throw new Error('Panoptes Javascript Client Error: Invalid Environment; ' +
    'try setting NODE_ENV to "staging" instead of "'+envFromShell+'".');
}

var defaultParams = {
  http_cache: true
}

var query = window.location.search.substring(1);
query = query.split('&');
query.map(function(param) {
  var pair = param.split('=');
  var key = decodeURIComponent(pair[0]);
  var value = decodeURIComponent(pair[1]);
  if (defaultParams[key]) {
    defaultParams[key] = value;
  }
});

module.exports = {
  host: hostFromBrowser || hostFromShell || API_HOSTS[env],
  clientAppID: appFromBrowser || appFromShell || API_APPLICATION_IDS[env],
  talkHost: talkFromBrowser || talkFromShell || TALK_HOSTS[env],
  sugarHost: sugarFromBrowser || sugarFromShell || SUGAR_HOSTS[env],
  statHost: statFromBrowser || statFromShell || STAT_HOSTS[env],
  oauthHost: OAUTH_HOSTS[env],
  params: defaultParams
};

// Try and match the location.search property against a regex. Basically mimics
// the CoffeeScript existential operator, in case we're not in a browser.
function locationMatch(regex) {
  var match;
  if (typeof location !== 'undefined' && location !== null) {
    match = location.search.match(regex);
  }

  return (match && match[1]) ? match[1] : undefined;
}

@@ -2074,24 +2074,86 @@
 he: true%0A%7D%0A%0A
+if (typeof location !== 'undefined' && location !== null) %7B%0A  
 var query = 
@@ -2189,16 +2189,18 @@
 ing(1);%0A
+  
 query = 
@@ -2217,16 +2217,18 @@
 t('&');%0A
+  
 query.ma
@@ -2245,24 +2245,26 @@
 (param) %7B%0A  
+ 
+ 
 var pair = p
@@ -2280,16 +2280,18 @@
 t('=');%0A
+  
   var ke
@@ -2323,16 +2323,18 @@
 ir%5B0%5D);%0A
+  
   var va
@@ -2368,16 +2368,18 @@
 ir%5B1%5D);%0A
+  
   if (de
@@ -2394,24 +2394,26 @@
 ams%5Bkey%5D) %7B%0A
+  
     defaultP
@@ -2438,13 +2438,19 @@
 ;%0A  
+ 
+ 
 %7D%0A
+  
 %7D);
+%0A%7D
 %0A%0Amo

Check for window.location before copying query params