// ページ初期化
var callback = function(){
  change();
  addPrevNextLink();

	const ancLink = document.getElementsByTagName("a");
	for(idx_a = 0; idx_a < ancLink.length ; idx_a++ ){
		var href_link = ancLink[idx_a].getAttribute("href");
		if ( href_link == null) {
			;
		} else {
			if ( href_link.match(/#/) ) {
				
				ancLink[idx_a].addEventListener('click',function(){
					addJump(this);
				});
			}
		}
	}
};

if (
    document.readyState === "complete" ||
    (document.readyState !== "loading" && !document.documentElement.doScroll)
) {
  callback();
} else {
  document.addEventListener("DOMContentLoaded", callback);
}

function addPrevNextLink(){
  var page_href = location.href;
  
  var tmp_local = page_href.split('.');
  
  if( tmp_local.pop().indexOf("#") > -1 ){
    page_href = page_href.substring(0,page_href.lastIndexOf("#"));
  }
  
  if( page_href.indexOf("?") > -1 ){
    page_href = page_href.substring(0,page_href.lastIndexOf("?"));
  }
  
  var page_location = page_href.split('/');
  
  var page_filename = page_location.pop();
  var page_bookname = page_location.pop();
  page_bookname = page_location.pop();
  
  var prev_page = "";
  var next_page = "";
  
  for( idx_page = 0 ; idx_page < pages.length ; idx_page++ ){
    if( pages[idx_page].fileName == page_filename ){
      if ( books[pages[idx_page].bookId].bookFolderName == page_bookname ){
        for( idx_prev = idx_page - 1; idx_prev >= 0 ; idx_prev-- ){
          prev_page = pages[idx_prev].fileName;
          if( prev_page.indexOf("#") == -1 ){
            if( prev_page.indexOf(".htm") > -1 ){
              var prev_book = books[pages[idx_prev].bookId].bookFolderName;
              prev_link = idx_prev;
              break;
            }
            prev_page = "";
          }
        }
        for( idx_next = idx_page + 1; idx_next < pages.length ; idx_next++ ){
          next_page = pages[idx_next].fileName;
          if( next_page.indexOf("#") == -1 ){
            if( next_page.indexOf(".htm") > -1 ){
              var next_book = books[pages[idx_next].bookId].bookFolderName;
              next_link = idx_next;
              break;
            }
            next_page = "";
          }
        }
        
        var prev_box = document.getElementsByClassName("navi-panel-prev")[0];
        var next_box = document.getElementsByClassName("navi-panel-next")[0];
        
        if( prev_page != "" ){
          if( page_bookname == prev_book ) {
            prev_href = pages[idx_prev].fileName;
          }else{
            prev_href = "../../" + prev_book + "/int/" + prev_page;
          }
          prev_box.setAttribute("href", prev_href);
        }
        
        if( next_page != "" ){
          if( page_bookname == next_book ) {
            next_href = pages[idx_next].fileName;
          }else{
            next_href = "../../" + next_book + "/int/" + next_page;
          }
          next_box.setAttribute("href", next_href);
        }
      }
    }
  }
}

function addJump(a_link){
	var href = a_link.getAttribute("href");
	
	if (href.substring(1,-1) == "#"){
		var target = getElementsByTagName("a");
		if (target.classList.contains("accordion")) {
			var position = target.offsetTop;
			target.addEventListener('click', () =>{
				window.scrollTo({
					top: position,
					behavior: 'smooth'
				});
			});
		}
	}else{
		var linkref = href.substring(2,href.indexOf("#"));
		var target = document.getElementById(href.substring(href.indexOf("#")+1));
		var myname = window.location.href.split('/').pop();
		if( myname.indexOf("#") > -1 ){
			myname = myname.substring(0,myname.indexOf("#"));
		}
		
		if (myname == linkref) {
			if (target.classList.contains("accordion-open")) {
			} else {
				target.classList.toggle("accordion-open");
			}
		}
	}
}

document.addEventListener('click', (e) =>{
	if(e.currentTarget.activeElement.localName == "a"){
		var target_link = document.getElementById(e.currentTarget.activeElement.hash.slice(1));
		if (target_link === null) {
		} else {
			var position = target_link.getBoundingClientRect().top + window.pageYOffset;
			window.scrollTo({
				top: position,
				behavior: 'smooth'
			});
			e.preventDefault();
		}
	}
});