/* --------------------------------------------------------------------------------------
 * tabselect.js
 * version 1.10
 * @20090323
 * Required common.js ver1.40 later
-------------------------------------------------------------------------------------- */
function fncTabSelect(tabId, contentsId, opts) {
  new VCOMN.TabSelect(tabId, contentsId, opts);
}

VCOMN.TabSelect = function () {
  this.initialize.apply(this, arguments);
}

VCOMN.TabSelect.prototype = {
  HASH_PREFIX: 'tab',
  SELECTED_CLASS: 'ac',
  MOUSEOVER_CLASS: 'on',
  NORMAL_CLASS: '',
  SELECTED_SUFFIX: 'ac',
  MOUSEOVER_SUFFIX: 'on',
  NORMAL_SUFFIX: '',
  COOKIE_NAME: 'vcom_tab_select',
  USE_IMAGE: false,
  initialize: function (tabId, contentsId, opts) {
    opts = opts || {};
    this.opts = {};
    this.opts.hashPrefix = this.setOptValue(opts.hashPrefix, this.HASH_PREFIX);
    this.opts.selectedClass = this.setOptValue(opts.selectedClass, this.SELECTED_CLASS);
    this.opts.normalClass = this.setOptValue(opts.normalClass, this.NORMAL_CLASS);
    this.opts.mouseoverClass = this.setOptValue(opts.mouseoverClass, this.MOUSEOVER_CLASS);
    this.opts.selectedSuffix = this.setOptValue(opts.selectedSuffix, this.SELECTED_SUFFIX);
    this.opts.normalSuffix = this.setOptValue(opts.normalSuffix, this.NORMAL_SUFFIX);
    this.opts.mouseoverSuffix = this.setOptValue(opts.mouseoverSuffix, this.MOUSEOVER_SUFFIX);
    this.opts.cookieName = opts.cookieName || this.COOKIE_NAME;
    this.opts.useImage = this.setOptValue(opts.useImage, this.USE_IMAGE);
    if (this.opts.useImage) {
      var suffixes = [];
      var classes = [this.opts.selectedSuffix, this.opts.normalSuffix, this.opts.mouseoverSuffix];
      for (var i = 0; i < classes.length; i++) {
        if (classes[i]) {
          suffixes.push('_' + classes[i]);
        }
      }
      this.regexp = new RegExp('(' + suffixes.join('|') + ')?(\.[A-Za-z]+)$');// (
    }
    var selected = this._initTab(tabId);
    if (selected === false) return;
    if (this._initContents(contentsId) === false) return;
    var index = this._getHashIndex(location.hash.replace('#', ''));
    if (index === false) {
      index = VCOMN.getCookie(this.opts.cookieName) || false;
    }
    if (index === false) {
      index = selected;
    }
    this.selectTab(index || 0);
    this.history = new VCOMN.History(VCOMN.bindFunc(this.selectTab, this));
  },
  setOptValue: function (value, def) {
    return typeof value != 'undefined' ? value : def;
  },
  _getTabHash: function (index) {
    return this.opts.hashPrefix + ((+index == index) ? (index + 1) : '');
  },
  _getHashIndex: function (hash) {
    var regexp = new RegExp('^' + this._getTabHash());
    if (hash.match(regexp)) {
      var index = hash.replace(regexp, '');
      if (+index !== index) {
        return +index - 1;
      }
    }
    return false;
  },
  _initTab: function (tabId) {
    var selected = undefined;
    var parentElem = document.getElementById(tabId);
    if (!parentElem || !parentElem.childNodes) return false;
    this.tabs = [];
    for (var i = 0; i < parentElem.childNodes.length; i++) {
      var elem = parentElem.childNodes[i];
      if (elem.nodeType != 1) continue;
      if (VCOMN.hasClass(elem, this.opts.selectedClass)) {
        selected = this.tabs.length;
      }
      VCOMN.EventObserve(elem, 'mouseover', VCOMN.bindFunc(this.mouseoverTab, this, elem), false);
      VCOMN.EventObserve(elem, 'mouseout', VCOMN.bindFunc(this.mouseoutTab, this, elem), false);
      VCOMN.addClass(elem, this.opts.normalClass);
      VCOMN.removeClass(elem, this.opts.mouseoverClass);
      var aElem = elem.getElementsByTagName('A');
      if (aElem) {
        var href = aElem[0].href.replace(location.href.replace(/#.*$/, ''), '');
        if (href != '' && href.indexOf('#') != 0) {
          this.tabs[this.tabs.length] = undefined;
          continue;
        }
        aElem[0].onclick = function () {return false;};
      }
      VCOMN.EventObserve(elem, 'click', VCOMN.bindFunc(this.clickTab, this, elem, this.tabs.length), false);
      this.tabs[this.tabs.length] = {elem:elem};
      if (this.opts.useImage) {
        var image = VCOMN.getElementsByClassName('swap', elem);
        if (image.length) {
          image = image[0];
          this.tabs[this.tabs.length - 1].image = image;
        }
      }
    }
    return this.defaultSelect = selected;
  },
  _initContents: function (contentsId) {
    var parentElem = document.getElementById(contentsId);
    if (!parentElem || !parentElem.childNodes) return false;
    this.contents = [];
    for (var i = 0; i < parentElem.childNodes.length; i++) {
      var elem = parentElem.childNodes[i];
      if (elem.nodeType != 1) continue;
      if (!this.tabs[this.contents.length]) {
        this.contents[this.contents.length] = undefined;
      }
      this.contents[this.contents.length] = elem;
    }
  },
  clickTab: function (taretElem, index) {
    window.focus();
    var hash = this.selectTab(index);
    location.hash = hash;
    if (this.history) {
      this.history.setHistory(hash);
    }
  },
  mouseoverTab: function (taretElem) {
    if (this.tabs[this.currentIndex].elem == taretElem) return;
    VCOMN.addClass(taretElem, this.opts.mouseoverClass);
    VCOMN.removeClass(taretElem, this.opts.normalClass);
  },
  mouseoutTab: function (taretElem) {
    if (this.tabs[this.currentIndex].elem == taretElem) return;
    VCOMN.addClass(taretElem, this.opts.normalClass);
    VCOMN.removeClass(taretElem, this.opts.mouseoverClass);
  },
  selectTab: function (index) {
    if (typeof index == 'string') {
      index = this._getHashIndex(index);
      if (index === false) {
        return;
      }
    }
    if (index < 0 || index >= this.tabs.length) {
      index = this.defaultSelect || 0;
    }
    if (this.currentIndex != index) {
      for (var i = 0; i < this.tabs.length; i++) {
        VCOMN.removeClass(this.tabs[i].elem, this.opts.mouseoverClass);
        if (i == index) {
          VCOMN.addClass(this.tabs[i].elem, this.opts.selectedClass);
          VCOMN.removeClass(this.tabs[i].elem, this.opts.normalClass);
          if (this.tabs[i].image && VCOMN.hasClass(this.tabs[i].image, 'swap')) {
            VCOMN.removeClass(this.tabs[i].image, 'swap');
            this.tabs[i].image.onmouseover = null;
            this.tabs[i].image.onmouseout = null;
            this.tabs[i].image.src = this.tabs[i].image.src.replace(this.regexp, '_' + this.opts.selectedSuffix + '$2');
          }
          if (this.contents[i]) {
            this.contents[i].style.display = '';
          }
          this.currentIndex = i;
        } else {
          VCOMN.addClass(this.tabs[i].elem, this.opts.normalClass);
          VCOMN.removeClass(this.tabs[i].elem, this.opts.selectedClass);
          if (this.tabs[i].image && !VCOMN.hasClass(this.tabs[i].image, 'swap')) {
            VCOMN.addClass(this.tabs[i].image, 'swap');
            this.tabs[i].image.onmouseover = function(){
              this.src = this.onSrc;
            }
            this.tabs[i].image.onmouseout = function(){
              this.src = this.offSrc;
            }
            this.tabs[i].image.src = this.tabs[i].image.offSrc;
          }
          if (this.contents[i]) {
            this.contents[i].style.display = 'none';
          }
        }
      }
    }
    var tabName = this._getTabHash(this.currentIndex);
    VCOMN.setCookie(this.opts.cookieName, tabName);
    return tabName;
  }
}


