/**  @author Gilles Coomans <gilles.coomans@gmail.com> */
var utils = module.exports = {
	destroyChildren: function(node, removeFromParent) {
		if (!node.childNodes)
			return;
		var childNodes = node.childNodes;
		if (removeFromParent)
			childNodes = [].slice.call(childNodes);
		for (var i = 0; i < childNodes.length; i++)
			utils.destroyElement(childNodes[i], removeFromParent);
	},
	destroyElement: function(node, removeFromParent) {
		if (node.context) {
			node.context.destroy();
			node.context = null;
		}

		if (node.__yContainer__) {
			if (node.childNodes)
				utils.destroyChildren(node, removeFromParent);
		} else {
			if (removeFromParent && node.parentNode)
				node.parentNode.removeChild(node);
			if (node.nodeType !== 3)
				utils.destroyChildren(node);
		}
		if (node.binds) {
			for (var i = 0, len = node.binds.length; i < len; i++)
				node.binds[i]();
			node.binds = null;
		}
	},
	emptyNode: function(node) {
		if (!node.childNodes || !node.childNodes.length)
			return;
		while (node.firstChild)
			node.removeChild(node.firstChild);
	},
	hide: function(node) {
		if (node.__yContainer__)
			return node.hide();
		if (node.style)
			node.style.display = 'none';
		return node;
	},
	show: function(node) {
		if (node.__yContainer__)
			return node.show();
		if (node.style)
			node.style.display = '';
		return node;
	},
	insertBefore: function(parent, node, ref) {
		if (node.__yContainer__) {
			if (node.childNodes)
				for (var i = 0, len = node.childNodes.length; i < len; ++i)
					utils.insertBefore(parent, node.childNodes[i], ref);
		} else
			parent.insertBefore(node, ref);
	},
	/**
	 * parse api method reference as "apiname:mywidget"
	 * @param  {[type]} env  [description]
	 * @param  {[type]} path [description]
	 * @return {[type]}      [description]
	 */
	getApiMethod: function(api, path) {
		if (!path.forEach)
			path = path.split(':');
		if (path.length !== 2)
			throw new Error('yamvish method call badly formatted : ' + path.join(':'));
		if (!api[path[0]])
			throw new Error('no api found with "' + path.join(':') + '"');
		var output = api[path[0]][path[1]];
		if (!output)
			throw new Error('no template/container found with "' + path.join(':') + '"');
		return output;
	},
	toBinds: function(node, func) {
		node.binds = nodes.binds || [];
		node.binds.push(func);
	},
	castNodeValueTo: function(node, type) {
		switch (type) {
			case 'text':
				return node.textContent;
			case 'integer':
				return parseInt(node.textContent, 10);
			case 'html':
				return node.innerHTML;
			default:
				throw new Error('content editable casting fail : unrecognised rule : ', type);
		}
	}
};

var objectUtils = require('nomocas-utils/lib/object-utils');
for (var i in objectUtils)
	utils[i] = objectUtils[i];

@@ -82,16 +82,753 @@
 rts = %7B%0A
+%09insertHTML: function(content, node, nextSibling) %7B%0A%09%09if (!content)%0A%09%09%09return;%0A%09%09var elems = %5B%5D,%0A%09%09%09div = document.createElement('div'),%0A%09%09%09wrapped;%0A%09%09if (content%5B0%5D !== '%3C') %7B // to avoid bug of text node that disapear%0A%09%09%09content = '%3Cp%3E' + content + '%3C/p%3E';%0A%09%09%09wrapped = true;%0A%09%09%7D%0A%09%09div.innerHTML = content;%0A%09%09var parent = wrapped ? div.firstChild : div,%0A%09%09%09childNodes = %5B%5D.slice.call(parent.childNodes),%0A%09%09%09frag;%0A%09%09if (nextSibling)%0A%09%09%09frag = new DocumentFragment();%0A%09%09for (var i = 0, len = childNodes.length; i %3C len; ++i) %7B%0A%09%09%09var el = childNodes%5Bi%5D;%0A%09%09%09elems.push(el)%0A%09%09%09if (!nextSibling)%0A%09%09%09%09node.appendChild(el);%0A%09%09%09else%0A%09%09%09%09frag.appendChild(el);%0A%09%09%7D%0A%09%09if (nextSibling)%0A%09%09%09node.insertBefore(frag, nextSibling);%0A%09%09return elems;%0A%09%7D,%0A
 %09destroy

add utils.insertHTML