var BookSwitcher = Class.create();

BookSwitcher.prototype = {
  timeout: null,
  book: null,
  initialize: function() {
    this.books = $$('#books .book');
    this.books.map(this.bindBook.bind(this));
    this.reset();
  },
  bindBook: function(book) {
    _this = this;
    book.onmouseover = (function(){
      clearTimeout(_this.timeout);
      _this.book = book;
      $$('#books .book').each(function(b){
        if(b != _this.book) b.hide();
      });
      _this.find('img').first().hide();
      _this.find('.description').each(function(p){ p.show(); });
    })
    book.onmouseout = _this._out.bind(_this);
  },
  reset: function() {
    if(!this.book) return;
    this.find('.description').each(function(p){ p.hide(); });
    this.find('img').first().show();
    this.books.each(function(b){
      if(b != _this.book) b.show();
    })
    this.book = null;
  },
  _out: function() {
    this.timeout = setTimeout(this.reset.bind(this), 500);
  },
  find: function(selector) {
    return $$('#'+this.book.id+' '+selector);
  }
};