Skip to content

Instantly share code, notes, and snippets.

@ctbarna
Created April 6, 2011 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctbarna/906180 to your computer and use it in GitHub Desktop.
Save ctbarna/906180 to your computer and use it in GitHub Desktop.
A simple image rotator I wrote because I don't have admin access in CommonSpot. Images are grabbed from the srcID selector in a custom page then rotated through targetDiv on a target page depending on the day of the year. Functionality is kind of hacky be
/**
* rotator.js
* @author Chris Barna <chris@unbrain.net>
* Rotate images based on the date.
**/
// Variables
var url = "rotation.cfm";
var srcID = "#cs_control_9951781";
var customPanelDivs = new Array("#cs_control_9963945", "#cs_control_9963960");
var targetDiv = new Array("#rotatorcontent", "#rotatorcontent1");
// Day of Year function.
Date.prototype.getDOY = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((this-onejan)/86400000);
};
$(document).ready(function() {
// Make the AJAX request.
$.ajax({url: url,
dataType: 'html',
success: function(data) {
var today = new Date();
var DOY = today.getDOY();
var imgNum = $(srcID + " img", data).length;
var imgIndex = DOY % imgNum;
var imgArray = new Array();
// Prioritized content
for(i = 0; i < customPanelDivs.length; i = i+1) {
if ($(customPanelDivs[i] + " img", data).length > 0) {
imgArray.push(imgInfo(customPanelDivs[i], 0));
}
}
// Only loop through until we have enough images to fill every spot.
var j = 0;
while (imgArray.length < targetDiv.length) {
var newIndex = (imgIndex +j) % imgNum;
imgArray.push(imgInfo(srcID, newIndex));
j = j+1;
}
// Add the images to the DOM.
$.each(targetDiv, function(index, value) {
$(value).html("<a href=\""+imgArray[index][1]+"\"><img src=\""+imgArray[index][0]+"\" /></a>");
});
// A function to return an array with the info about the image.
function imgInfo (div, imgIndex) {
selector = div + " img:eq("+imgIndex+")";
var img = new Array(
$(selector, data).attr("src"),
$(selector, data).parent().attr("href"));
return img;
}
},
error: function(xhr, estatus, error) {
console.log(xhr + " : " + estatus);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment