/**
 * Clinic addres block
 * @param {Object} config
 */
ClinicBlock = function(config) {
  // configurable {
  this.selector = ''; // required
  this.x = 0;
  this.height = 100;
  // }
  
  $.extend(this, config);
}

ClinicBlock.prototype = {
  animatedShow: function() {
    this.hideContent();
    this.getEl().css('top', this.x + this.height).height(0);
    this.show();
    this.getEl().animate({
      top: this.x,
      height: this.height
    }, 'fast', 'linear', this.showContent.createDelegate(this));
  },
  
  show: function() {
    this.getEl().stop().show();
  },
  
  animatedHide: function() {
    this.hideContent();
    this.getEl().css('top', this.x).height(this.height);
    this.show();
    this.getEl().animate({
      top: this.x + this.height,
      height: 0
    }, 'fast', 'linear', this.hide.createDelegate(this));
  },
  
  hide: function() {
    this.getEl().stop().hide();
  },
  
  showContent: function() {
    this.getContentEl().show();
  },
  
  hideContent: function() {
    this.getContentEl().hide();
  },
  
  getEl: function() {
    return $(this.selector);
  },
  
  getContentEl: function() {
    return $('.cont', this.getEl());
  }
}


/**
 * Clinic block manager
 * 
 * @param {Object} config
 */
ClinicBlockManager = function(config) {
  // configurable {
  // [ { activator: selector, block: ClinicBlock }, {...} ]
  this.blocks = [];
  this.auto_init = false;
  this.switch_delay = 5000;
  // }
  this.activated_block = -1;
  
  $.extend(this, config || {});
  
  if(this.auto_init)
    this.init();
}

ClinicBlockManager.prototype = {
  init: function() {
    this._initBlocks(); 
    this._initActivators();
    this._showActivated();
  },
  
  _initBlocks: function() {
    for(var i = 0, l = this.blocks.length; i < l; i ++) {
      this.blocks[i].block.hide();
    }
  },
  
  _initActivators: function() {
    for(var i = 0, l = this.blocks.length; i < l; i++) {
      var selector = this.blocks[i].activator;
      if(!selector)
        continue;
      
      $(selector).bind('mouseover', { num: i }, this.onMouseOver.createDelegate(this));
    }
  },
  
  _showActivated: function() {
    for(var i = 0, l = this.blocks.length; i < l; i ++) {
      if(this.blocks[i].activated) {
        this.switchTo(i);
        break;
      }
    }
  },
  
  switchTo: function(num, fast_hide) {
    if(this.activated_block == num)
      return;
       
    if(this.activated_block >= 0) {
      if(fast_hide)
        this.getBlock(this.activated_block).hide();
      else
        this.getBlock(this.activated_block).animatedHide();
    }
      
    this.activated_block = num;
    this.getBlock(num).animatedShow();
  },
  
  getBlock: function(num) {
    return this.blocks[this.activated_block].block;
  },
  
  onMouseOver: function(event) {
    this.switchTo(event.data.num, true);
  }
}

