window.sweKvgStationsIE = {};

(function (self) {
  'use strict'

  /***********
   * Private *
   ***********/

  let _requiredModules = [
      'sweKvgStationsHelper'
    ],
    _suggestedModules = [],
    _filename = 'swe-ie',
    _checked = false,
    _isIE = false,
    _extClass = ''

  /**
   * @private
   */
  function _check () {
    if (_checked) return
    if (!sweKvgStationsDebugger) throw '[' + _filename + '.js] Required namespace "sweKvgStationsDebugger" not found.'

    sweKvgStationsDebugger.checkModules(_requiredModules, 'required', true, _filename)
    sweKvgStationsDebugger.checkModules(_suggestedModules, 'suggested', false, _filename)
    _checked = true
  }

  /**
   * @private
   */
  function _initScrollableTables () {
    sweKvgStationsDebugger.debuggingFunc(sweKvgStationsDebugger.DEBUG_FUNC_START, 'sweKvgStationsIE._initScrollableTables')
    let tables = sweKvgStationsHelper.getElements('.scrollable table')

    for (let i = 0; i < tables.length; i += 1) {
      let table = tables[i],
        tableHeaders = sweKvgStationsHelper.getElements('th', table),
        maxWidth = 0

      for (let j = 0; j < tableHeaders.length; j += 1) {
        let width = tableHeaders[j].offsetWidth

        maxWidth = width > maxWidth ? width : maxWidth
      }

      for (let j = 0; j < tableHeaders.length; j += 1) {
        tableHeaders[j].style.width = maxWidth + 'px'
      }

      table.style.marginLeft = maxWidth + 'px'
    }
    sweKvgStationsDebugger.debuggingFunc(sweKvgStationsDebugger.DEBUG_FUNC_END)
  }

  /**********
   * Public *
   **********/

  /**
   * @return {boolean}
   */
  self.isIE = function () {
    return _isIE
  }

  /**
   * Set up module sweKvgStationsIE.
   */
  self.setup = function (extClass) {
    _check()
    _extClass = extClass
    _isIE = !!window.MSInputMethodContext && !!document.documentMode
    if (!_isIE) return self

    sweKvgStationsHelper.addDomClass(sweKvgStationsHelper.getElement('.' + _extClass), 'ie')
    _initScrollableTables()

    return self
  }

})(window.sweKvgStationsIE)
