/*! menu.js v1.0 | (c) Toshiba Tec Corporation All rights reserved
 * ・コンテンツページ左側に表示するコンテンツメニュー動作を制御するスクリプト
 */

//本文領域をタップしたらメニューを閉じる
var clickEventType = (( window.ontouchstart!==null ) ? 'click':'touchend');

document.addEventListener(clickEventType, function(event) {
    if(checkHasClassForEvent(event.target, ".topic-inner"))
    {
        if(checkHasClass("layout", "active","id"))
        {
            removeClass("layout","active","id");
        }
    }
});



  
//DOMで書き換えられる内容の中のイベント処理
document.addEventListener("click", function(event) {

    var eventTarget = event.target;
    
    //.menu-iconクリック時
    if(checkHasClassForEvent(eventTarget, ".menu-icon"))
    {

        if(checkHasClass("layout", "active","id"))
        {
            removeClass("layout", "active","id");
        }
        else{
            addClass("layout", "active", "id");
        }
    }
    else if(checkHasClassForEvent(eventTarget, "#menu .has-child"))
    {
        menuSelectedFlag = 'true';

        //スライド対象となる直近の要素を取得
        var next = eventTarget.closest("#menu .has-child").nextElementSibling

        slideToggle(next);
    }


    if(checkHasClassForEvent(eventTarget, "#menu a"))
    {
        menuSelectedFlag = 'true';
        //一度全ての選択状況を解除
        removeClassAll("#menu a","selected");

        if(eventTarget.localName == "li")
        {
            var clickNode = eventTarget.closest("#menu .has-child")

        }
        else
        {
            var clickNode = eventTarget;
        }

        if(clickNode != null)
        {
            //自身を選択中にする
            addClass(clickNode, "selected", "object");

            /*クリックした時、openクラスがあればcloseクラスに変更
            closeクラスだった場合はopenクラスに変更*/
            if (checkHasClassForEvent(clickNode,".open"))
            {
                removeClass(clickNode, "open", "object");
                addClass(clickNode, "close", "object");
            } else if (checkHasClassForEvent(clickNode,".close")) 
            {
                removeClass(clickNode, "close", "object");
                addClass(clickNode, "open", "object");
            }
        }
        

    }


});



//配列に格納されているノードに対して、引数のセレクタが含まれているかチェックする
function checkHasClassForEvent(target, selectors)
{
    if(target.closest(selectors) != null)
    {
        return true;
    }
    else
    {
        return false;
    }
}


function slideDown(element)
{
    duration = 800;

    element.style.removeProperty("display");
    var display = window.getComputedStyle(element).display;

    if (display === "none") {
        display = "block";
    }
    element.style.display = display;

    var height = element.offsetHeight;
    element.style.overflow = "hidden";
    element.style.height = 0;
    element.style.paddingTop = 0;
    element.style.paddingBottom = 0;
    element.style.marginTop = 0;
    element.style.marginBottom = 0;
    element.offsetHeight;
    element.style.transitionProperty = "height, margin, padding";
    element.style.transitionDuration = duration + "ms";
    element.style.transitionTimingFunction = "ease";
    element.style.height = height + "px";
    element.style.removeProperty("padding-top");
    element.style.removeProperty("padding-bottom");
    element.style.removeProperty("margin-top");
    element.style.removeProperty("margin-bottom");

    setTimeout(function(){
        element.style.removeProperty("height");
        element.style.removeProperty("overflow");
        element.style.removeProperty("transition-duration");
        element.style.removeProperty("transition-property");
        element.style.removeProperty("transition-timing-function");
    }, duration);

}

function slideUp(element){

    duration = 300;

    element.style.height = element.offsetHeight + "px";
    element.offsetHeight;
    element.style.transitionProperty = "height, margin, padding";
    element.style.transitionDuration = duration + "ms";
    element.style.transitionTimingFunction = "ease";
    element.style.overflow = "hidden";
    element.style.height = 0;
    element.style.paddingTop = 0;
    element.style.paddingBottom = 0;
    element.style.marginTop = 0;
    element.style.marginBottom = 0;

    setTimeout(function(){
        element.style.display = "none";
        element.style.removeProperty("height");
        element.style.removeProperty("padding-top");
        element.style.removeProperty("padding-bottom");
        element.style.removeProperty("margin-top");
        element.style.removeProperty("margin-bottom");
        element.style.removeProperty("overflow");
        element.style.removeProperty("transition-duration");
        element.style.removeProperty("transition-property");
        element.style.removeProperty("transition-timing-function");
    }, duration);

   }


function slideToggle(element){
    if (window.getComputedStyle(element).display === "none") 
    {
        return slideDown(element);
    } else {
        return slideUp(element);
    }
}



