Ajaxinator = function(config) {
  // configurable {
  this.links_selector = ''; // required
  this.links_block_selector = ''; // required
  this.content_selector = ''; // required
  this.loader_image = '/i/ajax-loader.gif';
  // }
  
  $.extend(this, config);
  
  this._init();
}

Ajaxinator.prototype = {
  _init: function() {
    this._initLinksBlock();
    this._initContentsLinks();
  },
  
  _initLinksBlock: function() {
    this.getLinksEls().bind('click', { target: this }, this.onClickLink);
  },
  
  _initContentsLinks: function() {
    this.getContentsLinksEls().bind('click', { target: this }, this.onClickContentsLink);
    $('#print_link').attr('href', $('#print_ref').attr('href'));
    //this.getNavigatorsLinksEls().bind('click', { target: this }, this.onClickContentsLink);
  },
  
  load: function(href) {
    this.scrollToTop();
    this.hideContent(function() {
      this.removeContent();
      this.showLoader();
      this.loadContent(href);
      this.showContent();
    }, this);
  },
  
  scrollToTop: function() {
    $.scrollTo(0);
  },
  
  hideContent: function(callback, scope) {
    if($.browser.msie) {
      if(callback) 
        callback.call(scope ? scope : this);
    } else {
      this.getContentEl().fadeOut('fast', function(){
        if(callback) 
          callback.call(scope ? scope : this);
      });
    }
  },
  
  showContent: function() {
    this.getContentEl().fadeIn('fast');
  },
  
  showLoader: function() {
    this.getContentEl().html('<p align="center"><img src="' + this.loader_image + '" alt="loading..."/></p>');
  },
  
  removeContent: function() {
    this.getContentEl().empty();
  },
  
  onClickLink: function(e) {
    e.data.target._deactivateLinks();
    $(this).addClass('act');
    e.data.target.load(this.href);
    return false;
  },
  
  onClickContentsLink: function(e) {
    e.data.target.load(this.href);
    return false;
  },
  
  loadContent: function(href) {
    var _this = this;
    $.get(href, function(data) {
      _this.setContent(data);
    });
  },
  
  setContent: function(content) {
    this.getContentEl().empty().html(content);
    this._initContentsLinks();
  },
  
  _deactivateLinks: function() {
    this.getLinksEls().removeClass('act'); 
  },
  
  getContentsLinksEls: function() {
    return $(this.links_selector, this.getContentEl());
  },
  
  getNavigatorsLinksEls: function() {
    return $('.nav a', this.getContentEl());
  },
  
  getLinksEls: function() {
    return $(this.links_selector, this.getLinksBlockEl());
  },
  
  getContentEl: function() {
    return $(this.content_selector);
  },
  
  getLinksBlockEl: function() {
    return $(this.links_block_selector);
  }
}

