var my_path = "/_javascript/inline_quiz/";
		 
vuDAT_Inline_Quiz = {
	checkAnswer: function(element, ans){
		if(ans)
			$(element).addClass("correctAns");
		else
			$(element).addClass("incorrectAns");
		//console.log(element.id);
		//setTimeout("vuDAT_Inline_Quiz.nextQuestion('"+element.id+"')",5000);
		vuDAT_Inline_Quiz.nextQuestion(element.id);
	},
	
	nextQuestion: function(id){
		if(id != ""){
			if($("."+id+":first").attr("class").search(/inline_content/) != -1)
				$($("."+id+":first").children("div.inline_quiz_all, div.inline_quiz")[0]).addClass("show");
			$("."+id).slideDown("slow");	
		}
	},
	
	initialize: function(){
		//<![CDATA[
		// loading the stylesheet into the document header
		if(document.createStyleSheet) { // non-standard IE way of doing it
			document.createStyleSheet(my_path+'styles_inline_quiz.css');
		} else { // do it the W3C DOM way
			var newCSS=document.createElement('link');
			newCSS.rel='stylesheet';
			newCSS.href=my_path+'styles_inline_quiz.css';
			newCSS.type='text/css';
			document.getElementsByTagName("head")[0].appendChild(newCSS);
		}
		$(".inline_quiz div.wrong").each(function(i){
			this.onclick = function(){
				image = this.getElementsByTagName('img')[0];
				image.src = my_path + 'x.gif';
				this.style.color = '#ff0000';
				if(this.getElementsByTagName("div")[0])
					if(this.getElementsByTagName("div")[0].className == 'why')
						this.getElementsByTagName("div")[0].className = 'because';
			}
		});
		
		$(".inline_quiz div.right").each(function(i){
			this.onclick = function(){
				image = this.getElementsByTagName('img')[0];
				image.src = my_path+'check.gif';
				this.style.color = '#2EB231';
				this.style.fontWeight = 'bold';
				if(this.getElementsByTagName("div")[0])
					if(this.getElementsByTagName("div")[0].className == 'why')
						this.getElementsByTagName("div")[0].className = 'because';
				elements = this.parentNode.getElementsByTagName("div");
				this.className = 'ilq_answer';
				flag = true;
				if(elements) {
					for(i = 0; i < elements.length; ++i){
						if(elements[i].className == 'right'){
							flag = false;
							break;
						}
					}
				}
				if(flag){
					if(elements) {
						for(i = 0; i < elements.length; ++i){
							elements[i].onclick = null;
						}
					}
					vuDAT_Inline_Quiz.checkAnswer(this.parentNode,true);
				}
			}
		});
		$(".inline_quiz div.right, .inline_quiz div.wrong").each(function(i){
			var image = document.createElement('img');
			image.setAttribute("src",my_path+"box.gif");
			image.className = 'ilq_box';
			this.insertBefore(image, this.firstChild);
			if(this.className == 'wrong')
				this.className = 'ilq_answer';	
		});
		$(".inline_quiz_all div.right, .inline_quiz_all div.wrong").each(function(i){
			var box = document.createElement('img');
			box.setAttribute("src",my_path+"box.gif");
			box.className = 'ilq_box';
			this.insertBefore(box, this.firstChild);
		});
		// Get the right responses for the "one correct" questions into an array
		$(".inline_quiz_all .right").each(function(i){
			this.onclick = function(){
				me = this;
				var cur_question = me.parentNode; // select the question
				var the_responses = cur_question.getElementsByTagName('div'); // select all of the divs in the question
				if (the_responses) {
					for (k = 0; k < the_responses.length ; k++) {
						this_response = the_responses[k];
						cur_image = this_response.getElementsByTagName('img')[0];
						if(this_response.className=='right') {
							cur_image.src = my_path+'check.gif';
							this_response.style.color = '#2EB231';
							this_response.style.fontWeight = 'bold';
						} 
						else if(this_response.className=='wrong') {
							cur_image.src = my_path+'x.gif';
							this_response.style.color = '#ff0000';
						}
						else if(this_response.className=='why') {
							this_response.className = 'because';
						}
						else {
							// don't add onclick functionality to divs that are part of the question
						}
					}
					for(i = 0; i < the_responses.length; ++i)
						the_responses[i].onclick = null;
				}
				vuDAT_Inline_Quiz.checkAnswer(this.parentNode,true);
			}
		});
			
		// Get the right responses for the "incorrect" questions into an array
		$(".inline_quiz_all .wrong").each(function(i){
			this.onclick = function(){
				me = this;
				var cur_question = me.parentNode; // select the question
				var the_responses = cur_question.getElementsByTagName('div'); // select all of the divs in the question
				if (the_responses) {
					for (k = 0; k < the_responses.length ; k++) {
						this_response = the_responses[k];
						cur_image = this_response.getElementsByTagName('img')[0];
						if(this_response.className=='right') {
							cur_image.src = my_path+'check.gif';
							this_response.style.color = '#2EB231';
							this_response.style.fontWeight = 'bold';
						} 
						else if(this_response.className=='wrong') {
							cur_image.src = my_path+'x.gif';
							this_response.style.color = '#ff0000';
						}
						else if(this_response.className=='why') {
							this_response.className = 'because';
						}
						else {
							// don't add onclick functionality to divs that are part of the question
						}
					}
					for(i = 0; i < the_responses.length; ++i)
						the_responses[i].onclick = null;
				}
				vuDAT_Inline_Quiz.checkAnswer(this.parentNode,false);
			}
		});
		
		//show first question of all quizzes
		elements = document.getElementsByTagName("div");
		if(elements) {
			for(i = 0; i < elements.length; ++i){
				if(elements[i].className.search(/vudatInlineQuiz/) != -1){
					the_element = elements[i].getElementsByTagName("div")[0];
					if(the_element){
						$(the_element).addClass("show");
						if(the_element.className.search(/inline_content/) != -1){
							$(the_element).children("div.inline_quiz, div.inline_quiz_all").each( function() {
								$(this).addClass("show");																	   
							});
						}
					}
				}
			}
		}
	}
};

$(document).ready(function() {
	vuDAT_Inline_Quiz.initialize();
});
/*
$.each( elements, function(i, n){
				
			});
*/
