function apsController () {

	//global States are
	//
	//ctrlConfigName
	//ctrlPracticumState
	//ctrlPracticumSessionState
	//ctrlMLPState
	
	this.configArray = new Array();
	this.configs = new Object();

	var me = this;

	return (this);
}


apsController.prototype.getSectionName = function() {
	//var url = document.location.href
	//url=url.replace(/\\/gi,"/");
	//var filename = url.substr (url.lastIndexOf("/")+1)
	//sectionName = filename.substring(0,filename.indexOf("_",1));
  //commented out above in favor of pulling data from hidden form field
	var sectionName = $("#course_section_name").attr("value"); //hidden html form field on each page
	return(sectionName);
}

apsController.prototype.checkForScore = function() {


	if ($("#practicum_section_score").length>0) {
	
			
		var score = $("#practicum_section_score").attr("value"); 

		var sectionName = this.getSectionName();
		
		saveCourseData (sectionName+"_score",score);
		markComplete(sectionName);
	}
}



apsController.prototype.updateState = function() {
	var ctrlPracticumState = getCourseData("ctrlPracticumState");
	var ctrlPracticumSessionState = getCourseData("ctrlPracticumSessionState");
	var ctrlMLPState = getCourseData("ctrlMLPState");

	var sectionName = this.getSectionName();
	//bookmarkSectionLocation (sectionName);

	var config;
	
	if (config= this.getActiveConfig()) {
    this.checkForScore();

		if(typeof(practicum_decisions)!="undefined") {
			for (var key in practicum_decisions) {
				learningPath.defineQuestion (key);
			}
		}

    if (isComplete(config.practicumSections[config.practicumSections.length-1]) && (ctrlPracticumState=="passed" || ctrlPracticumState=="failed") ) {
      saveCourseData("ctrlPracticumSessionState",ctrlPracticumState);
    }

    if (isComplete(config.practicumSections[config.practicumSections.length-1]) && ( !getCourseData("ctrlMLPState"))){
			learningPath.generateLearningPath();
			saveCourseData("ctrlMLPState","generated");
    }
	
    if (ctrlMLPState=="generated" && learningPath.learningPathComplete()) {
      saveCourseData("ctrlMLPState","completed");
      ctrlMLPState = "completed";
    }

	
    if (isComplete(config.practicumSections[config.practicumSections.length-1]) && (ctrlPracticumSessionState=="in-progress") &&( !(ctrlPracticumState=="passed" || ctrlPracticumState=="failed") )){

      learningPath.generateScore();

			var pscore = parseInt(getCourseData("pscore"));
			var nscore = parseFloat(getCourseData("nscore"));

			var attempt = getCourseData("ctrlAttempt");

			if (!attempt || attempt=="") {
				attempt = 0;
			}
			else {
				attempt = parseInt(attempt);
			}
			attempt++;

			saveCourseData ("ctrlAttempt", attempt);

			if ( config.passThreshold && (pscore >= config.passThreshold) ) {
				saveCourseData("ctrlPracticumSessionState","passed");
				saveCourseData("ctrlPracticumState","passed");
			}
      else if ( config.passPercentThreshold && (nscore >= config.passPercentThreshold) ) {
				saveCourseData("ctrlPracticumSessionState","passed");
				saveCourseData("ctrlPracticumState","passed");
			}
			else {
        if ( attempt < config.allowedAttempts ) {
					saveCourseData("ctrlPracticumSessionState","not-passed");
					saveCourseData("ctrlPracticumState","not-passed");
				}
				else {
					saveCourseData("ctrlPracticumSessionState","failed");
					saveCourseData("ctrlPracticumState","failed");
				}
			}

			apsNav.markLearningObjectByName("practicumcompletion");

			saveState();
		}

		if (getCourseData("ctrlMLPState")=="completed") {
			apsNav.markLearningObjectByName("learningpathcompletion");
		}

		if ( $("#course_section_name").attr("value") == courseController.evaluationCompletionID ) {
      //'Evaluation & Completion' interstitial page marks completion of LO (harvey ball full)
			if ( $("body").attr("id") == courseController.evaluationCompletionInterstitialID ) {
				apsNav.markLearningObjectByName("evaluationcompletion"); //see '_apsLearningObjects.js'
        //alert("Interstitial page");
			}
      //'Evaluation & Completion' data is not sent to LMS, so skip interstitial amd mark LO as completed
      else if ( courseController.isEvaluationDataStored == false ) {
				apsNav.markLearningObjectByName("evaluationcompletion"); //see '_apsLearningObjects.js'
        //alert("Evaluation & Completion page");
			}
		}
	}
}



apsController.prototype.updateDisplay = function() {
  var ctrlPracticumState = getCourseData("ctrlPracticumState");

	this.updateState();
		
	var mlpId = this.mlpSectionId;
	
	var objectId = "#"+mlpId;
	var config = this.getActiveConfig();
	
	var ctrlMLPState = getCourseData("ctrlMLPState");

		
	if (ctrlMLPState=="generated" || ctrlMLPState=="completed") {
		$(objectId).removeClass("disabled");
		$(objectId).attr("href", config.mplSectionPage);

	}
	
	else {
	
		$(objectId).addClass("disabled");
		$(objectId).attr("href", "#");
	
	
	}
	


	if (ctrlMLPState=="completed") {
		apsNav.markSectionByName(mlpId);
		$(objectId).removeClass("disabled");
		
		$(objectId).addClass(apsNav.sections[mlpId].anchorCSS())
		$(objectId).attr("href", config.mplSectionPage);

	}

	
	
	evaluationId = "#"+this.evaluationSectionId;
	
	$(evaluationId).attr("href", "#");	
	
	//$(evaluationId).click(function () {alert ("Not allowed"); return false});

		
	
	if (ctrlMLPState=="completed" && config.mlpAllowsCompletion) {
		$(evaluationId).removeClass("disabled");
		$(evaluationId).attr("href", config.evaluationSectionPage);
		$(evaluationId).click( function () { return (true);});
		

	}
	else if (ctrlPracticumState=="passed" && config.passAllowsCompletion) {
			$(evaluationId).removeClass("disabled");
			$(evaluationId).attr("href", config.evaluationSectionPage);
			$(evaluationId).click( function () { return (true);});
			
	
	} 
	
	else {
		$(evaluationId).addClass("disabled");
		$(evaluationId).attr("href", "#");
		$(evaluationId).click( function () { return (false);});
	
	
	}

	

}


apsController.prototype.showGetConfig = function() {
	$("#"+this.divGetConfig).show();

}

apsController.prototype.hideGetConfig = function() {
	$("#"+this.divGetConfig).hide();

}

apsController.prototype.showStart = function() {
	var config = this.getActiveConfig(); 
	$("#"+config.divStart).show();

}

apsController.prototype.showContinue = function() {

	var config = this.getActiveConfig();
	
	$("#"+config.divContinue).show();

}

apsController.prototype.showPassed = function() {

	var config = this.getActiveConfig();
	
	$("#"+config.divPassed).show();

}

apsController.prototype.showFailed = function() {

	var config = this.getActiveConfig();
	
	$("#"+config.divFailed).show();

}

apsController.prototype.showNotPassed = function() {

	var config = this.getActiveConfig();
	
	$("#"+config.divNotPassed).show();

}

apsController.prototype.showRetry = function() {

	var config = this.getActiveConfig();
	
	$("#"+config.divRetry).show();

}

apsController.prototype.getActiveConfig = function() {
	if (this.configArray.length>1) {
		configName =getCourseData("ctrlConfigName");
		config = this.configs[configName];
		return (config);
	}
	else {
		return (this.configArray[0]);	
	}
}



apsController.prototype.showFeedbackLinks = function() {
	var config = this.getActiveConfig();
		
	for (var i = config.practicumSections.length-1; i >=0; i--) { 
		if ( isComplete(config.practicumSections[i]) ) {
				$("#" +config.feedbackLinkIds[i]).show();
				$("#" +config.feedbackLinkIds[i]).removeClass("displayOff");
				
				
		} 
		else {
			$("#" +config.feedbackLinkIds[i]).hide();
			
			
		}
	}
	
	if (isComplete(config.practicumSections[config.practicumSections.length-1]) ) {
	
		if (config.globalFeedBackLinkId) {
			$("#" +config.globalFeedBackLinkId).show();
			$("#" +config.globalFeedBackLinkId).removeClass("displayOff");
		}
		
		

		
	
	}
	else {
	
		if (config.globalFeedBackLinkId) {
			$("#" +config.globalFeedBackLinkId).hide();
		}

	
	
	}
	
}


apsController.prototype.showRestartLink = function() {
	var config = this.getActiveConfig();
		
	var ctrlPracticumState =getCourseData("ctrlPracticumState"); 
	var ctrlMLPState =getCourseData("ctrlMLPState");


	
	if (config.linkRestartID) {

		$("#" +config.linkRestartID).hide();
	
		if (ctrlPracticumState=="passed" && config.allowRestartOnPass ) {
			$("#" +config.linkRestartID).show();
		}
		else if (ctrlPracticumState=="failed" && config.allowRestartOnFail ) {
			$("#" +config.linkRestartID).show();
		}
		else if (ctrlMLPState=="completed" && config.allowRestartOnMLP && ctrlPracticumState=="not-passed" ) {
			$("#" +config.linkRestartID).show();
		}	
		else if (ctrlMLPState=="completed" && config.allowRestartOnFailureWithMLP && ctrlPracticumState=="failed" ) {
			$("#" +config.linkRestartID).show();
		}	
	}
	
}



apsController.prototype.displayGlobalFeedback = function () {
	var config = this.getActiveConfig();

	document.location = config.globalFeedBackLink;
}

apsController.prototype.startPracticum = function() {
	saveCourseData("ctrlPracticumState","in-progress");
	saveCourseData("ctrlPracticumSessionState","in-progress");
	
	var config = this.getActiveConfig();
		
	document.location = config.startPage;
}

apsController.prototype.restartPracticum = function() {
	var ctrlPracticumState =getCourseData("ctrlPracticumState"); 

	saveCourseData("ctrlPracticumSessionState","in-progress");
	
	var config = this.getActiveConfig();
	for (var i = 0; i< config.practicumSections.length; i++) { 
		var section = config.practicumSections[i];
		saveCourseData(section +"_Bookmark", "")
		saveCourseData(section+"_path", "");
		saveCourseData (section+"_score","");
		saveCourseData (section+"_Status","");

	}


	document.location = config.startPage;
}

apsController.prototype.retryPracticum = function() {
	var ctrlPracticumState =getCourseData("ctrlPracticumState"); 

	saveCourseData("ctrlPracticumSessionState","in-progress");
	//saveCourseData("ctrlMLPState","");
		
	var config = this.getActiveConfig();
	for (var i = 0; i< config.practicumSections.length; i++) { 
		var section = config.practicumSections[i];
		saveCourseData(section +"_Bookmark", "")
		saveCourseData(section+"_path", "");
		saveCourseData (section+"_score","");
		saveCourseData (section+"_Status","");
		

	}


	document.location = config.startPage;
	
}



apsController.prototype.startConfig = function(name) {
	saveCourseData("ctrlConfigName",name);
	this.startPracticum();
	

}


apsController.prototype.sectionReturn = function(sectionIndex) {
	var config = this.getActiveConfig();
	
	
	var practicumLocation = config.startPage;
	
	if (getSectionBookmark(config.practicumSections[sectionIndex]) ) {
		practicumLocation = getSectionBookmark(config.practicumSections[sectionIndex]);
	}
	
	
	document.location = practicumLocation

}

apsController.prototype.continuePracticum = function() {
	var config = this.getActiveConfig();
	
	var practicumLocation = config.startPage;
	
	for (var i = 0; i< config.practicumSections.length; i++) { 
		var section = config.practicumSections[i];
		if ( getSectionBookmark(section) ) {
			practicumLocation = getSectionBookmark(section);
		}
		

	}
	document.location = practicumLocation
}


apsController.prototype.continuePracticumSection = function(sectionName, url) {
	var config = this.getActiveConfig();
	
	var practicumLocation = url;
	if (getSectionBookmark(sectionName)) {
			practicumLocation = getSectionBookmark(sectionName);
		}
	
	
	document.location = practicumLocation;
}		
		






apsController.prototype.enterMLP = function() {}





apsController.prototype.enterPracticum = function() {
	var config
	var ctrlPracticumState =getCourseData("ctrlPracticumState");
	var ctrlPracticumSessionState =getCourseData("ctrlPracticumSessionState");
	var ctrlMLPState =getCourseData("ctrlMLPState");
				
	apsNav.markLearningObjectByName("practicumstart");
	saveState();

		
	if (!(config=this.getActiveConfig())) {
		this.showGetConfig();
	}
	else {
		this.hideGetConfig();
	
		this.showFeedbackLinks();
		this.showRestartLink();
		
		
		//alert (ctrlPracticumSessionState);
		switch (ctrlPracticumSessionState) {
		
		case "":
			this.showStart();
		
			break;
		
		case "passed":
		
			this.showPassed();
			break;
		
		
		case "failed":
		
			this.showFailed();
			break;


		case "not-passed":
		
			if (ctrlMLPState=="completed" && config.mlpAllowsRetry) {
				this.showRetry();
				
			}
			else {
				this.showNotPassed();
			}

			break;
		
		
		case "in-progress":
			this.showContinue();
		
			break;
		
		
		default:
			alert ("Uexpected Practicum State");
			break;
		
		}
	}

}

apsController.prototype.completePracticum = function() {

	if (!this.queryPracticumFinished()) {
			
		if (this.practicumAttempt()==this.configAllowedAttempts()) {
			this.markPracticumComplete();
		}
			
	}
	if (this.queryRescore()) {
		this.doRescore();
	}
	if (this.queryGenerateMLP()) {
		this.doGenerateMLP();
	}
	

}


apsController.prototype.addConfig = function (name, config) {
	this.configArray.push(config);
	this.configs[name] = config;

}

apsController.prototype.selectConfig = function (name) {

}







function apsControllerConfig () {

	this.allowedPracticumAttempts =1;
	this.practicumFailureDivName ="practicumFailureDiv"
	this.practicumPassDivName ="practicumPassDiv"
	
	this.finalFailureDivName = false;
	
	this.practicumSections =["ps1","ps2","ps3"]
	
	return (this);
}








