function AniTab() {
  this.use_cache = true;
  this._cache = {};
  this._div;
  this._func;
  this._height;
  this._current;
  this._busy = true;
  this._loading = '';
}


AniTab.prototype = new AjaxBase();


AniTab.prototype.ajaxDone = function(req) {
  if (req.status == 200) {
    this._div.innerHTML = req.responseText;

    if (typeof this._func == 'function') {
      this._func();
    }

    this._func = null;
  }
  else {
    this._div.innerHTML = '<b style="color:#ff0000">ERROR:</b> <a href="' + 
      this.ajax.url + '" target="_blank">Request</a> has status ' + req.status + ' result!';
  }

  if (this.use_cache) {
    this._cache[this.ajax.url] = this._div.innerHTML;
  }
}


AniTab.prototype.set = function(id, height, loading_img_tl) {

  if (!document.getElementById(id)) {
    alert('set(id, height): no such id [' + id + ']');
    return;
  }

  if (height < 1) {
    alert('set(id, height): invalid height [' + height + ']');
    return;
  }

  this._div = document.getElementById(id);
  this._height = height;
  this._busy = false;

  if (loading_img_tl) {
    // @see http://www.ajaxload.info/
    this._loading = document.createElement('img');
    this._loading.src = 'script/lib/AniTab/loading.gif';
    this._loading.setAttribute('style', 'margin-top:' + loading_img_tl[0] + 'px;margin-left:' +  
      loading_img_tl[1] + 'px');
  }
}


AniTab.prototype.goto = function(url) {
  if (url) {
    window.location.href = url;
  }
}


AniTab.prototype._open = function(url) {

  if (url) {
    this._div.innerHTML = '';
    this._current = url;
    this._div.style.display = 'block';
  }

  h = this._div.offsetHeight;

  if (h <= this._height - 10) {
    this._div.style.height = h + 10 + "px";

    me = this;
    window.setTimeout(function() { me._open(); }, 10);
  }
  else {
    this._div.style.height = this._height + 'px';
    this._load(this._current);
  }
}


AniTab.prototype._load = function(url) {

  if (this._cache[url]) {
    this._div.innerHTML = this._cache[url];
  }
  else {
    if (this._loading) {
      this._div.appendChild(this._loading);
    }

    this.ajaxCall(url);
  }

	if (document.getElementById('ani_tab_bg')) {
	  document.getElementById('ani_tab_bg').style.display = 'block';  
	}

  this._current = url;
}


AniTab.prototype._close = function() {
  h = this._div.offsetHeight;

  if (h == this._height) {
    this._div.innerHTML = '';
  }

  if (h > 20) {
    this._div.style.height = h - 20 + 'px';

    me = this;
    window.setTimeout(function() { me._close(); }, 5);
  }
  else {
    this._div.style.height = '0px';
    this._div.style.display = 'none';
  }

	if (document.getElementById('ani_tab_bg')) {
	  document.getElementById('ani_tab_bg').style.display = 'none';  
	}
}


AniTab.prototype.show = function(url, func) {

  if (this._busy) {
    return;
  }

  this._func = func;
  this._busy = true;
  
  is_open = (this._div.style.display == 'block') ? true : false;
  is_current = (this._current == url) ? true : false;

  if (!url) {
    if (is_open) {
      this._close();
    }
  }
  else if (is_current && is_open) {
    this._close();
  }
  else if (is_current && !is_open) {
    this._open(url);
  }
  else if (!is_open) {
    this._open(url);
	}
  else {
    // change content ...
    this._div.innerHTML = '';
    this._load(url);
  }

  this._busy = false;
}


