document.observe('dom:loaded', function() {
  var path = buildUrl('img/');

  var images = $A([
    'install_warning_firefox.gif',
    'install_warning_safari.gif',
    'complete_btn_bg.gif'
  ]).collect(function(s) { return buildImageUrl(s); }); // Add the path to the image names.

  // Preload our background images.
  images.each(function(image) { (new Image()).src = image; });

  // Hide all install content sections
  $$('.installMethodSection').invoke('hide');

  Object.extend(detectionOptions, {
    macInstallerURL: getEnvironmentDownload() + '/autobahn.dmg',
    windowsInstallerURL: getEnvironmentDownload() + '/AutobahnAcceleratorInstall.exe',
    onNotFound: function() {
      // First, we need to update the current autobahn version span.
      $$('#autobahn_version span')[0]
        .update(_('Not Installed'))
        .setStyle({
          color: '#F00'
        });

      $$('#outdated_content')[0].hide();
    },
    onNeedsUpgrade: function() {
      // First, we need to update the current autobahn version span.
      var version = $H(Autobahn.Detections[0].peerVersion).values().join('.');
      if (Autobahn.Detections[0].peerVersion.developmentSnapshot)
        version = 'Development Snapshot';
      $$('#autobahn_version span')[0]
        .update(version)
        .setStyle({
          color: '#B3A61D'
        });

      $$('#outdated_content')[0].show();
    },
    onPassed: function() {
      // First, we need to update the current autobahn version span.
      var version = $H(Autobahn.Detections[0].peerVersion).values().join('.');
      if (Autobahn.Detections[0].peerVersion.developmentSnapshot)
        version = 'Development Snapshot';
      $$('#autobahn_version span')[0]
        .update(version)
        .setStyle({
          color: '#788B34'
        });

      $$('#outdated_content')[0].hide();

      var downloadButton = $$('.bigButton.download')[0];
      var installButton = $$('.bigButton.install')[0];

      var style = {
        backgroundImage: 'url(' + images[2] + ')',
        cursor: 'default'
      };
      var content = '<p class="installedContent">' + _('Installation Complete') + '</p>';

      downloadButton
        .setStyle(style)
        .update()
        .update(content)
        .stopObserving('click');

      installButton
        .setStyle(style)
        .update()
        .update(content)
        .stopObserving('click');
    }
  });

  new Autobahn.Detection(detectionOptions).start();

  if (Autobahn.Utils.isWindows()) {
    $$('#outdated_content span')[0].update(_('download'));
    $$('#download_section')[0].show();

    var downloadButton = $$('.bigButton.download')[0];

    // Observe the click on the download button so we can show the download.
    downloadButton.observe('click', Autobahn.Detections[0].startDownload.bind(Autobahn.Detections[0]));

    downloadButton.down('.infoContent').update('(' + _('Autobahn') + ' ' + Autobahn.Detections[0].options.requiredVersion + ', ' + _('Windows') + ')');
  } else if (Autobahn.Utils.isMac()) {
    $$('#outdated_content span')[0].update(_('install'));
    $$('#install_section')[0].show();

    var installButton = $$('.bigButton.install')[0];

    // Observe the click on the install button so we can redirect to the installer.
    installButton.observe('click', function() {
      document.location = buildLink('/install/mac' + document.location.search);
    });

    installButton.down('.infoContent').update('(' + _('Autobahn') + ' ' + Autobahn.Detections[0].options.requiredVersion + ', ' + _('Mac OSX') + ')');

    // Change the installer warning image to Safari if we are running Safari.
    if (Prototype.Browser.WebKit)
      $$('#install_warning_container')[0].setStyle({ backgroundImage: 'url(' + images[1] + ')' });
  } else if (Autobahn.Utils.isLinux())
    $$('#linux_section')[0].show();
});