document.observe('dom:loaded', function() {
  $$('h1.flashHeader').each(function(el) {
    // First before we try to replace the headers with the flash header, we will
    // need to make sure Flash is available.
    if (!swfobject.hasFlashPlayerVersion('9')) return;
    
    var flashVars = {
      'content': encodeURIComponent(el.innerHTML),
      'locale': getLocaleLanguage()
    };
    
    el.update();
    el.insert(new Element('span').hide().update(flashVars.content));
    
    var params = {
      allowFullScreen: 'true',
      allowScriptAccess: 'always'    
    };
    
    var o = new Element('div');
    el.insert(o);
    
    swfobject.embedSWF(buildUrl('/swf/header_replacement.swf'), o.identify(), el.getWidth(), '50', '9', '', flashVars, params, {});
    
    el.addClassName('replaced');
  });
  
  $$('dd:last-child').invoke('setStyle', { marginBottom: 0, paddingBottom: 0 });
   
  // Remove padding on last footer list item.
  $$('div#footer li:last-child').invoke('setStyle', { padding: 0 });

  // Observe a click for the set language flags.
  $$('#footer_set_lang').invoke('observe', 'click', setLanguage);
});

function setLanguage(e) {
  var element = Event.element(e);
  
  // If we didn't click an image, then don't continue
  if (!/img/i.test(element.tagName)) { e.stop(); return; }
  
  var newLocale = element.id.split('_')[2]; 
  
  // If we are trying to set the same locale, return.
  if (newLocale == getLocaleLanguage()) {
    e.stop();
    return;
  }
  
  // Get our current path. 
  var pathname = document.location.pathname;
  
  // If the pathname doesn't end with a slash, let's add it.
  if (!pathname.endsWith('/')) pathname += '/';
  
  // First, remove the baseUrl from the pathname
  if (getBaseUrl() != '') pathname = pathname.replace(getBaseUrl() + '/', '/');
  
  // Remove the current locale string from the pathname
  pathname = pathname.replace('/' + getLocaleLanguage() + '/', '/');
    
  // Add the new locale to the pathname
  pathname = '/' + newLocale + pathname;
  
  // Get the current tld from the host.
  var tld = document.location.hostname.replace(/^.*\./, '');
  
  // If the user is trying to load the default locale for the specified tld, then
  // we will want to remove that locale from the pathname.  This allows us to know
  // when a user is on swarmcast.jp and they switch to English (/en/) then back 
  // to Japanese (/) without needing the /ja/ in the path.  Also, if we can't find
  // a tld match and we are setting the locale to English, then we do not need to
  // have /en/ in the path since it's defaulted.
  if ((tldLocales[tld] == newLocale) || ((typeof tldLocales[tld] == 'undefined') && newLocale == 'en'))
    pathname = pathname.replace('/' + newLocale + '/', '/');  
  
  // Redirect the user to the new path.
  document.location = getBaseUrl() + pathname + document.location.search + document.location.hash;
}

function _(string) {
  // Convert HTML to it's entity equiv.
  string = string.escapeHTML();
  
  if (typeof _translations[string] == 'undefined') return string;
  
  return _translations[string];
}

function getBaseUrl() {
  if (typeof $('_base_url') == 'undefined') return '';
  
  return $('_base_url').innerHTML;
}

function getEnvironment() {
  if (typeof $('_environment') == 'undefined') return 'production';
  
  var environment = $('_environment').innerHTML;
 
  if (environment == '') return 'production';
  
  return environment;
}

function getEnvironmentDownload() {
  var environment = getEnvironment();
  environment = (environment == 'production') ? 'current' : environment;
  
  return 'http://updates.swarmcast.net/swarmcast/' + environment;
}

function isCdnEnabled() {
  if (typeof $('_cdn_enabled') == 'undefined') return false;
  
  return ($('_cdn_enabled').innerHTML == '1');
}

function isLocaleEnabled() {
  if (typeof $('_locale_enabled') == 'undefined') return false;
    
  return ($('_locale_enabled').innerHTML == '1');
}

function getLocaleLanguage() {
  if (typeof $('_locale_language') == 'undefined') return 'en';
  
  var language = $('_locale_language').innerHTML;
 
  if (language == '') return 'en';
  
  return language;
}

function buildUrl(filename) {
  if (!filename.startsWith('/')) filename = '/' + filename;
  
  // Use relative paths
  return getBaseUrl() + filename;
}

function buildCssUrl(filename) {
  filename = '/css/' + filename;
  
  return buildUrl(filename);
}

function buildJsUrl(filename) {
  filename = '/js/' + filename;
  
  return buildUrl(filename);
}

function buildImageUrl(filename, translate) {
  if (typeof translate == 'undefined') translate = true;
    
  filename = '/img/' + filename;  
  
  if ((getLocaleLanguage() != 'en') && (translate))
    filename = filename.replace(/(\..*)$/, '-' + getLocaleLanguage() + '$1');
    
  return buildUrl(filename);
}

function buildLink(path) {
  var baseUrl = getBaseUrl();
  
  if (isLocaleEnabled() && (getLocaleLanguage() != 'en')) 
    baseUrl += '/' + getLocaleLanguage();
    
  return baseUrl + path;
}