



window.onload = function()
{
	var tables = document.getElementsByTagName('table');
	for(var i=0; i<tables.length; i++)
	{
		if(tables[i].className.indexOf('results') != -1)
		{
			tables[i].behaviors = new resultsTable(tables[i]);
		}
	}
};



//results table object constructor
function resultsTable(table)
{
	//reference to this
	var self = this;


	//table object
	this.table = table;

	//get table cells
	var cells = this.table.getElementsByTagName('td');

	//for each cell
	for(var i=0; i<cells.length; i++)
	{
			//if it's a relevant data cell
			if(cells[i].getAttribute('headers') && cells[i].headers != 'event' && cells[i].headers != 'bbs-event')
			{
				//copy and remove the text node
				var text = cells[i].firstChild.nodeValue;
				cells[i].removeChild(cells[i].firstChild);

				//create and append a link
				var link = cells[i].appendChild(self.createElement('a'));

				//give it a placebo javascript: uri
				link.href  = 'javascript:void(null)';

				//write back the text
				link.appendChild(document.createTextNode(text));

				//if the TD had a title attriubte
				if(cells[i].getAttribute('title'))
				{
					//get browser, device and event details
					self.getDetails(cells[i]);

					//give it a title attribute with those details plus TD title
					link.setAttribute('title', self.device
						+ ' - ' + self.description + ': '
						+ '\r\n\r\n' + cells[i].title);
				}
			}
	}


	//tooltip object
	this.tooltip = null;


	//bind a click handler to the table
	this.table.onclick = function(e)
	{
		//don't continue for Internet Explorer
		//because it doesn't support position:fixed
		if(typeof document.uniqueID != 'undefined') { return; }

		//get event target
		var target = e ? e.target : window.event.srcElement;

		//convert text-node reference for safari
		if(target.nodeName == '#text') { target = target.parentNode; }

		//if target is a link, set to parent node
		if(target.href) { target = target.parentNode; }

		//if it's a TD and not a rowscope cell
		if(/td$/i.test(target.nodeName) && target.getAttribute('headers') && target.headers != 'event')
		{
			//store its title text
			this.text = target.getAttribute('title');

			//if there is some
			if(this.text)
			{
				//if we already have a custom tooltip
				if(self.tooltip)
				{
					//remove it and nullify the reference
					self.tooltip.parentNode.removeChild(self.tooltip);
					self.tooltip = null;

					//force a redraw in opera
					//which is needed to make the tooltip disappear
					self.operaRedraw();
				}

				//create a new tooltip element
				self.tooltip = self.createElement('p');

				//get browser, device and event details
				self.getDetails(target);

				//add a strong tag with the browser, device and event details
				this.label = self.tooltip.appendChild(self.createElement('strong'));
				this.label.appendChild(document.createTextNode(
					self.device
					+ ' - ' + self.description + ': ')
					);

				//add in the title text
				self.tooltip.appendChild(document.createTextNode(this.text));

				//append it to the TD
				target.appendChild(self.tooltip);


				//bind a double-click handler to dismiss it
				self.tooltip.ondblclick = function()
				{
					//remove it and nullify the reference
					this.parentNode.removeChild(this);
					self.tooltip = null;

					//force a redraw in opera
					//which is needed to make the tooltip disappear
					self.operaRedraw();
				};

				//add a title to that effect
				self.tooltip.title = 'Double-click to dismiss';
			}
		}

		return this;
	};
};


//create an element
resultsTable.prototype.createElement = function(tag)
{
	return typeof document.createElementNS != 'undefined' ? document.createElementNS('http://www.w3.org/1999/xhtml', tag) : document.createElement(tag);
};



//get browser, device and event details
//from other cell and header attributes
resultsTable.prototype.getDetails = function(cell)
{
	//get the browser and device from device header's title attribute
	this.device = document.getElementById(cell.getAttribute('headers').split(' ')[1]).title;

	//get the pertaining row-scope description
	//by iterating back through siblings until we find it
	var rowscope = cell.previousSibling;
	while(rowscope && (!/td$/i.test(rowscope.nodeName) || (rowscope.getAttribute('headers') != 'event' && rowscope.getAttribute('headers') != 'bbs-event')))
	{
		rowscope = rowscope.previousSibling;
	}

	//get the description from title attribute
	this.description = rowscope.title;
};



//force a redraw in opera
resultsTable.prototype.operaRedraw = function()
{
	//if this is opera
	if(typeof window.opera != 'undefined')
	{
		//generate a scrollBy event
		//which forces it to redraw the window
		//the visual change can't be helped
		//because if we used a value with no visual effect,
		//such as (-1,0), then the problem would remain
		window.scrollBy(0,1);
	}
};
