
function $(el) {
  if (typeof(el) == 'string') {
    return document.getElementById(el);
  } else {
    return el;
  }
}

function Photos(img, prev, next) {
  this.img = $(img);
  this.prev_btn = $(prev);
  this.next_btn = $(next);

  this.current = 0;

  this.items = [];
  
  this.add = function(url) {
    this.items.push({url: url});
    this.preload(this.items[this.items.length - 1]);
  }

  this.preload = function(item) {
    item['img'] = new Image();
    item['img'].src = item.url;
  }

  this.display = function() {
    try {
      var img = this.items[this.current].img;
      this.img.src = img.src;
      
      this.prev_btn.style.display = (this.current > 0)?'inline':'none';
      this.next_btn.style.display = (this.current < (this.items.length - 1))?'inline':'none';
    } catch(e) {}
  }

  this.next = function() {
    if (this.current < (this.items.length - 1)) {
      this.current++;
      this.display();
    }
  }

  this.prev = function() {
    if (this.current > 0) {
      this.current--;
      this.display();
    }
  }
}

function addEvent(event, fn) {
  if (window.attachEvent) {
    window.attachEvent('on' + event, fn);
  } else if (window.addEventListener) {
    window.addEventListener(event, fn, false);
  }
}
