// $Id: layerhandler.js,v 1.9 2008/04/16 09:05:15 goncalves Exp $

//require statements
dojo.require( "dojox.uuid.Uuid" );
dojo.require( "dojox.uuid.generateTimeBasedUuid" );

/**
 * Add the specified catalog layer to the current context
 */
function addCatalogLayer(catalogContext, targetContext, layerNode) {
	// Clone the node and modify the uuid (newUuid -> uuid)
	var clonedLayerNode = layerNode.cloneNode(true);
	var catalogUuidNode = clonedLayerNode.selectSingleNode("wmc:Extension/wmc:Infoterre/wmc:UnderlyingUUID");
	var uuidNode = clonedLayerNode.selectSingleNode("wmc:Extension/wmc:Infoterre/wmc:UUID");
	var idNode = clonedLayerNode.selectSingleNode("@id");
	var catalogUuid = catalogUuidNode.firstChild.nodeValue;
	var newUuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid()).toString();

	// Replace the uuid
	uuidNode.firstChild.data = newUuid;
	clonedLayerNode.setAttribute("id", newUuid);

	// Add the layer to the current context (notify automatically the server)
	targetContext.setParam('addLayer', clonedLayerNode);

	// Notify the modification
	notifyCatalogLayerAddition(targetContext, clonedLayerNode);
}

/**
 * Add the specified external layer to the current context
 */
function addExternalLayer(targetContext, layerNode) {
	// Modify the uuid (newUuid -> uuid)
	var clonedLayerNode = layerNode.cloneNode(true);
	var uuidNode = clonedLayerNode.selectSingleNode("wmc:Extension/wmc:Infoterre/wmc:UUID");
	var idNode = clonedLayerNode.selectSingleNode("@id");
	var newUuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid()).toString();

	// Replace the uuid
	uuidNode.firstChild.data = newUuid;
	clonedLayerNode.setAttribute("id", newUuid);

	// Add the layer to the current context (notify automatically the server)
	targetContext.setParam('addLayer', clonedLayerNode);

	// Notify the modification
	notifyExternalLayerAddition(targetContext, clonedLayerNode);
}

/**
 * Add the specified geocatalog layer to the current context
 */
function addGeocatalogLayer(targetContext, layerNode) {
	// Modify the uuid (newUuid -> uuid)
	var clonedLayerNode = layerNode.cloneNode(true);
	var uuidNode = clonedLayerNode.selectSingleNode("wmc:Extension/wmc:Infoterre/wmc:UUID");
	var idNode = clonedLayerNode.selectSingleNode("@id");
	var newUuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid()).toString();

	// Replace the uuid
	uuidNode.firstChild.data = newUuid;
	clonedLayerNode.setAttribute("id", newUuid);

	// Add the layer to the current context (notify automatically the server)
	targetContext.setParam('addLayer', clonedLayerNode);

	// Notify the modification
	notifyGeocatalogLayerAddition(targetContext, clonedLayerNode);
}