/*!
 * Dark Mode Switch v1.0.1 (https://github.com/coliff/dark-mode-switch)
 * Copyright 2021 C.Oliff
 * Licensed under MIT (https://github.com/coliff/dark-mode-switch/blob/main/LICENSE)
 */

var darkSwitch = document.getElementById("darkSwitch");

//window.addEventListener("load", function () {
  if (darkSwitch) {
    initTheme();
    darkSwitch.addEventListener("change", function () {
      resetTheme();
    });
  }
//});

/**
 * Summary: function that adds or removes the attribute 'data-theme' depending if
 * the switch is 'on' or 'off'.
 *
 * Description: initTheme is a function that uses localStorage from JavaScript DOM,
 * to store the value of the HTML switch. If the switch was already switched to
 * 'on' it will set an HTML attribute to the body named: 'data-theme' to a 'dark'
 * value. If it is the first time opening the page, or if the switch was off the
 * 'data-theme' attribute will not be set.
 * @return {void}
 */
function initTheme() {
  var darkThemeSelected =
    localStorage.getItem("darkSwitch") !== null &&
    localStorage.getItem("darkSwitch") === "dark";
  darkSwitch.checked = darkThemeSelected;
  darkThemeSelected
    ? document.body.setAttribute("data-theme", "dark")
    : document.body.removeAttribute("data-theme");
}

/**
 * Summary: resetTheme checks if the switch is 'on' or 'off' and if it is toggled
 * on it will set the HTML attribute 'data-theme' to dark so the dark-theme CSS is
 * applied.
 * @return {void}
 */
function resetTheme() {
  if (darkSwitch.checked) {
    document.body.setAttribute("data-theme", "dark");
    localStorage.setItem("darkSwitch", "dark");


	if (document.getElementById("sportsframe")) document.getElementById("sportsframe").src = "https://amggsp.com/amg_tVdark/index.html";
	if (document.getElementById("sportsframe_mobile")) document.getElementById("sportsframe_mobile").src = "https://amggsp.com/amg_tVdark/mobile.html";

	if (document.getElementById("btn-register")) document.getElementById("btn-register").href = "/bbs/register.php?dtype=y";
	if (document.getElementById("btn-register2")) document.getElementById("btn-register2").href = "/bbs/member_confirm.php?url=register_form.php&dtype=y";


  } else {
    document.body.removeAttribute("data-theme");
    localStorage.removeItem("darkSwitch");

	if (document.getElementById("sportsframe")) document.getElementById("sportsframe").src = "https://amggsp.com/amg/index.html";
	if (document.getElementById("sportsframe_mobile")) document.getElementById("sportsframe_mobile").src = "https://amggsp.com/amg/mobile.html";

	if (document.getElementById("btn-register")) document.getElementById("btn-register").href = "/bbs/register.php";
	if (document.getElementById("btn-register2")) document.getElementById("btn-register2").href = "/bbs/member_confirm.php?url=register_form.php";
  }
}

window.onload = function(){
	if(darkSwitch.checked){
		if (document.getElementById("sportsframe"))	document.getElementById("sportsframe").src = "https://amggsp.com/amg_tVdark/index.html";
		if (document.getElementById("sportsframe_mobile")) document.getElementById("sportsframe_mobile").src = "https://amggsp.com/amg_tVdark/mobile.html";

		if (document.getElementById("btn-register")) document.getElementById("btn-register").href = "/bbs/register.php?dtype=y";
		if (document.getElementById("btn-register2")) document.getElementById("btn-register2").href = "/bbs/member_confirm.php?url=register_form.php&dtype=y";
	}
};
