var frameworks = [
	'nwmatcher-1.2.2',
	'base2-1.0',
	'jquery-1.3.2',
//	'ext-2.2-core',
	'prototype-1.6.0.3',
	'mootools-1.2.1',
    'yui-2.6.0'
//    'dojo-1.2'
],

methods = [
	'NW.Dom.select',
	'document.querySelectorAll',
	'$',
//	'Ext.query',
	'$$',
	'$$',
    'YAHOO.util.Selector.query'
//    'dojo.query'
],

selectors = [
	'body',
	'div',
	'body div',
	'div p',
	'div > p',
	'div + p',
	'div ~ p',
	'div[class^=exa][class$=mple]',
	'div p a',
	'.note',
	'div.example',
	'ul .tocline2',
	'#title',
	'h1#title',
	'div #title',
	'ul.toc li.tocline2',
	'ul.toc > li.tocline2',
	'h1#title + div > p',
	'h1[id]:contains(Selectors)',
	'a[href][lang][class]',
	'div[class]',
	'div[class=example]',
	'div[class^=exa]',
	'div[class$=mple]',
	'div[class*=e]',
	'div[class|=dialog]',
	'div[class!=made_up]',
	'div[class~=example]',
	'div:not(.example)',
	'p:contains(selectors)',
	'p:nth-child(even)',
	'p:nth-child(2n)',
	'p:nth-child(odd)',
	'p:nth-child(2n+1)',
	'p:nth-child(n)',
	'p:only-child',
	'p:last-child',
	'p:first-child'
],

selectors1 = [
	'div:contains(CELIA)',
	'div:nth-child(n)',
	'div:nth-child(even)',
	'div:nth-child(2n)',
	'div:nth-child(odd)',
	'div:nth-child(2n+1)',
	'div:first-child',
	'div:only-child',
	'div:last-child',
	'div > div',
	'div + div',
	'div ~ div',
	'body',
	'body div',
	'div',
	'div div',
	'div div div',
	'.dialog',
	'div.dialog',
	'div .dialog',
	'#speech5',
	'div#speech5',
	'div #speech5',
	'div.scene div.dialog',
	'div#scene1 div.dialog div',
	'#scene1 #speech1',
	'div[class]',
	'div[class=dialog]',
	'div[class^=dia]',
	'div[class$=log]',
	'div[class*=sce]',
	'div[class|=dialog]',
	'div[class!=madeup]',
	'div[class~=dialog]'
];

function initialize() {

	var D = document, i, j,
		tb, tf, tr, th, td,

	// add table head cells
	th = D.getElementById('thead');
	tr = th.getElementsByTagName('tr')[0];
	for (i = 0; frameworks.length > i; i++) {
		th = D.createElement('th');
		th.className = 'framework';
		tr.appendChild(th);
		th.appendChild(D.createTextNode(frameworks[i]));
	}

	// add table body rows
	tb = D.getElementById('tbody');
	for (i = 0; selectors.length > i; i++) {
		tr = D.createElement('tr');
		th = D.createElement('th');
		th.className = 'selector';
		tr.appendChild(th);
		th.appendChild(D.createTextNode(selectors[i]));
		for (j = 0; frameworks.length > j; j++) {
			td = D.createElement('td');
			td.className = 'empty';
		    tr.appendChild(td);
		}
		tb.appendChild(tr);
	}

	// add table foot cells
	tf = D.getElementById('tfoot');
	tr = tf.getElementsByTagName('tr')[0];
	for (i = 0; frameworks.length > i; i++) {
		td = D.createElement('td');
		td.className = 'score';
		tr.appendChild(td);
	}

}

(function(global) {

	var i, iframe,
	context = global.document,
    root = context.documentElement;

	function attachIFrames() {
		// add an iframe for each framework
		for (i = 0; frameworks.length > i; i++) {
			iframe = context.createElement('iframe');
			iframe.id = frameworks[i];
			iframe.name = frameworks[i];
//			root.insertBefore(iframe, root.firstChild);
//			iframe = document.body.insertBefore(iframe, document.body.firstChild);
			iframe = document.body.appendChild(iframe);
			iframe.id = frameworks[i];
			iframe.name = frameworks[i];
			iframe.src = 'template.html?framework=' +
				frameworks[i] + '.js&method=' + methods[i];
		}
	}

	// needed to accomodate IE
//	if (typeof context.fileSize != 'undefined') {
/*
		(function start() {
			if (document.body !== null) {
				root = document.body;
				attachIFrames();
			} else {
				setTimeout(arguments.callee, 0);
			}
		})();
*/
//	} else {
//		attachIFrames();
//	}

	// initialize results table
	if (context.addEventListener) {
		context.addEventListener('DOMContentLoaded', function() {
			context.removeEventListener('DOMContentLoaded', arguments.callee, false);
			attachIFrames();
			initialize();
		}, false);
	} else {
		context.attachEvent('onreadystatechange', function() {
			if (context.readyState == 'complete') {
				context.detachEvent('onreadystatechange', arguments.callee);
				attachIFrames();
				initialize();
			}
		});
	}

})(this);
