var calcUnits = function(seconds, divv, modd) { return ((Math.floor(seconds/divv))%modd).toString(); } var localDate = new Date(); var stPatDayBegin = new Date(localDate.getFullYear(), 2, 17); var stPatDayEnd = new Date(localDate.getFullYear(),2,18); if (localDate > stPatDayEnd) { stPatDayBegin = new Date(localDate.getFullYear() + 1, 2, 17); stPatDayEnd = new Date(localDate.getFullYear() + 1, 2, 18); } var computeBeginEndTimes = function() { localDate = new Date(); stPatDayBegin = new Date(localDate.getFullYear(), 2, 17); stPatDayEnd = new Date(localDate.getFullYear(),2,18); if (localDate > stPatDayEnd) { stPatDayBegin = new Date(localDate.getFullYear() + 1, 2, 17); stPatDayEnd = new Date(localDate.getFullYear() + 1, 2, 18); } } var prefix = 'There are '; function StringBuilder(value) { this.strings = new Array(''); this.append(value); } StringBuilder.prototype.append = function (value) { if (value) { this.strings.push(value); } } StringBuilder.prototype.clear = function () { this.strings.length = 1; } StringBuilder.prototype.toString = function () { return this.strings.join(''); } var isStPatricksDay = function() { if (localDate > stPatDayBegin) { if (localDate < stPatDayEnd) return "YES"; } return "NO"; } var responseColor = function () { if (isStPatricksDay() == 'YES') return "#73B221"; return "#000000"; } var getCountDown = function () { localDate = new Date(); if (isStPatricksDay() == 'YES') return ''; var differenceMs = new Date(stPatDayBegin - localDate); if (differenceMs < 0) { stPatDayBegin = new Date(localDate.getFullYear()+1, 2, 2); stPatDayEnd = new Date(localDate.getFullYear()+1,2,3); differenceMs = new Date(stPatDayBegin - localDate); } var differenceSec = Math.floor(differenceMs.valueOf()/1000); var countDownOutput = new StringBuilder(); var daySingluar = 'day'; var dayPlural = 'days'; var hourSingular = 'hour'; var hourPlural = 'hours'; var minuteSingluar = 'minute'; var minutePlural = 'minutes'; var secondSingluar = 'second'; var secondPlural = 'seconds'; var days = calcUnits(differenceSec,86400,100000); var hours = calcUnits(differenceSec,3600,24); var minutes = calcUnits(differenceSec,60,60); var seconds = calcUnits(differenceSec,1,60); countDownOutput.append(prefix); if (days > 0) { countDownOutput.append(''); countDownOutput.append(days); countDownOutput.append('<\/b>'); countDownOutput.append(' '); if (days == 1) countDownOutput.append(daySingluar); else countDownOutput.append(dayPlural); } if (hours > 0) { countDownOutput.append(', '); countDownOutput.append(''); countDownOutput.append(hours); countDownOutput.append('<\/b>'); countDownOutput.append(' '); if (hours == 1) countDownOutput.append(hourSingular); else countDownOutput.append(hourPlural); } if (minutes > 0) { countDownOutput.append(', '); countDownOutput.append(''); countDownOutput.append(minutes); countDownOutput.append('<\/b>'); countDownOutput.append(' '); if (minutes == 1) countDownOutput.append(minuteSingular); else countDownOutput.append(minutePlural); } if (seconds > 0) { if (minutes >0) { countDownOutput.append(', '); } countDownOutput.append(''); countDownOutput.append(seconds); countDownOutput.append('<\/b>'); countDownOutput.append(' '); if (seconds == 1) countDownOutput.append(secondSingluar); else countDownOutput.append(secondPlural); } var suffix = ' until St. Patrick\'s Day ' + stPatDayBegin.getFullYear(); countDownOutput.append(suffix); return countDownOutput.toString(); } var updateYesNo = function() { var responseAnchor = document.getElementById('yesNo'); responseAnchor.innerHTML = isStPatricksDay(); } var showHideLeprechan = function() { var leprechanDiv = document.getElementById('leprechanDiv'); if (isStPatricksDay() == 'YES') { showLeprechan(leprechanDiv); } else { hideLeprechan(leprechanDiv); } var thisResponseAnchor = document.getElementById('responseAnchor'); thisResponseAnchor.style.color = responseColor(); thisResponseAnchor.title = isStPatricksDay(); } var showLeprechan = function(thisLeprechanDiv) { thisLeprechanDiv = document.getElementById('leprechanDiv'); thisLeprechanDiv.innerHTML = '\'leprechan\''; } var hideLeprechan = function(thisLeprechanDiv) { thisLeprechanDiv = document.getElementById('leprechanDiv'); thisLeprechanDiv.innerHTML = '\'leprechan\''; computeBeginEndTimes(); } var updateCountDownOutput = function() { var countDownOutputElement = document.getElementById('countDownOutput'); countDownOutputElement.innerHTML = getCountDown(); showHideLeprechan(); updateYesNo(); }