
(function($) {

    "use strict";
    var setTimer;
    var methods = {
            init: function (options) {
                return this.each(function () {

                });
            }
        }

    $.fn.countdown = function startTimer(options){
        methods[this];
        var attrId = $(this).attr('id');
        if(attrId == undefined){
            return;
        }
        setTimer=setInterval(function(){
            const japanStandardTime = new Date().toLocaleString({ timeZone: 'Asia/Tokyo' });
            var date1 = new Date(japanStandardTime);

            //var date2 = new Date(2021, 3, 28, 13, 20, 0);
            options.date = options.date.replace("-","/");
            var date2 = new Date(options.date);
            var termDay = date2.getTime() - date1.getTime();
            var day = Math.floor(termDay / (1000 * 60 * 60 * 24));
            var hour = Math.floor(termDay / (1000 * 60 * 60));
            hour = hour - 24 * day;
            var minute = Math.floor(termDay / (1000 * 60));
            minute = minute - day * 24 * 60 - hour * 60;
            var second = Math.floor(termDay / 1000);
            second = second- day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;

            if(day < 0){
                day = 0;
            }else if(day <= 9){
                day = "0"+day;
            }
            if(hour < 0){
                hour = 0;
            }else if(hour <= 9){
                hour = "0"+hour;
            }
            if(minute < 0){
                minute = 0;
            }else if(minute <= 9){
                minute = "0"+minute;
            }

            if(second < 0){
                second = 0;
            }else if(second <= 9){
                second = "0"+second;
            }
            if(termDay <= 0){
                stopTimer();
            }

            if(termDay <= 0){
                $("#"+attrId).html("<span class='cntdwtmr-day'></span> <span class='cntdwtmr-hour'>00:</span><span class='cntdwtmr-minute'>00:</span><span class='cntdwtmr-second'>00</span>");
            }else{
                if(options.locale == "ja"){
                    $("#"+attrId).html("<span class='cntdwtmr-day'>"+day+" days</span> <span class='cntdwtmr-hour'>"+hour+":</span><span class='cntdwtmr-minute'>"+minute+":</span><span class='cntdwtmr-second'>"+second+"</span>");
                }else if(options.locale == "en"){
                    $("#"+attrId).html("<span class='cntdwtmr-day'>"+day+" days</span> <span class='cntdwtmr-hour'>"+hour+":</span><span class='cntdwtmr-minute'>"+minute+":</span><span class='cntdwtmr-second'>"+second+"</span>");
                }else if(options.locale == "kr"){
                    $("#"+attrId).html("<span class='cntdwtmr-day'>"+day+" days</span> <span class='cntdwtmr-hour'>"+hour+":</span><span class='cntdwtmr-minute'>"+minute+":</span><span class='cntdwtmr-second'>"+second+"</span>");
                }
            }



        } , 1000);
    }

    function stopTimer(){
        clearInterval(setTimer);
    }


}(jQuery));