qTotal = 0;
quizInitialised = false;
function jumpToQuestion(qq) {
	qq *= 1;
	if (!quizInitialised) initialiseQuiz();
	responseSelected = false;
	if (qq < qTotal) {
		// hide intro screen
		document.getElementById('quiz0').style.display = 'none';
		// hide results screen
		document.getElementById('quiz_result').style.display = 'none';
		// show small skelanimals image
		document.getElementById('skelanimals_small').style.display = 'block';
		// show quiz questions
		document.getElementById('quizq').style.display = 'block';
		// find and display the question data
		qHTML = qData[qq];
		document.getElementById('q').innerHTML = qHTML;
		setTimeout("traverseResponses();",250);
	} else {
		// qtally the responses:
		qtally = new Array(0,0,0,0);
		for(r=0;r<qResponses.length;r++) {
			qtally[qResponses[r]-1]++;
		}
		// alert(qtally);
		// which was clicked the most?
		maxIndex = 0;
		for(r=0;r<4;r++) {
			maxTry = true;
			// check against other tallies
			for(rr2=0;rr2<4;rr2++) {
				if (r!=rr2) {
					if (qtally[rr2] > qtally[r]) {
						maxTry = false;
					}
				}
			}
			if (maxTry) {
				maxIndex = r;
				break;
			}
		}
		skelanimals = new Array("Jack", "Dax", "Kit", "Foxy");
		animalResult = skelanimals[maxIndex];
		// display final page
		// hide small skelanimals image
		document.getElementById('skelanimals_small').style.display = 'none';
		// clear quiz text
		document.getElementById('q').innerHTML = '';
		// show myspace code
		document.getElementById('myspace_code').style.display = 'block';
		// set myspace embed code
		embedCode = "<a href=\"http://www.peta2.com/furisdead/index.asp?c=p"+(22770+maxIndex)+"\"><img border=\"0\" src=\"http://www.peta2.com/furisdead/images/"+animalResult.toLowerCase()+".gif\" alt=\"Click here to find out which Skelanimal you are!\" width=\"400\" height=\"300\" /></a>";
		document.getElementById('myspace_code_textarea').value = embedCode;
		// show quiz result div
		document.getElementById('quiz_result').style.display = 'block';
		// set quiz result title image
		document.getElementById('quiz_result_name').style.backgroundImage = "url(images/i_am_"+animalResult.toLowerCase()+".gif)";
		// set quiz result description (as contained in <p> elements within the quiz_data.inc.asp
		document.getElementById('quiz_result_text').innerHTML = document.getElementById('description_'+animalResult.toLowerCase()).innerHTML;
	}
	setShortcuts(qq);
}

function setShortcuts(qq) {
	// initialise shortcut behaviour
	shortcuts = document.getElementById('shortcutLinks').getElementsByTagName('A');
	for(s=1;s<=shortcuts.length;s++) {
		shortcut = shortcuts[s-1];
		shortcut.setAttribute('qId',s);
		if (s > qq) {
			shortcut.style.cursor =  'default';
			shortcut.onclick = null;
			shortcut.onmouseover = null;
			shortcut.onmouseout = null;
			shortcut.className = '';
		} else {
			shortcut.className = 'done';
			shortcut.onclick = function() {
				jumpToQuestion(this.getAttribute('qId'));
			}
			shortcut.onmouseover = function() {
				// preview shortcut
				shortcuts = document.getElementById('shortcutLinks').getElementsByTagName('A');
				for(sc=1;sc<=shortcuts.length;sc++) {
					scEl = shortcuts[sc-1];
					scEl.className = (scEl.getAttribute('qId')*1 <= this.getAttribute('qId')*1) ? 'done' : '';
				}
			}
			shortcut.onmouseout = function() {
				// restore
				shortcuts = document.getElementById('shortcutLinks').getElementsByTagName('A');
				for(sc=1;sc<=shortcuts.length;sc++) {
					scEl = shortcuts[sc-1];
					scEl.className = (scEl.getAttribute('qId')*1 <= qq*1) ? 'done' : '';
				}
			}
			shortcut.style.cursor = 'pointer';
		}
	}
}

function traverseResponses() {
	// traverse and apply mouseover behaviour to question response options:
	responses = document.getElementById('q').getElementsByTagName('UL')[0].getElementsByTagName('LI');
	for(r=1;r<=responses.length;r++) {
		resp = responses[r-1];
		resp.id = 'r'+r;
		iEl = resp.getElementsByTagName('IMG')[0];
		iEl.id = 'i'+resp.id;
		iEl.onmouseover = function() {
			if (!responseSelected) {
				// strip all response classes
				for(i=1;i<=responses.length;i++) {
					document.getElementById('r'+i).className = (i==this.getAttribute('rId')) ? 'selected' : '';
				}
			}
		}
		iEl.onmousedown = function() {
			if (!responseSelected) {
				for(i=1;i<=responses.length;i++) {
					document.getElementById('r'+i).className = (i==this.getAttribute('rId')) ? 'selected' : 'unselected';
				}
			}
		}
		iEl.onclick = function() {
			// delay the press effect
			responseSelected = true;
			setTimeout("submitResponse(document.getElementById('"+this.id+"').getAttribute('qId'),document.getElementById('"+this.id+"').getAttribute('rId'));",250);
		}
	}
}

qResponses = new Array();
function submitResponse(qId,rId) {
	qId *= 1;
	rId *= 1;
	// store response in qtally
	qResponses[qId-1] = rId;
	jumpToQuestion((qId+1));
}

qData = new Array();
function initialiseQuiz() {
	quizInitialised = true;
	qDataEl = document.getElementById('quizQuestions');
	qEls = qDataEl.getElementsByTagName('DIV');
	// initialise question element behaviour
	for(qE=1;qE<=qEls.length;qE++) {
		qEl = qEls[qE-1];
		respList = qEl.getElementsByTagName('UL')[0];
		resps = respList.getElementsByTagName('LI');
		for(r=1;r<=resps.length;r++) {
			resp = resps[r-1];
			// create a clickable button inside this resp item to act as the response hotspot
			iEl = document.createElement('IMG');
			iEl.src = 'images/space.gif';
			iEl.style.position='absolute';
			iEl.style.height='25px';
			iEl.style.width='25px';
			iEl.style.left = "80px";
			iEl.style.cursor = 'pointer';
			iEl.setAttribute("qId",qE);
			iEl.setAttribute("rId",r);
			resp.appendChild(iEl);
		}
		qData[qE] = qEl.innerHTML;
	}
	qTotal = qData.length;
	// add alt tags to shortcuts
	shortcuts = document.getElementById('shortcutLinks').getElementsByTagName('A');
	for(sc1=1;sc1<=shortcuts.length;sc1++) {
		scEl = shortcuts[sc1-1];
		scEl.title = "Question "+sc1;
	}
}

