var rainbows = rainbows || {}; (function() { var $ = jQuery; rainbows.last_offset = 0; rainbows.colors = [ 'red', 'green', 'yellow', 'blue', 'orange', 'purple', 'pink', 'brown', 'black', 'gray', 'white' ]; rainbows.wrap = function(target) { var target = $(target), new_target = $('
'), nodes = target.contents().clone(); nodes.each(function() { if (this.nodeType == 3) { // text var new_html = ""; text = this.wholeText; for (var i=0; i < text.length; i++) { if (text[i] == ' ') new_html += " "; else new_html += "" + text[i] + ""; } new_target.append(new_html); } else { $(this).html(rainbows.wrap(this)); new_target.append(this); } }); return new_target.html(); }; rainbows.colorize = function(target, offset) { var colors = $.extend([], rainbows.colors); if (typeof offset !== 'undefined') colors = colors.concat(colors.splice(0, offset)); $(target).find('span').each(function(idx, el) { $(el).attr('class', colors[(idx % colors.length)]); }); }; rainbows.start = function(target, interval) { $(target).html(rainbows.wrap(target)); rainbows.intervalId = setInterval(function() { rainbows.colorize(target, rainbows.last_offset); rainbows.last_offset = (rainbows.last_offset + 1) % rainbows.colors.length; }, interval || 500); }; }());