/* 
  This program is free software. It comes without any warranty, to
  the extent permitted by applicable law. You can redistribute it
  and/or modify it under the terms of the Do What The Fuck You Want
  To Public License, Version 2, as published by Sam Hocevar. See
  http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
Questions and obstacles are defined in the end of this file.
*/
function ld(num,d) {
if (d==0) num=Math.floor(num/30)
else num=Math.ceil(num/30)
return num
}

function calc(dn) {
if (!clcn) {
clcn=true
var pstr
if (dn==star[num][3]) {
pstr="class='green'>CORRECT!"
saa[num]=3
corrects++
}
else {
pstr="class='red'>WRONG! ("+abc.substr(star[num][3]-1,1)+" was correct)"
saa[num]=1
}
starstaken++
bxd.innerHTML+="<p onclick='bhidequestion()' "+pstr+"</p>"
}
}

function showquestion() {
bxd.style.display="block"
bxd.style.width=(sqp*6.4-10)+"px"
bxd.style.height=(sqp*4-10)+"px"
bxd.style.marginLeft=(sqp*-3.2)+"px"
bxd.style.marginTop=(sqp*-2)+"px"
if (sqp<75) {
sqp+=3
setTimeout("showquestion()",10)
}
else {
clcn=false
rsqw=true
bxd.innerHTML="<h1>"+star[num][1]+"</h1>"
for (var i=0;i<star[num][2].length;i++) {
bxd.innerHTML+="<p onclick='calc("+(i+1)+")'>"+abc.substr(i,1)+": "+star[num][2][i]+"</p>"
}
}
}

function bhidequestion() {
bxd.innerHTML=""
rsqw=false
hidequestion()
}

function hidequestion() {
bxd.style.width=(sqp*6.4-10)+"px"
bxd.style.height=(sqp*4-10)+"px"
bxd.style.marginLeft=(sqp*-3.2)+"px"
bxd.style.marginTop=(sqp*-2)+"px"
if (sqp>0) {
sqp-=3
setTimeout("hidequestion()",10)
}
else {
bxd.style.display="none"
sqp=1
qs=false
}
}

function nrmlclr() {
if (ncc) {
qe=true
hclr="#ff0000"
ctxo.style.backgroundColor="#ff0000"
ncc=false
setTimeout("nrmlclr()",500)
}
else {
ncc=true
hclr="#000"
ctxo.style.backgroundColor="#000"
qe=false
}
}

function motion() {
var i,pi
ctx.clearRect(0,0,640,400)
if (p>305 && p<end-290) pp=305
else if (p>end-291) pp=305+p-end+291
else pp=p

if (!rsqw) {
if (j<0) {
jumping=false
j=0
jc=5
l=41
a=21
}
if (jumping) {
if (j>200) jc=-6
if (jc>=0) j+=jc-j/100
else j+=jc+j/100
}
}

if (!walkingr && !walkingl) {
cutd=0
ta=1
}
if (cutd<10) speed=ospeed+cutd/3

ctx.strokeStyle="#fff"
ctx.lineWidth=2

// BACKGROUND
for (pi=0;pi<bgl;pi++) {
ci=Math.floor((p-pp+bg[pi][1]*p/ospeed)*bg[pi][1]/bg[pi][3])
for (i=ci;i<bg[pi][4]+ci;i++) ctx.drawImage(bg[pi][0],i*bg[pi][3]+(pp-p-bg[pi][1]*p/ospeed)*bg[pi][1],bg[pi][2])
}

// STARS
if (stad) {
for (i=0;i<starl;i++) {
x=star[i][0][0]-(p-pp)
y=400-star[i][0][1]-starh
//document.title=ld(x,0)+" "+ld(pp-starw/2+30,0)+" "+y+" "+(y+starh)+" "+(400-j-130)+" "+(400-j)
if ((ld(x,0)==ld(pp-starw/2+30,0) || ld(x,1)==ld(pp-starw/2+30,1)) && 400-j-130<y+starh && 400-j>y && saa[i]==2 && !qs) {
qs=true
num=i
sqp=1
showquestion()
}
if (x+starw>0 && x<640) {
if (saa[i]==2) ctx.drawImage(starimg,x,y)
else if (saa[i]==1 && stadu) ctx.drawImage(starimgu,x,y)
else if (saa[i]==3 && stadk) ctx.drawImage(starimgk,x,y)
}
}
}

// TRAPS
if (trapd) {
for (i=0;i<trapl;i++) {
x=trap[i][0]-(p-pp)
y=400-trap[i][1]-traph
if (pp+20>x && pp+40<x+trapw && 400-j-130<y+traph && 400-j>y && !qs && !qe) {
timeshurt++
nrmlclr()
}
if (x+trapw>0 && x<640) ctx.drawImage(trapimg,x,y)
}
}

// STICK FIGURE START

// LEGS
ctx.beginPath()
ctx.moveTo(pp+l,400-j)
ctx.lineTo(pp+20,350-j)
ctx.lineTo(pp+40-l,400-j)

// BODY
ctx.moveTo(pp+20,350-j)
ctx.lineTo(pp+20,300-j)

// ARMS
ctx.moveTo(pp+20,320-j)
ctx.lineTo(pp+10+a,350-j)
ctx.moveTo(pp+20,320-j)
ctx.lineTo(pp+30-a,350-j)
ctx.stroke()

// HEAD
ctx.fillStyle=hclr
ctx.beginPath()
ctx.arc(pp+20,285-j,15,0,Math.PI*2,true)
ctx.fill()
ctx.stroke()

// EYE
ctx.fillStyle="#fff"
ctx.beginPath()
ctx.arc(pp+eyex,eyey-j,3,0,Math.PI*2,true)
ctx.fill()

// MOUTH
ctx.beginPath()
ctx.lineCap="round"
ctx.moveTo(pp+mouthax,mouthay-j)
ctx.lineTo(pp+mouthbx,mouthby-j)
ctx.stroke()

// STICK FIGURE END

if (!rsqw) {
if (p>=end) p=end-1
else if (p<0) p=0
if (p<end && p>-1 && (walkingr || walkingl)) {
if (!jumping) {
if (l>40) lc=-1
else if (l<1) lc=1
if (a>20) ac=-.5
else if (a<1) ac=.5
l+=lc*speed
a+=ac*speed
}
if (walkingr) p+=speed
else p-=speed
}

if (jumping) {
l=60
a=40
}
if (p>end || p<0) {
a=20
l=40
}
}

if (p>=end) endgame()

cht=setTimeout("motion()",20)
}

function endgame() {
if (starstaken<starl) return
bxd.style.display="block"
qs=true

if (self.innerHeight) {
h=self.innerHeight
w=self.innerWidth
}
else if (document.documentElement && document.documentElement.clientHeight) {
h=document.documentElement.clientHeight
w=document.documentElement.clientHeight
}
else if (document.body) {
h=document.body.clientHeight
w=document.body.clientWidth
}

openend()
}

function openend() {
bxd.style.width=(sqp*w/100-10)+"px"
bxd.style.height=(sqp*h/100-10)+"px"
bxd.style.marginLeft=(sqp*-w/200)+"px"
bxd.style.marginTop=(sqp*-h/200)+"px"
if (sqp<97) {
sqp++
setTimeout("openend()",10)
}
else {
rsqw=true
corrects==1 ? ife="" : ife="s"
timeshurt==1 ? ifeb="" : ifeb="s"
prc=Math.round(corrects/starl*100)
hurtpoints=Math.round(timeshurt/trapl*100)
fpc=prc-hurtpoints
if (fpc<10) grade="F"
else if (fpc<20) grade="E"
else if (fpc<30) grade="C"
else if (fpc<55) grade="D"
else if (fpc<85) grade="B"
else if (fpc<95) grade="A"
else grade="A+"
clearTimeout(cht)
bxd.innerHTML="<h2>The quiz is over!</h2><h3>You managed to get "+corrects+" right answer"+ife+" out of "+starl+" possible, i.e. "+prc+"%. You were wounded "+timeshurt+" time"+ifeb+", deducting "+hurtpoints+"%.</h3><h3>Your grade is... <div>"+grade+"</div></h3><h4 onclick='endall()'>OK</h4>"
}
}

function endall() {
document.body.removeChild(bxd)
document.body.removeChild(ctxo)
}

function md(e) {
if (window.event) kc=window.event.keyCode
else if (e) kc=e.which

if (!walkingr && kc==39) {
walkingr=true
walkingl=false
cutd=0
}
if (!walkingl && kc==37) {
walkingr=false
walkingl=true
cutd=0
}
else if (!jumping && kc==38) jumping=true

if (walkingl) {
eyex=12
eyey=283
mouthax=20
mouthay=293
mouthbx=10
mouthby=295
}
else if (walkingr) {
eyex=28
eyey=283
mouthax=30
mouthay=295
mouthbx=20
mouthby=293
}
}

function mu(e) {
if (window.event) kc=window.event.keyCode
else if (e) kc=e.which

if (walkingr && kc==39) walkingr=false
if (walkingl && kc==37) walkingl=false
}

function time() {
if (cutd==0) ta=1
cutd+=ta
}

function load() {
var i
fdng=100
ospeed=3
speed=ospeed
p=0
l=0
a=0
walkingr=false
walkingl=false
jumping=false
jc=5
j=0
qs=false
qe=false
rsqw=false
corrects=0
starstaken=0
abc="ABCDEFGH"
cutd=0
ta=1
ncc=true
hclr="#000"
timeshurt=0

// BACKGROUNDS
bg=new Array()
bg[0]=new Array()
bg[0][0]=new Image()
bg[0][0].src="Cerebral_lobes.png"
bg[0][1]=.5
bg[0][2]=20
bg[1]=new Array()
bg[1][0]=new Image()
bg[1][0].src="Cerveau_anime.gif"
bg[1][1]=1
bg[1][2]=90
bg[2]=new Array()
bg[2][0]=new Image()
bg[2][0].src="Friendly_stickman.png"
bg[2][1]=1.5
bg[2][2]=170
bgl=bg.length

// STARS
star=new Array()
star[0]=new Array()
star[0][0]=[200,3]
star[0][1]="To what extent does bicycles apply to world economics?"
star[0][2]=["A great extent","A noticable extent","Nothing at all","I don't know"]
star[0][3]=4
star[1]=new Array()
star[1][0]=[650,80]
star[1][1]="What is a chocobo?"
star[1][2]=["A fictional bird","A fictional frog","A real bird","A real frog"]
star[1][3]=1
star[2]=new Array()
star[2][0]=[850,100]
star[2][1]="What is the size of an average male human brain?"
star[2][2]=["1200-1300cm<sup>3</sup>","1100-1200cm<sup>3</sup>","1300-1400cm<sup>3</sup>","1000-1100cm<sup>3</sup>"]
star[2][3]=2
star[3]=new Array()
star[3][0]=[1200,23]
star[3][1]="What is 2 * 8^(1/3) + 4?"
star[3][2]=["2","4","6","8"]
star[3][3]=4
star[4]=new Array()
star[4][0]=[1577,150]
star[4][1]="What colour is the beak of a male blackbird?"
star[4][2]=["Grey","Black","Brown","Orange"]
star[4][3]=4
star[5]=new Array()
star[5][0]=[1700,10]
star[5][1]="Do you like this quiz?"
star[5][2]=["Yes","No","Perhaps","Eventually","Maybe","Depends"]
star[5][3]=1
star[6]=new Array()
star[6][0]=[1900,20]
star[6][1]="Who is Horacio Peņa?"
star[6][2]=["An actor","A writer"]
star[6][3]=2
star[7]=new Array()
star[7][0]=[2222,111]
star[7][1]="When did Monkey Island reveal its truth?"
star[7][2]=["1990","1991","1992","Hasn't happened yet"]
star[7][3]=4
star[8]=new Array()
star[8][0]=[2500,23]
star[8][1]="Do you gimp?"
star[8][2]=["No, I photoshop.","Yes."]
star[8][3]=2
starl=star.length

Array.prototype.add=function(x,y){this[this.length]=[x,y]}

// TRAPS
trap=new Array()
trap.add(100,5)
trap.add(300,60)
trap.add(350,43)
trap.add(660,20)
trap.add(700,250)
trap.add(750,250)
trap.add(800,250)
trap.add(860,2)
trap.add(920,30)
trap.add(930,40)
trap.add(940,50)
trap.add(1250,40)
trap.add(1400,68)
trap.add(1537,10)
trap.add(1567,10)
trap.add(1597,10)
trap.add(1700,170)
trap.add(1800,64)
trap.add(1950,3)
trap.add(1950,23)
trap.add(1950,43)
trap.add(1950,63)
trap.add(1950,203)
trap.add(2200,10)
trap.add(2375,10)
trap.add(2435,10)
trapl=trap.length

saa=new Array()
for (i=0;i<starl;i++) {
saa[i]=2
}

end=star[starl-1][0][0]+350

starimg=new Image()
starimg.src="star.png"
starimgu=new Image()
starimgu.src="staru.png"
starimgk=new Image()
starimgk.src="stark.png"

trapimg=new Image()
trapimg.src="trap.png"

eyex=28
eyey=283
mouthax=30
mouthay=295
mouthbx=20
mouthby=293

ctxo=document.getElementById("canvas")
ctx=ctxo.getContext("2d")
bxd=document.getElementById("box")
strt=document.getElementById("start")

stad=false
starimg.onload=function(){
starh=starimg.height
starw=starimg.width
stad=true
}
stadu=false
starimgu.onload=function(){
stadu=true
}
stadk=false
starimgk.onload=function(){
stadk=true
}

trapd=false
trapimg.onload=function(){
traph=trapimg.height
trapw=trapimg.width
trapd=true
}

bg[0][0].onload=function(){
bg[0][3]=bg[0][0].width
bg[0][4]=Math.ceil(640/bg[0][3])+1
}
bg[1][0].onload=function(){
bg[1][3]=bg[1][0].width
bg[1][4]=Math.ceil(640/bg[1][3])+1
}
bg[2][0].onload=function(){
bg[2][3]=bg[2][0].width
bg[2][4]=Math.ceil(640/bg[2][3])+1
}

document.onkeydown=startgame
document.onclick=startgame
}

function startgame() {
document.onclick=null
strt.style.mozOpacity=fdng/100
strt.style.opacity=fdng/100
strt.style.filter="alpha(opacity="+fdng+")"
if (fdng>0) {
fdng-=8
setTimeout("startgame()",10)
}
else {
document.onkeydown=md
document.onkeyup=mu
strt.style.display="none"
motion()
setInterval("time()",500)
}
}

window.onload=load