/***************************************************************************
 * gallery.js
 *     :
 ***************************************************************************/

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

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

// TODO: Move this.
ModuleGallery.prototype.StopPropagation =
function(eventObj)
{
// IE -> W3C:
	if (typeof(window.event) != 'undefined')
	{	eventObj = window.event;	}
	if (eventObj.target == null)
	{	eventObj.target  = eventObj.srcElement;	}

// Cancel the event.
	if (typeof(event) != 'undefined')
	{	eventObj.cancelBubble = true;	}
	else
	{	eventObj.stopPropagation();		}
}

/***************************************************************************
 * ModuleGallery::InitCell
 ***************************************************************************/
ModuleGallery.prototype.InitNode =
function(domNode)
{
// Parameter validation:
	if (domNode == null)
	{	return false;	}

// Gather information.
	var codeStr = '; '+
	              'if (typeof(event) != \'undefined\') '+
	              '{ event.cancelBubble = true; } '+
	              'else '+
	              '{ event.stopPropagation(); }';

// Prevent mouse events from bubbling past this node.
// TODO: All events?
	if (domNode.attachEvent)
	{
		domNode.attachEvent('onclick'    ,ModuleGallery.prototype.StopPropagation);
		domNode.attachEvent('onmouseover',ModuleGallery.prototype.StopPropagation);
		domNode.attachEvent('onmouseout' ,ModuleGallery.prototype.StopPropagation);
	}
	else
	{
		domNode.addEventListener('click'    ,ModuleGallery.prototype.StopPropagation,false);
		domNode.addEventListener('mouseover',ModuleGallery.prototype.StopPropagation,false);
		domNode.addEventListener('mouseout' ,ModuleGallery.prototype.StopPropagation,false);
	}

// Initialize the children.
	ModuleGallery.prototype.__InitNode(domNode);
	return true;
}
ModuleGallery.prototype.__InitNode =
function(domNode)
{
// For each child...
	var  count = domNode.childNodes.length;
	var  i;
	for (i = 0; i < count; ++i)
	{
	// Alter clickable children so the click event stops there.
		var childNode = domNode.childNodes[i];
		switch (childNode.nodeName.toLowerCase())
		{
		case 'a' :
		// Alter the mouse events.
		// TODO: All events?
			if (childNode.attachEvent)
			{
				childNode.attachEvent('onclick'    ,ModuleGallery.prototype.StopPropagation);
				childNode.attachEvent('onmouseover',ModuleGallery.prototype.StopPropagation);
				childNode.attachEvent('onmouseout' ,ModuleGallery.prototype.StopPropagation);
			}
			else
			{
				childNode.addEventListener('click'    ,ModuleGallery.prototype.StopPropagation,false);
				childNode.addEventListener('mouseover',ModuleGallery.prototype.StopPropagation,false);
				childNode.addEventListener('mouseout' ,ModuleGallery.prototype.StopPropagation,false);
			}

		// Make the link open in a new window.
			childNode.setAttribute('target','_blank');
			break;
		}

	// Process the children.
		if (childNode.hasChildNodes())
		{	ModuleGallery.prototype.__InitNode(childNode);	}
	}
}

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

// Highlight the node.
	if (eventObj.currentTarget.className == '')
	{	eventObj.currentTarget.className  = 'highlight';	}
}

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

// Un-highlight the node.
	if (eventObj.currentTarget.className == 'highlight')
	{	eventObj.currentTarget.className  = '';	}
}

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

// Open the photo in a new window.
	window.open(photoData['photo']['url'],
	            '',// TODO: 'ModuleGallery::Photo',
	            'resizable=yes,'+
	            'scrollbars=yes,'+
	            'width=' +(40+photoData['photo']['width' ])+','+
	            'height='+(30+photoData['photo']['height']));
}
