(function ($) {
	/**
	 * Replaces text contained in element by colourful text created by separate
	 * spans.
	 */
	$.fn.colorize = function () {
		var CLASSES = Array('green', 'yellow', 'red');
		
		this.each(function () {
			var content = $(this).html();
			var newContent = '';
			
			for (i = 0; i < content.length; i++) {
				newContent += '<span class="' + CLASSES[i % 3] + '">' + content.charAt(i) + '</span>';
			}
			
			$(this).html(newContent);
		});
	};
	
	/**
	 * Replaces spans created by $.colorize by common text.
	 */
	$.fn.uncolorize = function () {
		this.each(function () {
			var newContent = '';
			var spans = $(this).find('span');
			
			for (i = 0; i < spans.length; i++) {
				newContent += $(spans.get(i)).text();
			}
			
			$(this).html(newContent);
		});
	};
})(jQuery);

$(document).ready(function () {
    $.colorizeMenu = function () {
        $('#page-menu > ul > li[class!=colorize] > a').hover(
			function () { $(this).colorize(); },
			function () { $(this).uncolorize(); }
    	);
    	
    	$('#page-menu > ul > li.colorize > a').colorize();
    };
   
   $.colorizeMenu();
   $('.poll').poll();
   
});
