﻿function countdownControl(
        controlID, countdownControlDivClientID,
        finalDateString, criticalDateString, yearsLeftValueClientID, monthsLeftValueClientID, daysLeftValueClientID, hoursLeftValueClientID, minutesLeftValueClientID,
        displayOptionYears, displayOptionMonths, displayOptionDays, displayOptionHours, displayOptionMinutes,
        maxDisplayOption, minDisplayOption
        ) {
    this.controlID = controlID;
    this.countdownControlDivClientID = countdownControlDivClientID;
    this.updateCountdownValues = UpdateCountdownValues;
    this.deadlinedate = new Date(finalDateString);
    this.criticalDate = new Date(criticalDateString);
    this.yearsLeftValueClientID = yearsLeftValueClientID;
    this.monthsLeftValueClientID = monthsLeftValueClientID;
    this.daysLeftValueClientID = daysLeftValueClientID;
    this.hoursLeftValueClientID = hoursLeftValueClientID;
    this.minutesLeftValueClientID = minutesLeftValueClientID;
    this.displayOptionYears = displayOptionYears;
    this.displayOptionMonths = displayOptionMonths;
    this.displayOptionDays = displayOptionDays;
    this.displayOptionHours = displayOptionHours;
    this.displayOptionMinutes = displayOptionMinutes;
    this.maxDisplayOption = maxDisplayOption;
    this.minDisplayOption = minDisplayOption;
}

function UpdateCountdownValues() {

	var isActive = false;
    var isCritical = false;
	var now = new Date();
	            
    if(this.deadlinedate > now){

        isCritical = (now > this.criticalDate);

        diff = this.deadlinedate - now;
	    days = Math.floor( diff / (1000*60*60*24) );
	    hours = Math.floor( diff / (1000*60*60) );
	    mins = Math.floor( diff / (1000*60) );
	    secs = Math.floor( diff / 1000 );

        yy = this.deadlinedate.getFullYear()-now.getFullYear();
        if(yy > 0 && DateDiff.monthIsLessThan(this.deadlinedate, now)){
            yy -= 1;
        }

        if(this.deadlinedate.getMonth() >= now.getMonth()){
            mt = this.deadlinedate.getMonth()-now.getMonth();
            if(mt > 0 && DateDiff.dayIsLessThan(this.deadlinedate, now)){
                mt -= 1;
            }
            else if(mt == 0 && DateDiff.dayIsLessThan(this.deadlinedate, now)){
                mt = 11;
            }
        }
        else{
            mt = this.deadlinedate.getMonth() + (12 - now.getMonth());
        }

        if((this.maxDisplayOption == this.displayOptionMonths) && yy > 0){
            mt = mt - (yy * 12);
        }

        if(this.maxDisplayOption > this.displayOptionDays){
            dd = this.deadlinedate.getDate()-now.getDate();
            if(dd > 0 && DateDiff.hourIsLessThan(this.deadlinedate, now)){
                dd -= 1;
            }
            else if(dd <= 0){
                //Calculate days between the dates using a date from last month
                //"MMMM dd, yyyy HH:mm:ss"
                oldMonthsDays = daysInMonth(now.getMonth(), now.getFullYear());
                dd = (oldMonthsDays - now.getDate()) + this.deadlinedate.getDate();
                if(dd > 0 && DateDiff.hourIsLessThan(this.deadlinedate, now)){
                    dd -= 1;
                }
            }
        }
        else{
	        dd = days;
	    }

	    if (dd > 0 && this.maxDisplayOption > this.displayOptionHours) {
	        hh = hours - dd * 24;
	    }
	    else {
	        hh = hours;
	    }
	    mm = mins  - hours * 60;
	    ss = secs  - mins  * 60;

        if(yy >= 0){
            if(yy == 0 && (this.minDisplayOption == this.displayOptionYears)){
                $('#' + this.yearsLeftValueClientID).text('< 1');
            }
            else{
                $('#' + this.yearsLeftValueClientID).text(yy);
            }
            if(mt >= 0){
                if (mt == 0 && (this.minDisplayOption == this.displayOptionMonths)) {
                    $('#' + this.monthsLeftValueClientID).text('< 1');
                }
                else{
                    $('#' + this.monthsLeftValueClientID).text(mt);
                }
                if (dd >= 0) {
                    if (dd == 0 && (this.minDisplayOption == this.displayOptionDays)) {
                        $('#' + this.daysLeftValueClientID).text('< 1');
                        isActive = true;
                    }
                    else {
                        $('#' + this.daysLeftValueClientID).text(dd);
                        if (hh >= 0) {
                            $('#' + this.hoursLeftValueClientID).text(hh);
                            if (mm >= 0) {
                                if (mm == 0 && (this.minDisplayOption == this.displayOptionHours)) {
                                    $('#' + this.minutesLeftValueClientID).text('< 1');
                                }
                                else {
                                    $('#' + this.minutesLeftValueClientID).text(mm);
                                }
                                if (ss >= 0) {
                                    isActive = true;
                                    setTimeout(this.updateCountdownValues, 750);
                                }
                            }
                        }
                    }
	            }
            }
        }

    }
	            
    if(isActive && isCritical){
        $('#' + this.countdownControlDivClientID + ' .listItemCountdownStatistic').addClass("listItemCountdownStatisticCritical");
    }
	else if(!isActive){
	    $('#' + this.yearsLeftValueClientID).text(0)
	    $('#' + this.monthsLeftValueClientID).text(0)
	    $('#' + this.daysLeftValueClientID).text(0);
	    $('#' + this.hoursLeftValueClientID).text(0);
	    $('#' + this.minutesLeftValueClientID).text(0);
	    $('#' + this.countdownControlDivClientID + ' .listItemCountdownStatistic').addClass("listItemCountdownStatisticExpired");
	}
	            
}
                                    

