if (!(typeof(addEvent)=='function')) {
  function addEvent(name,obj,f) {
    if (window.attachEvent) {
      obj.attachEvent("on"+name,f);
    } else if (window.addEventListener) {
      obj.addEventListener(name,f,false);
    }
  }
}

addEvent('load',window,function() {
  var buttons=[
    {'id':'pdfbtn','button':document.getElementById('pdfbtn'),'popup':document.getElementById('pdfpopup')},
    {'id':'audiobtn','button':document.getElementById('audiobtn'),'popup':document.getElementById('audiopopup')},
    {'id':'videobtn','button':document.getElementById('videobtn'),'popup':document.getElementById('videopopup')}
  ];
  var current=-1;

  function removeEvent(name,obj,f) {
    if (window.detachEvent) {
      obj.detachEvent("on"+name,f);
    } else if (window.addEventListener) {
      obj.removeEventListener(name,f,false);
    }
  }

  function fixIE6(popup) {
    // Hack -- Of JS-capable browsers, pretty much just IE6 and below have no
    //         XMLHttpRequest object.
    if (!window.XMLHttpRequest) {
      // Replace backgrounds with filters.
      var ie6divs=popup.getElementsByTagName('div');
      for (var i=0;i<ie6divs.length;i++) {
        var img=ie6divs[i].currentStyle.backgroundImage;
        img=img.replace(/url\("([^)]+)"\)/,"$1");
        if ((img.indexOf('pdfpopup-top.png') > -1) || (img.indexOf('pdfpopup-bottom.png') > -1)) {
          ie6divs[i].style.background='none';
          ie6divs[i].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src='"+img+"');";
        } else if (img.indexOf('pdfpopup-middle.png') > -1) {
          ie6divs[i].style.background='none';
          var shim=document.createElement('div');
          shim.style.position='absolute';
          shim.style.width=''+ie6divs[i].offsetWidth+'px';
          shim.style.height=''+ie6divs[i].offsetHeight+'px';
          shim.style.top='11px';
          shim.style.left='0px';
          shim.style.zIndex=-1;
          shim.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='"+img+"');";
          ie6divs[i].appendChild(shim);
        }
      }
    }
  }

  function closePopup(e) {
    if (buttons[current] && buttons[current].popup) {
      buttons[current].popup.style.visibility='hidden';
    }
    removeEvent('click',document.body,closePopup);
    current=-1;
  }

  function openPopup(e) {
    if (window.event) {
      window.event.cancelBubble=true;
      target=window.event.srcElement;
    } else {
      target=e.target;
    }
    if (e.preventDefault) e.preventDefault();
    if (e.stopPropagation) e.stopPropagation();
    while ((target) && (target.nodeName.toLowerCase() != 'a')) target=target.parentNode;
    if (!target) return;

    if (current != -1) buttons[current].popup.style.visibility='hidden';

    var i=0;
    while ((i<buttons.length) && (buttons[i].id != target.id)) {
      i++;
    }
    if (i<buttons.length) {
      current=i;
      buttons[current].popup.style.visibility='visible';
      buttons[current].button.blur();
      addEvent('click',document.body,closePopup);
    }
    return false;
  }

  function init() {
    for (var i=0;i<buttons.length;i++) {
      if ((buttons[i].button) && (buttons[i].popup)) {
        fixIE6(buttons[i].popup);
        addEvent('click',buttons[i].button,openPopup);
      }
    }
  }

  init();
});