/**
 * Container - Easy use of container
 *
 * @author 	Eric Jeker <eric.jeker@virtua.ch>
 * @version	0.1
 * @licence DWYW : Do Whatever You Want
**/

var Container = {
	uri : "http://www.w3.org/1999/xhtml",
	
	mouseOver 	: null,
	mouseOut 	: null,
	mouseDown 	: null,
	mouseUp 	: null,
	mouseMove	: null,
	
	// Set function of mouseOver event
	setMouseOver : function (func)	{
		Container.mouseOver = func ;
	},

	// Set function of mouseOut event
	setMouseOut : function (func)	{
		Container.mouseOut = func ;
	},
	
	// Set function of mouseDown event
	setMouseDown : function (func)	{
		Container.mouseDown = func ;
	},

	// Set function of mouseUp event
	setMouseUp : function (func)	{
		Container.mouseUp = func ;
	},

	// Set function of mouseMove event
	setMouseMove : function (func)	{
		Container.mouseMove = func ;
	},
	
	// Attach an element to the container
	attachElement : function (el)	{
		el.onmouseover 	= Container.mouseOver ;
		el.onmouseout 	= Container.mouseOut ;
		el.onmousedown 	= Container.mouseDown ;
		el.onmouseup 	= Container.mouseUp ;
	},
	
	// Return a child node by its class name
	getChildNodeByClass : function (el, attr)	{
		els = el.childNodes ;
		for (var i = 0 ; i < els.length ; i++)	{
			if (els[i].nodeType == 1)	{
				if (els[i].className.match(attr))	{
					return els[i] ;
				}
			}
		}
		
		return null ;
	},

	// Return a child node by its id
	getChildNodeById : function (el, attr)	{
		els = el.childNodes ;
		for (var i = 0 ; i < els.length ; i++)	{
			if (els[i].nodeType == 1)	{
				if (els[i].getAttribute('id').match(attr))	{
					return els[i] ;
				}
			}
		}
		
		return null ;
	},
	
	// Show / Hide the container
	showHide : function (r)	{
		if (r.style)	{
			if (r.style['display'] == '' || r.style['display'] == 'none')	{
				r.style.display = 'block' ;
			} else {
				r.style.display = 'none' ;
			}
			return true ;
		}
		
		return false ;
	},
	
	// Create a container
	create : function(tag, id)	{
		container = document.createElementNS ? document.createElementNS(Container.uri, tag) : document.createElement(tag);
		
		if (id && id != '')	{
			container.setAttribute("id", id);
		}
		
		return container ;
	},
	
	// Append a container to any object
	append : function(obj, container)	{
		obj.appendChild(container)
		
		return obj ;
	}
}