/**
 * Runs analyze-css helper script and adds CSS related metrics
 *
 * Run phantomas with --analyze-css option to use this module
 */
exports.version = '0.1';

exports.module = function(phantomas) {
	if (!phantomas.getParam('analyze-css')) {
		phantomas.log('To enable CSS in-depth metrics please run phantomas with --analyze-css option');
		return;
	}

	function ucfirst(str) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   bugfixed by: Onno Marsman
		// +   improved by: Brett Zamir (http://brett-zamir.me)
		// *     example 1: ucfirst('kevin van zonneveld');
		// *     returns 1: 'Kevin van zonneveld'
		str += '';
		var f = str.charAt(0).toUpperCase();
		return f + str.substr(1);
	}

	phantomas.on('recv', function(entry, res) {
		if (entry.isCSS) {
			phantomas.log('CSS: analyzing <%s>...', entry.url);

			phantomas.runScript('analyze-css.js', ['--url', entry.url, '--json'], function(err, results) {
				if (err !== null) {
					phantomas.log('CSS: failed!');
					return;
				}

				//phantomas.log('CSS: results - %s', JSON.stringify(results));

				Object.keys(results).forEach(function(metric) {
					phantomas.incrMetric('css' + ucfirst(metric), results[metric]);
				});
			});
		}
	});
};

@@ -758,16 +758,40 @@
 1);%0A%09%7D%0A%0A
+%09var cssMessages = %5B%5D;%0A%0A
 %09phantom
@@ -1090,71 +1090,109 @@
 %09%09%09%09
-//phantomas.log('CSS: results - %25s', JSON.stringify(results));%0A
+var metrics = results.metrics %7C%7C %7B%7D,%0A%09%09%09%09%09messages = results.messages %7C%7C %5B%5D;%0A%0A%09%09%09%09// increase metrics
 %0A%09%09%09
@@ -1204,22 +1204,22 @@
 ct.keys(
-result
+metric
 s).forEa
@@ -1295,38 +1295,589 @@
 c), 
-results%5Bmetric%5D
+metrics%5Bmetric%5D);%0A%09%09%09%09%7D);%0A%0A%09%09%09%09// register CSS notices%0A%09%09%09%09cssMessages = cssMessages.concat(messages);%0A%09%09%09%7D);%0A%09%09%7D%0A%09%7D);%0A%0A%09phantomas.on('report', function() %7B%0A%09%09// limit number of messages%0A%09%09var limit = 50,%0A%09%09%09len = cssMessages.length;%0A%0A%09%09phantomas.setMetric('cssNotices', len);%0A%0A%09%09if (len %3E 0) %7B%0A%09%09%09cssMessages = cssMessages.slice(0, limit
 );%0A
+%0A
 %09%09%09
-%09%7D);%0A%09%09%09%7D
+phantomas.addNotice('CSS notices (%25s):', len);%0A%09%09%09cssMessages.forEach(function(msg) %7B%0A%09%09%09%09phantomas.addNotice(' ' + msg);%0A%09%09%09%7D);%0A%0A%09%09%09if (len %3E limit) %7B%0A%09%09%09%09phantomas.addNotice(' (%25d more...)', len - limit);%0A%09%09%09%7D%0A%0A%09%09%09phantomas.addNotice(
 );%0A%09

add CSS messages in notices section