/*

---

Script for detecting and outlining elements of a page with font sizes based on fixed units.

author: Jon Gibbins (a.k.a. dotjay)

---

*/



// --- copy and paste for crushing starts here ---

// highlight colour
var hc = '#F00';



// highlightFixedFontSizesInChildTextNodes()
// iteratively highlights all elements with child text nodes and fixed font sizes
function hctn(el) {
	var cn = el.childNodes;

	// go through child nodes
	for (var i=0;i<cn.length;i++) {

		// if the element has child nodes, check them too
		if (cn[i].childNodes.length > 0) hctn(cn[i]);

		// check it's type and highlight if its font size is fixed
		if ((cn[i].nodeType == 3) && ff(cn[i].parentNode)) cn[i].parentNode.style.outline = '1px solid '+hc;

	}
}

// fixedFontSizeTest()
// test for a fixed font size
function ff(el) {
	if(el.style&&el.style.fontSize.match('px|pt|pc|cm|mm|in')) return true;return false;
}

// highlights all elements with fixed font sizes, whether set in CSS or inline
function highlightFixedFontSizesAll() {
	// of course, we can just use the functions we have, but we'll want an all-inclusive script to use as a bookmarklet
	/*
	highlightFixedFontSizesInStyleSheets();
	highlightFixedFontSizesInline();
	*/

	if (!document.styleSheets||!document.getElementsByTagName) return false;
	var ss=document.styleSheets;
	for (var i=0;i<ss.length;i++) {
		var cr=new Array();
		if (ss[i].cssRules) cr=ss[i].cssRules;
		else continue;
		for (var j=0;j<cr.length;j++){
			if (ff(cr[j])) cr[j].style.outline='1px solid '+hc;
		}
	}
	hctn(document.getElementsByTagName('body')[0]);
}



// --- copy and paste for crushing ends here ---



// highlights all elements with fixed font sizes set in CSS
function highlightFixedFontSizesInStyleSheets() {
	if (!document.styleSheets) return false;

	var styleSheets = document.styleSheets;

	for (var i=0;i<styleSheets.length;i++) {
		var cssRules = new Array();
		if (styleSheets[i].cssRules)
			cssRules = styleSheets[i].cssRules;
		// for IE, although outline won't work on IE :oP
		else if (styleSheets[i].rules)
			cssRules = styleSheets[i].rules;

		for (var j=0;j<cssRules.length;j++) {
			if (ff(cssRules[j])) cssRules[j].style.outline = '1px solid '+hc;
		}
	}
}

// highlights all elements with fixed font sizes set inline in the HTML
function highlightFixedFontSizesInline() {	
	if (!document.getElementsByTagName) return false;

	// iteratively check all text nodes within the body
	hctn(document.getElementsByTagName('body')[0]);
}

// should clear results, but sets these permanently?
function clearResultStyles() {
	if (!document.getElementsByTagName) return false;
	elements = document.getElementsByTagName('*');
	for (var i=0;i<elements.length;i++) {
		if (elements[i].nodeName == "LI") elements[i].style.outline = "0";
	}
}
