define(['scripts/knockout-3.3.0','scripts/act.js', 'scripts/ACTUtils.js'], function(ko, ACTSocket, ACTUtils) {

	return function InvokeCallback(service, cb, args) {
		ACTUtils.Log("invoke callback " + cb.Id, 1);
		var mousePos = ACTUtils.GetMousePosition();
		var obj = { 
			$type: "Ansys.ACT.Core.UI.HTMLRenderer.CallWebObject, Ansys.ACT.Core, Culture=neutral, PublicKeyToken=null",
			Id: cb.Id,
			Args: args,
			mousePositionX: mousePos.x,
			mousePositionY: mousePos.y,
		};

		if (cb.LockOnCall && service != "") {
			var mainComponentElement = $(".mainComponent")[0];
			var mainComponent = ko.dataFor(mainComponentElement);
			mainComponent.lock(cb.Id, cb.NoLocksComps);

			// message
			if (cb.LockMessage) {
				var path = cb.LockMessage.Path;
				var message = cb.LockMessage.Message;

				// retrieve the parent component corresponding to the path
				var currentComponent = mainComponent; 
				for (var pathIndex = 0; pathIndex < path.length - 1; pathIndex++) {
					var pathElem = path[pathIndex];
					var nextComponent = currentComponent.componentModels[pathElem];
					if (nextComponent != null) {
						var elem = $(nextComponent.element).children().first().children()[0];
						currentComponent = ko.dataFor(elem);
					}
				}

				// retrieve the last base component
				currentComponent = currentComponent.componentModels[path[path.length - 1]];
				currentComponent.setLockMessage(message);
			}
		}

		ACTSocket.send(service, obj);
	};
	
});
