Created
April 6, 2011 18:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* rotator.js | |
* @author Chris Barna <[email protected]> | |
* 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