$.gallery = function() {
  window.timer=null;
  window.hideCaption=null;
  $("#wonk1").after($("<div />").attr("id","galleryNav"));
  $("#galleryNav").append($("<a />").attr("id","navLeft"));
  $("#galleryNav").append($("<a />").attr("id","navRight"));

  current=window.location.hash?window.location.hash:"#i1";

  $(current+' .caption').show().css({opacity:.999});

  $galleryimg = $("td.slide img");
  total = $galleryimg.length;
  pos=parseInt(current.slice(2))-1;

  //ie bug somewhere right here:
  $galleryimg.css("opacity",0).each(function(i) {
    var self = $(this);
    self.parent().addClass('loading');
    if (i!=pos) {
      self.attr("original",self.attr("src"));
    }
    self.attr("src",self.attr("src"));
  }).bind("load", function() {
    $(this).parent().removeClass('loading');
    $(this).animate({opacity:1},500);
  });

  $.localScroll({ axis:'x', duration:500, hash:true });

  $(document).keydown(function(e) {
    if(e.which==37|e.which==75) {
      navLeft();
      clearTimeout(window.hideCaption);
      window.hideCaption = setTimeout(function() {$('.caption').animate({opacity:.001},200)}, 5000);
      return false;
    }
    else if(e.which==39|e.which==74) {
      navRight();
      clearTimeout(window.hideCaption);
      window.hideCaption = setTimeout(function() {$('.caption').animate({opacity:.001},200)}, 5000);
      return false;
    }
  });

  window.hideArrowL=setTimeout(function() {$('#navLeft').stop().animate({opacity: .05}, 200) }, 5000);
  window.hideArrowR=setTimeout(function() {$('#navRight').stop().animate({opacity: .05}, 200) }, 5000);

  $captions = $('span.caption');

  $('#navLeft')
  .bind("mousemove click",function(event) {
    clearTimeout(window.hideArrowL);
    if (parseInt(current.slice(2))>1) {
      $('#navLeft').stop().css({opacity:1});
    }
    else
    $('#navLeft').stop().css({opacity:.05});
    $captions.stop().css({opacity:.999});
    window.hideArrowL=setTimeout(function() {
      $('#galleryNav a#navLeft').stop().animate({opacity: .05}, {queue:false, duration:200});
      $captions.animate({opacity:.001},200);
      }, 5000);
    }).bind("mouseout",(function(event) {
      clearTimeout(window.hideArrowL);
      window.hideArrowL=setTimeout(function() {$('#galleryNav a#navLeft').stop().animate({opacity: .05},{queue:false, duration:200})}, 10);      
    })
  ).click(function(){
    navLeft();
    return false;
  });

  $('#navRight')
  .bind("mousemove click",function(event) {
    clearTimeout(window.hideArrowR);
    if (parseInt(current.slice(2))<total)
    $('#navRight').stop().css({opacity:1});
    else {
      $('#navRight').stop().css({opacity:.05});
    }       
    $captions.stop().css({opacity:.999});
    window.hideArrowR=setTimeout(function() {
      $('#galleryNav a#navRight').stop().animate({opacity: .05}, {queue:false, duration:200});
      $captions.animate({opacity:.001},200);
      }, 5000);
    }).bind("mouseout",(function(event) {
      clearTimeout(window.hideArrowR);
      window.hideArrowR=setTimeout(function() {$('#galleryNav a#navRight').stop().animate({opacity: .05},{queue:false, duration:200})}, 10);      
    })
  ).click(function(){
    navRight();
    return false;
  });

  $(window).scroll(function() {
    $captions.css("opacity",.001);
      clearTimeout(window.timer);
      current = "#i"+(Math.ceil(
        ($(window).scrollLeft()+$('#i1').width()/2)/$('#i1').width()
      ));
    window.timer=setTimeout(moveIt, 500, current);
  });

  function navRight() {
    current = '#i'+Math.min((parseInt(current.slice(2))+1),total);
    moveIt(current);
  }
  function navLeft() {
    current = '#i'+Math.max((parseInt(current.slice(2)))-1,1);
    moveIt(current);
  }

  function moveIt(loc) {
    $captions.hide();
    $(window).scrollTo(loc, 500, {
      onAfter: function() {
        $(loc + ' span.caption').show().animate({opacity:.999},{queue:false,duration:200});
        if (!($.browser.msie && $.browser.version.substr(0,1)<8))
          window.location.hash=loc;
        setTimeout(function() {clearTimeout(window.timer);},1);
      }
    });
  }
}