// requires mootools
function addIEStyleOverride() {

  var d = document;
  var h = $(d.getElementsByTagName('head')[0]);
  var l = new Element(d.createElement('link'), {
      'rel': 'stylesheet',
      'type' : 'text/css',
      'href' : 'modules/mod_smooth_gallery_hedame/gallery-hedame-IE.css'
      });
  h.appendChild(l);

}
if (navigator.userAgent.match(/MSIE/)) { 
  window.addEvent('domready', addIEStyleOverride);
}

// requires mootools
function startGallery() {
  var myGallery = new gallery($('myGallery'), {
    timed: true,
    showArrows: false,
    showCarousel: false,
    embedLinks: false
  });
}
window.addEvent('domready', startGallery);

// requires mootools
function addOnClick() {
  var div = $(document.getElementById('myGallery'));
  if (div) {
    div.addEvent('click', function() {
      sg_displayProfileImage(this);
    });
  }
}
window.addEvent('domready', addOnClick);

// requires mootools
function sg_displayProfileImage(div) {

  var width = 640 + 10;
  var height = 640 + 10;
  piw = window.open('', 'ProfileImageWindow', 'location=no,menubar=no,status=no,toolbar=no,width=' + width + ',height=' + height + ',top=' + (screen.height - height - 80) / 2 + ',left=' + (screen.width - width) / 2);

  var d = piw.document;

  // remove any previous content (if $user clicks again on a profile image
  // while this window is still open fex)
  d.close();
  // whats the proper way to generate the body?
  d.open();
  d.close();
//  d.bgColor = '#f3f2e7';

  var divs = div.getElementsByTagName('div');
  var bgurl;
  for (i = 0; i < divs.length; i++) {
    s = divs[i].style;
    if (s.opacity == 1) {
      bgurl = s.backgroundImage.replace(/^url\(|\)$/g, '');
    }
  }

  b = $(d.body);
  b.setStyles('height: 100%; background: black url(' + bgurl.replace(/-sg\.jpg$/, '.jpg') + ') center fixed no-repeat;');
  b.addEvent('click', function() { piw.close(); });

}
