if(typeof Util == "undefined"){ Util = {}; }

Util.Component = function(options)
{
	//this.id = id;
	this.options = options;
	//si l'element a �t� recr�er
	this.created = true;
	if(typeof options.id != "undefined")
	{
		if(document.getElementById(options.id))
		{
			this.element = document.getElementById(options.id);
			this.created = false;
		}
		else
		{
			if(typeof options.tag != "undefined")
			{
				this.element = document.createElement(options.tag);
				this.setId(options.id);
			}
		}
	}
	else
	{
		if(typeof options.tag != "undefined")
		{
			this.element = document.createElement(options.tag);
		}
		else
		{
			this.element = options.HTMLElement;
		}
	}

	this.element.handlers = {};
	this.init();
};


Util.Component.prototype = 
{
	init: function()
	{
		//this.element = document.getElementById(this.id);
		this.element.setScope = function(scope)
		{
			this.scope = new Object();
			
			for(var v in scope)
			{
				this.scope[v] = scope[v];
			}
			
		}
		this.element.getScope = function()
		{
			return this.scope;
		}
		
		if(typeof this.options.content != "undefined")
		{
			this.setContent(this.options.content);
		}
		
		if(this.options.attributes)
		{
			for(var v in this.options.attributes)
			{
				this.addAttribute(v, this.options.attributes[v]);
			}
		}
	},
	
	isCreated: function()
	{
		return this.created;
	},
	
	getElement: function()
	{
		return this.element;
	},
	
	addAttribute: function(name, value)
	{
		this.element.setAttribute(name, value);
	},
	
	getAttribute: function(name)
	{
		return this.element[name];
	},
	
	removeAttribute: function(name)
	{
		this.element.removeAttribute(name);
	},
	
	getHTMLElement: function()
	{
		return this.element;
	},
	
	getId: function()
	{
		return this.id;
	},
	
	setId: function(id)
	{
		this.id = id;
	},

	getTop: function()
	{
		t = this.element.offsetTop;
		
		var parent=this.getDOMParent(this.element);
		while (parent)
		{

			t += parent.offsetTop - parent.scrollTop;
			
			parent=this.getDOMParent(parent);			
		}
		
		return t;
	},
	
	getLeft: function()
	{
		l = this.element.offsetLeft;
		var parent=this.getDOMParent(this.element);
		while (parent)
		{
			l += parent.offsetLeft;
			parent=this.getDOMParent(parent);
		}

		return l;
	},
	
	getBottom: function()
	{
		return this.getTop() + this.getHeight();
	},
	
	getRight: function()
	{
		return this.getLeft() + this.getWidth();
	},
	
	getWidth: function()
	{
		return this.element.offsetWidth;
	},
	
	getHeight: function()
	{
		return this.element.offsetHeight;
	},
	
	getInnerHeight: function()
	{	
		var styles = this.getStyles();
		return this.element.offsetHeight - parseInt(styles.borderTopWidth) - parseInt(styles.borderBottomWidth) - parseInt(styles.paddingTop)  - parseInt(styles.paddingBottom);
	},
	
	getDOMParent: function(d)
	{
		/*var parent = null;
		if(d.parentElement)
		{
			parent = d.parentElement;
		}
		else
		{
			parent = d.parentNode;
		}*/
		if(typeof d.offsetParent != "undefined")
		{
			var parent = d.offsetParent;
			return parent;
		}
		else
		{
			return false;
		}
	},
	
	setXY: function(x, y)
	{
		if(isNaN(x))
		{
			x = 0;
		}
		
		if(isNaN(y))
		{
			y = 0;
		}
		this.element.style.left = x+"px";
		this.element.style.top = y+"px";
	},
	
	setLeft: function(l)
	{
		this.element.style.left = l+"px";
	},
	
	setWidth: function(w)
	{
		w = w+"";
		if(w.indexOf("%") > 0)
		{
			this.element.style.width = w;
		}
		else
		{
			this.element.style.width = w+"px";
		}
	},
	
	setHeight: function(h)
	{
		h = h+"";
		if(h.indexOf("%") > 0)
		{
			this.element.style.height = h;
		}
		else
		{
			this.element.style.height = h+"px";
		}
	},
	
	setHeightParent: function()
	{
		if(this.element.parentElement)
		{
			this.setHeight(this.element.parentElement.offsetHeight);
		}
	},
	
	setTop: function(y)
	{
		if(isNaN(y))
		{
			y = 0;
		}
		this.element.style.top = y+"px";
	},
	
	setBottom: function(y)
	{
		this.element.style.top = (y - this.getHeight())+"px";
	},
	
	getBoundingRect: function()
	{
		var bound = {
			top: this.getTop(),
			left: this.getLeft(),
			width: this.getWidth(),
			height: this.getHeight()
		}
		
		return bound;
	},
	
	setBoundingRect: function(bound)
	{
		this.setTop(bound.top);
		this.setLeft(bound.left);
		this.setWidth(bound.width);
		this.setHeight(bound.height);
	},
	
	setScrollTop: function(y)
	{
		this.element.scrollTop = y+"px";
	},
	
	setMaxHeight: function(h)
	{
		var maxTop = 0;
		var minTop = 9999999;
		
		var nodes = this.element.childNodes;
		
		for(var i=0; i < nodes.length; i++)
		{
			var thisChild = nodes[i];
			if(minTop > thisChild.offsetHeight)
			{
				minTop = thisChild.offsetTop;
			}
			var bottom = thisChild.offsetTop + thisChild.offsetHeight;
			
			if(maxTop < bottom)
			{
				maxTop = bottom;
			}
		}
		
		var height = maxTop - minTop;
		
		if(height > h)
		{
			this.element.style.height = h+"px";
			
			//la hauteur a �t� chang�e
			return true;
		}
		else
		{
			this.element.style.height = "auto";
			//la hauteur n'a pas �t� chang�e
			return false;
		}
		
		
	},
	
	show: function()
	{
		if(this.element.style.display != "")
		{
			this.element.style.display = "";
		}
	},
	
	hide: function()
	{
		this.element.style.display = "none";
	},
	
	isDisplayed: function()
	{
		if(this.element.style.display == "none")
		{
			return false;
		}
		else
		{
			return true;
		}
	},
	
	/*getParent: function()
	{
		if(this.element.parentNode.id)
		{
			return new SearchEngine.el(this.element.parentNode.id);
		}
		else
		{
			return false;
		}
	},*/
	
	hasChild: function(id)
	{
		var test = false;
		for(var i in this.element.childNodes)
		{
			
		}
	},
	
	append: function(el)
	{
		this.element.appendChild(el);
	},
	
	getElementsByClassName: function(className)
	{
		var els = this.element.childNodes;
		ret = new Array();
		for(var i = 0; i < els.length; i++)
		{
			if(els[i].className == className)
			{
				ret.push(els[i]);
			}
		}		
		return ret;
	},
	
	removeElement: function(el)
	{		
		this.element.removeChild(el);
	},
	
	getContent: function()
	{
		return this.element.innerHTML;
	},
	
	setContent: function(html)
	{
		this.element.innerHTML = html;
	},
	
	putContent: function(html)
	{
		this.element.innerHTML += html;
	},
	
	putElement: function(parameters)
	{	
		var node = new Util.Component.Node(this.element);
		node.putElement(parameters);
	},
	
	setStyle: function(attribut, value)
	{
		this.element.style[attribut] = value;
	},
	
	getStyles: function()
	{
		if(this.element.currentStyle)
		{
			return this.element.currentStyle;
		}								   
		else if (window.getComputedStyle)
		{
			return window.getComputedStyle(this.element, null);
		}  
		else
		{
			return false;
		}
	},
	
	setClass: function(name)
	{
		this.element.className = name;
	},
	
	setId: function(id)
	{
		this.element.id = id;
	},
	
	//events
	on: function(e, fct, scope)
	{
		//components : objet permettant de passer dans l'evenement les instances qui sont utilis�
		switch(e)
		{
			case "enter": 
				this.element.handlers[e] = new Util.Handler({handler: fct, scope: scope});
				eval("this.element.onkeypress = function(e){ if(e.keyCode == 13){ this.handlers."+e+".execute(e); } };");
				break;
			default :
				this.element.handlers[e] = new Util.Handler({handler: fct, scope: scope});
				eval("this.element.on"+e+" = function(e){ this.handlers."+e+".execute(e) };");
				break;
		}
	},
	
	clear: function()
	{
		if(this.element.id == "sidebar"){
			//Trace.put(this.element.id+" "+this.element.childNodes.length);

		}
		for(var i=this.element.childNodes.length - 1; i >= 0; i--)
		{
			this.element.removeChild(this.element.childNodes.item(i));
		}
	},
	
	getElementsByTagName: function(name)
	{
		var tags = this.element.getElementsByTagName(name);
		
		var returns = {};
		
		for(var i in tags)
		{
			returns[i] = new Util.Component({HTMLElement: tags[i]});
		}
		
		return returns;
	}
	
	
};

Util.Component.exists = function(id)
{
	if(document.getElementById(id))
	{
		return true;
	}
	else
	{
		return false;
	}
		
}

Util.Component.Node = function(n)
{
	this.currentNode = n;
	this.childNode = null;
}

Util.Component.Node.prototype = 
{
	putElement: function(parameters)
	{
		/*
		 * format json de parameters
		 * { 
		 * 		tag: '',
		 * 		content: '' ou content: [{tag: ...}, {tag:...}],
		 * 		attributes: {
		 * 			class: '',
		 * 			...
		 * 		}
		 * }
		 */
	
		
		this.childNode = document.createElement(parameters.tag.toUpperCase());

		for(var att in parameters.attributes)
		{
			//pour IE
			if(att == "class")
			{
				this.childNode.className = parameters.attributes[att];
			}
			else
			{
				this.childNode.setAttribute(att, parameters.attributes[att]);
			}
			
		}

		if(typeof parameters.content != "string")
		{
			for(var i in parameters.content)
			{
				var newNode = new Util.Component.Node(this.childNode);
				newNode.putElement(parameters.content[i]);
				//el.innerHTML = "un autre tag";
			}
		}
		else
		{
			if(parameters.content)
			{
				this.childNode.innerHTML = parameters.content;
			}
			//this.childNode.appendChild(document.createTextNode(parameters.content));
		}
		
		//logue(this.currentNode);
		this.currentNode.appendChild(this.childNode);
	}
}

function log(txt, reset)
{
	if(typeof reset == "undefined")
	{
		document.getElementById("logs").innerHTML += txt;
	}
	else
	{
		document.getElementById("logs").innerHTML = txt;
	}
}

function getProps(obj)
{
	var txt = "";
	for(var v in obj)
	{
		txt += v+"=("+typeof obj[v]+") "+obj[v]+"<br />";
	}
	return txt;
}

/*HTMLElement.prototype.setScope = function(scope)
{
	this.scope = new Object();
	
	for(var v in scope)
	{
		this.scope[v] = scope[v];
	}
	
}

HTMLElement.prototype.getScope = function()
{
	return this.scope;
}*/

