var asycLoadingScenesCount = 0;
function loadScene(path, timeout) {
    REGISTER_LISTENER(Event.EVT_IO_SCENE_IMPORT_DONE);
    if (timeout == undefined) {
        timeout = 0;
    }
    var evt = Event.GET_EVT_IO_SCENE_IMPORT(path);
	evt.PostToKernel();

	var evt1 = Event.WaitForEvent(Event.GET_EVT_IO_SCENE_IMPORT_DONE(-1, -1, ""), timeout);
	if ((evt1 != undefined) && (evt1.type == Event.EVT_IO_SCENE_IMPORT_DONE))
	    return evt1.sceneID;
	return 0;
}

function exportScene(path, sceneID, timeout) {
	var evt=Event.Get(Event.EVT_IO_SCENE_EXPORT)
	evt.string=path;
	evt.int=-1;
	evt.id=sceneID;
	evt.PostToKernel();

	return 0;
}

function loadInto(path, sceneID, rootID, timeout) {
    REGISTER_LISTENER(Event.EVT_IO_SCENE_IMPORT_DONE);
    if (timeout == undefined) {
        timeout = 0;
    }
    var evt = Event.GET_EVT_IO_SCENE_IMPORT(path, sceneID, rootID);
    evt.PostToKernel();

    Event.WaitForEvent(Event.GET_EVT_IO_SCENE_IMPORT_DONE(sceneID, -1, ""), timeout);
}

function loadIntoAsync(path, sceneID, rootID) {
    var evt = Event.GET_EVT_IO_SCENE_IMPORT(path, sceneID, rootID);
	evt.PostToKernel();
}

function loadIntoAsync2(path, sceneID, rootID) {
    Event.GET_EVT_IO_SCENE_IMPORT(path, sceneID, rootID).PostToKernel();
    ++asycLoadingScenesCount;
}

function waitUntilLoaded(numScenes, timeout) {
    REGISTER_LISTENER(Event.EVT_IO_SCENE_IMPORT_DONE);
    if (timeout == undefined) {
        timeout = 0;
    }
	for (i=0; i<numScenes; i++) {
	    Event.WaitForEvent(Event.GET_EVT_IO_SCENE_IMPORT_DONE(-1, -1, ""), timeout);
	}
}

function waitUntilLoaded2(timeout) {
    waitUntilLoaded(asycLoadingScenesCount, timeout);
}

function renderScene(sceneID, timeout, cameraID, windowID, useActiveStereo) {
    REGISTER_LISTENER(Event.EVT_RENDER_SCENE_DONE);
    if (timeout == undefined) {
        timeout = 0;
    }
    if (cameraID == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID);
    else if (windowID == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID);
    else if (useActiveStereo == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID, windowID);
    else
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID, windowID, useActiveStereo);
    evt.PostToKernel();

	var evt1 = Event.WaitForEvent(Event.GET_EVT_RENDER_SCENE_DONE(-1, -1, -1), timeout);
	if ((evt1 != undefined) && (evt1.type == Event.EVT_RENDER_SCENE_DONE))
	    return evt1.cameraID;
	return -2;
}

function renderScene2(sceneID, timeout, cameraID, windowID, useActiveStereo) {
    REGISTER_LISTENER(Event.EVT_RENDER_SCENE_DONE);
    if (timeout == undefined) {
        timeout = 0;
    }
    if (cameraID == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID);
    else if (windowID == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID);
    else if (useActiveStereo == undefined)
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID, windowID);
    else
        var evt = Event.GET_EVT_RENDER_SCENE(sceneID, cameraID, windowID, useActiveStereo);
    evt.PostToKernel();

    return Event.WaitForEvent(Event.GET_EVT_RENDER_SCENE_DONE(-1, -1, -1), timeout);
}

function getFirstNameNodeID(scene, nodeName) {
    var nodes = scene.GetNodes(nodeName);
    if (nodes != undefined) {
        if (nodes.length > 1)
            LOGWARNING("getFirstNameNodeID: more then one node (" + nodeName + "): " + nodes);
        if (nodes.length > 0)
            return nodes[0].GetID();
    }
    LOGWARNING("Node " + nodeName + " not found");
    return -1;
}

function getNameNodeID(scene, nodeName, index) {
    var nodes = scene.GetNodes(nodeName);
    if (nodes != undefined) {
        if (nodes.length > 1)
            LOGWARNING("getFirstNameNodeID: more then one node (" + nodeName + "): " + nodes);
        if (nodes.length > index)
            return nodes[index].GetID();
    }
    LOGWARNING("Node " + nodeName + " not found");
    return -1;
}

function getLastNodeID(scene, nodeName) {
    var nodes = scene.GetNodes(nodeName);
    if (nodes != undefined) {
        if (nodes.length > 0)
            return nodes[nodes.length - 1].GetID();
    }
    LOGWARNING("Nodes not found");
    return -1;
}
