/* onhoverclick.js */
/*
	Assumptions: 
		- table id='freerolls'
		- <a> with $cardroomname is somewhere in column 4
		- cardroom link is opened in a new window
		
	2005.Nov.27  dwl  Original
	2005.Nov.29  dwl  added findParentWithTag to better locate the tr of the click
					  event's target element
*/

// install onclick handlers for all data rows of the 'freerolls' table
function initPage()
{
	var elTable = document.getElementById('freerolls');
	if (elTable)
	{
		// assume just one body
		var aRows = elTable.tBodies[0].rows;
		// skip over first row (index 0): our th column headers
		for (var nIt = 1; nIt < aRows.length; ++nIt)
		{
			AttachEvent(aRows[nIt], 'click', rowClick, false);
		}
	}
}	// initPage()


//
function rowClick(e)
{
	if (!e) var e = window.event;
	// event handler's on the row, but the target will be one of its cells, or the link
	var elTarget = (e.srcElement ? e.srcElement : e.target);
	var bIsLink = 'A' == elTarget.tagName;
	var bIsIE = /MSIE \d/.test(navigator.userAgent);
/*
	When clicking on a link in a cell in a row, first, the rowClick() function is called and
	completes execution.  In IE, that's all that happens normaly:  the click on the link itself
	is ignored.  However, in FireFox, Safari, and others after rowClick() handler completes 
	the link click is then recognized and obeyed.
*/
	if (bIsIE || !bIsLink)
	{
		// for both link and row clicks in IE and for non-link clicks elsewhere
		// we have to open the link for the cardroom column ourselves
		if (bIsLink)
		{
			sURL = elTarget.getAttribute('href');
		}
		else
		{
			// if there are <div>s or other stuff inside the td, they might be
			// what gets clicked on and hence they're the target, so we must climb up until
			// we find the tr
			sURL = getLinkFromNthColumnOfParentRow(elTarget, 4);
		}
		if (sURL.length > 0)
		{
			window.open(sURL, "_blank" );
		}
	}
	
	return false;
}	// rowClick()


// be sure to pass sTag as upper case
function findParentWithTag(child, sTag)
{
	var parent = null;
	if (child)
	{
		var node = child;
		if (node.nodeName == "#text")	// Safari's target: #text; others TD
		{
			node = node.parentNode;
		}

		while (parent == null && node.parentNode != null)
		{
			if (node.tagName && node.tagName == sTag)
			{
				parent = node;
			}
			else
			{			
				node = node.parentNode;
			}
		}	// while
	}
	
	return parent;
}	// findParentWithTag()


// incoming column number parameter is 1-based
function getLinkFromNthColumnOfParentRow(elChild, nCol)
{
	var	sURL = '';
	var elRow = findParentWithTag(elChild, 'TR');
	if (elRow)
	{
		elCell = elRow.cells[nCol - 1];
		
		var elA = findFirstChildTag(elCell, 'A');
		if (elA)
		{
			sURL = elA.getAttribute('href');
		}
	}
	
	return sURL;
}	// getLinkFromNthColumnOfParent()


//  recursive function to find first sTag element within the elRoot element or its children
function findFirstChildTag(elRoot, sTag)
{
	var elFirstFound = null;
	if (elRoot != null)
	{
		for (var nIt = 0; nIt < elRoot.childNodes.length && elFirstFound == null; ++nIt)
		{
			var elChild = elRoot.childNodes[nIt];
			if (elChild.tagName == sTag)
			{
				elFirstFound = elChild;
			}
			else
			{
				elFirstFound = findFirstChildTag(elChild, sTag);
			}
		}	
	}
	
	return elFirstFound;
}	// findFirstChildTag()


//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}
//*** end of code copyright 2003 by Gavin Kistner
