var times = ["01:00 - 02:55", "02:00 - 04:05", "05:00 - 07:35", "08:00 - 10:15", "11:00 - 13:25", "14:00 - 16:10", "15:00 - 17:30", "16:00 - 18:21", "17:05 - 19:10", "18:05 - 20:18", "19:15 - 21:40", "20:35 - 21:55", "21:40 - 22:45", "22:00 - 23:55", "23:00 - 00:05", "00:35 - 01:55", "01:15 - 02:30", "02:10 - 04:30", "05:20 - 07:30", "09:30 - 11:20", "12:25 - 14:40", "15:40 - 17:40", "19:00 - 21:30", "23:20 - 01:45", "03:15 - 05:30", "07:10 - 09:20", "11:15 - 13:55", "15:05 - 17:10", "19:25 - 21:30", "22:05 - 00:15", "01:45 - 03:25", "05:10 - 06:55", "08:45 - 10:25", "11:25 - 13:15", "14:45 - 16:15", "17:55 - 19:35", "20:45 - 22:05", "23:15 - 01:10", "02:25 - 04:50", "06:30 - 08:15", "09:45 - 11:25", "12:55 - 14:50", "15:25 - 17:15", "18:45 - 20:30", "21:40 - 23:05", "00:25 - 02:40", "03:40 - 05:45", "06:35 - 08:40", "10:50 - 13:15", "14:25 - 17:40", "19:30 - 21:15", "22:20 - 00:15", "01:03 - 02:33", "02:08 - 04:35", "05:18 - 07:15", "08:09 - 10:19", "11:07 - 13:15", "14:09 - 16:17", "15:06 - 17:36", "16:07 - 18:24", "17:08 - 19:47", "18:02 - 20:38", "19:18 - 21:43", "20:34 - 21:58", "21:46 - 22:13", "22:01 - 23:59", "23:03 - 00:28", "00:39 - 01:59", "01:17 - 02:39", "02:13 - 04:37", "05:28 - 07:33", "09:34 - 11:27", "12:23 - 14:48", "15:41 - 17:49", "19:04 - 21:37", "23:23 - 01:47", "03:13 - 05:36", "07:17 - 09:29", "11:16 - 13:53", "15:13 - 17:19", "19:26 - 21:38", "22:45 - 00:13", "01:51 - 03:53", "05:52 - 06:15", "08:38 - 10:27", "11:26 - 13:31", "14:31 - 16:18", "17:37 - 19:39", "20:43 - 22:52", "23:43 - 01:31", "02:26 - 04:47", "06:38 - 08:17", "09:47 - 11:29", "12:57 - 14:43", "15:28 - 17:18", "18:43 - 20:38", "21:41 - 23:37", "00:21 - 02:47", "03:57 - 05:42", "06:38 - 08:43", "10:53 - 13:18", "14:27 - 17:48", "19:37 - 21:16", "22:21 - 00:16"];
function shuffletIME(a) {
    for (i = 0 ; i<a.length;i++) {
        var j = Math.floor(Math.random() * (i + 1));
        //[a[i], a[j]] = [a[j], a[i]];
    if(j!=i){
      var temp = a[i];
      a[i] = a[j];
      a[j] = temp;
    }
    }
   for (i = 0 ; i<a.length;i++) {
        console.log(a[i]+"");
    }
  
    return a;
}

function refresh() {
    var midNight = new Date();
    midNight.setHours(0,0,0,0);

    // Retrieve last saved settings
    var timeSettingscuanhb = localStorage.getItem('timeSettingscuanhb');
    if (timeSettingscuanhb) timeSettingscuanhb = JSON.parse(timeSettingscuanhb);

    // If no settings or they are from before midnight, 
    //    or they have a different count of times: regenerate
    if (!timeSettingscuanhb || timeSettingscuanhb.date < midNight.toJSON() 
            || timeSettingscuanhb.times.length !== times.length) {
        // Re-initialise for a new day
        timeSettingscuanhb = {
            times: shuffle(times), // randomly shuffle the times
            date: midNight.toJSON() // Take note of the date of generation
        };
        // Save this in local storage so that it will be read when page reloads
        localStorage.setItem('timeSettingscuanhb', JSON.stringify(timeSettingscuanhb));
    }

    var msSinceMidnight = new Date() - midNight,
        msPerQuote = 1000*60*60*24 / timeSettingscuanhb.times.length,
        index = Math.floor(msSinceMidnight / msPerQuote),
    index2 =index;
    if(index==times.length-1){
      index2 = index-1
    }
    else{
      index2 = index+1
    }
        nextAt = new Date((index + 1) * msPerQuote + midNight.getTime());

    // Get quote from shuffled array at an index that depends on time of the day
    var randomQuote = timeSettingscuanhb.times[index];
    var randomQuote2 = timeSettingscuanhb.times[index2];
    // Display
  
  var idx = 0;
  var randomIdx = 10-times.length;

  for(i=0;i<181;i++){
    var quoteSpan = document.getElementById("Jam" + i);
    quoteSpan.innerHTML = timeSettingscuanhb.times[idx]+"";
    idx++;
    if(idx==times.length){
      idx = 0;
    }
  }
  document.getElementById("nextAt").textContent = nextAt.toLocaleTimeString();
    setTimeout(refresh, nextAt - new Date());
}

refresh();