/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */

componentShowExerciseFeedback = function(name) {
	this.componentButton = componentButton;
	this.componentButton(name);
	this.flashVars = "";
	this.confirmAnswer = true;
};

componentShowExerciseFeedback.prototype = new componentButton;
componentShowExerciseFeedback.prototype.type = "componentShowExerciseFeedback";

componentShowExerciseFeedback.prototype.onBaseCreate = function(obj)
{
	this.resourcesURL = resourcesURL;
	//this.standardButton = this.useFlash; //pg2- used only in MouseOver/Out events, and with this line when element is locked- inherited from button images are shown
	
	this.resourcesStatus = "online";
	if (lms && lms.name != "lmsOffline") {
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	};
	
	if (!this.useFlash) this.standardButton = false; //pg2- without it, mouseOver/Out will not be working, but in case of using flash...
	
	this.imagesNameArr = [	this.src, this.mouseOverSrc , this.clickSrc ];	
	this.getPreloadImagesArr();
	this.captureMouseEvents();	
};

componentShowExerciseFeedback.prototype.componentShowExerciseFeedbackBaseSetValue = componentShowExerciseFeedback.prototype.setValue;
componentShowExerciseFeedback.prototype.setValue = function(value)
{
	if (this.useFlash)
	{
		this.createFlashObject(this.resourcesURL + this.flashSrc);		
	}else
	{
		this.componentShowExerciseFeedbackBaseSetValue(value);
	}
}

componentShowExerciseFeedback.prototype.createFlashObject = function(value)
{
	var fv = "";
	if (PageInterface.GetValue("state") != psDesigning) {
		fv = "mediaStatus=" + lms.mediaStatus() + "&" + this.formatVar(this.flashVars);
	}

	var t = "";
	t += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="'+ this.w +'" height="'+ this.h +'" id="'+ this.id +'flashPlayer" align="middle">';
	t += '  <param name="movie" value="'+ value +'">';
	t += '  <param name="allowScriptAccess" value="sameDomain" />';
	t += '  <param name="BGColor" value="#FFFFFF">';
	t += '  <param name="quality" value="high">';
	t += '  <param name="play" value="false">';
	t += '  <param name="loop" value="true">';
	t += '  <param name="WMode" value="transparent">';
	t += '  <param name="scale" value="exactfit">';
	t += '  <param name="salign" value="CC">';
	t += '  <param name="FlashVars" value="'+fv+'">';
	t += '  <embed wmode="transparent" src="'+ value +'" quality="high" bgcolor="#FFFFFF" name="'+ this.id +'flashPlayer" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+ this.w +'" height="'+ this.h +'" FlashVars="'+fv+'"></embed>';
	t += '</object>';
	this.setHTML(t);
};

componentShowExerciseFeedback.prototype.componentShowExerciseFeedbackBaseEventClick = componentShowExerciseFeedback.prototype.eventClick;
componentShowExerciseFeedback.prototype.eventClick = function(e)
{
	if (this.locked) return false;
	
	this.invokeEvent('Click');
	
	if (PageInterface.GetValue("pageType") == "Exercise") {
		PageInterface.SetValue("allowGetMaxScore", true);
		
		PageInterface.Execute("checkAnswer", this);
		
		var userScore = PageInterface.GetValue("userScore");
		var masteryScore = PageInterface.GetValue("masteryScore");
		var maxScore = PageInterface.GetValue("maxScore");
		var answerWeighting = PageInterface.GetValue("answerWeighting");
		var countAnswers = PageInterface.GetValue("countAnswers");
		var corAns = (answerWeighting)?(userScore >= masteryScore):(userScore == maxScore);
		
		this.hideFeedbacks();
		
		if (userScore == 100)
		{
			/// brillant
			this.showFeedback(this.feedbackBrilliant);
			this.invokeEvent('FeedbackBrilliant');
			this.lockPage();
		}else if (corAns){
			if (countAnswers <= 0){
				/// very good
				this.showFeedback(this.feedbackCorrect);
				this.invokeEvent('FeedbackCorrect');
				this.lockPage();
			}else{
				/// good
				this.showFeedback(this.feedbackGood);				
				this.invokeEvent('FeedbackGood');
			}		
		}else{
			if (countAnswers <= 0){
				/// bad
				this.showFeedback(this.feedbackIncorrect);
				this.invokeEvent('FeedbackIncorrect');
				this.lockPage();
			}else{
				/// try again
				this.showFeedback(this.feedbackAgain);			
				this.invokeEvent('FeedbackAgain');				
			}			
		}		
		PageInterface.SetValue("allowGetMaxScore", false);
		return true;
	} else {
		return this.componentShowExerciseFeedbackBaseEventClick(e);
	}
};

componentShowExerciseFeedback.prototype.repaintPageComponents = function() 
{
	var isExam = PageInterface.GetValue("isExam");
	if (!isExam) {	
		PageInterface.Execute("repaintAndLockElements", this);
	}
}

componentShowExerciseFeedback.prototype.lockPage = function() 
{
	PageInterface.Execute("lock");
	this.repaintPageComponents();
}

componentShowExerciseFeedback.prototype.hideFeedbacks = function() 
{
	var component = this.getFeedbackComponent(this.feedbackBrilliant);
	if (component) component.hide();
	var component = this.getFeedbackComponent(this.feedbackCorrect);
	if (component) component.hide();
	var component = this.getFeedbackComponent(this.feedbackGood);
	if (component) component.hide();
	var component = this.getFeedbackComponent(this.feedbackIncorrect);
	if (component) component.hide();
	var component = this.getFeedbackComponent(this.feedbackAgain);
	if (component) component.hide();	
}

componentShowExerciseFeedback.prototype.showFeedback = function(feedbackName) 
{
	var component = this.getFeedbackComponent(feedbackName);
	if (component) {
		component.show();	
	}
}

componentShowExerciseFeedback.prototype.getFeedbackComponent = function(feedbackName) 
{
	var component = PageInterface.GetValue("getChild", feedbackName);	
	if (component) {
		return component;
	}else{
		component = PageInterface.GetValue("getChild", this.getFeedbackNameWithBckg()+feedbackName);	
		return component;
	}
}

componentShowExerciseFeedback.prototype.getFeedbackNameWithBckg = function(feedbackName) 
{
	var bckgName = this.id.split("_");
	var bckgStr = ""
	for (var i=0; i<bckgName.length-1; i++) bckgStr += bckgName[i]+"_";	
	
	return bckgStr;
}
















