var isSilverlightInstalled = false;
var isAutoBahnInstalled = false;
var enter = function() {
  $('wrapper').hide();
  $('silverlightControlHost').show();
};
var fail = function() {
  $('wrapper').show();
  $('silverlightControlHost').hide();
};
var setSectionContent = function(section, text) {
  if (typeof section === 'string') {
    section = $$('#' + section)[0];
  }
  var content = section.select('div.contentInner')[0];
  content.update(text);
};
var setSectionButtonState = function(section, state) {
  if (typeof section === 'string') {
    section = $$('#' + section)[0];
  }
  var states = ['install', 'installing', 'installed'];
  var button = section.select('li.btn_install')[0];
  if (!button) {
    return;
  }
  states.each(function(state) {
    button.removeClassName(state);
  });
  if (state === 'install') {
    button.addClassName('button');
  }
  button.addClassName(state);
};
var disableSection = function(section) {
  if (typeof section === 'string') {
    section = $$('#' + section)[0];
  }
  var div = new Element('div', {
    className: 'disabled'
  });
  section.insert({
    bottom: div
  });
};
var enableSection = function(section) {
  if (typeof section === 'string') {
    section = $$('#' + section)[0];
  }
  var nextElement = section.down();
  if (nextElement && nextElement.hasClassName('disabled')) {
    nextElement.remove();
  }
  var div = new Element('div', {
    className: 'button enabled'
  });
  section.insert({
    bottom: div
  });
};
var setSectionState = function(name, state) {
  var section = $$('#' + name)[0];
  switch (state) {
  case 'disabled':
    disableSection(section);
    if (name === 'enter') {
      section.stopObserving('click');
    } else {
      section.select('li.btn_install')[0].stopObserving('click');
    }
    break;
  case 'enabled':
    enableSection(section);
    setSectionButtonState(section, 'install');
    if (name === 'enter') {
      section.observe('click', window[name]);
    } else {
      section.select('li.btn_install')[0].observe('click', window['download' + name.capitalize()]);
    }
    break;
  case 'installing':
    enableSection(section);
    setSectionButtonState(section, 'installing');
    break;
  case 'installed':
    enableSection(section);
    setSectionButtonState(section, 'installed');
    section.select('li.btn_install')[0].stopObserving('click');
    break;
  }
};

//
// Autobahn (Swarmcast) no longer required by Setanta
//
/*var processAutobahnDetectionResponse = function(detector) {
  var Detector = Swarmcast.Autobahn.Detection.Detector;
  if (detector.getStatus() === Detector.Status.AUTOBAHN_VERSION_CURRENT) {
    setSectionState('autobahn', 'installed');
    isAutoBahnInstalled = true;
    if (isSilverlightInstalled) {
      setSectionState('enter', 'enabled');
      enter();
    } else {
      fail();
    }
  } else {
    fail();
  }
};
var startAutobahnDetection = function() {
  var Detector = Swarmcast.Autobahn.Detection.Detector;
  
  setSectionState('autobahn', 'enabled');
  Detector.getInstance().start({
    requiredVersion: '4.2.16.96',
    onMissing: processAutobahnDetectionResponse,
    onUpdateRequired: processAutobahnDetectionResponse,
    onVersionCurrent: processAutobahnDetectionResponse
  });
};
var downloadAutobahn = function() {
  var downloadIframe = document.getElementById('__download_iframe');
  
  if (downloadIframe)
    downloadIframe.parentNode.removeChild(downloadIframe);
  
  downloadIframe = document.createElement('IFRAME');
  downloadIframe.id = '__download_iframe';
  downloadIframe.style.cssText = 
    'position:absolute;top:-5px;left:-5px;width:0;height:0;border:0;';
  
  document.body.appendChild(downloadIframe);
   
  if (/win/i.test(navigator.platform))
    downloadIframe.src = 'http://updates.swarmcast.net/swarmcast/current/AutobahnAcceleratorInstall.exe';
  else if (/mac/i.test(navigator.platform))
    downloadIframe.src = 'http://updates.swarmcast.net/swarmcast/current/autobahn.dmg';

  setSectionState('autobahn', 'installing');
};
*/
var startSilverlightDetection = function() {
  setSectionState('silverlight', 'enabled');
  if (Silverlight.isInstalled('2.0.31005.0')) {
    setSectionState('silverlight', 'installed');
    isSilverlightInstalled = true;
  }
  //startAutobahnDetection();
  if (isSilverlightInstalled) {
      setSectionState('enter', 'enabled');
      enter();
  } else {
      fail();
    }
};
var downloadSilverlight = function() {
  setSectionState('silverlight', 'installing');
  Silverlight.onSilverlightInstalled = startSilverlightDetection();
  Silverlight.getSilverlight('2.0.31005.0');
};
document.observe('dom:loaded',
function() {
  var images = $A(['custom_install_btn_install.gif', 'custom_install_btn_installing.gif', 'custom_install_btn_installed.gif']).collect(function(s) {
    return '/images/install/' + s;
  });
  images.each(function(image) { (new Image()).src = image;
  });
  setSectionState('enter', 'disabled');
  Silverlight.__startup();
  startSilverlightDetection();
});