// JavaScript Document

// local home
//var ABS_ROOT = 'http://localhost:8888/iov_videoskills/';
//var REL_ROOT = '/iov_videoskills/';
// local work
//var ABS_ROOT = 'http://192.168.1.89/iov_videoskills/';
//var REL_ROOT = '/iov_videoskills/';
// test development
//var ABS_ROOT = 'http://www.zone1media.net/development_sites/iov_videoskills/';
//var REL_ROOT = '/development_sites/iov_videoskills/';
// LIVE
var ABS_ROOT = 'http://vs.iov.co.uk/';
var REL_ROOT = '/';




var numAns;
var oneUp;
var count = 0;
var answer;
var thePage;
var theQues;

function init(numAnswers) {
	numAns = parseInt(numAnswers); //  number of correct answers
	oneUp = numAns + 1;
}

function page(page, question) {
	thePage = page;
	theQues = question;
}

$(document).ready(function() {
	// sources of state images
	var over = ABS_ROOT+'images/radio_button_over.gif';
	var out = ABS_ROOT+'images/radio_button.gif'
	var selected = ABS_ROOT+'images/radio_button_selected.gif';
	
	// hover functions (only change image if not a selected one)
	$('img.answerBtns').hover(
		function() {
			if(this.src !== selected) {
				this.src = over;
			}
		},
		function() {
			if(this.src !== selected) {
				this.src = out;
			}
		}
	);
	
	//$('#nxtQ').hide();
	
	// click function (change all other btns to out state)
	$('img.answerBtns').click(
		function() {
			// if more than one correct answer
			if(numAns > 1) {
				// if button is already selected
				// unselect and take one off answer count
				if(this.src == selected) {
					this.src = out;
					if(count !== 0) {
						count--;
					}
				
				// if button is not selected
				} else {
					// if less than total number of answers selected then change button to selected
					if(count < numAns) {
						this.src = selected;
						count++;	
					}
				}
			// just one correct answer
			} else { 
				$('img.answerBtns').each(
					function() {
						this.src = out;	
					}
				);
				this.src = selected;
				if(count == numAns) {
					//$('#nxtQ').show();
				}
			}
			checkNum();
		}
	);
	
	// submit fake form 
	$('#nxtQ').click(function() {
			
			var id = "";
			var info = "";
			var count = 0;
			
			var mode = $('#nxtQ').attr('mode');
		
			// loop round each btn
			$('img.answerBtns').each(
				function() {
				// check if selected state
				if(this.src == selected) {
					id += $(this).attr('id')+',';
					info = $(this).attr('info');
					count++;
				}
				if(count == numAns) {
					answer = true;	
				} else if(count == 0) {
					answer = false;
				} else {
					answer = false;	
				}
			});

			// no answer submitted
			if(answer == false) {
				alert("Please submit the correct amount of answers");
			// send answer to submit_ans.ajax.php
			} else {
				$('#page_links_container').html('<span style="margin-right:30px">Loading...</span>');
				$('#leaveExam').hide();
				$('input[@name=info]').val(id+';'+info+';'+mode);
				$('form[@name=info_form]').submit();
			}
	});

	
	$('#changeAns').click(function() {
		var id = "";
		var info = "";
		var count = 0;
		
		var mode = $('#changeAns').attr('mode');
			
		// loop round each btn
		$('img.answerBtns').each(
			function() {
			// check if selected state
			if(this.src == selected) {
				id += $(this).attr('id')+',';
				info = $(this).attr('info');
				count++;
			}
			if(count == numAns) {
				answer = true;	
			} else if(count == 0) {
				answer = false;
			} else {
				answer = false;	
			}
		});
		
		if (answer == false) {
			alert("Please submit the correct amount of answers");
		// send answer to submit_ans.ajax.php
		}
		else {
			$('#page_links_container').html('<span style="margin-right:30px">Loading...</span>');
			var mode = $(this).attr('mode');
			$('#leaveExam').hide();
			$('input[@name=info]').val(id+';'+info+';'+mode);
			$('form[@name=info_form]').submit();
		}
	});
});

function checkNum() {
	if (numAns > 1) {
		if (count == numAns) {
			$('#nxtQ').show();
		}
	}
	else {
		if (count == (numAns - 1)) {
			$('#nxtQ').show();
		}
	}
}

// animate progress bar according to question number
/*function progress(prog) {
	var progress = prog+"%";
	$('#progress').animate({
		width: progress,					   
	}, 1500);
}*/