// $Id: contexthandler.js,v 1.11 2008/09/04 15:10:43 goncalves Exp $

// Prerequisites


/**
 * Rename the current context
 */
function renameCurrentContext(newName) {

	// Notify the server
	notifyContextRenaming(newName);

	refreshContextDisplay();
}

/**
 * Save the current context as WMC
 */
function saveCurrentContext() {
	window.location = "./currentContext?mode=strict&datestamp=" + new Date().getTime();
}

/**
 * Load a WMC as new current context
 */
function loadWMC() {

	var uploadField = document.getElementById("loadWMC.file");
	
	if (uploadField.value) {
		// Load the WMC as new current context
		document.getElementById('loadWMCForm').submit();
	} else {
		displayErrorMessage(gErrors["error.wmc.loading.file.mandatory"]);	
	}
}

/**
 * Create a new context (and set it as current context)
 */
function createContext() {
	var name = document.getElementById("newContextName").value;
	if (name) {
		// Build the request
		var request = 
			{
				method : "createContext", 
				name: name
			};
		
		// Send it to the server side
		dojo.xhrGet({
	        url: "wsControler",
			content: request,
			load: function(response, ioArgs){updateContextHandler();}
		});
	}
}

/**
 * Switch the current context
 */
function switchCurrentContext(newUUID) {
	// Build the request
	var request = 
		{
			method : "switchCurrentContext", 
			uuid: newUUID
		};
	
	// Send it to the server side
	dojo.xhrGet({
        url: "wsControler",
		content: request,
		load: function(response, ioArgs){updateContextHandler();}
	});
}

/**
 * Load the selected cartotheque context (if any)
 */
function loadContextFromCartotheque(selId) {
	if (selId){
		// Build the request
		var request = 
			{
				method : "loadCartothequeContext", 
				uuid: selId
			};
		
		// Send it to the server side
		dojo.xhrGet({
	        url: "wsControler",
			content: request,
			load: function(response, ioArgs){updateContextHandler();}
		});
	}
}

/**
 * Load the selected cartotheque context (if any)
 */
function loadContextFromCartothequeSplash(uuid) {
	// Build the request
	var request = 
		{
			method : "loadCartothequeContext", 
			uuid: uuid
		};
	
	// Send it to the server side
	dojo.xhrGet({
        url: "wsControler",
		content: request,
		load: function(response, ioArgs){updateContextHandler(); showMainPage();}
	});
}

/**
 * Delete the current context
 */
function deleteCurrentContext() {
	// Build the request
	var request = 
		{
			method : "deleteCurrentContext"
		};
	
	// Send it to the server side
	dojo.xhrGet({
        url: "wsControler",
		content: request,
		load: function(response, ioArgs){updateContextHandler();}
	});
}

/**
 * Handle context switching
 */
function updateContextHandler() {
	refreshContextDisplay();
	config.objects.mainMap.callListeners("reloadModel");
}

/**
 * Refresh the different pane where the current context is displayed
 */
function refreshContextDisplay() {
	dijit.byId("menuBarContentPane").refresh();
	dijit.byId("authenticatedContentPane").refresh();
	//dijit.byId("cartothequeLinkPane").refresh();
	//dijit.byId("cartothequeAdminLinkPane").refresh();
}