var showMacContent = navigator.platform.indexOf('Mac') != -1

// we're observing window.onload instead of document.dom:loaded because we're serving xml files
Event.observe(window, 'load', addPlatformToggler)
Event.observe(window, 'load', attachHandlers)

function attachHandlers(event) {
  if ($('showform')) {
    $('showform').observe('click', function(e) {
      $('prompt').toggle()
      $('form').toggle()
      e.stop()
    })
  }
}

function addPlatformToggler(event) {
  // TODO consider precalculating this value in xslt
  var isDocMultiPlatform = $$('.win').size() > 0 || $$('.mac').size() > 0
  if (isDocMultiPlatform) {
    var link = new Element('a')
    link.id = 'ostoggle'
    link.href = '#'
    link.observe('click', togglePlatform)
    link.update(' show the ' + (showMacContent ? 'Windows' : 'Mac') + ' version of this page')
    $('title').insert({'bottom': link})
  }
  
  showMacContent = !showMacContent
  togglePlatform(null)
}

function togglePlatform(event) {
  showMacContent = !showMacContent
  
  var taggableElements = $w("span div li")
  
  $$('.win').each(function (elem) {
    if (!elem.hasClassName('mac') && taggableElements.indexOf(elem.tagName.toLowerCase()) != -1)
      showMacContent ? elem.hide() : elem.show()
  })
  
  $$('.mac').each(function (elem) {
    if (!elem.hasClassName('win') && taggableElements.indexOf(elem.tagName.toLowerCase()) != -1)
      showMacContent ? elem.show() : elem.hide()
  })
  
  $$('img.mac').each(function (elem) {
    if (showMacContent) {
      elem.src = elem.src.replace('images/', 'images/mac/')
    }
    else {
      elem.src = elem.src.replace('images/mac/', 'images/')
    }
  })
  
  if ($('ostoggle')) {
    $('ostoggle').update(' show the ' + (showMacContent ? 'Windows' : 'Mac') + ' version of this page')
  }
  
  if (event != null) {
    event.stop()
  }
}
