/*
このページのトップへ
-----------------------------------------------*/

$(function() {
    var topBtn = $('#toTop p span'); 
	 
    //スクロールしてトップに戻る
    //500の数字を大きくするとスクロール速度が遅くなる
    topBtn.click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500);
        //}, 0);
        return false;
    });
});




/*
２度押し防止
-----------------------------------------------*/
function submitonce(theform) {
    if (document.all || document.getElementById) {
        for (i=0; i<theform.length; i++) {
            var tempobj=theform.elements[i];
            if ( tempobj.type) {                // この辺が新しい
                if(tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") {
                    tempobj.disabled=true;
                }
            }  // この辺が新しい
        }
    }
}



/*
TEL
----------------------------------------------- */

$(function(){
    var ua = navigator.userAgent;
    if(ua.indexOf('iPhone') > 0 || ua.indexOf('Android') > 0){
        $('.tel-link').each(function(){
            if($(this).is('img')) {
                var str = $(this).attr('alt');
                $(this).wrap('<a href="tel:'+str.replace(/-/g,'')+'"></a>');
            } else {
                var str = $(this).text();
                $(this).replaceWith('<a href="tel:'+str.replace(/-/g,'')+'">' + str + '</a>');
            }
        });
    }
});




/*
メニューボタン
-----------------------------------------------*/
$(function() {
  $("#panel-btn").click(function() {
    $("#panel").slideToggle(200);
    $("#panel-btn-icon").toggleClass("close");
    return false;
  });
});


$(window).resize(function(){
    var w = window.innerWidth ? window.innerWidth: $(window).width();
    if( w >= 1025) {
        $("#panel").css("display" , "block");
        $("#panel-btn-icon").removeClass("close");
    } else {
        $("#panel").css("display" , "none");
    }
});



/*
マイページ 施設申し込み 完了画面
-----------------------------------------------*/

$(function(){
    var w = window.innerWidth;
    $('.mypageReserve tbody tr').each(function(){
        // 変数
        var trH = $(this).outerHeight() - 17;
        var trClass = $(this).attr('class');

        if (w <= 1024) {
            $(this).find('.today, .days').height(trH);
            if (trClass === 'trHoliday') {
                $(this).find('.today, .days').height(trH + 4);
            }

        } else {
            $('.mypageReserve .today, .mypageReserve .days').removeAttr('style');
        }
    });
});



//リンク書き換えするためのもの
//hostnameがdevelop.localはdockerしかありえないから
$(function() {
    var mode = location.search;
    mode = mode.replace( "?mode=" , "" );

//引数ついてる時用
    if(mode.match(/&/)){
        mode= mode.substring(0,mode.indexOf("&"));
    }

//ページ名で判断する
//ページ名で処理を大きく分類するのでそのための処理
    var tmp = window.location;
    var path = tmp.href.split('/');
    var file_name = path.pop();
    file_name = file_name.substring(0,file_name.indexOf("?"));



    //host取得
    var hostname = window.location.hostname;
    var pathname = window.location.pathname;
    //ファイルネーム取得
    var filename = pathname.replace("/public_html/hokago.netartz.com/hug/","");
    filename=filename.substring(0,filename.indexOf("/"));




    if (hostname == "develop.local"){
        if (filename == "service-record") {
            pathname = pathname.replace("service-record","service-record.php");
            //リダイレクト
            window.location.href = "http://"+hostname+pathname;
            return false;
        }


        if (filename=="article") {
            pathname = pathname.replace("article","article.php");
            //リダイレクト
            window.location.href = "http://"+hostname+pathname;
            return false;
        }


        if (filename=="article-detail") {
            pathname = pathname.replace("article-detail","article-detail.php");
            //リダイレクト
            window.location.href = "http://"+hostname+pathname;
            return false;
        }



        if (filename=="contact-book") {
            pathname = pathname.replace("contact-book","contact-book.php");
            //リダイレクト
            window.location.href = "http://"+hostname+pathname;
            return false;
        }

        if (filename=="contact-book-detail") {
            pathname = pathname.replace("contact-book-detail","contact-book-detail.php");
            //リダイレクト
            window.location.href = "http://"+hostname+pathname;
            return false;
        }
    }
});


//テンプレートから直接submit用処理
$(function(){

    $(document).on("click", ".js_token_form", (function() {

        $(this).prop("disabled", true);

        var mode = $('#mode_token').val();
        var token = $('#csrf_token_from_client').val();
        var hug_page_url = $('#hug_page_url').val();
        var domain = $('#domain').val();
        var check_login = $('#check_login').val();

        var formObj = $(this).closest('form');

        var url = domain + "ajax/ajax_token.php"+"?token="+token+"&mode="+mode+"&hug_page_url="+hug_page_url+"&check_login="+check_login;
        $.get(url, function(data) {
            if (data == 1) {
                $(formObj).submit();
            }else{
                $(".dialogMsg").click();
                return false;
            }
        });
    }));
});


//js側でのsubmit用トークンチェック処理
function doCheckCSRF(){

    var mode = $('#mode_token').val();
    var token = $('#csrf_token_from_client').val();
    var hug_page_url = $('#hug_page_url').val();
    var domain = $('#domain').val();

    var url = domain + "ajax/ajax_token.php"+"?token="+token+"&mode="+mode+"&hug_page_url="+hug_page_url;

    var result = $.ajax({
        type: 'GET',
        url: url,
        async: false
    }).responseText;

    return result;
}



//enter押すと、ボタンクリックさせる
$(function(){
    $('input').keypress(function(e){
        if(e.which == 13) {
            e.preventDefault();
            $(".js_token_form").trigger("click");
        }
    });
});
