GF_Err gf_node_replace(GF_Node *node, GF_Node *new_node, Bool updateOrderedGroup)
{
#ifndef GPAC_DISABLE_SVG
	u32 type;
#endif
#ifndef GPAC_DISABLE_VRML
	Bool replace_proto;
#endif
	Bool replace_root;

#ifndef GPAC_DISABLE_SVG
	type = (node->sgprivate->tag>GF_NODE_RANGE_LAST_VRML) ? 1 : 0;
	if (type) {
		Replace_IRI(node->sgprivate->scenegraph, node, new_node);
	}
#endif

	/*first check if this is the root node*/
	replace_root = (node->sgprivate->scenegraph->RootNode == node) ? 1 : 0;

#ifndef GPAC_DISABLE_VRML
	/*check for proto replacement*/
	replace_proto = 0;
	if (node->sgprivate->scenegraph->pOwningProto
	        && (gf_list_find(node->sgprivate->scenegraph->pOwningProto->node_code, node)>=0)) {
		replace_proto = 1;
	}
#endif

	while (node->sgprivate->parents) {
		Bool do_break = node->sgprivate->parents->next ? 0 : 1;
		GF_Node *par = node->sgprivate->parents->node;

#ifndef GPAC_DISABLE_SVG
		if (type)
			ReplaceIRINode(par, node, new_node);
		else
#endif
			ReplaceDEFNode(par, node, new_node, updateOrderedGroup);

		if (new_node) gf_node_register(new_node, par);
		gf_node_unregister(node, par);
		gf_node_changed(par, NULL);
		if (do_break) break;
	}

	if (replace_root) {
		GF_SceneGraph *pSG = node->sgprivate->scenegraph;
		gf_node_unregister(node, NULL);
		pSG->RootNode = new_node;
	}
#ifndef GPAC_DISABLE_VRML
	if (replace_proto) {
		GF_SceneGraph *pSG = node->sgprivate->scenegraph;
		gf_list_del_item(pSG->pOwningProto->node_code, node);
		if (pSG->pOwningProto->RenderingNode==node) pSG->pOwningProto->RenderingNode = NULL;
		gf_node_unregister(node, NULL);
	}
#endif
	return GF_OK;
}
GF_Err gf_node_replace(GF_Node *node, GF_Node *new_node, Bool updateOrderedGroup)
{
#ifndef GPAC_DISABLE_SVG
	u32 type;
#endif
#ifndef GPAC_DISABLE_VRML
	Bool replace_proto;
#endif
	Bool replace_root;

#ifndef GPAC_DISABLE_SVG
	type = (node->sgprivate->tag>GF_NODE_RANGE_LAST_VRML) ? 1 : 0;
	if (type) {
		Replace_IRI(node->sgprivate->scenegraph, node, new_node);
	}
#endif

	 
	replace_root = (node->sgprivate->scenegraph->RootNode == node) ? 1 : 0;

#ifndef GPAC_DISABLE_VRML
	 
	replace_proto = 0;
	if (node->sgprivate->scenegraph->pOwningProto
	        && (gf_list_find(node->sgprivate->scenegraph->pOwningProto->node_code, node)>=0)) {
		replace_proto = 1;
	}
#endif

	while (node->sgprivate->parents) {
		Bool do_break = node->sgprivate->parents->next ? 0 : 1;
		GF_Node *par = node->sgprivate->parents->node;

#ifndef GPAC_DISABLE_SVG
		if (type)
			ReplaceIRINode(par, node, new_node);
		else
#endif
			ReplaceDEFNode(par, node, new_node, updateOrderedGroup);

		if (new_node) gf_node_register(new_node, par);
		gf_node_unregister(node, par);
		gf_node_changed(par, NULL);
		if (do_break) break;
	}

	if (replace_root) {
		GF_SceneGraph *pSG = node->sgprivate->scenegraph;
		gf_node_unregister(node, NULL);
		pSG->RootNode = new_node;
	}
#ifndef GPAC_DISABLE_VRML
	if (replace_proto) {
		GF_SceneGraph *pSG = node->sgprivate->scenegraph;
		gf_list_del_item(pSG->pOwningProto->node_code, node);
		if (pSG->pOwningProto->RenderingNode==node) pSG->pOwningProto->RenderingNode = NULL;
		gf_node_unregister(node, NULL);
	}
#endif
	return GF_OK;
}