updateProgress();

function Engine() {
	
	
	this.init = init;
	
	this.canGoBackward = canGoBackward;
	this.canGoForward = canGoForward;
	this.canGoDown = canGoDown;
	this.canGoUp = canGoUp;
	
	this.goForward = goForward;
	this.goBackward = goBackward;
	
	this.goUp = goUp;
	this.goDown = goDown;
	
	this.isGlossaryEnabled = isGlossaryEnabled;
	this.isFaqEnabled = isFaqEnabled;
	this.isHelpEnabled = isHelpEnabled;
	
	this.isCurrentStepAExercise = isCurrentStepAExercise;
	this.isFeedbackEnabled = isFeedbackEnabled;
	
	this.getVariable = getVariable;
	this.evaluateVariables = evaluateVariables;
	
	this.openDocument = openDocument;
	this.openListpage = openListpage;
	this.openHelp = openHelp;
	this.openGlossary = openGlossary;
	this.openFAQ = openFAQ;
	
	this.toggleAudio = toggleAudio;
	
	this.showFeedback = showFeedback;
	this.hideFeedback = hideFeedback;
	
	this.showHotspotContent = showHotspotContent;
	this.hideHotspotContent = hideHotspotContent;
	this.showHotspotWindow = showHotspotWindow;
	
	this.getCurrentStepIndex = getCurrentStepIndex;
	this.getCurrentStepPosition = getCurrentStepPosition;
	this.getCurrentNumberOfSteps = getCurrentNumberOfSteps;
	this.getCurrentStep = getCurrentStep;
	this.getProjectTitle = getProjectTitle;
	
	this.exit = exit;
	this.beforeExit = beforeExit;
	this.jumpToStep = jumpToStep;
	this.jumpToFirstStep = jumpToFirstStep;
	this.jumpToScore = jumpToScore;
	this.alertScoreTimeout = alertScoreTimeout;
	this.updatePage = updatePage;
	this.updateThread = updateThread;
	this.dropAction = dropAction;
	this.setUserVariable = setUserVariable;
	this.setPreviewMode = setPreviewMode;
	this.autoplay = autoplay;
	this.showNavigationTree = showNavigationTree;
	
	this.getNextId = getNextId;
	
	this.checkEnabled = checkEnabled;
	this.callAction = callAction;
	
	this.openURL = openURL;
	
	this.shutdown = shutdown;
	
	this.doAutoplay = doAutoplay;
	this.audioFinished = audioFinished;
	
	this.test = test;
	this.log = log;
	
	this.trigger = trigger;
	this.remove = remove;
	this.toggle = toggle;
	this.stop = stop;
	
	this.showTooltip = showTooltip;
	this.hideTooltip = hideTooltip;
	this.setTooltipVisible = setTooltipVisible;
	this.updateCurrentStep = updateCurrentStep;
	this.addTask = addTask;
	
	this.checkboxSelected = checkboxSelected;
	this.isSingleChoice = isSingleChoice;
	this.setAudio = setAudio;
	
	this.getListpageHTML = getListpageHTML;
	this.getListpageRenderer = getListpageRenderer;
	
	this.isInPreviewMode = isInPreviewMode;
	this.isInFeedbackMode = isInFeedbackMode;
	this.setFeedbackMode = setFeedbackMode;
	this.isInAudioMode = isInAudioMode;
	this.isInAutoplayMode = isInAutoplayMode;
	this.setIsInAutoplayMode = setIsInAutoplayMode;
	this.getCurrentAudio = getCurrentAudio;
	this.setCurrentResult = setCurrentResult;
	this.positionsRelative = false;
	
	this.fillContentWindow = fillContentWindow;
	
	this.registerComponent = registerComponent;
	
	this.currentMouseX = 0;
	this.currentMouseY = 0;
	
	this.stopTimer = stopTimer;
	
	this.reloadStep = reloadStep;
	this.updateContent = updateContent;
	this.currentContent = null;
	
	var previewMode = false;	
	var currentAudio = null;
	var inAudioMode = AUDIO_ACTIVE_AT_STARTUP;
	var inAutoplayMode = false; 
	
	this.inFeedbackMode = false;
	
	var currentStep = null;
	var currentStepIndex = 0;
	var currentResult = 0;
	var recentStepIndex = 0;
	var testunitBackwardDisabled = false;
	
	var currentTooltipSourceContent = null;
	var windowContent = null;
	
	var nextId = 0;
	var timeout = null;
	var hotspotWindowTimeout = null;
	
	var scoreTimeout = null;
	var scoreTimeoutAlert = null;
	var timeLimitAlert = 0;
	var currentTimelimitId = "";
	
	var listpageHTML = "";
	var listpageRenderer = new Array();
	var tasks = new Array();
	
	
	this.browserwindow = new Array();
	
	var userVariables = new Array();
	
	function registerComponent(name, mycomponent) {
		//alert("register " + name + " " + mycomponent);
		component[name] = mycomponent;
	}
	
	function exit() {
		doShutdown();
	}
	
	function beforeExit() {
		var exitStep = database.getStepByTitle("#EXIT");
		if (exitStep) {
			jumpToStep(exitStep.id);
		}
		else {
			exit();
		}
	}
	
	function setPreviewMode(preview) {
		previewMode = preview;
	}
	
	function addTask(taskString, time) {
		tasks[taskString] = window.setTimeout(taskString, time);
	}
	
	function getListpageHTML() {
		return listpageHTML;
	}
	
	function getListpageRenderer(type) {
		return listpageRenderer[type];
	}
	
	function isInPreviewMode() {
		return previewMode;
	}
	
	function test() {
		alert("test");
	}
	
	
	
	function audioFinished(audioId) {
		var audio = database.getContentById(audioId);
		eventcontroller.fireEvent(audio, "On_Stop");
	}
	
	function stopTimer(id) {
		var currentStep = engine.getCurrentStep();
		var content = currentStep.getContentById(id);
		if (content) {
			content.stop();
		}
	}
	
	function trigger(id) {
		var content = database.getContentById(id);
		if (!content) {
			content = layoutrenderer.getMainContent().getContentById(id);
		}
		if (content) {
			content.visible = true;
			if (content.popup) {
    			if (content.visible) {
    				this.browserwindow[content.id] = openContentWindow(content);
    			}
    		}
    		else {		
    			content.update();
    		}
			
		}
	}
	
	function toggle(id) {
		var content = database.getContentById(id);
		var mainContent = layoutrenderer.getMainContent();
		
		if (!content) {
			content = mainContent.getContentById(id);
		}
		if (content) {
			
			var myButton = null;
			var wasVisible = content.visible;
			content.visible = !content.visible;
			
			
			content.update();
			
					
			// get button that toggles the content and set it to the right state
			var allContents = mainContent.getAllContents();
			for (var i = 0; i < allContents.length; i++) {
				var events = allContents[i].events;
				if (events) {
					for (var ii = 0; ii < events.length; ii++) {
						if (events[ii].objectId == content.id) {
							if (allContents[i] instanceof ToggleButton) {
								myButton = allContents[i];
							}
							
						}
					}
				}
			}
			
			if (myButton) {
				// ------------------------------------- hack
				if (myButton.id == "audiobutton") {
				// -------------------------------- end hack, audiobutton works the opposite way
					if (!content.visible) {
						myButton.state = "active";
					}
					else {
						myButton.state = "default";
					}
				}
				else {
					if (!content.visible) {
						myButton.state = "default";
					}
					else {
						myButton.state = "active";
					}
				}
				myButton.update();
			}
			
		}
	}
	
	function remove(id) {
		var content = database.getContentById(id);
		if (!content) {
			content = layoutrenderer.getMainContent().getContentById(id);
		}
		if (content) {
			content.visible = false;
			content.update();
		}
	}
	
	function stop(id) {
		var content = database.getContentById(id);
		if (!content) {
			content = layoutrenderer.getMainContent().getContentById(id);
		}
		if (content && content instanceof Media) {
			content.pause();
		}
	}
	
	function log(string) {
		if (!debug) return;
		var logWindow = document.getElementById("logwindow");
		if (!logWindow) return;
		if (logWindow.style.visibility == "hidden") {
			logWindow.style.visibility = "visible";
		}
		var value = logWindow.innerHTML;
		value += "<br>" + string;
		logWindow.innerHTML = value;
		logWindow.scrollTop = logWindow.scrollHeight;
	}
		
		
	function updateCurrentStep() {
		var currentStep = getCurrentStep();
		currentStep.update();
	}
	
	function init() {
		var audiobutton = layoutrenderer.getMainContent().getContentById("audiobutton");
    	if (audiobutton) {
			audiobutton.state = "active";
			audiobutton.update();
		}
	}
		
	function isInFeedbackMode() {
		return this.inFeedbackMode;
	}
	
	function setFeedbackMode(enable) {
		this.inFeedbackMode = enable;
	}
	
	function isInAudioMode() {
		return inAudioMode;
	}
	
	function isInAutoplayMode() {
		return inAutoplayMode;
	}
	
	function setIsInAutoplayMode(autoplay) {
		inAutoplayMode = autoplay;
	}
	
	function getCurrentAudio() {
		return currentAudio;
	}
	
	function setCurrentResult(percent) {
		currentResult = percent;
	}
	
	function createAudioFinishedLink(audio) {
		var html = new StringBuffer();
		var onStopEventAvailable = false;
		var events = audio.events;
		for (var i = 0; i < events.length; i++) {
			if (events[i].type == "On_Stop") {
				if (events[i].varvalue != "autoplay") {
					onStopEventAvailable = true;
					break;
				}
				else {
					addTask("engine.autoplay()", 1);
				}
			}
		} 
		if (onStopEventAvailable) {
			html.append("<a class=\"nextAudioLink\" href=\"javascript:engine.audioFinished('");
			html.append(audio.id);
			html.append("')\"><img src=\"img/nextaudio.gif\" alt=\"");
			html.append(utils.getI18nValue("next"));
			html.append("\" border=\"0\"></a>");
		}
		return html.toString();
	}
	
	function setAudio(audio) {
		currentAudio = audio;
		var audiotext = layoutrenderer.getMainContent().getContentById("audiotext");
		if (audiotext) {
			audiotext.update();
		}
		
		var linkbar = layoutrenderer.getMainContent().getContentById("linkbar");
		if (linkbar) {
			linkbar.update();
		}
		
	    if (inAudioMode) {
			var audiobutton = layoutrenderer.getMainContent().getContentById("audiobutton");
	    	if (audiobutton) {
    			audiobutton.state = "active";
    			audiobutton.update();
    		}
		}
		
		
		
	}
	
	function getNextId() {
	    return nextId++;
	}
	
	function jumpToStep(id) {
			    
	    var steps = database.getSteps();
	    var found = false;
	    var foundStep = null;
	    
	    for (var i = 0; i < steps.length; i++) {
	        if (steps[i].id == id && !steps[i].hidden) {
	            foundStep = steps[i];
	            if (foundStep.singleStep) {
	            	currentStepIndex = -1;
	            }
	            else {
	            	currentStepIndex = i;
	            }
	            break;
	        }
	    }
	    if (!foundStep) {
	    	recentStepIndex = currentStepIndex;
	    	currentStep = database.getStepById(id);
	    	currentStepIndex = -1;
	    	if (!currentStep) {
	    		currentStep = database.getErrorStep("step " + id + " was not found in this project");
	    	} 
	    	
	    }
	    
	    window.setTimeout("engine.updatePage()", 1);
	}
	
	function jumpToFirstStep() {
		var firstStep = database.getStepByTitle("#START");
		if (firstStep) {
			jumpToStep(firstStep.id);
		}
		else {
			var steps = database.getSteps();
			if (steps.length > 0) {
				jumpToStep(steps[0].id);
			}
		}
	}
	
	function jumpToScore() {
		scoreTimeout = null;
		var currentStep = getCurrentStep();
		var currentStructure = database.getStructureById(currentStep.structureId);
		var parentStructure = database.getStructureById(currentStructure.parentId);
		if (parentStructure.type == "testunit") {
			var scoreStep = findStepByType(parentStructure, "Score");
			if (scoreStep != null) {
				currentTimelimitId = "";
				jumpToStep(scoreStep.id);
			}
		}
	}
	
	function alertScoreTimeout() {
		var message = new String(utils.getI18nValue("timeLimitAlert"));
		message = message.replace(/\$timeleft/, getVariable("$timeleft"));
		alert(message);
	}
	
	function findStepByType(structure, type) {
		var myStep = null;
		var steps = structure.getSteps();
		for (var i = 0; i < steps.length; i++) {
			if (steps[i].type == type) {
				myStep = steps[i];
				break;
			}
		}
		if (myStep == null) {
			var structures = getChildrenOfStructure(structure);
			for (var i = 0; i < structures.length; i++) {
				myStep = findStepByType(structures[i], type);
				if (myStep != null) break;
			}
		}
		
		return myStep;
	}
	
	function getChildrenOfStructure(structure) {
		var myStructures = new Array();
		var structures = database.getStructures();
		for (var i = 0; i < structures.length; i++) {
			if (structures[i].parentId == structure.id) {
				myStructures.push(structures[i]);
			}
		}
		return myStructures;
	}
	
	function hideNavigationTree() {
		
		/*
		var treeLayer = document.getElementById("tree");
		if (treeLayer) {
			treeLayer.innerHTML = "";
			tree = null;
		}
		*/
	}
	
	function showNavigationTree(rootId, draw) {
		//alert("show: " + rootId);
		/*
		var treeLayer = document.getElementById("tree");
		if (treeLayer) {
			if (!draw) {
				if (treeLayer.innerHTML != "") return;
			}
			else {
				treeLayer.innerHTML = "";
			}
			tree = new Tree("tree", rootId);
			tree.draw();
			
    	}
    	*/
    }
	
	function getCurrentStepIndex() {
	    return currentStepIndex;
	}
	
	function getCurrentStep() {
	    if (currentStepIndex == -1) return currentStep;
	    var steps = database.getSteps();
	    currentStep = steps[currentStepIndex];
	    if (!currentStep) {
	    	currentStep = database.getErrorStep("no visible step was defined in this project");
	    }
	    return currentStep;
	    
	}
	
	function getCurrentStepPosition() {
		if (currentStepIndex == -1) return 1;
	    var currentStepPosition = 0;
	    var currentStep = getCurrentStep();
	    var structure = database.getStructureById(currentStep.structureId);
	    if (structure) {
		    var parentStructure = database.getStructureById(structure.parentId);
		    if (parentStructure) {
		    	var myStructures = parentStructure.getVisibleStructures();
		    	for (var i = 0; i < myStructures.length; i++) {
			        if (myStructures[i].id == structure.id) {
			            currentStepPosition = i;
			            break;
			        }
			    }
		    }
		}
	    return currentStepPosition;
	}
	
	function getCurrentNumberOfSteps() {
		var numberOfSteps = 0;
		if (currentStepIndex == -1) return 1;
	    var currentStep = getCurrentStep();
	    var structure = database.getStructureById(currentStep.structureId);
	    if (structure) {
	    	var parentStructure = database.getStructureById(structure.parentId);
		    if (parentStructure) {
		    	var myStructures = parentStructure.getVisibleStructures("step");
		    	
		    	numberOfSteps = myStructures.length;
		    }
	    }
	    return numberOfSteps;
	}
	
	function getProjectTitle() {
	    return database.getProjectTitle();
	}
		
	function canGoBackward() {
		var currentStep = getCurrentStep();
		if (currentStep.singleStep) return false;
		if (testunitBackwardDisabled) return false;
		
		if (currentStepIndex == -1) {
			return true;
		}
		return currentStepIndex > 0;
	}
	
	function canGoForward() {
		if (currentStepIndex == -1) return false;
		var steps = database.getSteps();
		return currentStepIndex < steps.length - 1; 
	}
	
	function goBackward() {
		if (canGoBackward()) {
			if (currentStepIndex == -1) {
				currentStepIndex = recentStepIndex;
				updatePage();
			}
			else {
				currentStepIndex--;
				updatePage();
			}
		}
	}
	
	function goForward() {
	    if (canGoForward()) {
			currentStepIndex++;
			updatePage();
		}
	}
	
	function canGoDown() {
		return false;
	}
	
	function canGoUp() {
		return false;
	}
	
	function goDown() {
		if (canGoDown()) {
			alert("down");
			updatePage();
		}
	}
	
	function goUp() {
	    if (canGoUp()) {
			alert("up");
			updatePage();
		}
	}
	
	function isGlossaryEnabled() {
		return database.getListpageEntries("glossary").length > 0;
	}
	
	function isHelpEnabled() {
		return database.getListpageEntries("help").length > 0;
	}
	
	function isFaqEnabled() {
		return database.getListpageEntries("faq").length > 0;
	}
	
	function isCurrentStepAExercise() {
	    var isExercise = false;
	    var currentStep = getCurrentStep();
	    if (currentStep != null) {
	        isExercise = currentStep.isExercise();
	    }
	    return isExercise;
	}
	
	function isFeedbackEnabled() {
	    var enabled = false;
	    var currentStep = getCurrentStep();
	    if (currentStep != null) {
	        enabled = currentStep.isFeedbackEnabled();
	    }
	    return enabled;
	}
	
	function doAutoplay() {
		if (inAutoplayMode && inAudioMode) {
			var currentStep = getCurrentStep();
			if (currentStep.type == "Normal_Step" && canGoForward()) {
				goForward();
			}
		}
	}
	
	/* this function determines if a reload of the step is needed.
	
		true: Normal_Step, Exercises with Feedback
		false: Other Exercises
	*/
	function reloadNeeded(step) {
		if (RELOAD_PAGE_ON_REVISIT) return true;
		var reloadStep = false;
		if (step.type == "Normal_Step") {
			reloadStep = true;
		}
		else {
			if (step.type == "Single_Choice" || 
				step.type == "Multiple_Choice" || 
				step.type == "Drag_Drop" ||
				step.type == "Gap_Text") {
				
				var feedbacks = step.getContentsByClassname("Feedback");
				if (feedbacks.length > 0) {
					reloadStep = true;
				}
			}
		}
		
		return reloadStep;
	
	}
	
	
	function updatePage() {
		if (autoplayTask) {
			window.clearTimeout(autoplayTask);
		}
		
		
		layoutrenderer.resetNextButton();
		
		var currentStep = getCurrentStep();
		
		hideHotspotContent(null, currentStep.id);
		
		var reloadStep = reloadNeeded(currentStep);
		if (reloadStep) {
			currentStep.loaded = false;
		}
		
		for (i in tasks) {
			window.clearTimeout(tasks[i]);
		}
		tasks = new Array();
		
		if (mediaTracker) {
			mediaTracker.shutdown();
		}
		
		var soundlayer = document.getElementById("soundlayer");
		if (soundlayer) {
			soundlayer.innerHTML = "";
		} 
		
		this.inFeedbackMode = false;
						
		mediaTracker = new MediaTracker("mediaTracker");
		
		pagerenderer.setStepId(currentStep.id);
		pagerenderer.render();
		
		
		// handle testunit
		testunitBackwardDisabled = false;
		
		
		var currentStructureStep = database.getStructureById(currentStep.structureId);
	    var currentStructure = database.getStructureById(currentStructureStep.parentId);
	    
	    var showTree = true;
	    
	    if (currentStructure) {
			if (currentStructure.type == "testunit") {
				if (!currentStructure.backwardEnabled) {
					testunitBackwardDisabled = true;
				}
				if (currentStructure.hideNavigationTree) {
					showTree = false;
				}
				if (scoreTimeout == null && currentStructure.timeLimit > 0 && currentStep.type != "Normal_Step" && currentStep.type != "Score") {
					scoreTimeout = window.setTimeout("engine.jumpToScore()", currentStructure.timeLimit * 60000);
					//alert(currentStructure.timeLimit);
					//scoreTimeout = window.setTimeout("alert('ok')", currentStructure.timeLimit * 60000);
				
				}
				if (currentStructure.timeLimitAlert > 0 && currentTimelimitId != currentStructure.id && currentStep.type != "Normal_Step" && currentStep.type != "Score") {
					timeLimitAlert = currentStructure.timeLimitAlert;
					currentTimelimitId = currentStructure.id;
					scoreTimeoutAlert = window.setTimeout("engine.alertScoreTimeout()", currentStructure.timeLimitAlert * 60000);
				}
			}
						
			var treeContent = layoutrenderer.getMainContent().getContentById("tree");
			if (treeContent) {
				var treeShowing = treeContent.visible;
				if (!treeShowing && showTree) {
			    	treeContent.visible = true;
			    	
			    }
			   	if (treeShowing && !showTree) {
			       	treeContent.visible = false;
			    }
			}
		    		    
		}
		
		layoutrenderer.update();
		
		
		
		currentStep.visited = true;
		if (currentStep.type != "Score") {
			currentStep.hideFeedback();
		}
		
		var audiotextObject = layoutrenderer.getMainContent().getContentById("audiotext");
		if (audiotextObject) {
			if (!isAudioEnabled()) {
				audiotextObject.divRef.style.visibility = "hidden";
			}
			else {
				if (!inAudioMode) {
					if (currentAudio.audiotext != "") {
						audiotextObject.visible = true;
					}
					else {
						audiotextObject.visible = false;
					}
					audiotextObject.update();
				}
			}
		}
		
		
		
		// nextbutton-highlighting
		if (AUTOPLAY_ACTION != "none") {
			var stepContents = currentStep.getAllContents();
			var hasEvent = false;
			for (var i = 0; i < stepContents.length; i++) {
				if (stepContents[i].events && stepContents[i].events.length > 0) {
					hasEvent = true;
					break;
				}
			}
			layoutrenderer.resetNextButton();
			
			if (!hasEvent && currentStep.type == "Normal_Step") {
				autoplayTask = window.setTimeout("engine.autoplay()", AUTOPLAY_DELAY);
				//addTask("engine.autoplay()", AUTOPLAY_DELAY);
			}
		}
		
		
		if (debug) {
			engine.log("currentStep: " + currentStep.id);
		
		}
		
		eventcontroller.fireEvent(currentStep, "On_Variable");
		
	}
	
	function updateThread() {
		
		
	}
	
	function updateContent(id) {
		var content = database.getContentById(id);
		if (!content) {
			content = layoutrenderer.getMainContent().getContentById(id);
		}
		if (content) {
			content.update();
		}
	}
	
	function dropAction(contentId) {
	    var currentStep = getCurrentStep();
	    var contents = currentStep.getContents();
	    var content = currentStep.getContentById(contentId);
	    
	    var x1 = parseInt(content.left);
        var y1 = parseInt(content.top);
        var w1 = parseInt(content.width);
        var h1 = parseInt(content.height);
	    
	    var targetId = null;
	    var xDrop = 0;
	    var yDrop = 0;
	    
	    for (var i = 0; i < contents.length; i++) {
	        if (contents[i].id != contentId && !contents[i].isSource) {
    	        var x2 = parseInt(contents[i].left);
    	        var y2 = parseInt(contents[i].top);
    	        var w2 = parseInt(contents[i].width);
    	        var h2 = parseInt(contents[i].height);
    	        
    	        if (contents[i] instanceof Answer && (x1 + w1 > x2 && x1 < x2 + w2) && (y1 + h1 > y2 && y1 < y2 + h2)) {
    	            targetId = contents[i].id;
    	            xDrop = parseInt(contents[i].left) - 10;
    	            yDrop = parseInt(contents[i].top) - 10;
    	            break;
    	        }
	        }
	    }
	    if (targetId) {
            var boundObject = currentStep.getContentById(targetId);
            
            var m = 1;
            for (var i = 0; i < contents.length; i++) {
            	if (contents[i].boundObject == boundObject) {
            		m++;
            	}
            }
            content.moveTo(xDrop + (DROP_POSITION_OFFSET * m), yDrop - (DROP_POSITION_OFFSET * m));
            content.boundObject = boundObject;
        }
        else {
            content.boundObject = null;
        }
	}
	
	function openDocument(href) {
		var newWindow = window.open(href, '','');
	}
	
	function openHelp(href) {
		openListpage("help", href, true, utils.getI18nValue("help"));
	}
	function openGlossary(href) {
		openListpage("glossary", href, true, utils.getI18nValue("glossary"));
	}
	function openFAQ(href) {
		openListpage("faq", href, true, utils.getI18nValue("faq"));
	}
	function openListpage(type, href, sorted, title) {
		
		listpageRenderer[type] = new ListpageRenderer();
		listpageRenderer[type].render(type);
		var mainContent = listpageRenderer[type].getMainContent();
						
		var options = 'toolbar=no,width=' + mainContent.width + ',height=' + mainContent.height + ',directories=no,status=no,scrollbars=no,resize=yes,menubar=no,top=20,left=80';
		var url = "listpage.html?type=" + type + "&sorted=";
		sorted ? url += "true" : url += "false";
		if (href) {
			url += "&id=" + href;
		}
		if (title) {
			url += "&title=" + title;
		}
		var listpage = window.open(url, title, options);
		if (listpage) {
			listpage.focus();
		}
		
	}
	
	function openURL(href, fullscreen, width, height, relative) {
	
	    var a = href.indexOf("http://");
		var b = href.indexOf("file://");
		
		if (a == -1 && b == -1) {
	        if (!relative) {
	        	href = "http://" + href;
	        }
		}
		else {
			if (relative) {
				href = href.substring(7);
			}
		}
	    	    
		/*
		//change by ingo: special for man -captivates!!!!!!!!!!
		
		 if (href.indexOf("http://") == 0) {
	        href = href.substring(7);
			
	    }
	    */
	    var fullscreenScript = "fullscreen=0";
	    if (fullscreen) fullscreenScript = "fullscreen=1";
		
		if (width && height) {
			window.open(href,'fullscreen', 'width='+width+',height='+height+',' + fullscreenScript + ', scrollbars=0,left='+(0)+',top='+(0));
		}
		else {
			window.open(href);
		}   
	}
	
	function showFeedback(content) {
	    exercisemanager.showFeedback(content);
	}
	
	function hideFeedback() {
		exercisemanager.hideFeedback();
	}
	
	function showHotspotContent(event, stepId, closable) {
		var x = 0;
		var y = 0;
		if (event) {
			x = event.clientX;
			y = event.clientY;
		}
		else {
			x = this.currentMouseX;
			y = this.currentMouseY;
		}
		
		var step = database.getStepById(stepId);
		var contents = step.getContents();
		
		var html = new StringBuffer();
		
		if (closable) {
			html.append(createWindowTitlebar(stepId));
		}
		
		var innerTop = 0;
		closable ? innerTop = WINDOW_TITLEBAR_DEFAULT_HEIGHT : 0;
		
		html.append("<span style=\"position:absolute;top:" + innerTop + "px;left:0px;");
		html.append(HOTSPOTS_POPUP_DEFAULT_PADDING / 2);
		html.append(";width:");
		html.append(step.getMaxWidth());
		html.append(";height:");
		html.append(step.getMaxHeight());
		html.append(";\">");
		
		for (var i = 0; i < contents.length; i++) {
			var myHTML = contents[i].draw();
			html.append(myHTML);
		}
		html.append("</span>");
		
		var rightBorder = parseInt(contentlayer.style.width) + parseInt(contentlayer.style.left);
		var bottomBorder = parseInt(contentlayer.style.height) + parseInt(contentlayer.style.top);
		
		var hotspotWindowLeft = parseInt(x + HOTSPOTS_POPUP_DEFAULT_PADDING);	
		var hotspotWindowTop = parseInt(y + HOTSPOTS_POPUP_DEFAULT_PADDING);	
		var hotspotWindowWidth = step.getMaxWidth();
		var hotspotWindowHeight = step.getMaxHeight();
		
		if (hotspotWindowLeft + hotspotWindowWidth > rightBorder) {
			hotspotWindowLeft -= hotspotWindowWidth;
		}
		if (hotspotWindowTop + hotspotWindowHeight > bottomBorder) {
			hotspotWindowTop -= hotspotWindowHeight;
			if (hotspotWindowTop + hotspotWindowHeight > y - HOTSPOTS_POPUP_DEFAULT_PADDING) {
				hotspotWindowTop = y - HOTSPOTS_POPUP_DEFAULT_PADDING - hotspotWindowHeight;	
			}
		}	
		
		
		var hotspotWindow = document.getElementById("hotspotWindow");
		
		if (!document.all) {
			//hotspotWindowWidth += HOTSPOTS_POPUP_DEFAULT_PADDING / 2;
			//hotspotWindowHeight += HOTSPOTS_POPUP_DEFAULT_PADDING / 2;
		}
		
		if (hotspotWindowTop < 10) hotspotWindowTop = 10;
		if (hotspotWindowLeft < 10) hotspotWindowLeft = 10;
		
		hotspotWindow.style.top = hotspotWindowTop;
		hotspotWindow.style.left = hotspotWindowLeft;
		hotspotWindow.style.width = hotspotWindowWidth + 2;
		//alert(closable);
		hotspotWindow.style.height = closable ? hotspotWindowHeight + WINDOW_TITLEBAR_DEFAULT_HEIGHT : hotspotWindowHeight;
		
		hotspotWindow.innerHTML = html.toString();
		
		hotspotWindowTimeout = window.setTimeout("engine.showHotspotWindow('" + stepId + "')", HOTSPOTS_POPUP_DELAY);
	}
	
	function createWindowTitlebar(stepId) {
		var html = "<span class=\"defaultWindowTitlebar\"><a href=\"javascript:engine.hideHotspotContent(event,'" + stepId + "')\"><img src=\"img/close.gif\" border=\"0\"></a></span>";
		return html;
	}
	
	
	function showHotspotWindow(stepId) {
		window.clearTimeout(hotspotWindowTimeout);
		var hotspotWindow = document.getElementById("hotspotWindow");
		var step = database.getStepById(stepId);
		var contents = step.getContents();
		for (var i = 0; i < contents.length; i++) {
			contents[i].update();
		}
		hotspotWindow.style.display = "block";
		
	}
	
	function hideHotspotContent(event, stepId) {	
		window.clearTimeout(hotspotWindowTimeout);
		var hotspotWindow = document.getElementById("hotspotWindow");
		hotspotWindow.style.display = "none";
		var step = database.getStepById(stepId);
		var contents = step.getContents();
		for (var i = 0; i < contents.length; i++) {
			if (contents[i] instanceof Media) {
				var player = contents[i].player;
				if (player) {
					player.stop();
				}
				contents[i].innerHTML = "";
			}
		}
		
	}
	    
    function checkEnabled(content) {
        if (!content) return false;
        var enabled = true;
        if (content.id == "backbutton") {
            enabled = canGoBackward();
        }
        else if (content.id == "nextbutton") {
            enabled = canGoForward();
        }
        else if (content.id == "feedbackbutton") {
            enabled = isFeedbackEnabled();
        }
        else if (content.id == "faqbutton") {
            enabled = isFaqEnabled();
        }
        else if (content.id == "glossarybutton") {
            enabled = isGlossaryEnabled();
        }
        else if (content.id == "helpbutton") {
            enabled = isHelpEnabled();
        }
        else if (content.id == "audiobutton") {
            enabled = isAudioEnabled();
        }
        else if (content.id == "autoplaybutton") {
            enabled = isAutoplayEnabled();
        }
        else if (content.id == "downbutton") {
            enabled = canGoDown();
        }
        else if (content.id == "upbutton") {
            enabled = canGoUp();
        }
       
        else if (content.type == "media" || content.type == "audio") {
        	enabled = inAudioMode;
        }
        
        
        return enabled;
    }
    
    function callAction(content) {
    	if (!content) content = this.currentContent;
    	if (!content) return;
		
    	if (content.id == "nextbutton") {
            goForward();
        }
        else if (content.id == "backbutton") {
            goBackward();
        }
        else if (content.id == "upbutton") {
            goUp();
        }
        else if (content.id == "downbutton") {
            goDown();
        }
        else if (content.id == "faqbutton") {
            openFAQ();
        }
        else if (content.id == "glossarybutton") {
            openGlossary();
        }
        else if (content.id == "helpbutton") {
            openHelp();
        }
        else if (content.id == "audiobutton") {
            toggleAudio(content);
        }
        else if (content.id == "autoplaybutton") {
            toggleAutoplay(content);
        }
        else if (content.id == "bookmarkbutton") {
            toggleBookmarks(content);
        }
        
        else if (content.id == "repeatbutton") {
            this.inFeedbackMode = false;
            var myStep = getCurrentStep();
            myStep.loaded = false;
            updatePage();
        }
        
    }
    
    function reloadStep() {
		this.inFeedbackMode = false;
	    var myStep = getCurrentStep();
	    myStep.loaded = false;
	    
	    updatePage();
	}
    
    function toggleAutoplay(autoplaybutton) {
    	inAutoplayMode = !inAutoplayMode;
    }
    
    function toggleAudio() {
    	if (inAudioMode) {
    		mediaTracker.muteAll();
    		inAudioMode = false;
    	}
    	else {
    		mediaTracker.unMuteAll();
    		inAudioMode = true;
    		reloadStep();
    	}
    	
    	
    	
    }
    
    function toggleBookmarks(bookmarkbutton) {
    	var parent = bookmarkbutton.getParent();
    	var bookmarks = parent.getContentById("bookmarks");
    	
    	if (bookmarkbutton.state == "default" || bookmarkbutton.state == "rollover") {
    		bookmarkbutton.state = "active";
    		bookmarks.visible = true;
    	}
    	else {
    		bookmarkbutton.state = "default";
    		bookmarks.visible = false;
    	}
    	    	
    	bookmarkbutton.update();
    	if (bookmarks.popup) {
    		if (bookmarks.visible) {
    			this.browserwindow["bookmarks"] = openContentWindow(bookmarks);
    		}
    		else {
    			if (this.browserwindow["bookmarks"]) {
    				this.browserwindow["bookmarks"].close();
    			}
    		}
    	}
    	else {		
    		bookmarks.update();
    	}
    	
    }
    
    function openContentWindow(content) {
    	var options = 'toolbar=no,width=' + content.width + ',height=' + content.height + ',directories=no,status=no,scrollbars=no,resizable=no,menubar=no,top=' + content.top + ',left=' + content.left;
		var myWindow = window.open("contentwindow.html", content.id, options);
		windowContent = content;
		return myWindow;
	}
	
	function fillContentWindow() {
		var myWindowDocument = this.browserwindow[windowContent.id].document;
		var html = windowContent.draw();
		myWindowDocument.title = windowContent.title; 
		myWindowDocument.getElementsByTagName("body")[0].innerHTML = html;
		windowContent.update(this.browserwindow[windowContent.id].document);
		windowContent.divRef.style.top = 0;
		windowContent.divRef.style.left = 0;
		this.browserwindow[windowContent.id].focus();
	}
        
    
    function isAutoplayEnabled() {
    	var currentStep = getCurrentStep();
    	return currentStep.type == "Normal_Step";
    }
    
    function isAudioEnabled() {
    	var enabled = (currentAudio != null);
    	return enabled;
    }
    
    function isSingleChoice() {
    	var currentStep = getCurrentStep();
    	return currentStep.type == "Single_Choice";
    }
    
    function checkboxSelected(checkbox) {
    	var currentStep = getCurrentStep();
    	if (isSingleChoice()) {
	    	var contents = currentStep.getContentsByClassname("Checkbox");
	    	for (var i = 0; i < contents.length; i++) {
	    		if (contents[i].id != checkbox.id) {
	    			contents[i].state = "default";
	    			contents[i].update();
	    			if (contents[i].getParent() != null) {
	    				contents[i].getParent().state = "default";
	    			}
	    			
	    		}	
	    	}
    	}
    }
    
    function showTooltip(event, content) {
        if (document.all) return; // IE shows tooltip automatically
        if (!event) return;
        var value = content.tooltip;
        if (!value) return;
        var x = parseInt(event.pageX);
	    var y = parseInt(event.pageY);
	    var tooltip = document.getElementById("tooltip");
	    
	    tooltip.innerHTML = value;
	    tooltip.style.left = -50;
	    tooltip.style.top = -50;
	    tooltip.style.display = "block";
	    
	    var tWidth = parseInt(tooltip.offsetWidth);
	    var tHeight = parseInt(tooltip.offsetHeight);
	    tooltip.style.display = "none";
	   	
	   	var maxWidth = parseInt(mainlayer.style.width);
	    var maxHeight = parseInt(mainlayer.style.height);
	    
	    var tx = x + TOOLTIP_POSITION_OFFSET_X;
	    var ty = y + TOOLTIP_POSITION_OFFSET_Y;
	   	
	   	if (tx + tWidth >= maxWidth) {
	    	tx -= (tWidth + TOOLTIP_POSITION_OFFSET_X);
	    }
	    if (ty + tHeight >= maxHeight) {
	    	ty -= (tHeight + TOOLTIP_POSITION_OFFSET_Y);
	    }	
	    	    
	    tooltip.style.left = tx;
		tooltip.style.top = ty;
		timeout = window.setTimeout('engine.setTooltipVisible()', TOOLTIP_DELAY);
    }
    
    function setTooltipVisible() {
        var tooltip = document.getElementById("tooltip");
        tooltip.style.display = "block";
        timeout = window.setTimeout('engine.hideTooltip()', TOOLTIP_DURATION);
    }
    
    function hideTooltip() {
    	window.clearTimeout(timeout);
        var tooltip = document.getElementById("tooltip");
        tooltip.style.display = "none";
    }
    
    function evaluateVariables(text, content) {
    	if (text.indexOf("$") == -1) return text;
    	if (text.indexOf("$ ") > -1) text = text.replace(/\$./, "&#x0024; ");
    	if (text.indexOf("$.") > -1) text = text.replace(/\$./, "&#x0024;.");
    	if (text.indexOf("$,") > -1) text = text.replace(/\$,/, "&#x0024;,");
    	if (text.indexOf("$;") > -1) text = text.replace(/\$;/, "&#x0024;;");
    	
    	
    	while (true) {
    		var a = text.indexOf("$");
			if (a == -1) break;
			var b = -1;
			for (var i = a + 1; i < text.length; i++) {
				var myChar = text.charAt(i);
				if (myChar < ',' || myChar > 'z' || myChar == " " || myChar == "<" || myChar == ">") {
					b = i;
					break;
				}
				
			}
			if (b == -1) b = text.length;
			var myVariable = text.substring(a, b);
			var newValue = getVariable(myVariable, content);
			text = text.substring(0, a) + newValue + text.substring(b);
		}
    	return text;
    }
    
    
    
    function getVariable(variable, content, supressDefaultValue) {
        var value = "";
        if (!supressDefaultValue) {
        	value = "[" + variable.substring(1) + " is not defined]";
        }
        
        var styleClass = variable.substring(1);
        
        if (variable.indexOf(":input") >= 0) {
        	value = createInputFieldForVariable(variable, content);
        }
        else if (variable == "$stepposition") {
            value = getFormattedStepposition();
        }
        else if (variable == "$title") {
            value = database.getProjectTitle();
        }
        else if (variable == "$lessonstatus") {
        	value = getLessonStatus();
        }
        else if (variable == "$glossarylink") {
            var caption = utils.getI18nValue("glossary");
            if (isGlossaryEnabled()) {
            	value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.openGlossary()\" HIDEFOCUS>" + caption + "</a>";
            }
            else {
                styleClass += "_disabled";
                value = "<span class=\"" + styleClass + "\">" + caption + "</span>";
            }
        }
        else if (variable == "$faqlink") {
            var caption = utils.getI18nValue("faq");
            if (isFaqEnabled()) {
                value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.openFAQ()\" HIDEFOCUS>" + caption + "</a>";
            }
            else {
                styleClass += "_disabled";
                value = "<span class=\"" + styleClass + "\">" + caption + "</span>";
            }
        }
        else if (variable == "$helplink") {
            var caption = utils.getI18nValue("help");
            if (isHelpEnabled()) {
                value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.openHelp()\" HIDEFOCUS>" + caption + "</a>";
            }
            else {
                styleClass += "_disabled";
                value = "<span class=\"" + styleClass + "\">" + caption + "</span>";
            }
        }
        else if (variable == "$bookmarkslink") {
            var caption = utils.getI18nValue("bookmarks");
            value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.trigger('bookmarks')\" HIDEFOCUS>" + caption + "</a>";
        }
        else if (variable == "$exitlink") {
            var caption = utils.getI18nValue("exit");
            value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.beforeExit()\" HIDEFOCUS>" + caption + "</a>";
        }
        else if (variable == "$linkseparator") {
            var caption = LINKBAR_LINK_SEPARATOR;
            value = "<span class=\"linkseparator\">" + caption + "</span>";
        }
        else if (variable == "$audiolink") {
        	var caption = utils.getI18nValue("audio");
        	if (isAudioEnabled()) {
	        	if (isInAudioMode()) {
	        		caption = utils.getI18nValue("audio_off");
	        	}
	        	else {
	        		caption = utils.getI18nValue("audio_on");
	        	}
	        	value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.toggle('audiotext');engine.toggleAudio();engine.updateContent('" + content.id + "')\" HIDEFOCUS>" + caption + "</a>";
        	}
        	else {
        		styleClass += "_disabled";
            	value = "<span class=\"" + styleClass + "\">" + caption + "</span>";
            }
        }
        else if (variable == "$homelink") {
        	var caption = utils.getI18nValue("home");
        	value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.jumpToFirstStep()\" HIDEFOCUS>" + caption + "</a>";
        }
        else if (variable == "$audiotext") {
        	value = getCurrentAudiotext();
        }
        else if (variable == "$bookmarks") {
        	value = bookmarkmanager.drawBookmarkList(content);
        }
        else if (variable == "$result") {
        	value = currentResult;
        }
        else if (variable == "$attempt") {
        	var currentStep = getCurrentStep();
        	value = currentStep.attempt + 1;
        }
        else if (variable == "$tries") {
        	var currentStep = getCurrentStep();
        	value = currentStep.tries;
        }
        else if (variable == "$date") {
        	value = createDate();
        }
        else if (variable == "$timeleft") {
        	value = timeLimitAlert;
        }
        else if (variable == "$location") {
        	value = createLocation();
        }
        else if (variable == "$searchfield") {
        	value = createSearchfield();
        }
        else if (variable == "$plugincheck_flash") {
        	value = createPluginCheck("ShockwaveFlash");
        }
        else if (variable == "$plugincheck_quicktime") {
        	value = createPluginCheck("Apple Quicktime");
        }
        else if (variable == "$plugincheck_realmedia") {
        	value = createPluginCheck("RealPlayer");
        }
        else if (variable == "$plugincheck_activex") {
        	value = createPluginCheck("ActiveX");
        }
        else if (variable.indexOf("$steplink") == 0) {
        	var caption = utils.getI18nValue(variable.substring(1));
        	var stepTitle = variable.substring(variable.indexOf("-") + 1);
        	//alert(stepTitle);
        	
        	var myStep = database.getStepByTitle("#" + stepTitle);
        	if (myStep) {
        		value = "<a class=\"" + styleClass + "\" href=\"javascript:engine.jumpToStep('" + myStep.id + "')\" HIDEFOCUS>" + caption + "</a>";
        	}
        	else {
        		styleClass += "_disabled";
        		value = "<span class=\"" + styleClass + "\">" + caption + "</span>";
        	}
        	
        }
        else {
        	value = getUserVariable(variable);
        } 
        return value;    
    }
    
    /*
    function createMenubar() {
    	var html = "";
    	//var menubar = new MenuBar();
    	//var html = menubar.draw();
    	
    	
    	var structures = database.getStructuresOfLevel(1);
    	var html = "<table class=\"menuBar\"><tr>";
    	var stepToSelect = null;
    	
    	var stepId = "";
    	for (var i = 0; i < structures.length; i++) {
    		stepToSelect = database.findFirstStepInStructure(structures[i]);
    		if (stepToSelect) stepId = stepToSelect.id;
    		var link = "<a class=\"menuItem" + i + "\" href=\"javascript:engine.showNavigationTree('" + structures[i].id + "', true);engine.jumpToStep('" + stepId + "')\">"; 
    		html += "<td class=\"menuItem" + i + "\">" + link + structures[i].title + "</a></td>";
    	}
    	html += "</tr></table>";
    	
    	return html;
    
    
    }
    */
    
    function createDate() {
    	var now = new Date();
		var value = now.toLocaleString();
		value = value.substring(0, value.lastIndexOf(" ")); // only date, without time
		return value;
    }
    
    function createInputFieldForVariable(variable, content) {
    	var html = new StringBuffer();
    	var a = variable.indexOf(":");
    	if (a == -1) return variable;
    	var varname = variable.substring(1, a);
    	var cmd = variable.substring(a + 1);
    	
    	var cmds = cmd.split(",");
    	
    	var cols = 10;
    	var rows = 1;
    	
    	if (cmds.length > 0) {
    		cols = parseInt(cmds[1]);
    		if (cmds.length > 1) {
    			rows = parseInt(cmds[2]);
    		}
    	}
    	
    	if (cmds[0] == "input") {
    		if (rows == 1) {
    			html.append("<input type=\"text\" class=\"defaultInputField\" onkeyup=\"engine.setUserVariable('");
    			html.append(varname);
    			html.append("','input:");
    			html.append(varname);
    			html.append("')\" name=\"input:");
    			html.append(varname);
    			html.append("\" size=\"");
    			html.append(cols);
    			html.append("\" value=\"");
    			html.append(getVariable("$" + varname, content, true));
    			html.append("\">");
    		}
    		if (rows > 1) {
    			html.append("<textarea class=\"defaultInputField\" name=\"variable_");
    			html.append(varname);
    			html.append("\" rows=\"");
    			html.append(rows);
    			html.append("\" cols=\"");
    			html.append(cols);
    			html.append("\">");
    			html.append(getVariable("$" + varname, content, true));
    			html.append("</textarea>");
    		}
    	}	
    	return html.toString();
    	
    }
    
    function setUserVariable(name, value) {
    	if (name.indexOf("$") == 0) name = name.substring(1);
    	
    	if (name == "command") {
    		if (value == "exit") {
    			exit();
    			return;
    		}
    		if (value == "print_certificate") {
    			printCertificate();
    		}
    		if (value == "autoplay") {
    			autoplay();
    		}
    	}
    	
    	if (name == "lessonstatus") {
    		if (value == "") {
    			var allSteps = database.getSteps();
				var max = allSteps.length;
				var visited = 0;
				for (var i = 0; i < max; i++) {
					allSteps[i].visited = false;
				}
    		}
    	}
    	
    	if (value.indexOf("input:") == 0) {
    		value = document.getElementById("input:" + name).value;
    	}
    	userVariables["$" + name] = value;
    	
    	
    }
    
    function getUserVariable(name) {
    	var value = "";
    	if (userVariables[name]) {
    		value = userVariables[name];
    	}
    	return value;
    }
    
    function autoplay() {
    	if (AUTOPLAY_ACTION == "highlight") {
    		layoutrenderer.highlightNextButton();
    	}
    	else if (AUTOPLAY_ACTION == "forward") {
    		if (canGoForward()) {
    			goForward();
    		}
    	}
    }
    
    function printCertificate() {
    	var firstname = escape(getVariable("$firstname"));
    	var lastname = escape(getVariable("$lastname"));
    	var pnumber = escape(getVariable("$pnumber"));
    	var department = escape(getVariable("$department"));
    	
    	var url = "certificate.html?";
    	url += "firstname=" + firstname;
    	url += "&lastname=" + lastname;
    	url += "&pnumber=" + pnumber;
    	url += "&department=" + department;
    	
    	var options = 'toolbar=yes,width=' + (screen.availWidth - 6) + ',height=' + screen.availHeight + ',directories=yes,status=no,scrollbars=yes,resizable=yes,menubar=yes,top=0,left=0';

    	var myWindow = window.open(url, "certificate", options);
    }
    
    function createSearchfield() {
    	var html = "<input id=\"searchfield\" class=\"inputlistpage\" style=\"width:100%\" type=\"text\" autocomplete=\"off\">";
    	return html;
    }
    
    function createPluginCheck(pluginName) {
    	return detectPlugins(pluginName)
    }
    
    function createLocation() {
    	var currentStep = getCurrentStep();
    	var html = new StringBuffer();
    	html.append("<span class=\"location\">");
    	if (!currentStep.singleStep) {
	    	var ancestors = currentStep.getAncestors();
	    	for (var i = 0; i < ancestors.length; i++) {
	    		var type = ancestors[i].type;
	    		var title = ancestors[i].title;
	    		if (ancestors[i] instanceof CTStep) type = "step";
	    		    		
	    		html.append("<span class=\"location_");
	    		html.append(type);
	    		html.append("\">");
	    		html.append(title);
	    		html.append("</span>");
	    		if (i < ancestors.length - 1) {
	    			html.append("<span class=\"location_path_separator\">");
	    			html.append(LOCATION_PATH_SEPARATOR);
	    			html.append("</span>");
	    		}
	    		
	    	}
	    }
	    html.append("</span>");
    	return html.toString();
    }
    
    function getCurrentAudiotext() {
    	if (currentAudio) {
    		return "<table class=\"audiotextvalue\"><tr height=\"100%\"><td class=\"audiotextvalue\">" + currentAudio.audiotext + "</td></tr><tr><td class=\"nextAudioLink\">" + createAudioFinishedLink(currentAudio) + "</td></tr></table>";
    	}
    	else return "";
    }
    
    function getFormattedStepposition() {
        var html = "<span class=\"stepposition\">";
        var currentStepPosition = getCurrentStepPosition() + 1;
        var currentNumberOfSteps = getCurrentNumberOfSteps();
        if (currentStepPosition > currentNumberOfSteps) {
        	currentStepPosition = currentNumberOfSteps;
        }
        html += currentStepPosition + " / " + currentNumberOfSteps;
        html += "</span>";
        return html;
    }
    
    function getLessonStatus() {
    	var allSteps = database.getSteps();
		var max = allSteps.length;
		var visited = 0;
		for (var i = 0; i < max; i++) {
			if (allSteps[i].visited) visited++;
		}
		var percent = Math.round((visited / max) * 100);
		return percent;
    }
    
    function shutdown() {
    	if (standardapi) {
    		var percent = getLessonStatus();
    		var statusLabel = "incomplete";
			if (percent == 100) {
				statusLabel = "completed";
			}
			standardapi.setLMSValue("cmi.core.lesson_status", statusLabel);
		}
    }    	
	
}