diff --git a/index.php b/index.php index 9b53f5d5..ded089b3 100644 --- a/index.php +++ b/index.php @@ -27,6 +27,7 @@ + diff --git a/js/compliments/compliments.js b/js/compliments/compliments.js new file mode 100644 index 00000000..c70177a8 --- /dev/null +++ b/js/compliments/compliments.js @@ -0,0 +1,63 @@ +var compliments = { + complimentLocation: '.compliment', + currentCompliment: '', + complimentList: { + 'morning': morning, + 'afternoon': afternoon, + 'evening': evening + }, + updateInterval: 30000, + fadeInterval: 4000, + intervalId: null +}; + +compliments.updateCompliment = function () { + + var _list = []; + + var hour = moment().hour(); + + if (hour >= 3 && hour < 12) { + // Morning compliments + _list = compliments.complimentList['morning']; + } else if (hour >= 12 && hour < 17) { + // Afternoon compliments + _list = compliments.complimentList['afternoon']; + } else if (hour >= 17 || hour < 3) { + // Evening compliments + _list = compliments.complimentList['evening']; + } else { + // Edge case in case something weird happens + // This will select a compliment from all times of day + Object.keys(compliments.complimentList).forEach(function (_curr) { + + _list = _list.concat(compliments.complimentList[_curr]); + + }); + } + + // Search for the location of the current compliment in the list + var _spliceIndex = _list.indexOf(compliments.currentCompliment); + + // If it exists, remove it so we don't see it again + if (_spliceIndex !== -1) { + _list = _list.slice(_spliceIndex, 1); + } + + // Randomly select a location + var _location = Math.floor(Math.random() * _list.length); + compliments.currentCompliment = _list[_location]; + + $('.compliment').updateWithText(compliments.currentCompliment, compliments.fadeInterval); + +} + +compliments.init = function () { + + this.updateCompliment(); + + this.intervalId = setInterval(function () { + this.updateCompliment(); + }.bind(this), this.updateInterval) + +} \ No newline at end of file diff --git a/js/main.js b/js/main.js index 0a51f0b0..2ef82c92 100755 --- a/js/main.js +++ b/js/main.js @@ -200,32 +200,7 @@ jQuery(document).ready(function($) { }, 1000); })(); - (function updateCompliment() - { - //see compliments.js - while (compliment == lastCompliment) { - - //Check for current time - var compliments; - var date = new Date(); - var hour = date.getHours(); - //set compliments to use - if (hour >= 3 && hour < 12) compliments = morning; - if (hour >= 12 && hour < 17) compliments = afternoon; - if (hour >= 17 || hour < 3) compliments = evening; - - compliment = Math.floor(Math.random()*compliments.length); - } - - $('.compliment').updateWithText(compliments[compliment], 4000); - - lastCompliment = compliment; - - setTimeout(function() { - updateCompliment(true); - }, 30000); - - })(); + compliments.init(); (function updateCurrentWeather() {