/*************************************************************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/23/2010
 * 
 * This is the Question class for the CEE application
 * CS5 version of com.digitec.cee.Question
 *  
 * An example of the XML used is displayed it at the end of this file 
******************************************************************************************/
package com.digitec.cee
{
	import flash.display.MovieClip;	
	import flash.display.Sprite;		
	
		
	public class Question 
	{								
		private var question_str:String;
		private var correctAnswer_str:String;
		private var correctFeedback_str:String;
		private var incorrectFeedback_str:String;
		private var incorrectAnswers_arr:Array;		
		private var allAnswers_arr:Array;
		private var posCorrectAnswer:int;
		private var answersRandom:Boolean;		
		private var num_answers:int;				
		private var num_incorrect_answers:int; 
		
		private var questionId:String;
		private var correctAnswerId:String;
		private var incorrectAnswersId_arr:Array = new Array();		
		//private var allAnswersId_arr:Array;
		
		public function Question( _xml:XML, maxAnswers:int )		
		 {															
		// trace(_xml);
		 	// WANT ANSWERS IN RANDOM ORDER?
			if( _xml.random_answers.toString() == "true" || _xml.random_answers.toString() == "yes" ) { answersRandom = true; }
			else{ answersRandom = false; }
			
			//GET THE QUESTION FROM XML PIECE
			question_str = _xml.question.toString();	
			questionId = _xml.question.@id;	 ////////////////////////////////////////
			trace( "_xml.question.@id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+ _xml.question.@id);
			
			//GET CORRECT ANSWER AND FEEDBACK STRINGS FROM XML PIECE
			correctAnswer_str = _xml.correct_answer.toString();	
			correctAnswerId = _xml.correct_answer.@id;	 ///////////////////////////////////////////////
			trace( "_xml.correct_answer.@id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+ _xml.correct_answer.@id);
			
			correctFeedback_str = _xml.correct_question_fb.toString();	
			incorrectFeedback_str = _xml.incorrect_question_fb.toString();	
			
			//GET THE ANSWERS FOR THE answer NODES IN THE XML
			//INCORRECT + 1 (CORRECT) HAVE TO BE <= ANSWER DISPLAY OBJECTS
			num_incorrect_answers = _xml.elements("answer").length();			
			
			//SET INCORRECT ANSWERS TO A MAX OF 4
		    if( num_incorrect_answers > maxAnswers - 1)	{ num_incorrect_answers = maxAnswers - 1; }
			
			//CREATE AN ARRAY WITH THE INCORRECT ANSWERS
			//trace("incorrectAnswers_arr[i] = " + incorrectAnswers_arr[i] );
			incorrectAnswers_arr = new Array();
			for( var i:int=0; i < num_incorrect_answers; i++ )
			{	
	//trace(_xml.answer[i].@id.toString());
				incorrectAnswers_arr[i] =  _xml.answer[i].toString();	
				incorrectAnswersId_arr[i] = _xml.answer[i].@id;	/////////////////////////////////////
				trace( "" );
				//incorrectAnswers_arr[i].push(_xml.answer[i].toString());
				//incorrectAnswersId_arr[i].push(_xml.answer[i].@id);
			}		
			
			//CREATE AN ARRAY WITH CORRECT & INCORRECT ANSWERS			
			allAnswers_arr = createAllAnswerArray();			
		}		
		
		/*******************************************************************
	 	* SET FUNCTIONS
		*******************************************************************/				
		public function set correctAnswer( correctAnswer:String ):void{ correctAnswer_str = correctAnswer; }
		
		public function set incorrectAnswers( incorrectAnswers:Array ):void	{ incorrectAnswers_arr =incorrectAnswers; }		
		
		public function set correctFeedback( correct:String ):void	{ correctFeedback_str =correct; }		
		
		public function set incorrectFeedback( incorrect:String ):void	{ incorrectFeedback_str =incorrect; }		
		
		
		/*******************************************************************
	 	* GET FUNCTIONS
		*******************************************************************/				
		public function get question():String{ return question_str; }
		
		public function get thequestionId():String{ return questionId; }
			
		public function get correctAnswer():String{ return correctAnswer_str; }
		
		public function get correctFeedback():String	{ return correctFeedback_str; }		
		
		public function get incorrectFeedback():String	{ return incorrectFeedback_str; }		
		
		public function get incorrectAnswers():Array	{ return incorrectAnswers_arr; }
		
		//for(var i:int = 0; i< allAnswers_arr.length; i++){trace("allAnswers_arr = " + allAnswers_arr[i]);}
		public function get answers():Array	{	return allAnswers_arr; 	}
		
		public function get correctAnswerPosition():int	{ return posCorrectAnswer; }		
		
		
		/*******************************************************************
	 	* FUNCTIONCREATE AN ARRAY WITH CORRECT & INCORRECT ANSWERS
		*  AND RETURN THE POSITION OF THE CORRECT ANSWER
		*******************************************************************/				
		private function createAllAnswerArray():Array
		{
			//CREATE ANSWER ARRAY PUSHING CORRECT ANSWER
			//IN LAST ARRAY POSITION
			allAnswers_arr = new Array();			
			for(var i:int=0; i < incorrectAnswers_arr.length; i++)
			{
				allAnswers_arr.push( {Answer:incorrectAnswers_arr[i].toString(), AnswerId:incorrectAnswersId_arr[i].toString() } );
				//allAnswersId_arr.push( incorrectAnswersId_arr[i] );/////////////////////////////////////////////////////////////
			}
			//allAnswers_arr = incorrectAnswers_arr;				
			//allAnswersId_arr =  incorrectAnswersId_arr;				/////////////////////////////////////////////////////////////
			
			allAnswers_arr.push({ Answer:correctAnswer_str.toString(), AnswerId:correctAnswerId.toString() });
			//allAnswersId_arr.push(correctAnswerId_str.toString());
			
			posCorrectAnswer =  allAnswers_arr.length - 1;	
			
			//IF WANT ANSWERS IN RANDOM ORDER			
			if(answersRandom == true)	{  allAnswers_arr = randomAnswers( allAnswers_arr );	}		
			//else { posCorrectAnswer =  allAnswers_arr.length - 1;	 }					
			
			return allAnswers_arr;
		}		
		
		
		
		/*******************************************************************
	 	* FUNCTION RETURNS ALL ANSWERS ARRAY & PLACE
		*  CORRECT ANSWER IN RANDOM POSITION
		*******************************************************************/				
		private function randomAnswers( answers_arr:Array):Array
		{						
			if(allAnswers_arr != null)
			{
				//var randomInt:int = Math.floor(Math.random()*(allAnswers_arr.length+1));							
				var randomInt:int = Math.floor(Math.random()*(allAnswers_arr.length));							
				var temp:Object = allAnswers_arr[randomInt];
				//put correct answer in random pos
				allAnswers_arr[randomInt] = allAnswers_arr[posCorrectAnswer];
				//put temp in old correct position
				allAnswers_arr[posCorrectAnswer] = temp;			
				//new array position of correct answer
				posCorrectAnswer = randomInt;
			}
			return allAnswers_arr;			
		}
		
		 
	} // Main class
 
} //end package


/*******************************************************************
* PART OF XML FILE EXAMPLE
*******************************************************************/		
/*****************************************************************************************************
		<random_answers>yes</random_answers> <!--tag is used to display the answers randonly = true, or not = false-->
		<question_exercise>
		<question>Question number 1. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Answer 2 </answer>
			<answer>Answer 3 </answer>
			<answer>Answer 4 </answer>
			<answer>Answer 5 </answer>
			<answer>Answer more </answer>
		</question_exercise>		

******************************************************************************************/






