function examinecontrast()
{
	//first check page to see if variaables are loaded from the text labels script
if (typeof(testvar) == "undefined"){

 setTimeout ('examinecontrast()', 1000);
}

else {
//open window here so it has time to instantiate?, otherwise it throws an error on slower connections
//var lw=window.open('','lw');
//var sw=lw.document;
//sw.body.appendChild(sw.createTextNode('Element'));
//var objbod = document.createElement('body');
//lw.document.appendChild(objbod);
//lw.document.body.appendChild(lw.document.createTextNode('Element'));
 
 
	

 
 
 //iType=1;
switch (document.getElementById("cca").title)
{
	case "all":
	iType=1
	break;
	case "lcr":
	iType=2
	break;
	case "aert":
	iType=3
	break;
	
	}
	
	
	
	
	
	
	
	
	
	
	
	// iType(1) == all
	// iType(2) == Luminosity
	// iType(3) == AERT
	
	var objBody = document.getElementsByTagName('body')[0];
	
	function clearcca() {
  if(window.event && window.event.keyCode == 13)
  {document.body.removeChild(document.getElementById('result'))
   }}

	if (!objBody)
		return;

	var objElements = objBody.getElementsByTagName('*');

	if (!objElements)
		return;

	var arElements = new Array();

	// Only add the body to the list if there is text that isn't in a container
	if (objBody.firstChild)
	{
		var objChild = objBody.firstChild;
		if (checkText(objChild))
		{
			var objElementDetails = new Object();

			objElementDetails.element = objBody.tagName;
			objElementDetails.className = objBody.className;
			objElementDetails.IDName = objBody.id;
			objElementDetails.forecolour = getColour(objBody, 'color');
			objElementDetails.backcolour = getColour(objBody, 'background-color');
			objElementDetails.parents = '';
			objElementDetails.luminosity = getLuminosity(objElementDetails.forecolour, objElementDetails.backcolour);
			objElementDetails.colourbrightness = getBrightness(objElementDetails.forecolour, objElementDetails.backcolour);
			objElementDetails.colourdifference = getColourDifference(objElementDetails.forecolour, objElementDetails.backcolour);
			arElements.push(objElementDetails);
		}
	}
	
	// Get details for each element in the DOM
	for (var iCounter=0; iCounter<objElements.length; iCounter++)
	{
		// If there is a text node, add this element to the list
		if (objElements[iCounter].firstChild && objElements[iCounter].tagName && !objElements[iCounter].tagName.match(/script/i))
		{
			var objChild = objElements[iCounter].firstChild;

			if (checkText(objChild))
			{
				var objElementDetails = new Object();

				objElementDetails.element = objElements[iCounter].tagName;
				objElementDetails.className = objElements[iCounter].className;
				objElementDetails.IDName = objElements[iCounter].id;
				objElementDetails.forecolour = getColour(objElements[iCounter], 'color');
				objElementDetails.backcolour = getColour(objElements[iCounter], 'background-color');
				objElementDetails.parents = getParentElements(objElements[iCounter]);
				if (iType == 1 || iType == 2)
					objElementDetails.luminosity = getLuminosity(objElementDetails.forecolour, objElementDetails.backcolour);
				if (iType == 1 || iType == 3)
				{
					objElementDetails.colourbrightness = getBrightness(objElementDetails.forecolour, objElementDetails.backcolour);
					objElementDetails.colourdifference = getColourDifference(objElementDetails.forecolour, objElementDetails.backcolour);
				}

				// Avoid adding duplicate details
				if (!alreadygot(arElements, objElementDetails))
					arElements.push(objElementDetails);
			}
		}
	}

	if (arElements.length > 0)
	{
		
		
		var objTable = document.createElement('table');
		var objCaption = document.createElement('caption');
		var objTHead = document.createElement('thead');
		var objTBody = document.createElement('tbody');
		var objTR, objTH, objTD, objNodeList, objNodeItem;
		var arParents;

		//var objHeading = document.createElement('h1');
		//objHeading.appendChild(document.createTextNode('Colour Contrast Analyser'));
		//document.body.appendChild(objHeading);
var cssLink = document.createElement('link');
	cssLink.setAttribute('href', 'http://wat2.z6i.org/resources/WAT/scripts/css/cca_opera.css');
	cssLink.setAttribute('rel', 'stylesheet');
	cssLink.setAttribute('type', 'text/css');
	//document.appendChild(cssLink);
	document.getElementsByTagName('head')[0].appendChild(cssLink);
		
		objTable.setAttribute('summary', cca3);
objTable.setAttribute('id', 'result');
	
	objCaption.setAttribute('style', 'background-color:#ffffff');	
	objCaption.setAttribute('onclick', 'document.body.removeChild(document.getElementById(\'result\'))');	
	objCaption.setAttribute('tabindex', '1');
	objCaption.setAttribute('onkeypress', 'clearcca()');
		if (iType == 1)
			objCaption.appendChild(document.createTextNode(cca4));
		else if (iType == 2)
			objCaption.appendChild(document.createTextNode(cca5));
		else if (iType == 3)
			objCaption.appendChild(document.createTextNode(cca6));

		objTable.appendChild(objCaption);
		objTR = document.createElement('tr');
		objTH = document.createElement('th');
		objTH.setAttribute('scope', 'col');
		objTH.appendChild(document.createTextNode(cca7));
		objTR.appendChild(objTH);
		objTH = document.createElement('th');
		objTH.setAttribute('scope', 'col');
		objTH.appendChild(document.createTextNode(cca8));
		objTR.appendChild(objTH);
		objTH = document.createElement('th');
		objTH.setAttribute('scope', 'col');
		objTH.appendChild(document.createTextNode(cca9));
		objTR.appendChild(objTH);
		objTH = document.createElement('th');
		objTH.setAttribute('scope', 'col');
		objTH.appendChild(document.createTextNode(cca10));
		objTR.appendChild(objTH);
		objTH = document.createElement('th');
		objTH.setAttribute('scope', 'col');
		objTH.appendChild(document.createTextNode(cca11));
		objTR.appendChild(objTH);
		if (iType == 1 || iType == 2)
		{
			objTH = document.createElement('th');
			objTH.setAttribute('scope', 'col');
			objTH.appendChild(document.createTextNode(cca12));
			objTR.appendChild(objTH);
		}
		if (iType == 1 || iType == 3)
		{
			objTH = document.createElement('th');
			objTH.setAttribute('scope', 'col');
			objTH.appendChild(document.createTextNode(cca13));
			objTR.appendChild(objTH);
			objTH = document.createElement('th');
			objTH.setAttribute('scope', 'col');
			objTH.appendChild(document.createTextNode(cca14));
			objTR.appendChild(objTH);
		}
		objTHead.appendChild(objTR);
		objTable.appendChild(objTHead);

		for (var iCounter=0; iCounter<arElements.length; iCounter++)
		{
			objTR = document.createElement('tr');
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].element));
			if (arElements[iCounter].className)
			{
				var objP = document.createElement('p');
				var objCode = document.createElement('code');
				objCode.appendChild(document.createTextNode('class'));
				objP.appendChild(objCode);
				objP.appendChild(document.createTextNode(': ' + arElements[iCounter].className));
				objTD.appendChild(objP);
			}
			if (arElements[iCounter].IDName)
			{
				var objP = document.createElement('p');
				var objCode = document.createElement('code');
				objCode.appendChild(document.createTextNode('id'));
				objP.appendChild(objCode);
				objP.appendChild(document.createTextNode(': ' + arElements[iCounter].IDName));
				objTD.appendChild(objP);
			}
			objTR.appendChild(objTD);

			objTD = document.createElement('td');

			if (arElements[iCounter].parents != '')
			{
				arParents = arElements[iCounter].parents;
				objNodeList = document.createElement('ul');
				for (var iParentCounter=0; iParentCounter<arParents.length; iParentCounter++)
				{
					objNodeItem = document.createElement('li');
					objNodeItem.appendChild(document.createTextNode(arParents[iParentCounter]));
					objNodeList.appendChild(objNodeItem);
				}

				objTD.appendChild(objNodeList);
			}
			else
				objTD.appendChild(document.createTextNode(cca15));

			objTR.appendChild(objTD);

			objTD = document.createElement('td');
			objTD.style.backgroundColor = arElements[iCounter].backcolour.hexcolour;
			objTD.style.color = arElements[iCounter].forecolour.hexcolour;
			objTD.appendChild(document.createTextNode(cca16));
			objTR.appendChild(objTD);
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].forecolour.hexcolour));
			objTR.appendChild(objTD);
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].backcolour.hexcolour));
			objTR.appendChild(objTD);
			if (iType == 1 || iType == 2)
		{
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].luminosity[0]));
			objTR.appendChild(objTD);
		}
		if (iType == 1 || iType == 3)
		{
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].colourbrightness[0]));
			objTR.appendChild(objTD);
			objTD = document.createElement('td');
			objTD.appendChild(document.createTextNode(arElements[iCounter].colourdifference[0]));
			objTR.appendChild(objTD);
		}
			if (iType == 1)
			{
				if (arElements[iCounter].luminosity[1] == 0 || arElements[iCounter].colourbrightness[1] == 0 || arElements[iCounter].colourdifference[1] == 0)
				{
					objTR.className = 'failure';
				}
			}
			else if (iType == 2)
			{
				if (arElements[iCounter].luminosity[1] == 0)
				{
					objTR.className = 'failure';
				}
			}
			else if (iType == 3)
			{
				if (arElements[iCounter].colourbrightness[1] == 0 || arElements[iCounter].colourdifference[1] == 0)
				{
					objTR.className = 'failure';
				}
			}
			objTBody.appendChild(objTR);
		}
//var iframes = document.getElementsByTagName('iframe')
	//for (var frm=0;frm<iframes.length;frm++){
	//document.body.removeChild(iframes[frm]);
	//}
		objTable.appendChild(objTBody);
		document.body.appendChild(objTable);
		
		
	}
}
}
function getColour(objElement, strProperty)
{
	var objColourDetails = new Object();
	var iRed, iGreen, iBlue;
	var arColour = getColourParts(objElement, strProperty);

	objColourDetails.hexcolour = getHex(objElement, strProperty);
	objColourDetails.red = arColour[0];
	objColourDetails.green = arColour[1];
	objColourDetails.blue = arColour[2];

	return objColourDetails;
}

function getColourParts(objElement, strProperty)
{
	var strHex = "0123456789abcdef";
	var arColour = new Array();

	var strColour = objElement.ownerDocument.defaultView.getComputedStyle(objElement, null).getPropertyValue(strProperty);

	if (strColour && strColour != 'transparent')
	{
		var iRed = strHex.indexOf(strColour.substring(1, 2)) * 16 + strHex.indexOf(strColour.substring(2, 3));
		var iGreen = strHex.indexOf(strColour.substring(3, 4)) * 16 + strHex.indexOf(strColour.substring(4, 5));
		var iBlue = strHex.indexOf(strColour.substring(5, 6)) * 16 + strHex.indexOf(strColour.substring(6, 7));

		arColour = new Array(iRed, iGreen, iBlue);
		
	}
	else
	{
		try
		{
			arColour = getColourParts(objElement.parentNode, strProperty);
		}
		catch (err)
		{
			if (strProperty == 'color')
				arColour = new Array(0, 0, 0);
			else
				arColour = new Array(255, 255, 255);
		}
	}

	return arColour;
}

function getHex(objElement, strProperty)
{
	var strColour = objElement.ownerDocument.defaultView.getComputedStyle(objElement, null).getPropertyValue(strProperty);

	if (strColour && strColour != 'transparent')
		return strColour
	else
	{
		try
		{
			strColour = getHex(objElement.parentNode, strProperty);
		}
		catch (err)
		{
			if (strProperty == 'color')
				strColour = '#000000';
			else
				strColour = '#ffffff';
		}
	}

	return strColour;
}

function getBrightness(objForeColour, objBackColour)
{
	var iForegroundBrightness = ((objForeColour.red * 299) + (objForeColour.green * 587) + (objForeColour.blue * 114)) / 1000;
	var iBackgroundBrightness = ((objBackColour.red * 299) + (objBackColour.green * 587) + (objBackColour.blue * 114)) / 1000;
	var iBrightDifference;
	var strBrightResult;

	if (iBackgroundBrightness > iForegroundBrightness)
		iBrightDifference = parseInt(iBackgroundBrightness - iForegroundBrightness, 10);
	else
		iBrightDifference = parseInt(iForegroundBrightness - iBackgroundBrightness, 10);

	if (iBrightDifference > 125){
		strBrightResult = iBrightDifference + cca17;
	return [strBrightResult, 1];
		}
		
		strBrightResult = iBrightDifference + cca18;
	return [strBrightResult, 0];
	}

function getColourDifference(objForeColour, objBackColour)
{
	var iColourDifference = Math.abs(objBackColour.red - objForeColour.red) + Math.abs(objBackColour.green - objForeColour.green) + Math.abs(objBackColour.blue - objForeColour.blue);
	var strColourResult;

	if (iColourDifference > 500){
		strColourResult = iColourDifference + cca17;
	   return [strColourResult, 1];
	}
		strColourResult = iColourDifference + cca18;
	   return [strColourResult, 0];
}

function getLuminosity(objForeColour, objBackColour)
{
	var fFore, fBack, fL1, fL2, fDifference, strResult;
	var fPlaces = Math.pow(10, 2);

	fFore = calculateLuminosity(objForeColour.red, objForeColour.green, objForeColour.blue, 255);
	fBack = calculateLuminosity(objBackColour.red, objBackColour.green, objBackColour.blue, 255);

	if (fFore > fBack)
	{
		fL1 = fFore;
		fL2 = fBack;
	}
	else
	{
		fL1 = fBack;
		fL2 = fFore;
	}

	fDifference = (fL1 + 0.05) / (fL2 + 0.05);

	fDifference = Math.round(fDifference * fPlaces) / fPlaces;

	if (fDifference < 5){
		strResult = fDifference + cca18;
		return [strResult, 0];
		}
	 if (fDifference < 10){
		strResult = fDifference + cca19;
		return [strResult, 1];
		}

		strResult = fDifference + cca20;
	return [strResult, 1];
}

function calculateLuminosity(fRed, fGreen, fBlue, fFullScale)
{
	fLinearisedRed = Math.pow((fRed / fFullScale), 2.2);
	fLinearisedGreen = Math.pow((fGreen / fFullScale), 2.2);
	fLinearisedBlue = Math.pow((fBlue / fFullScale), 2.2);

	return (0.2126 * fLinearisedRed + 0.7152 * fLinearisedGreen + 0.0722 * fLinearisedBlue);
}

function getParentElements(objElement)
{
	var arElements = new Array();
	var objParent = objElement;
	var strElementName;

	do
	{
		objParent = objParent.parentNode;
		if (objParent.tagName != undefined)
		{
			strElementName = objParent.tagName;
			if (objParent.id)
				strElementName += '#' + objParent.id
			else if (objParent.className)
				strElementName += '.' + objParent.className.replace(/\s/g, '.');
			arElements.push(strElementName);
		}
	} while (objParent.parentNode);
	arElements.reverse();

	return arElements;
}

function alreadygot(arElements, objElementDetails)
{
	for (var iCounter=0; iCounter<arElements.length; iCounter++)
		if (arElements[iCounter].element == objElementDetails.element && arElements[iCounter].className == objElementDetails.className && arElements[iCounter].IDName == objElementDetails.IDName && sameParents(arElements[iCounter].parents, objElementDetails.parents))
			return true;

	return false;
}

function sameParents(arFirst, arSecond)
{
	if (arFirst.length != arSecond.length)
		return false;

	for (var iCounter=0; iCounter<arFirst.length; iCounter++)
		if (arFirst[iCounter] != arSecond[iCounter])
			return false;

	return true;
}

function checkText(objNode)
{
	do
	{
		if (objNode.nodeType == 3 && objNode.data.replace(/\s+/g, '') != '')
			return true;
	} while (objNode = objNode.nextSibling);

	return false;
}

examinecontrast();



