﻿var endDate = 0;
var countDownID = -1;
// aia = Aankoop in Afronding
var aiaItems = new Array();
var countDownAIAID = -1;

function countDown() {


        var diff = new Date(endDate - new Date());
		var days = parseInt(diff / (1000*60*60*24));
		var hours = parseInt(diff / (1000*60*60)) - (days *24);
		var minutes = parseInt(diff / (1000*60))- (parseInt(diff / (1000*60*60)) *60);
		var seconds = parseInt((diff - (minutes * 1000 * 60) - (hours * 1000 * 60 * 60)) / 1000);

		if (hours >= 0 && minutes >= 0 && seconds >= 0 )
		{        		
			// because the current second is not displayed
		    minutes += 1;
		    if (minutes > 59)
		        minutes = 59;
		    
		    if ((hours + "").length == 2) {
		        $('.counter1').val((hours + "")[0])
		        $('.counter2').val((hours + "")[1])
		    }
		    else {
		        $('.counter1').val(0)
		        $('.counter2').val(hours)
		    }
		    if ((minutes + "").length == 2) {
		        $('.counter3').val((minutes + "")[0])
		        $('.counter4').val((minutes + "")[1])
		    }
		    else {
		        $('.counter3').val(0)
		        $('.counter4').val(minutes)
		    }

		    if ((seconds + "").length == 2) {
		        $('.counter5').val((seconds + "")[0])
		        $('.counter6').val((seconds + "")[1])
		    }
		    else {
		        $('.counter5').val(0)
		        $('.counter6').val(seconds)
		    }
		    
        }   
		else
		{
			$('.counter1').val(0);
			$('.counter2').val(0);
			$('.counter3').val(0);
			$('.counter4').val(0);
			$('.counter5').val(0);
			$('.counter6').val(0);
			clearInterval(countDownID);
		}

}

function isNull(a, b) {
    return (a == null) ? b : a;
}

function countDownAIA() {

    for (var i in aiaItems) {
        var item = aiaItems[i];
        
        item.secondsLeft = item.secondsLeft - 1;
        if (item.secondsLeft < 1) {
            item.m1.text("0");
            item.m2.text("0");
            item.s1.text("0");
            item.s2.text("0");
            clearInterval(countDownAIAID);
            if ($('#pageAfterTimeUp')[0] == null)
                location.reload();
            else
                location.href = $('#pageAfterTimeUp').val();
        }
        else {

            var minutes = parseInt(item.secondsLeft / 60, 10) + "";

            var seconds = parseInt(item.secondsLeft % 60, 10) + "";
        
            if (minutes != "0" || seconds != "0") {
                if (minutes.length > 1) {
                    item.m1.text(isNull(minutes.charAt(0), "0"));
                    item.m2.text(isNull(minutes.charAt(1), "0"));
                }
                else {
                    item.m1.text("0");
                    item.m2.text(isNull(minutes.charAt(0), "0"));
                }
                if (seconds.length > 1) {
                    item.s1.text(isNull(seconds.charAt(0), "0"));
                    item.s2.text(isNull(seconds.charAt(1), "0"));
                }
                else {
                    item.s1.text("0");
                    item.s2.text(isNull(seconds.charAt(0), "0"));
                }
            }
         

        }
    }
}

$(document).ready(function () {
    if ($('.endDate').val() != null) {
        endDate = new Date($('.endDate').val());
        countDownID = setInterval(countDown, 1000);
    }

    $('.aankoopInAfronding').each(function () {
        var item = { m1: $(this).find('.m1'),
            m2: $(this).find('.m2'),
            s1: $(this).find('.s1'),
            s2: $(this).find('.s2'),
            secondsLeft: 0
        };
        var minutes = parseInt(item.m1.text() + item.m2.text(), 10);
        var seconds = parseInt(item.s1.text() + item.s2.text(),  10);

        item.secondsLeft = minutes * 60 + seconds;

        aiaItems.push(item);
    });
    countDownAIAID = setInterval(countDownAIA, 1000);
});
