/* 
  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.
*/

/*
  This code is MESSY! It is UNCOMMENTED! It is JAVASCRIPT!
  In short: It's not perfect. WTF.
*/

function nothing() {
// NOTHING
}

function finalend() {
head.removeChild(stylesheet)
document.body.style.backgroundColor="#000"
document.body.style.color="#fff"
document.body.style.fontFamily="'lucida console','lucida sans',verdana,arial"
document.body.style.fontSize="14px"
document.body.innerHTML="DOS<br />>"
document.title=""
}

function gameover() {
if (!goa) {
goa=true
dir=2
mvn[2]=false
document.onkeydown=nothing
document.onkeyup=nothing
gbdiv.style.top="43px"
gbdiv.style.left="57px"

for (i=0;i<dirnum;i++) {
mvn[i]=false
for (ii=0;ii<gbl;ii++) {
gb[i][ii].style.visibility="hidden"
}
}
gb[2][0].style.visibility="visible"

msg="<img src='sa.png' style='float:right' />Well done. Satan has finally been defeated. The treasure you found consisted of gold and silver, but it also had a super amazing weapon, which you apparently just used to kill Satan.<br /><br />To the right is a picture of one of Satan's former minions."
endtf=true
boxmsg()
}
}

function boxmsgb() {
bmdd.innerHTML=msg
}

function boxmsg() {
if (bcblo && bctm==1) bmd.style.display="block"
else if (!bcblo && bctm==100) {
bmdd.innerHTML=""
if (ft) {
document.onkeydown=mstart
document.onkeyup=mstop
ft=false
}
}
bmd.style.width=Math.round(440*bctm/100)+"px"
bmd.style.height=Math.round(200*bctm/100)+"px"
bmd.style.marginLeft=Math.round(-1*440*bctm/100/2)+"px"
bmd.style.marginTop=Math.round(-1*200*bctm/100/2)+"px"
if (bcblo && bctm<100 || !bcblo && bctm>1) {
if (bcblo) bctm++
else bctm--
bccgt=setTimeout("boxmsg()",1)
}
else {
if (bcblo) {
bcblo=false
boxmsgb()
}
else {
bcblo=true
bmd.style.display="none"
if (endtf) finalend()
}
}
}

function startmove(direction) {
clearTimeout(at)
clearTimeout(mt)
dir=direction
for (i=0;i<dirnum;i++) {
if (i!=dir) {
mvn[i]=false
for (ii=0;ii<gbl;ii++) {
gb[i][ii].style.visibility="hidden"
}
}
}
mvn[dir]=true
ani()
move()
}

function stopmove(direction) {
clearTimeout(at)
clearTimeout(mt)
dir=direction

gb[dir][0].style.visibility="visible"
for (i=1;i<gbl;i++) {
gb[dir][i].style.visibility="hidden"
}
s=1
mvn[dir]=false
}

function move() {
gl=gbdiv.offsetLeft
gt=gbdiv.offsetTop
nl=gl
nt=gt
ng=false
dt=false
end=false
//document.title=nl+" "+nt
if ((dir==3 && nt==objft || dir==2 && nt==objrt) && nl>objla && nl<objlb || (dir==1 && nl==objlb || dir==0 && nl==objla) && nt>objrt && nt<objft) dt=true;ng=false
if ((dir==3 && nt==nobjft || dir==2 && nt==nobjrt) && nl>nobjla && nl<nobjlb || (dir==1 && nl==nobjlb || dir==0 && nl==nobjla) && nt>nobjrt && nt<nobjft) dt=true;ng=false
if (nl>28 && nl<92 && nt>16 && nt<64) {dt=true;ng=false;end=true}
if (!dt) {
if (nt<objl) objd.style.zIndex="1"
else objd.style.zIndex="0"
if (nt<nobjl) nobjd.style.zIndex="1"
else nobjd.style.zIndex="0"
if (dir<2) {
ptml=dir*moveby*(-2)+moveby
nl=gl+ptml
if (nl>-4 && nl<580) ng=true
}
else {
ptmt=(dir-2)*moveby*(-2)+moveby
nt=gt+ptmt
if (nt>-4 && nt<308) ng=true
}
}
if (ng) {
gbdiv.style.left=nl+"px"
gbdiv.style.top=nt+"px"
}
else stopmove(dir)
if (end) gameover()
mt=setTimeout("move()",100)
}

function ani() {
for (i=0;i<gbl;i++) {
if (i==s) gb[dir][i].style.visibility="visible"
else gb[dir][i].style.visibility="hidden"
}
if (s<gbl-1) s++
else s=1
at=setTimeout("ani()",250)
}

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

if (kc==39 && !mvn[0]) startmove(0)
else if (kc==37 && !mvn[1]) startmove(1)
else if (kc==40 && !mvn[2]) startmove(2)
else if (kc==38 && !mvn[3]) startmove(3)
}

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

if (kc==39 && mvn[0]) stopmove(0)
else if (kc==37 && mvn[1]) stopmove(1)
else if (kc==40 && mvn[2]) stopmove(2)
else if (kc==38 && mvn[3]) stopmove(3)
}

function load() {
stylesheet=document.createElement("link")
stylesheet.rel="stylesheet"
stylesheet.type="text/css"
stylesheet.href="style.css"
head=document.getElementsByTagName("head")[0]
head.appendChild(stylesheet)

gameload()
}

function gameload() {
s=1
gbl=3
dirnum=4
dir=2
mt=false
at=false
moveby=4
bcblo=true
bctm=1
ft=true
goa=false
endtf=false

document.body.innerHTML=""

bmd=document.createElement("div")
bmd.id="bmd"
bmdd=document.createElement("div")
bmdd.id="bmdd"
bmdc=document.createElement("div")
bmdc.id="bmdc"
bmdc.innerHTML="OK"
bmdc.onclick=function(){boxmsg()}
bmd.appendChild(bmdd)
bmd.appendChild(bmdc)
document.body.appendChild(bmd)

msg="<img src='sq.png' style='margin-left:10px;display:block' />Welcome to Satan's Quest. This \"game\" is really simple. Navigate Gabruel the Angel with the arrow keys. You can't walk through objects (like a tree). Your goal is to find the treasure and terminate Satan once and for all. Your wit is your smartest weapon."
boxmsg()

game=document.createElement("div")
game.id="game"
game.style.backgroundImage="url(bg/1.png)"
objd=document.createElement("div")
objd.style.width="146px"
objd.style.height="220px"
objd.style.top="90px"
objd.style.left="247px"
objd.style.backgroundImage="url(bg/101.png)"
objdl=document.createElement("div")
objdl.style.width="146px"
objdl.style.height="220px"
objdl.style.top="90px"
objdl.style.left="247px"
objdl.style.zIndex="0"
objdl.style.backgroundImage="url(bg/102.png)"
objl=156
objft=168
objrt=148
objla=256
objlb=320

nobjd=document.createElement("div")
nobjd.style.width="74px"
nobjd.style.height="98px"
nobjd.style.top="47px"
nobjd.style.left="52px"
nobjd.style.backgroundImage="url(bg/111.png)"
nobjdl=document.createElement("div")
nobjdl.style.width="74px"
nobjdl.style.height="98px"
nobjdl.style.top="47px"
nobjdl.style.left="52px"
nobjdl.style.zIndex="0"
nobjdl.style.backgroundImage="url(bg/112.png)"
nobjl=16
nobjft=20
nobjrt=4
nobjla=4
nobjlb=116

gbdiv=document.createElement("div")
gbdiv.id="gb"
gbdiv.style.top="236px"
gbdiv.style.left="312px"
game.appendChild(objd)
game.appendChild(objdl)
game.appendChild(nobjd)
game.appendChild(nobjdl)
game.appendChild(gbdiv)

gb=new Array()
mvn=new Array()
for (i=0;i<4;i++) {
gb[i]=new Array()
for (ii=0;ii<3;ii++) {
gb[i][ii]=document.createElement("div")
gb[i][ii].id="gb"+i+""+ii+""
gbdiv.appendChild(gb[i][ii])
}
mvn[i]=false
}
document.body.appendChild(game)
}

window.onload=load