YAHOO.util.Event.onDOMReady(function() {
  
  var Photoshow = function(config) {
    this.init(config);
  };
  
  Photoshow.prototype = {
    
    init: function(config) {
      
      var Dom = YAHOO.util.Dom,
          Event = YAHOO.util.Event;
      
      this.photos = [];
      this.index = -1;
      if (config.classname) {
        var photos = Dom.getElementsByClassName(config.classname);
        for (var i=0; i<photos.length; i++) {
          this.photos.push({elem: photos[i], url: photos[i].href, link: photos[i].href, caption: photos[i].title});
        }
      } else if (config.photos && config.photos.length) {
        this.photos = config.photos;
      }
      
      for (var i=0; i<this.photos.length; i++) {
        Event.addListener(this.photos[i].elem, 'click', function(e,i) {
          Event.preventDefault(e);
          this.show(i);
        }, i, this);
      }
      
      document.body.appendChild(this.makeNode(
        {tag: 'div', id: 'photoshow-overlay'}
      ));
      
      document.body.appendChild(this.makeNode(
        {tag: 'div', id: 'photoshow-container', children: [
          {tag: 'div', id: 'photoshow-frame', children: [
            {tag: 'a', id: 'photoshow-link', children: [
              {tag: 'img', id: 'photoshow-image'}
            ]},
            {tag: 'div', id: 'photoshow-loading'},
            {tag: 'div', id: 'photoshow-caption'}
          ]}
        ]}
      ));
      
      this.overlay   = document.getElementById('photoshow-overlay');
      this.container = document.getElementById('photoshow-container');
      this.frame     = document.getElementById('photoshow-frame');
      this.link      = document.getElementById('photoshow-link');
      this.image     = document.getElementById('photoshow-image');
      this.caption   = document.getElementById('photoshow-caption');
      this.loading   = document.getElementById('photoshow-loading');
      
      Event.addListener(this.overlay, 'click', this.close, this, true);
      Event.addListener(this.container, 'click', this.close, this, true);
      Event.addListener(this.link, 'click', Event.stopPropagation);
      
      Event.addListener(this.image, 'load', function() {
        this.image.style.display = 'block';
        this.loading.style.display = 'none';
        this.caption.innerHTML = this.photos[this.index].caption || '';
        if (this.caption.innerHTML) this.caption.style.display = 'block';
        this.link.href = this.photos[this.index].link || this.photos[this.index].url;
      }, this, true);
      
      Event.addListener(document, 'keydown', function(e) {
        var escape = e.DOM_VK_ESCAPE ? e.DOM_VK_ESCAPE : 27;
        if (e.keyCode == escape) this.close();
      }, this, true);
    },
    
    show: function(index) {
      
      var height = this.getHeight();
      var scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      
      this.overlay.style.height = height.page+'px';
      this.container.style.top = (scroll+height.viewport/10)+'px';
      this.overlay.style.display = 'block';
      this.container.style.display = 'block';
      
      if (this.index == index) {
        this.image.style.display = 'block';
      } else {
        this.index = index;
        this.image.src = this.photos[index].url;
        this.image.style.display = 'none';
        this.caption.style.display = 'none';
        this.loading.style.display = 'block';
      }
    },
    
    close: function() {
      this.overlay.style.display = 'none';
      this.container.style.display = 'none';
    },
    
    makeNode: function(node) {
      var n = document.createElement(node.tag);
      n.id = node.id;
      if (node.children && node.children.length) {
        for (var i=0; i<node.children.length; i++) {
          n.appendChild(this.makeNode(node.children[i]));
        }
      }
      return n;
    },
    
    getHeight: function() {
	    
	    var yScroll, height;
      
      if (window.innerHeight && window.scrollMaxY)
        yScroll = window.innerHeight + window.scrollMaxY;
      else if (document.body.scrollHeight > document.body.offsetHeight)
        yScroll = document.body.scrollHeight;
      else
        yScroll = document.body.offsetHeight;
      
      if (window.innerHeight)
        height = window.innerHeight;
      else if (document.documentElement && document.documentElement.clientHeight)
        height = document.documentElement.clientHeight;
      else if (document.body)
        height = document.body.clientHeight;
      
      return {viewport: height, page: (yScroll > height) ? yScroll : height};
    }
  };
  
  var photos = YAHOO.util.Dom.getElementsByClassName('photoshow'),
      config = {photos: []};
  for (var i=0; i<photos.length; i++) {
    var id = photos[i].firstChild.src.match(/farm(\d)[^\/]*\/([^\/]+)\/([^_]+)_([^\_]+)/);
    var img = 'http://farm'+id[1]+'.static.flickr.com/'+id[2]+'/'+id[3]+'_'+id[4]+'.jpg';
    config.photos.push({elem: photos[i], url: img, link: photos[i].href});
  }
  
  new Photoshow(config);
});