/***************************************************************************
 * table.js
 *     :
 ***************************************************************************/

/***************************************************************************
 * class ModuleTable
 ***************************************************************************/
function ModuleTable()
{
// Ensure this function is only called as a constructor.
	if (!(this instanceof ModuleTable))
	{	return new ModuleTable();	}
	return this;
}

// Inherit from class Object.
ModuleTable.prototype = new Object();

// Static members:

/***************************************************************************
 * ModuleTable::Init
 ***************************************************************************/
ModuleTable.prototype.Init =
function(tableNode)
{
// Parameter validation:
	if (tableNode == null)
	{	return false;	}

// Initialize the table row actions.
	jQuery('tr[action][action!=""]',tableNode).each(function(idx,nodeObj)
	{
	// Gather information.
		var action = nodeObj.getAttribute('action');

	// Apply the specified action.
		if (action == '#')
		{
		// Set the cursor.
			nodeObj.style.cursor = 'default';

		// Stop propagation of the event, but do not prevent the default handler.
			jQuery(nodeObj).click(function(eventObj)
			{	eventObj.stopPropagation();	});
			jQuery(nodeObj).mouseover(function(eventObj)
			{	eventObj.stopPropagation();	});
			jQuery(nodeObj).mouseout(function(eventObj)
			{	eventObj.stopPropagation();	});
		}
		else
		{
		// Set the cursor.
			nodeObj.style.cursor = 'pointer';

		// Define event handlers.
			jQuery(nodeObj).click(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnClick(eventObj,this,action);
			});
			jQuery(nodeObj).mouseover(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnMouseOver(eventObj,this);
			});
			jQuery(nodeObj).mouseout(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnMouseOut(eventObj,this);
			});
		}
	});

// Initialize the table cell actions.
	jQuery('td[action][action!=""]',tableNode).each(function(idx,nodeObj)
	{
	// Gather information.
		var action = nodeObj.getAttribute('action');

	// Apply the specified action.
		if (action == '#')
		{
		// Set the cursor.
			nodeObj.style.cursor = 'default';

		// Stop propagation of the event, but do not prevent the default handler.
			jQuery(nodeObj).click(function(eventObj)
			{	eventObj.stopPropagation();	});
			jQuery(nodeObj).mouseover(function(eventObj)
			{	eventObj.stopPropagation();	});
			jQuery(nodeObj).mouseout(function(eventObj)
			{	eventObj.stopPropagation();	});
		}
		else
		{
		// Set the cursor.
			nodeObj.style.cursor = 'pointer';

		// Define event handlers.
			jQuery(nodeObj).click(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnClick(eventObj,this,action);
			});
			jQuery(nodeObj).mouseover(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnMouseOver(eventObj,this);
			});
			jQuery(nodeObj).mouseout(function(eventObj)
			{
			// Prevent the default handler.
			// TODO: Will this stop propagation, too?
				ModuleTable.prototype.OnMouseOut(eventObj,this);
			});
		}
	});
}

/***************************************************************************
 * ModuleTable::OnMouseOver
 ***************************************************************************/
ModuleTable.prototype.OnMouseOver =
function(eventObj,currentTarget)
{
// IE -> W3C:
	if (typeof(event) != 'undefined')
	{	eventObj = event;	}
	if (eventObj.target == null)
	{	eventObj.target  = eventObj.srcElement;	}
	if (eventObj.currentTarget == null)
	{	eventObj.currentTarget  = currentTarget;	}

	var domNode = eventObj.currentTarget;
	switch (domNode.className.trim())
	{
	case '' :
		domNode.className = 'highlight';
		break;
	case 'alt' :
		domNode.className = 'alt highlight';
		break;
	}
}

/***************************************************************************
 * ModuleTable::OnMouseOut
 ***************************************************************************/
ModuleTable.prototype.OnMouseOut =
function(eventObj,currentTarget)
{
// IE -> W3C:
	if (typeof(event) != 'undefined')
	{	eventObj = event;	}
	if (eventObj.target == null)
	{	eventObj.target         = eventObj.srcElement;	}
	if (eventObj.currentTarget == null)
	{	eventObj.currentTarget  = currentTarget;	}

	var domNode = eventObj.currentTarget;
	switch (domNode.className.trim())
	{
	case 'highlight' :
		domNode.className = '';
		break;
	case 'alt highlight' :
		domNode.className = 'alt';
		break;
	}
}

/***************************************************************************
 * ModuleTable::OnClick
 ***************************************************************************/
ModuleTable.prototype.OnClick =
function(eventObj,currentTarget,action)
{
// IE -> W3C:
	if (typeof(event) != 'undefined')
	{	eventObj = event;	}
	if (eventObj.target == null)
	{	eventObj.target         = eventObj.srcElement;	}
	if (eventObj.currentTarget == null)
	{	eventObj.currentTarget  = currentTarget;	}

	regEx = new RegExp('^javascript:(.*)$','i');
	if (matches = regEx.exec(action))
	{	eval(matches[1]);			}
	else
	{	document.location = action;	}

// Stop the event propagation.
/* TODO: IE does not support this.
	eventObj.stopPropagation();
*/
}

