﻿var cdbaseunit;
var isUpdateTime=false;
function cdLocalTime(container, servermode, offsetMinutes, targetdate, debugmode) {
    if (!document.getElementById || !document.getElementById(container)) return
    this.container = document.getElementById(container)
    this.localtime = this.serverdate = new Date(servertimestring)
    this.targetdate = new Date(targetdate)
    this.debugmode = (typeof debugmode != "undefined") ? 1 : 0
    this.timesup = false
    this.localtime.setTime(this.serverdate.getTime() + offsetMinutes * 60 * 1000)
   
    if (isUpdateTime) {
        this.updateTime();
    }
}
cdLocalTime.prototype.updateTime = function () {
    var thisobj = this
    this.localtime.setSeconds(this.localtime.getSeconds() + 1)
    setTimeout(function () { thisobj.updateTime() }, 1000)
}
cdLocalTime.prototype.displaycountdown = function (baseunit, functionref) {
    this.baseunit = baseunit
    cdbaseunit = baseunit;


    this.formatresults = functionref
    this.showresults()
}
cdLocalTime.prototype.showresults = function () {
    var thisobj = this
    var debugstring = (this.debugmode) ? "<p style=\"background-color: #FCD6D6; color: black; padding: 5px\"><big>Debug Mode on!</big><br /><b>Current Local time:</b> " + this.localtime.toLocaleString() + "<br />Verify this is the correct current local time, in other words, time zone of count down date.<br /><br /><b>Target Time:</b> " + this.targetdate.toLocaleString() + "<br />Verify this is the date/time you wish to count down to (should be a future date).</p>" : ""
    var timediff = (this.targetdate - this.localtime) / 1000
    if (timediff < 0) {
        this.timesup = true
        this.container.innerHTML = debugstring + this.formatresults()
        return
    }
    var oneMinute = 60
    var oneHour = 60 * 60
    var oneDay = 60 * 60 * 24
    var dayfield = Math.floor(timediff / oneDay)
    var hourfield = Math.floor((timediff - dayfield * oneDay) / oneHour)
    var minutefield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour) / oneMinute)
    var secondfield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute))
    if (this.baseunit == "days") {

    }
    else if (this.baseunit == "hours") {
        hourfield = dayfield * 24 + hourfield
        dayfield = ""
    }
    else if (this.baseunit == "minutes") {
        minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield
        dayfield = hourfield = ""
    }
    else if (this.baseunit == "seconds") {
        var secondfield = timediff
        dayfield = hourfield = minutefield = ""
    }
    this.container.innerHTML = debugstring + this.formatresults(dayfield, hourfield, minutefield, secondfield)
    if (isUpdateTime) {
        setTimeout(function () { thisobj.showresults() }, 1000)
    }
}
function formatresults() {
    if (this.timesup == false) { var displaystring = "<span style='background-color: #CFEAFE'>" + arguments[1] + " hours " + arguments[2] + " minutes " + arguments[3] + " seconds</span> left until launch time" }
    else { var displaystring = "Launch time!" }
    return displaystring
}
function formatresults2() {
    if (this.timesup == false) {
        var arg2 = arguments[2]
        var arg3 = arguments[3]
        if (arg2 < 10) { arg2 = '0' + arg2 }
        if (arg3 < 10) { arg3 = '0' + arg3 }
        this.baseunit = cdbaseunit;
        var displaystring = "<span class='lcdstyle'>Ending In " + arguments[1] + ":" + arg2 + ":" + arg3 + "</span>";
        if (this.baseunit == "seconds") {
            displaystring == "<span class='lcdstyle'>Ending In "  + arg3 + " seconds </span>";
        }
        else if (this.baseunit == "minutes") {
            displaystring = "<span class='lcdstyle'>Ending In " + arg2 + " minutes </span>";
        }
        else if (this.baseunit == "hours") {
            displaystring = "<span class='lcdstyle'>Ending In " + arguments[1] + " hours </span>";
        }
        else if (this.baseunit == "days") {
            displaystring = "<span class='lcdstyle'>Ending In " + arguments[0] + " days </span>";
        }
      
       

    }
    else { window.location.reload(); var displaystring = "" }
    return displaystring
}
function mydiff(date1, date2, interval) {
    var second = 1000, minute = second * 60, hour = minute * 60, day = hour * 24, week = day * 7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
        case "years": return date2.getFullYear() - date1.getFullYear();
        case "months": return (
            (date2.getFullYear() * 12 + date2.getMonth())
            -
            (date1.getFullYear() * 12 + date1.getMonth())
        );
        case "weeks": return Math.floor(timediff / week);
        case "days": return Math.floor(timediff / day);
        case "hours": return Math.floor(timediff / hour);
        case "minutes": return Math.floor(timediff / minute);
        case "seconds": return Math.floor(timediff / second);
        default: return undefined;
    }
}

function launchTimer(startDate, endDate, today) {
    if ((startDate < today) && (endDate > today)) {        
         var diff = mydiff( today, endDate, 'minutes');
        var diffseconds = mydiff(today, endDate, 'seconds');             
        var unit = "hours";
        if (diffseconds < 60) {
            unit = "seconds";
            isUpdateTime = true;
        } else if (diff < 60) {
            unit = "minutes"
            isUpdateTime = true;
        } else if (diff < 1440) {
            unit = "hours"
        } else if (diff > 1440) {
            unit = "days"
        }
     
        var launchdate = new cdLocalTime("cdcontainer", "asp", -1, endDate)
        launchdate.displaycountdown(unit, formatresults2);
    }

}
