;(function () {
  'use strict'

  var download = document.getElementById('download')
  const downloadMobile = document.getElementById('mobile-download')

  document.addEventListener('scroll', function () {
    var navbar = document.querySelector('.navbar')

    if (window.scrollY > 0) {
      navbar.classList.add('active')
    } else {
      navbar.classList.remove('active')
    }
  })

  function downloadFile (isMobile) {
    var pathname = window.location.pathname
    var path = pathname.split('/')
    var isInternal = path[4]?.includes('-s')
    var filename = path[3]
      ? path[3] === 'swlab-docs'
        ? 'docs'
        : isInternal
          ? `docs-${path[3]}-s`
          : `docs-${path[3]}`
      : ''
    var directory = path[3] && path[4]
      ? path[3] === 'swlab-docs'
        ? path[3]
        : path[4]
      : ''

    const downloadLink = `https://swlab.bwg.co.kr/web/dms/download-file?filename=${filename}.zip&directory=${directory}`

    if (isMobile) {
      window.open(downloadLink, '_blank')
    } else {
      window.location.href = downloadLink
    }
  }

  // PC 다운로드 버튼
  download.addEventListener('click', () => downloadFile(false))

  // 모바일 다운로드 버튼
  downloadMobile.addEventListener('click', () => downloadFile(true))
})()
