var gallery = {
  current: 0,
  photos: [],
  docs: [],

  show: function() {
    var arrayPageSize = this.getPageSize();
    $('black_bg').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    new Effect.Appear($('black_bg'), { duration: 0.2, from: 0.0, to: 0.6 });

    $('gallery_wrapper').show();
    gallery.photo(0);
  },

  hide: function() {
    $('gallery_wrapper').hide();
    new Effect.Fade($('black_bg'), { duration: 0.2, from: 0.6, to: 0.0 });
  },

  photo: function(num, url) {
    $$('#g_pages a')[gallery.current].className = '';
    gallery.current = num;
    $$('#g_pages a')[gallery.current].className = 'active';
    var p = $('g_loading');
    p.style.top = ((525 - p.height)/2) + 'px';
    p.style.display = 'block';
    $('g_title').innerHTML = gallery.docs[num];

    p = $('g_photo');
    p.style.display = 'none';
    p.onload = function() {
      $('g_loading').style.display = 'none';
      this.style.display = 'block';
      this.style.top = ((525 - p.height)/2) + 'px';
    };
    p.src = gallery.photos[num];
    preloadNeighborImages();
  },

  previous: function() {
    $('g_photo').style.marginTop = 0;
    var n = gallery.current - 1;
    if (n < 0) n = gallery.photos.length - 1;
    gallery.photo(n);
  },

  next: function() {
    $('g_photo').style.marginTop = 0;
    var n = gallery.current + 1;
    if (n >= gallery.photos.length) n = 0;
    gallery.photo(n);
  },

  preloadNeighborImages: function(){
    var preloadNextImage, preloadPrevImage;
    if (gallery.photos.length > gallery.current + 1){
      preloadNextImage = new Image();
      preloadNextImage.src = gallery.photos[gallery.current + 1];
    }
    if (gallery.current > 0){
      preloadPrevImage = new Image();
      preloadPrevImage.src = gallery.photos[gallery.current - 1];
    }
  },

  getPageSize: function() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    yScroll = yScroll + 15;

    var windowWidth, windowHeight;

    if (self.innerHeight) { // all except Explorer
      if(document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth; 
      } else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    } 

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
      pageWidth = xScroll;
    } else {
      pageWidth = windowWidth;
    }

    return [pageWidth,pageHeight];
  }
};