/* --------------------------------------------------------------------------------------
 * tabselect.js
 * version 2.00
 * @20110324
 * Required common.js ver1.42 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,
  USE_HISTORY: true,
  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.isCookie = this.setOptValue(opts.isCookie, true);
    this.opts.useImage = this.setOptValue(opts.useImage, this.USE_IMAGE);
    this.opts.useHistory = this.setOptValue(opts.useHistory, this.USE_HISTORY);
    this.opts.tabNames = this.setOptValue(opts.tabNames, null);
    this.opts.noUseHash = this.setOptValue(opts.noUseHash, false);
    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]+)$');// (
    }
    if (typeof VCOMN._tabSelects == 'undefined') {
      VCOMN._tabSelects = [];
    }
    this.hashValueIndex = VCOMN._tabSelects.length;
    this.cookieValueIndex = 0;
    for (var i = 0; i < VCOMN._tabSelects.length; i++) {
      if (this.opts.isCookie && VCOMN._tabSelects[i].getCookieName() === this.opts.cookieName) {
        this.cookieValueIndex++;
      }
    }
    VCOMN._tabSelects.push(this);
    var selected = this._initTab(tabId);
    if (selected === false) return;
    if (this._initContents(contentsId) === false) return;
    var isLoadCookie = false;
    var index = this.opts.noUseHash ? false : this._getHashIndex(location.hash.replace(/^#/, ''), 'hash', true);
    if (index === false && this.opts.isCookie) {
      index = VCOMN.getCookie(this.opts.cookieName) || false;
      isLoadCookie = !!index;
      index = this._getHashIndex(index, 'cookie');
    }
    if (index === false) {
      index = selected;
    }
    this.selectTab(index || 0, null, true, isLoadCookie);
    if (this.opts.useHistory && !this.opts.noUseHash) {
      this.history = new VCOMN.History(VCOMN.bindFunc(this.historyCallback, this));
    }
  },
  setOptValue: function (value, def) {
    return typeof value != 'undefined' ? value : def;
  },
  _getTabHash: function (index) {
    if (this.tabs[index] && this.tabs[index].tabName) {
      return this.tabs[index].tabName;
    }
    if (this.opts.tabNames && typeof this.opts.tabNames != 'string' && this.opts.tabNames[index]) {
      return this.opts.tabNames[index];
    }
    return this.opts.hashPrefix + ((+index == index) ? (index + 1) : '');
  },
  _getHashIndex: function (hash, type, first) {
    if (!first && (hash === '' || hash === false)) {
      return 0;
    }
    if (type) {
      var _index = 0;
      switch (type) {
      case 'hash':
        _index = this.hashValueIndex;
        break;
      case 'cookie':
        _index = this.cookieValueIndex;
        break;
      }
      var hashs = hash.split('/');
      hash = hashs[_index];
    }
    for (var i = 0; i < this.tabs.length; i++) {
      if (this.tabs[i].tabName == hash) {
        return i;
      }
    }
    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 tabName = '';
      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;};
        if (this.opts.tabNames == 'anchor' && href.indexOf('#') == 0) {
          tabName = href.replace(/^#/, '');
        }
      }
      if (tabName === '') {
        tabName = this._getTabHash(this.tabs.length);
      }
      VCOMN.EventObserve(elem, 'click', VCOMN.bindFunc(this.clickTab, this, elem, this.tabs.length), false);
      this.tabs[this.tabs.length] = {elem:elem, tabName:tabName};
      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);
    if (!this.opts.noUseHash && location.hash.replace(/^#/, '') != hash) {
      var elem = document.getElementById(hash);
      if (elem) {
        elem.id += '_';
      }
      location.hash = hash;
      if (elem) {
        setTimeout(function () {elem.id = hash;}, 0);
      }
      if (this.history) {
        this.history.setHistory(hash);
      }
    }
    return false;
  },
  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);
  },
  historyCallback: function (hash) {
    this.selectTab(hash, 'hash');
  },
  selectTab: function (index, type, force, isLoadCookie) {
    if (typeof index == 'string') {
      index = this._getHashIndex(index, type);
      if (index === false) {
        if (force) {
          index = -1;
        } else {
          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 hashs = [];
    var cookieValues = [];
    for (var i = 0; i < VCOMN._tabSelects.length; i++) {
      var tabName = VCOMN._tabSelects[i].getTabName();
      hashs.push(tabName);
      if (this.opts.isCookie && VCOMN._tabSelects[i].getCookieName() === this.opts.cookieName) {
        cookieValues.push(tabName);
      }
    }
    if (this.opts.isCookie && !isLoadCookie) {
      VCOMN.setCookie(this.opts.cookieName, cookieValues.join('/'));
    }
    return hashs.join('/');
  },
  getCookieName: function () {
    return this.opts.isCookie ? this.opts.cookieName : false;
  },
  getTabName: function () {
    return this._getTabHash(this.currentIndex);
  }
}


