var _IMGMAN = {
image_ids: new Array(),
image_urls: new Array(),
/*
current_image_ids: new Array(),
current_image_urls:  new Array(),
*/
current_offset: 0,
max_thumbs_to_show: 5,
scrollThumbsUp:
  function() {
    if (this.current_offset > 0) {
      this.current_offset--;
/*
      for (var i=this.current_image_ids.length - 2; i>=0; i--) {
        this.current_image_ids[i+1] = this.current_image_ids[i];
        this.current_image_urls[i+1] = this.current_image_urls[i];
      }
      this.current_image_urls[0] = this.image_urls[this.current_offset];
*/
      this.reloadThumbs();
    }
  },
scrollThumbsDown:
  function() {
    if (this.current_offset + this.max_thumbs_to_show < this.image_ids.length) {
      this.current_offset++;
/*
      for (var i=1, len=this.current_image_ids.length; i<len; i++) {
        this.current_image_ids[i-1] = this.current_image_ids[i];
        this.current_image_urls[i-1] = this.current_image_urls[i];
      }
      this.current_image_urls[len - 1] = this.image_urls[this.current_offset + len - 1];
*/
      this.reloadThumbs();
    }
  },
reloadThumbs:
  function() {
    for (var i=1, len=this.image_ids.length; i<=len; i++) {
      if (i > this.current_offset && i <= (this.current_offset + this.max_thumbs_to_show)) {
        $('#thumb_row_' + i).show();
      } else {
        $('#thumb_row_' + i).hide();
      }
    }
/*
    for (var i=0, len=this.current_image_ids.length; i<len; i++) {
      $('#thumb_' + (i + 1)).get(0).src = this.current_image_urls[i];
      $('#thumb_link_' + (i + 1)).click(function() {_IMGMAN.openGalleryAtThumbnail(this.current_image_ids[i]);return false;});
    }
*/
    /* Show up arrow?  */
    if (this.current_offset > 0) {
      $('#thumb_scroller_up_arrow').show();
      $('#thumb_scroller_up_arrow_off').hide();
    } else {
      $('#thumb_scroller_up_arrow').hide();
      $('#thumb_scroller_up_arrow_off').show();
    }
    /* Show down arrow? */
    if (this.current_offset + this.max_thumbs_to_show < this.image_ids.length) {
      $('#thumb_scroller_down_arrow').show();
      $('#thumb_scroller_down_arrow_off').hide();
    } else {
      $('#thumb_scroller_down_arrow').hide();
      $('#thumb_scroller_down_arrow_off').show();
    }
  },
addImage:
  function(image_id, image_url) { //, add_to_current_images) {
    this.image_ids[this.image_ids.length] = image_id;
    this.image_urls[this.image_urls.length] = image_url;
/*
    if (add_to_current_images) {
      this.current_image_ids[this.current_image_ids.length] = image_id;
      this.current_image_urls[this.current_image_urls.length] = image_url;
    }
*/
  },
openGalleryAtThumbnail:
  function(listing_id, image_id) {
    wopen('/view/listing/gallery/?id=' + listing_id + '&thumb=' + image_id, 'imagegal' + listing_id, 800, 640);
  }
};

function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}
