
/*****
* QuarterHourlyChanger function
** First param - arr - REQUIRED must be array of objects.
** Second param - imgId - id of image or false,
** Third param - linkId - id of link or false,
** Fourth param - textElemId - id of text element or false.
* Will always restart the variable 'num' at 0 on January 1st 00:00:00:000 of current year,
* so the first item in the array passed in to the function will always be used first in the new year,
* and will display, progressively, items from array advancing, in order, automatically on a timer every quarter hour,
* and re-starting from 0 after last item in array has been shown.
* The cycle is always based on number of 15 minute periods since January 1 of current year.
* So, if user enters page at 4:07 p.m. it will run, then run again at 4:15 p.m. and every quarter hour from there on.
* Theoretically, 35,040 15 minute cycles in non-leap year and 35,136 15 minute cycles in leap years.
*****/
function QuarterHourlyChanger(arr,imgId,linkId,textElemId) {
if (arr) {
var d = new Date(), newYear, num, ms, wait;
//newYear gets January 1st 00:00:00:000 of current year
newYear = Date.parse("Jan 1," + d.getFullYear());
//num will be a number from 0 to one less than the length of the array that is passed in
//(array's items start numbered from 0, except when counting length of array ~ length starts at 1 instead. How absurd.)
//num will always be 0 from 00:00:00 through 00:15:00 on January 1st (first fifteen minutes)
num = Math.floor((d.getTime() - newYear)/900000)%arr.length; //86400000 900,000 milliseconds = 15 minutes
if (imgId && arr[num].pic) {
document.getElementById(imgId).src = arr[num].pic;
}
if (linkId && arr[num].link) {
document.getElementById(linkId).href = arr[num].link;
}
if (textElemId && arr[num].text) {
document.getElementById(textElemId).innerHTML = arr[num].text;
}
////now let's synchronize and set up a timer for this function to run on the quarter hour
//(assuming you can get a user to stay on your page for 15 minutes [you're good!],
//the items will be changed on every quarter hour).
//ms gets how many milliseconds are left in current hour (an hour has 3,600,000 milliseconds total)
ms = 3600000 - ((d.getMinutes()*60000) + (d.getSeconds()*1000));
//wait calculates how many milliseconds until the next quarter hour
wait = (ms > 2700000) ? ms - 2700000 : (ms > 1800000) ? ms - 1800000 : (ms > 900000) ? ms - 900000 : ms;
window.setTimeout(function() {
QuarterHourlyChanger(arr,imgId,linkId,textElemId);
},wait);
}
};

window.onload = function(){
var someArr = [
{  text:'We were born to succeed, not to fail' },
{  text:'We were born to succeed, not to fail' },
{  text:'We were born to succeed, not to fail' },
{  text:'We were born to succeed, not to fail' },
{  text:'We were born to succeed, not to fail' } //<--- note, no comma after last object!, continue pattern as needed
];
QuarterHourlyChanger(someArr,'quarterHourImg','quarterHourLink','quarterHourSpan');
//example if you want to skip an element and not use a link element:
//QuarterHourlyChanger(someArr,'quarterHourImg',false,'quarterHourSpan'); //won't need link:value in array object to be passed
//or if just an image change is desired:
//QuarterHourlyChanger(someArr,'quarterHourImg'); //won't need link:value or text:value in array objects, just pic:value
};

