// JavaScript Document
// ヘッダーの上のナビ部分の表示
$(function() {
    "use strict";
    var window_width = window.innerWidth;

    $(".link-common").click(function() {
        $(".common-sub-nav").slideToggle();
    });
    //スマホメニュー終わり
	
	//ドロワーメニュー開始位置
	var headH = $('.common-nav').height();
    $('.common-sub-nav').css('top', headH);
    $('.common-sub-nav').css('height', "auto");
    
    $("a.link-common").blur(function() {
        window_width = window.innerWidth;
        if ( window_width > 1024){
            $(".common-sub-nav").hide();
        }
    });
	
});

// グローバルナビ
$(function() {
    "use strict";
	var headD = 0;
    var window_width = window.innerWidth;
    var flag_resize = 0;

    $(window).resize(function() {
        window_width = window.innerWidth;
        if ( window_width > 1024){
            $('header .nav-area').css('display', 'block');
            flag_resize = 1;
        }else{
            if ( flag_resize == 1 ){
                $('header .nav-area').css('display', 'none');
                $(".sp_menu").removeClass('active');
            }
        }
    });
    
	// PCナビ
    $(".sub_nav li:last-child a").blur(function() {
        if ( window_width > 1024){
            $(".sub_nav").hide();
        }
    });

    var contents01 = $("li.pc_link01 > div.sub_nav");
    $("li.pc_link01")
    .mouseover(function() {
        if ( window_width > 1024){
            $(contents01).stop().slideDown(500);
        }
    })
    .mouseout(function() {
        if ( window_width > 1024){
            $(contents01).stop().slideUp(500);
        }
    });

    var contents02 = $("li.pc_link02 > div.sub_nav");
    $("li.pc_link02")
    .mouseover(function() {
        if ( window_width > 1024){
            $(contents02).stop().slideDown(500);
        }
    })
    .mouseout(function() {
        if ( window_width > 1024){
            $(contents02).stop().slideUp(500);
        }
    });

    /* タブで操作するときの動作 */
    $("li.pc_link01 a.nav_link").focus(function() {
        if ( window_width > 1024){
            $(".pc_link01_2").slideDown(500);
            $(".pc_link02_2").hide();
        }
    });

    $("li.pc_link02 a.nav_link").focus(function() {
        if ( window_width > 1024){
            $(".pc_link02_2").slideDown(500);
            $(".pc_link01_2").hide();
        }
    });

    $(window).resize(function() {
        window_width = window.innerWidth;
        if ( window_width > 1024){
            //グローバルナビがロゴと重ならないための設定
            var widthL = $('.top_logo').width();
            window_width = window.innerWidth;
            widthL = $('.top_logo').width();
            $('.nav-area').css('max-width', window_width - widthL - 80);
            $('.nav-area').css('width', '');

            //ドロワーメニュー開始位置
            headD = $('.top').height();
            var userAgent = window.navigator.userAgent.toLowerCase();
            if(userAgent.indexOf('msie') != -1 || userAgent.indexOf('trident') != -1) {
                headD = headD - 1;
            }
            $('.sub_nav').css('top', headD);
            $('.sub_nav').css('height', "auto");
        } else {
            $('.nav-area').css('width', '100%');
            $('.nav-area').css('max-width', '');
        }
        
    });
    //スマホメニュー
    $(".sp_menu, .nav-area a.close").click(function() {
        if ( window_width <= 1024){
            $(".nav-area").slideToggle();
            if ($(".sp_menu").hasClass('active')) {
                // activeを削除
                $(".sp_menu").removeClass('active');
            } else {
                // activeを追加
                $(".sp_menu").addClass('active');
            }
        }
        //ドロワーメニュー開始位置
        headD = $('.top').height();
        $('.nav-area').css('top', headD);
    });
    $("#link01 > a").click(function() {
        if ( window_width <= 1024){
            $("#link01 .sub_nav").slideToggle();
            if ($(this).hasClass('active')) {
                // activeを削除
                $(this).removeClass('active');
            } else {
                // activeを追加
                $(this).addClass('active');
            }
        }
    });
    $("#link02 > a").click(function() {
        if ( window_width <= 1024){
            $("#link02 .sub_nav").slideToggle();
            if ($(this).hasClass('active')) {
                // activeを削除
                $(this).removeClass('active');
            } else {
                // activeを追加
                $(this).addClass('active');
            }
        }
    });  
	//スマホメニュー終わり
	
    // グローバルナビがロゴと重ならないための設定 ロード時
    window_width = window.innerWidth;
    if ( window_width > 1024){
        var widthL = $('.top_logo').width();
        window_width = window.innerWidth;
        widthL = $('.top_logo').width();
        $('.nav-area').css('max-width', window_width - widthL - 80);
        $('.nav-area').css('width', '');

        //ドロワーメニュー開始位置
        headD = $('.top').height();
        var userAgent = window.navigator.userAgent.toLowerCase();
        if(userAgent.indexOf('msie') != -1 || userAgent.indexOf('trident') != -1) {
            headD = headD - 1;
        }
        $('.sub_nav').css('top', headD);
        $('.sub_nav').css('height', "auto");
    } else {
        $('.nav-area').css('width', '100%');
        $('.nav-area').css('max-width', '');
    }
    
    
	var headH = $('.nav-area').height();
	//gBizINFOロゴ表示位置
	$('.top_logo').css('height', headH);
	if ( window_width > 1024){
		$('.top_logo').css('height', headH);
	} else {
		headH = $('.nav-area-menu').height();
		$('.top_logo').css('height', headH + 20);
	}
});

//トップへ戻るボタン押下時
$(function() {
    var pagetop = $('#cp-pagetop');   
    pagetop.hide();
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {  //100pxスクロールしたら表示
            pagetop.fadeIn();
        } else {
            pagetop.fadeOut();
        }
    });
    pagetop.click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500); //0.5秒かけてトップへ移動
        return false;
    });
});

//ページ内リンク
//$(function() {
//	$('a[href^="#"]').click(function() {
//		var speed = 500;
//		var href= $(this).attr("href");
//		var target = $(href == "#" || href == "" ? 'html' : href);
//		var position = target.offset().top;
//		$('body,html').animate({scrollTop:position}, speed, 'swing');
//		return false;
//	});
//});

$(function() {
    //トップページタブ切り替え
    //クリックしたときのファンクションをまとめて指定
    $('.tab-lab li').click(function() {
        //.index()を使いクリックされたタブが何番目かを調べ、
        //indexという変数に代入します。
        var index = $('.tab-lab li').index(this);
        //コンテンツを一度すべて非表示にし、
        $('.content li.tab_li').css('display', 'none');
        //クリックされたタブと同じ順番のコンテンツを表示します。
        $('.content li.tab_li').eq(index).css('display', 'block');
        //一度タブについているクラスselectを消し、
        $('.tab-lab li').removeClass('active');
        //クリックされたタブのみにクラスselectをつけます。
        $(this).addClass('active');
    });
    //トップページタブ切り替え終わり
});
    
/* Skip Link
=========================================================================================== */
$(function() {
	$('#sim_search').submit(function(event) {
		// 入力した内容のの前後スペースを除く
		$("#search_Word").val($("#search_Word").val().replace(/^[\s　]+|[\s　]+$/g, ""));
		if ($("#search_Word").val() == null || $("#search_Word").val() == "") {
			alert("条件を入力してください");
			return false;
		}
    });
});

function skiplink(target_id) {

	function focusIn(event) { // eslint-disable-line
		var focus_class = 'focus';
		this.className = focus_class;
		window.scroll(document.documentElement.scrollTop || document.body.scrollTop, 0);
	}
	function focusOut(event) { // eslint-disable-line
		this.className = '';
	}
	function jumpTo(event) {
		var VK_ENTER = 13;
		if ((event.type == 'keydown') && (event.keyCode != VK_ENTER)) return;
		this.className = '';
		var hrefID = this.getAttribute('href').replace(/.*#/g,'');
		window.location.hash = '#' + hrefID;
		var timeID = setTimeout(function(){
			clearTimeout(timeID);
			var target = document.getElementById(hrefID);
			target.setAttribute('href','');
			target.focus();
			target.removeAttribute('href');
		}, 100);
	}

	function setListeners(e) { // eslint-disable-line
		if (document.getElementById(target_id)) {
			var targetAnchor = document.getElementById(target_id).getElementsByTagName('a');
			if (targetAnchor.length > 0) {
				targetAnchor = targetAnchor[0];
				addListener(targetAnchor, 'focus', focusIn, false);
				addListener(targetAnchor, 'blur', focusOut, false);
				addListener(targetAnchor, 'keydown', jumpTo, false);
				addListener(targetAnchor, 'mousedown', jumpTo, false);
			}
		} else {
			return false;
		}
	}

	function addListener(elem, eventType, func, cap) {
		if (elem.addEventListener) {
			elem.addEventListener(eventType, func, cap);
		} else if (elem.attachEvent) {
			elem.attachEvent('on' + eventType, function() {
				func.call(elem, window.event);
			});
		} else {
			return false;
		}
	}

	addListener(window, 'load', setListeners, false);

}

skiplink('page-top');