// document.addEventListener("DOMContentLoaded", function() {
// 	loadCountDown2();
// });

$(document).ready(function(){
    loadCountDown2();
})

function loadCountDown2(){

	var countDownDate2 = "";
	var cd_endtext2 	  = "Count down was already expired.";

	$("#expired_msg2").hide();

	var cd_enddate2 = $(".countdown_time_content2:first").attr('cd_enddate');
	var cd_endtime2 = $(".countdown_time_content2:first").attr('cd_endtime');
	if(cd_enddate2!= undefined && cd_enddate2!= null && cd_enddate2!=""){
		var endtime2  = "";
		if(cd_endtime2!= undefined && cd_endtime2!= null && cd_endtime2!=""){
			var endtime_arr2 = cd_endtime2.split(" ");
			var time_arr2 = endtime_arr2[0].split(":");
			if(endtime_arr2[1]=="AM"){
				endtime2 = " "+time_arr2[0]+":"+time_arr2[1]+":00";
			}else{
				var hr =  parseInt(time_arr2[0])+12;
				endtime2 = " "+hr+":"+time_arr2[1]+":00";
			}
		}
		countDownDate2 = new Date(cd_enddate2+endtime2).getTime();
		console.log(countDownDate2);
		console.log(endtime2);
		$(".countdown_time_content2:first").removeAttr('cd_enddate');
		$(".countdown_time_content2:first").removeAttr('cd_endtime');
		cd_endtext_msg  = $(".countdown_time_content2:first").attr('cd_endtext');
		if(cd_endtext_msg!= undefined && cd_endtext_msg!= null && cd_endtext_msg!=""){
			cd_endtext2  = cd_endtext_msg;
		}
		$(".countdown_time_content2:first").removeAttr('cd_endtext');
	}else{
		countDownDate2 = new Date("May 28, 2020 10:50:00 AM").getTime();
	}


	// Update the count down every 1 second
	var countdown2 = setInterval(function() {

	    // Get todays date and time
	    var now2 = new Date().getTime();
	    
	    // Find the distance between now an the count down date
	    var distance2 = countDownDate2 - now2;

	    // If the count down is over, write some text 
	    if (distance2 < 0 || now2>=countDownDate2) {
	        clearInterval(countdown2);
	        $("#countdown_time_content2:first").fadeOut();

	        $("#expired_msg2").fadeIn().text(cd_endtext2);

	        return false;
	    }
	    
	    // Time calculations for days, hours, minutes and seconds
	    var days2 = Math.floor(distance2 / (1000 * 60 * 60 * 24));
	    var hours2 = Math.floor((distance2 % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
	    var minutes2 = Math.floor((distance2 % (1000 * 60 * 60)) / (1000 * 60));
	    var seconds2 = Math.floor((distance2 % (1000 * 60)) / 1000);
	    
	    // Output the result in an element with id="demo"
	    $("#day_number2").text(days2);
	    $("#hrs_number2").text(hours2);
	    $("#mins_number2").text(minutes2);
	    $("#sec_number2").text(seconds2);

	    // document.getElementById("demo").innerHTML = days + "d " + hours + "h "
	    // + minutes + "m " + seconds + "s ";
	    
	}, 1000);
}