// JavaScript Document
//佈景外框高度
//目前編號slidein1
//CSS調整
/*$(document).ready(function(){
	$(".caption div").css("font-size", "50%");   
}
*/





var jstextArr = JSON.parse(jstext);
//開啟收合左區塊
$(document).ready(function() {
  //按鈕開關
  $("body").on("click", "#leftmenubtn", function() {
    if ($(this).attr("name") == 'open') { //開啟	
      openleftblock();        
      $("html,body").animate({
        scrollTop: 0
      }, 900);
    } else { //關閉
      closeleftblock();
    }
  })
  	  
  $("body").on("keydown", "#leftmenubtn", function(event) {
    if (event.key === 'Enter' || event.keyCode === 13) {
      event.preventDefault(); // 防止表單提交或其他預設行為
        if ($(this).attr("name") == 'open') { 
        openleftblock();
        $("html,body").animate({
          scrollTop: 0
        }, 900);
      } else { //關閉    
        closeleftblock();      	
      }
       $("#leftmenubtnbox").focus(); 
    }
  });
  
  $("body").on("keydown", "#leftmenubtnbox", function(event) {
    if (event.key === 'Enter' || event.keyCode === 13) {
      event.preventDefault(); // 防止表單提交或其他預設行為
      openleftblock();
      $("#headerform .list-group a:nth-child(1)").focus(); 
    }
  });
  
  
  
  //區塊外的點擊關閉	 
  $(this).mouseup(function(e) {
    if ($("#leftfloatbox").css("left") == '0px') {
      var container = $("#leftfloatbox"); // 這邊放你想要排除的區塊
      if (!container.is(e.target) && container.has(e.target).length === 0) {
        $("#leftfloatbox").css({
          "animation-name": "leftclose",
          "left": "-350px"
        });
      }
    }
  });
});
//顯示字元限制
$(document).ready(function() {
    $("#sidediv .blocktitle").each(function(index) {
        $idname = $(this).attr("name");
        var str = $("#sidediv .blocktitleto" + $idname).text();
        $("#sidediv .blocktitleto" + $idname).text(str.substr(0, (languagevar == 'en' ? "20" : "8")));
    });
    if($("#bottomrlbox").length == '1'){
    $("#bottomrlbox .blocktitle").each(function(index) {
        $idname = $(this).attr("name");
        var str = $("#bottomrlbox .blocktitleto" + $idname).text();
        $("#bottomrlbox .blocktitleto" + $idname).text(str.substr(0, (languagevar == 'en' ? "20" : "8")));
    });
    }
    if($("#bottomrlbox2").length == '1'){
    $("#bottomrlbox2 .blocktitle").each(function(index) {
        $idname = $(this).attr("name");
        var str = $("#bottomrlbox2 .blocktitleto" + $idname).text();
        $("#bottomrlbox2 .blocktitleto" + $idname).text(str.substr(0, (languagevar == 'en' ? "20" : "8")));
    });
    }
     $("#centertpldiv .blocktitle").each(function(index) {
        $idname = $(this).attr("name");
        var str = $("#centertpldiv .blocktitleto" + $idname).text();
        $("#centertpldiv .blocktitleto" + $idname).text(str.substr(0, (languagevar == 'en' ? "45" : "32")));
    }); 
    $(".contentframe .blocktitle").each(function(index) {
        $idname = $(this).attr("name");
        var str = $(".contentframe .blocktitleto" + $idname).text();
        $(".contentframe .blocktitleto" + $idname).text(str.substr(0, (languagevar == 'en' ? "48" : ($centerdivwcss == 'ldivtrue' ? "34" : "38"))));
    });      
});
//迴圈操作區塊樣式關閉開啟
$(document).ready(function() {
    $(".blockseach").each(function(index) {
        $ajaxsetuptop = $(this).attr("name");
        if ($ajaxsetuptop != undefined) {
            var $ajaxsetuptop_arr = $ajaxsetuptop.split(','); //切割陣列
        }
        var $btn = "#blocksoperating" + $ajaxsetuptop_arr[0] + "";
        $($btn).click(function() {
            $ajaxsetup = $(this).attr("name");
            if ($ajaxsetup != undefined) {
                var $ajaxsetup_arr = $ajaxsetup.split(','); //切割陣列
            }
            $.ajax({
                url: xoopsjsurl + '/modules/neillibrary/ajax.php',
                type: 'POST',
                data: {
                    id: 166,
                    var1: $ajaxsetup_arr[0], //bid
                    var2: $ajaxsetup_arr[1], //狀態
                    var3: $ajaxsetup_arr[2] //title              	            	         	     			       		
                },
                success: function(response) {
                    history.go(0); //更新頁面
                },
                error: function() {
                    console.log('ajax error!');
                }
            })
        });
    });
});
if ($configListenable == '0') {
    //佈景版本判斷
    $(document).ready(function() {
        var $xoopsmodule = "neothemesadmin";
        $.ajax({
            url: xoopsjsurl + '/modules/neillibrary/ajax.php',
            type: 'POST',
            data: {
                id: 126,
                dbid: $themesversion,
                var1: $descriptionurl,
                var2: $downloadurl,
                xoopsmodule: $xoopsmodule
            },
            success: function(response) {
                $('#updatenotice').html(response);
            },
            error: function() {
                console.log('ajax error!');
            }
        })
        //關閉通知
        $("body").on("click", "#kclosebtn", function(e) {
            $("#updatenotice").toggle("slow");
            $.ajax({
                url: xoopsjsurl + '/modules/neillibrary/ajax.php',
                type: 'POST',
                data: {
                    id: 127,
                    dbid: 1
                },
                success: function(response) {},
                error: function() {
                    console.log('ajax error!');
                }
            })
        })
    });
}
//去除nowrap
if ($xoopsdirname == 'tad_uploader') { //只有在tad_uploader模組執行
    $(document).ready(function() {
        $('#sort tr').each(function(index) {
            var $sorttext = $(this).html(); //取得tr中的HTML內容   
            var $sorttextreplace = $sorttext.replace(/nowrap/g, ''); //去除nowrap標籤
            $(this).replaceWith($(this).html($sorttextreplace)); //輸出內容
        });
    });
}

//所有a元素加上另開新視窗
$(document).ready(function() {
    $('a').each(function() {
        var $this = $(this); // 取得當前的<a>元素
        if ($this.attr('target') === '_blank') { // 檢查target是否為_blank
            var currentTitle = $this.attr('title'); // 取得當前的title屬性
            if (currentTitle) {
                var newTitle = ''+jstextArr['onwtext']+', ' + currentTitle; // 在title前添加 "另開新視窗"
                $this.attr('title', newTitle); // 更新title屬性
            } else {
                $this.attr('title', ''+jstextArr['onwtext']+''); // 如果沒有title屬性，就直接設定為 "另開新視窗"
            }
        }
    });
});







//LOGO動畫
function logoAnimationfunction() {
  async function myFunction() {
    await new Promise(resolve => setTimeout(resolve, 0));
    $(".animation #logoimgbox").css({
      "animation-name": "logoimgbox",
      "animation-duration": "0.8s",
      "opacity": "1",
      "transform": "scale(1,1)"
    });
    await new Promise(resolve => setTimeout(resolve, 200));
    $(".animation #logoimgbox1").css({
      "animation-name": "logoimgbox1",
      "animation-duration": "1.3s",
      "opacity": "1",
      "left": "172px"
    });
    await new Promise(resolve => setTimeout(resolve, 300));
    $(".animation #logoimgbox2").css({
      "animation-name": "logoimgbox2",
      "animation-duration": "0.7s",
      "opacity": "1",
      "top": "5px"
    });
    await new Promise(resolve => setTimeout(resolve, 400));
    $(".animation #logoimgbox3").css({
      "animation-name": "logoimgbox3",
      "animation-duration": "0.7s",
      "opacity": "1",
      "left": "195px"
    });
    await new Promise(resolve => setTimeout(resolve, 6000)); //重置動畫
   $(".animation #logoimgbox,.animation #logoimgbox1,.animation #logoimgbox2,.animation #logoimgbox3").css({
      "animation-name": ""
    });      
  }
  myFunction();
}
setInterval(function() {
    $(".animation #logoimgbox,.animation #logoimgbox1,.animation #logoimgbox2,.animation #logoimgbox3").css({
     "opacity": "0"	
    });	
  logoAnimationfunction();
}, 20000);

$(document).ready(function() {
    $("body").on("click", ".webbtnmenu", function() {
        $("#fixmenu").css("z-index", "30");
        $("#jointhelinkbtn").css("z-index", "20");
    })
});
//標語區塊
$(document).ready(function() {
    $("#logobottom").mouseover(function() {
        $("#logotext").css({
            "animation-name": "slidein",
            "opacity": "0.9",
            "display": "block"
        });
     $("#logobottom a").attr('title', '');    //隱藏title  	        	
    });
    $("#logobottom").mouseout(function() {
        $("#logotext").css("display", "none");
    });
});

//開關區塊
//目前按鈕使用ID：slowdivbotton3
//目前區塊使用ID：slowdiv3
$(document).ready(function() {
  $(".eachclass").each(function(index) {
    $("#slowdivbotton" + index + "").css("cursor", "pointer");
    $("#slowdivbotton" + index + "").click(function() {
      $(".slowdiv" + index + "").toggle("slow");
      return false;
    });
    $("body").on("keydown", "#slowdivbotton" + index + "", function(event) {
      if (event.key === 'Enter' || event.keyCode === 13) {
        event.preventDefault(); // 防止表單提交或其他預設行為
        $(".slowdiv" + index + "").toggle("slow");
        if (index == '0') {
          $("#keyword_title").focus();
        }
        if (index == '1') {
          $("#slowdivbotton2").focus();
        }
        if (index == '2') {
          $(".searclabel").focus();
        }
        if (index == '3') {
          $(".searclabel").blur();
        }
        return false;
      }
    });
  });
  const isTabPressed_1 = setupTabKeyDetection("#keywords_box");
  $(".web_keywords_kclose").on("blur", function() {
    if (isTabPressed_1()) { // 僅在使用 Tab 鍵離開時才執行
      $("#keywords_box").hide();
      $("#slowdivbotton0").focus();
    }
  });
  const isTabPressed_2 = setupTabKeyDetection("#xo-searchdiv");
  $(".web_search_kclose").on("blur", function() {
    if (isTabPressed_2()) { // 僅在使用 Tab 鍵離開時才執行
      $("#xo-searchdiv").hide();
      $("#slowdivbotton2").focus();
    }
  });
  const isTabPressed_3 = setupTabKeyDetection("#toolbox");
  // 監聽離開搜尋按鈕的 blur 事件
  $("#slowdivbotton2").on("blur", function() {
    // 僅在使用 Tab 鍵離開時才執行
    if (isTabPressed_3() && $("#xo-searchdiv").css("display") === 'none') {
      $("#addressbox a").attr("tabindex", "0");
      $("#addressbox  a:nth-child(1)").focus();
    }
  });
  const isTabPressed_11 = setupTabKeyDetection("#addressbox");
  $("#addressbox  a:last-child,#addressbox .register_btn").on("blur", function() {
    // 僅在使用 Tab 鍵離開時才執行
    if (isTabPressed_11()) {
       openleftblock(); // 打開左方選單  
      $("#headerform .list-group a:nth-child(1)").focus();
    }
  });
  //離開左滑動區塊
  $("#leftmenubtn").on("blur", function() {
    closeleftblock(); //關閉左方選單     
    $("#addressbox a").attr("tabindex", "-1");
  });
  const isTabPressed_4 = setupTabKeyDetection("#footer");
  //離開下方焦點移轉右按鈕
  $("#goTop").on("blur", function() {
    if (isTabPressed_4()) { // 僅在使用 Tab 鍵離開時才執行
      $("#float_box").focus();
    }
  });
  const isTabPressed_5 = setupTabKeyDetection("#fixmenu");
  //離開右按鈕焦點移轉最上方
  $("#goTop2").on("blur", function() {
    if (isTabPressed_5()) { // 僅在使用 Tab 鍵離開時才執行
      $("html,body").animate({
        scrollTop: 0
      }, 900);
    }
  });
  

  
  $("#first_brick_0,#first_brick_1").on("focus", function() {
    $(this).removeClass("sr-only"); // 移除 sr-only 顯示元素
    $(this).css({
      "margin": "10px 10px",
      "text-align": "center",
      "display": "block"
    });
  });
  $("#first_brick_0,#first_brick_1").on("blur", function() {
    $(this).addClass("sr-only"); // 重新隱藏元素但保留在結構中
       $(this).css({
      "margin": "0px 0px"
    });
  });
  
});

//監聽事件->擊還是Enter的function
//使用isTabPressed_14
function setupTabKeyDetection(id) {
  let isTabKey = false;
  // 監聽 keydown 檢查是否按下 Tab 鍵
  $(document).on("keydown", function(event) {
    if (event.key === 'Tab') {
      isTabKey = true; // Tab 鍵按下時，設置為 true
    }
  });
  // 監聽 mousedown (滑鼠點擊)，將 isTabKey 重置為 false
  $(document).on("mousedown", function() {
    isTabKey = false; // 滑鼠操作時，設置為 false
  });
  // 監聽 focus，延遲重置 isTabKey
  $(id).on("focus", function() {
    // 在這裡直接檢查並設置 isTabKey
    setTimeout(function() {
      isTabKey = false;
    }, 0); // 使用 setTimeout 延遲重置，讓 Tab 事件先處理完
  });
  // 提供方法讓外部檢查目前是否是 Tab 鍵觸發
  return function() {
    return isTabKey;
  };
}

//開啟左方區塊
function openleftblock() {
  $("#leftfloatbox").css({
    "animation-name": "leftopen",
    "left": "0px"
  });
  $("#leftfloatbox").css("min-height", "" + $(document).height() + "px");
}
//關閉左方區塊
function closeleftblock() {
  $("#leftfloatbox").css({
    "animation-name": "leftclose",
    "left": "-350px"
  });
}


//右浮動選單
if ($rmenufloat == true) {
    $(window).scroll(function() {
        //取的頂端位置
        if (($(this).scrollTop()) > '150') {
            $("#fixmenu").css({
                "display": "block",
                "animation-name": "fixmenuslidein",
                "opacity": "1"
            });              	           	
        } else {
            $("#fixmenu").css({
                "display": "none",
                "animation-name": "",
                "opacity": "0"
            });
        }
    });
}
 //內容頁面由標題開始讀取
 $(window).load(function() {
   if ($contentstrue) {
   if ($opver != 'form' && $opver != 'releaseform' && ($opver != 'default' || $xoopsdirname!='neilperiodical')  ) {
       $(window).mousewheel(function(e) {
         if (e.deltaY == 1) {
            css_definition();
         }
       });
     }
     $('#topbox').click(function() {
       css_definition();
     });     
    $("body").on("keydown", "#topbox", function(event) {
        if (event.key === 'Enter' || event.keyCode === 13) {
        event.preventDefault(); // 防止表單提交或其他預設行為
        css_definition();
        $(".top-accesskey").focus();
        }
    });     
   }
 });

//CSS定義涵式
function css_definition() {
  $("#header").css({
    "visibility": "visible",
    "height": "auto"
  });
  $("#menutbl").css({
    "visibility": "visible",
    "height": "55px"
  });
  $("#topbox").hide();
}



//滑動按鈕特效
$(window).scroll(function() {
  if ($tplenutype == '0') return false;
  var $scrollTopa = $(this).scrollTop();
  var position = $('#toplayerdiv').offset();
  var zoomval =$("#toplayerdiv").attr("zoom"); 
  var trigger=(zoomval == '' || zoomval == '1') ? '230' : ('230' * zoomval);  
  if ($scrollTopa > trigger) {
    if ($zoomswitch) {
      //啟用放大縮小
      var $menulocation = (zoomval == '' || zoomval == '1') ? 0 : $scrollTopa / zoomval;
      var $menutop = (zoomval == '' || zoomval == '1') ? -5 : -5;
    } else {
      //不啟用放大縮小
      var $menulocation = 0;
      var $menutop = 0;
      var x = position.left;
      $("#menutbl").css("left", x + "px");
    }
    //啟用滑動效果關閉上方區塊
    $(".slowdiv2").hide();
    $(".slowdiv1").hide();
    if ($browser == 'FF') {
      $("#menutbl").css({
        "animation-name": "slidein1",
        "position": "fixed",
        "margin-top": $menutop + "px",
        "top": $menulocation + "px"
      });
    } else {
      $("#menutbl").css({
        "animation-name": "slidein1",
        "position": "fixed",
        "margin-top": $menutop + "px"
      });
    }
    $("#jGrowl").css("display", "none");
  } else {
    if ($browser == 'FF') {
    $("#menutbl").css({
      "animation-name": "",
      "position": "absolute",
      "margin-top": "161px",
      "left": "0px",
      "top": "0px"
    });     
    }else{
    $("#menutbl").css({
      "animation-name": "",
      "position": "absolute",
      "margin-top": "161px",
      "left": "0px",
    });      
    }
  }
});
$(document).ready(function() {
    $('.di01').html("<a name='neodw' href='javascript:void(0);' onclick='return false;' tabindex='0' class='a11y_box' aria-label=" + $di01 + ">" + $di01 + "</a>");
   if ($iframe == '') {
        $('.di02').html("<a target='_blank' href='https://creativecommons.org/licenses/by-sa/3.0/tw/legalcode' title=" + $di02 + ">" + $di02 + "</a>");
        if ($(".di01").length == 0) {
            $("#toplayerdiv").remove();
        } else if ($(".di02").length == 0) {
            $("#toplayerdiv").remove();
        }
    }
});
$(document).ready(function() {
    $('a[name="neodw"],#close').css("cursor", "pointer");
    $('a[name="neodw"]').click(function() {
        $('html,body').animate({
            scrollTop: $('#neodwboxanchored').offset().top
        }, 600);
        $("#neodwboxcentents").css({
            "animation-name": "slidein",
            "opacity": "0.9"
        });
        $("#neodwbox").show();
    });
    $('#close').click(function() {
        $("#neodwbox").hide();
    });
    $('a[name="neodw"]').mouseover(function() {
        $("#neodwtext").css({
            "animation-name": "slidein",
            "opacity": "0.9",
            "display": "block"
        });
    });
    $('a[name="neodw"]').mouseout(function() {
        $("#neodwtext").css("display", "none");
    });
    $("#goTop,#goTop2").click(function() {
        $("html,body").animate({
            scrollTop: 0
        }, 900);
    });
});