// Pri izdelavi slovenske razlicice je bil uporabljen vir http://www.24fun.com.
// Avtorja originalne igre: Urs Dudli in Peter Gehrig.
// Izdelava slovenske razlicice by Sergej Pogacnik

if (document.all) {

// povprecna hitrost sovraznih obektov. Visja vrednost je vecja hitrost.
var floatingspeed=1

// širina igralnega obmocja v pikslih
var bgpicturewidth=300

// širina igralnega obmocja v pikslih
var bgpictureheight=246

// odmik od levega robu za statusna sporocila, glede na igralno obmocje
var statustextdistanceleft=20

// odmik od zgornjega robu za statusna sporocila, glede na igralno obmocje
var statustextdistancetop=216

// odmik od levega robu za števec casa, glede na igralno obmocje
var timesigndistanceleft=220

// odmik od zgornjega robu za števec casa, glede na igralno obmocje
var timesigndistancetop=214

// odmik od levega robu za nastavitev stopnje, glede na igralno obmocje
var markerdistanceleft=0

// odmik od zgornjega robu za nastavitev stopnje, glede na igralno obmocje
var markerdistancetop=183

var x,y

var youlostgame=false
var youwongame=false
var gamehasstarted=false
var alreadycrashed=false
var test=false

// do not edit the variables below
// No, jaz sem dal vsem drugo sliko, da so vsi sovražniki enaki.
// Originalni vrstni red pa je bil: 1,2,3,4,5,1,2,3,4,5,1,2,2.
var your_image=new Array()
your_image[0]="igrica/dangerpic2.gif"
your_image[1]="igrica/dangerpic2.gif"
your_image[2]="igrica/dangerpic2.gif"
your_image[3]="igrica/dangerpic2.gif"
your_image[4]="igrica/dangerpic2.gif"
your_image[5]="igrica/dangerpic2.gif"
your_image[6]="igrica/dangerpic2.gif"
your_image[7]="igrica/dangerpic2.gif"
your_image[8]="igrica/dangerpic2.gif"
your_image[9]="igrica/dangerpic2.gif"
your_image[10]="igrica/dangerpic2.gif"
your_image[11]="igrica/dangerpic2.gif"
your_image[12]="igrica/dangerpic2.gif"

var statustextcontent=""
var tempo=20
var timer
var timetinmer
var starttime
var datumbin
var datum
var nowtime
var check_standtimer
var numberofimages=your_image.length-1
var stepx=new Array()
var stepy=new Array()
for (i=0;i<=numberofimages;i++) {
	stepx[i]=randommaker(floatingspeed)
	stepy[i]=randommaker(floatingspeed)
}
var imgwidth=new Array()
var imgheight=new Array()
for (i=0;i<=numberofimages;i++) {
	imgwidth[i]=10
	imgheight[i]=10
}

var marginleft=bgpictureposleft
var marginright=bgpictureposleft+bgpicturewidth
var margintop=bgpicturepostop+50
var marginbottom=bgpictureheight+bgpicturepostop-50
var timer
var spancontent=new Array()
var imgpreload=new Array()
for (i=0;i<=your_image.length;i++) {
	imgpreload[i]=new Image()
	imgpreload[i].src=your_image[i]
}

for (i=0;i<=numberofimages;i++) {
	spancontent[i]="<img src='"+your_image[i]+"' border='0'>"
}

function positiongame() {
	if (document.all) {
		document.all.bgpicture.style.posLeft=bgpictureposleft
		document.all.bgpicture.style.posTop=bgpicturepostop
		document.all.bgpicture.style.visibility="visible"
        document.all.statustextsign.style.posLeft=bgpictureposleft+statustextdistanceleft
		document.all.statustextsign.style.posTop=bgpicturepostop+statustextdistancetop
		document.all.rules.style.posLeft=bgpictureposleft
		document.all.rules.style.posTop=bgpicturepostop
        document.all.timesign.style.posLeft=bgpictureposleft+timesigndistanceleft
		document.all.timesign.style.posTop=bgpicturepostop+timesigndistancetop
		document.all.marker.style.posTop=bgpicturepostop+markerdistancetop
		document.all.marker.style.visibility="visible"
		statustext("Izberi poljubno stopnjo!")
		changelevel(1)
		
	}
	if (document.layers) {
		document.bgpicture.left=bgpictureposleft
		document.bgpicture.top=bgpicturepostop
		document.bgpicture.visibility="visible"
        document.statustextsign.left=bgpictureposleft+statustextdistanceleft
		document.statustextsign.top=bgpicturepostop+statustextdistancetop
		document.rules.left=bgpictureposleft
		document.rules.top=bgpicturepostop
		document.timesign.left=bgpictureposleft+timesigndistanceleft
		document.timesign.top=bgpicturepostop+timesigndistancetop
		statustext("Izberi poljubno stopnjo")
	}
}

function startgame() {
	resetvalues()
    startnewtime()
	statustext("Zacni!")
	setValues()
//	Tale stavek vpiše v polje "t1" cas (v neki binarni ali kakšni obliki...)
	document.forma.datum.value=datum


}

function startnewtime() {
	starttime= new Date()
	starttime=starttime.getTime()
// Za potrebe zapisa datuma igranja na strežnik nova spremenljivka za datum (datumbin je nek binarni zapis v milisekundah,
// datum pa je že formatiran datumbin v prijazno obliko (+1 je pac teba dati, zakaj: glej str. 211 v JavaScript knjigi)):
	datumbin= new Date()
	datum=datumbin.getDate() + "/" + (datumbin.getMonth() + 1) + "/" + datumbin.getYear()
	

    checktime()
}

function resetvalues() {
	youlostgame=false
	youwongame=false
	gamehasstarted=true
	alreadycrashed=false
	test=false
}

function setValues() {
	if (document.all) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan = eval("document.all.span"+i)
    		thisspan.innerHTML=spancontent[i]
			var thisspan = eval("document.all.span"+(i)+".style")
			var randomposleft=marginright-marginleft
			var startstoneleft=marginleft+randomposleft/3
			randomposleft=randomstartposition(randomposleft)
			randomposleft=randomposleft+marginleft	
			var randompostop=marginbottom-margintop
			randompostop=randomstartposition(randompostop)
			randompostop=randompostop+margintop	
			if (randomposleft<startstoneleft) {randomposleft+=(marginright-marginleft)/3}
			thisspan.posLeft=randomposleft
			thisspan.posTop=randompostop
			thisspan.visibility="visible"
		}
		for (i=0;i<=numberofimages;i++) {
			var thisspan = eval("document.all.span"+i)
			imgwidth[i]=thisspan.offsetWidth
			imgheight[i]=thisspan.offsetHeight
			
		}
		document.all.bgpicture.style.posLeft=bgpictureposleft
		document.all.bgpicture.style.posTop=bgpicturepostop
		document.all.bgpicture.style.visibility="visible"
        gamehasstarted=true
		checkmovement()
	}
	if (document.layers) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan=eval("document.span"+i+".document")
    		thisspan.write(spancontent[i])
			thisspan.close()
			var randomposleft=marginright-marginleft
			var startstoneleft=marginleft+randomposleft/3
			randomposleft=randomstartposition(randomposleft)
			randomposleft=randomposleft+marginleft	
			var randompostop=marginbottom-margintop
			randompostop=randomstartposition(randompostop)
			randompostop=randompostop+margintop	
			if (randomposleft<startstoneleft) {randomposleft+=(marginright-marginleft)/3}
			var thisspan=eval("document.span"+i)
			thisspan.left=randomposleft
			thisspan.top=randompostop
			thisspan.visibility="visible"
		}
		for (i=0;i<=numberofimages;i++) {
			var thisspan=eval("document.span"+i+".document")
			imgwidth[i]=thisspan.width
			imgheight[i]=thisspan.height
		}
		document.bgpicture.left=bgpictureposleft
		document.bgpicture.top=bgpicturepostop
		document.bgpicture.visibility="visible"
		gamehasstarted=true
    	checkmovement()
	}
}

function randommaker(range) {		
	rand=Math.floor(range*Math.random())
	if (rand==0) {rand=Math.ceil(range/2)}
    return rand
}

function randomstartposition(range) {		
	var randomposition=Math.floor(range*Math.random())
    return randomposition
}

function checkmovement() {
	if (document.all && youlostgame==false) {
		checkposition()
		movepictures()
    	timer=setTimeout("checkmovement()",tempo)
	}
	if (document.layers && youlostgame==false) {
		checkposition()
   		movepictures()
    	timer=setTimeout("checkmovement()",tempo)
	}
	else if (youlostgame) {
		lostgame()
	}
}

function movepictures() {
		if (document.all) {
			for (i=0;i<=numberofimages;i++) {  
    			var thisspan=eval("document.all.span"+(i)+".style")
    			thisspan.posLeft+=stepx[i]
				thisspan.posTop+=stepy[i]
    		}
		}
		if (document.layers) {
			for (i=0;i<=numberofimages;i++) {  
    			var thisspan = eval("document.span"+i)
    			thisspan.left+=stepx[i]
				thisspan.top+=stepy[i]
    		}
		}
}

function checkposition() {
	if (document.all) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan=eval("document.all.span"+i+".style")
			
			if (x>=thisspan.posLeft&&x<thisspan.posLeft+imgwidth[i]&&y>=thisspan.posTop&&y<thisspan.posTop+imgheight[i]) {
				youlostgame=true
			}
			if (thisspan.posLeft>marginright-imgwidth[i]) {
				thisspan.posLeft-=Math.abs(stepx[i]+1)
				stepx[i]=randommaker(floatingspeed)*-1	
			}
			if (thisspan.posLeft<marginleft) {
				thisspan.posLeft+=Math.abs(stepx[i])
				stepx[i]=randommaker(floatingspeed)			
			}	
			if (thisspan.posTop>marginbottom-imgheight[i]) {
				thisspan.posTop-=Math.abs(stepy[i])
				stepy[i]=randommaker(floatingspeed)*-1

			}
			if (thisspan.posTop<margintop) {
				thisspan.posTop+=Math.abs(stepy[i])
				stepy[i]=randommaker(floatingspeed)
			}
		}
	}
	if (document.layers) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan=eval("document.span"+i)
			if (x>=thisspan.left&&x<thisspan.left+imgwidth[i]&&y>=thisspan.top&&y<thisspan.top+imgheight[i]) {
				youlostgame=true
			}
			if (thisspan.left>marginright-imgwidth[i]) {
				thisspan.left-=Math.abs(stepx[i]+1)
				stepx[i]=randommaker(floatingspeed)*-1	
			}
			if (thisspan.left<marginleft) {
				thisspan.left+=Math.abs(stepx[i])
				stepx[i]=randommaker(floatingspeed)			
			}	
			if (thisspan.top>marginbottom-imgheight[i]) {
				thisspan.top-=Math.abs(stepy[i])
				stepy[i]=randommaker(floatingspeed)*-1

			}
			if (thisspan.top<margintop) {
				thisspan.top+=Math.abs(stepy[i])
				stepy[i]=randommaker(floatingspeed)
			}
		}
	}
}

function showrules() {
	if (document.all) {
		document.all.rules.style.visibility="visible"
	}
	if (document.layers) {
		document.rules.visibility="visible"
	}
}

function hiderules() {
	if (document.all) {
		document.all.rules.style.visibility="hidden"
	}
	if (document.layers) {
		document.rules.visibility="hidden"
	}
}

function testnatocka() {
	test=true
}


function wongame() {
// zato daš true, ker sicer po zmagi vseeno explodira, ce greš cez crto
youwongame=true
// Preverja, ce je miška šla cez testno obmocje na drugi polovici proge; if stavek (test=true je v funkciji testnatocka())
	if (test==true) {
	

	if (gamehasstarted && !youlostgame) {
		clearTimeout(timer)
        clearTimeout(timetimer)
		if (document.all) {
            statustext("Uspel si! Klikni ponovno Start!")
		}
		if (document.layers) {
            statustext("Uspel si! Klikni ponovno Start!")
		}

	}

//	To vrstico sem dodal, da polje "hidrezultat" v formi "forma" prejme vrednost variable "nowtime" (trenutni cas).
	document.forma.hidrezultat.value=nowtime
	}
	
	else if (gamehasstarted && youlostgame) {
		clearTimeout(timer)
        clearTimeout(timetimer)
		if (document.all) {
            statustext("Ne goljufaj")
		}
		if (document.layers) {
            statustext("Ne goljufaj")
		}
	}

}

function lostgame() {
if (!youwongame) {
	if (gamehasstarted) {
		youlostgame=true
		clearTimeout(timer)
        clearTimeout(timetimer)
		if (document.all) {
            statustext("Bum! Zacni znova!")
			showcrashsign()
		}
		if (document.layers) {
            statustext("Bum! Zacni znova!")
			showcrashsign()
		}
	}
}
}
function showcrashsign() {
	if (document.all && !alreadycrashed) {
		alreadycrashed=true
		crashsign.innerHTML="<img src='igrica/crash.gif'>"
		document.all.crashsign.style.visibility="visible"
		document.all.crashsign.style.posLeft=x-20
		document.all.crashsign.style.posTop=y-20
	}
	if (document.layers && !alreadycrashed) {
		alreadycrashed=true
		document.crashsign.document.write("<img src='igrica/crash.gif'>")
		document.crashsign.document.clear()
		document.crashsign.visibility="visible"
		document.crashsign.left=x-10
		document.crashsign.top=y
	}
	var crashtimer=setTimeout("hidecrashsign()",800)
}

function hidecrashsign() {
	if (document.all) {
		document.all.crashsign.style.visibility="hidden"
		hidestones()
	}
	if (document.layers) {
		document.crashsign.visibility="hidden"
		hidestones()
	}
}

function hidestones() {
	if (document.all && youlostgame) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan = eval("document.all.span"+(i)+".style")
			thisspan.visibility="hidden"
		}
	}
	if (document.layers && youlostgame) {
		for (i=0;i<=numberofimages;i++) {             
			var thisspan=eval("document.span"+i)
			thisspan.visibility="hidden"
		}
	}
}

function checktime() {
    if (gamehasstarted && !youlostgame) {
	    nowtime= new Date()
	    nowtime=nowtime.getTime()
        nowtime=(nowtime-starttime)/1000
        if (document.all) {
            timesign.innerHTML="<font size=2 face=Verdana color=red><B>"+nowtime+"</B></font>"
        }
        if (document.layers) {
            document.timesign.document.write("<font size=1 face=Verdana color=yellow>"+nowtime+"</font>")
            document.timesign.document.close()
        }
        timetimer=setTimeout("checktime()",200)
    }
    else {
        clearTimeout(timetimer)
    } 
}

function statustext(text) {
	statustextcontent="<font size=1 face=Verdana color=yellow>"+text+"</font>"
	if (document.all) {
		statustextsign.innerHTML=statustextcontent
	}
	if (document.layers) {
		document.statustextsign.document.write(statustextcontent)
		document.statustextsign.document.close()
	}
}

function changelevel(level) {
//	Vrsice sem spremenil v komentarje zato, da ne moreš spremeniti stopnje
//	if (level==1) {
		numberofimages=2;floatingspeed=4;document.all.marker.style.posLeft=bgpictureposleft+146;
//	}
//	if (level==2) {numberofimages=4;floatingspeed=6;document.all.marker.style.posLeft=bgpictureposleft+157;}
//	if (level==3) {numberofimages=6;floatingspeed=8;document.all.marker.style.posLeft=bgpictureposleft+168;}
//	if (level==4) {numberofimages=8;floatingspeed=10;document.all.marker.style.posLeft=bgpictureposleft+179;}
//	if (level==5) {numberofimages=12;floatingspeed=15;document.all.marker.style.posLeft=bgpictureposleft+189;}
	statustext("Klikni rumen krogec Start")
}


function handlerMM(e){
	x = (document.layers) ? e.pageX : document.body.scrollLeft+event.clientX
	y = (document.layers) ? e.pageY : document.body.scrollTop+event.clientY
	flag=1
}

if (document.layers){
	document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove = handlerMM;

document.write("<div id='bgpicture' style='position:absolute;visibility:hidden;left:0px;top:0px'>")
document.write("<IMG SRC='igrica/bgpicture.gif' USEMAP='#bgpicturemap' BORDER=0>")
document.write("<MAP NAME='bgpicturemap'>")
document.write("<AREA SHAPE=RECT COORDS='129,164,174,178' HREF='#' onClick='showrules()'>")
document.write("<AREA SHAPE=RECT COORDS='144,185,152,196' HREF='#' onClick='changelevel(1)'>")
document.write("<AREA SHAPE=RECT COORDS='153,185,162,195' HREF='#' onClick='changelevel(2)'>")
document.write("<AREA SHAPE=RECT COORDS='164,184,172,195' HREF='#' onClick='changelevel(3)'>")
document.write("<AREA SHAPE=RECT COORDS='175,185,182,196' HREF='#' onClick='changelevel(4)'>")
document.write("<AREA SHAPE=RECT COORDS='185,184,192,196' HREF='#' onClick='changelevel(5)'>")
//document.write("<AREA SHAPE=RECT COORDS='233,11,292,37' HREF='http://www.24fun.com' target='_blank'>")
document.write("<AREA SHAPE=POLY COORDS='1,1,0,198,299,198,298,1,280,63,285,71,283,80,279,91,271,102,276,134,273,160,265,176,252,187,231,188,209,174,203,157,215,132,227,118,228,111,209,110,188,91,171,78,152,80,146,85,146,93,158,97,179,100,191,107,198,122,191,142,172,152,154,151,135,137,128,131,105,130,101,135,100,156,88,173,67,183,41,183,24,172,16,146,21,125,36,111,46,108,64,105,70,99,66,84,50,77,33,82,30,85,21,87,15,77,19,67,41,60,60,60,78,67,89,89,87,107,80,116,65,124,48,126,34,137,34,153,44,165,70,164,83,151,85,129,94,116,114,111,134,113,149,124,160,133,170,133,178,126,168,115,143,113,127,97,131,71,154,61,179,60,195,70,210,88,214,90,234,92,245,99,246,120,233,142,222,155,222,159,239,169,248,166,256,152,256,124,251,96,254,92,264,80,264,69,270,62,278,60' HREF='#' onMouseOver='lostgame()'>")

document.write("<AREA SHAPE=POLY COORDS='20,69,27,69,32,73,33,78,26,84,22,85,17,81,18,73,22,69' HREF='#' OnClick='startgame()'>")
document.write("<AREA SHAPE=POLY COORDS='223,124,215,135,229,147,238,134' HREF='#'  onMouseOver='testnatocka()'>")
document.write("<AREA SHAPE=POLY COORDS='272,65,279,66,282,71,282,76,278,80,271,79,267,75,267,69,272,65,272,65' HREF='#'  onMouseOver='wongame()'>")
document.write("</MAP>")
document.write("</div>")
 
document.write("<div id='timesign' style='position:absolute;visibility:visible;left:0px;top:0px'>&nbsp;</div>")

document.write("<div id='rules' style='position:absolute;visibility:hidden;left:0px;top:0px'>") 
document.write("<table cellpadding=3 border=1><tr><td bgcolor=red>")
document.write("<font size=1 face=Verdana color=yellow>")
document.write("<b>PRAVILA:</b><br>")
document.write("- Klikni Start (rumen krogec levo)<br>")
document.write("- S kazalcem miske potuj po progi<br>")
document.write("- Poskusi doseci cilj (rumen krogec desno)<br>")
document.write("- Ne zaidi s proge<br>")
document.write("- Ne dotakni se zabic<br>")
document.write("<A HREF='javascript:hiderules()'><font size=1 face=Verdana color=yellow><b>Zapri</b></font></A>")
document.write("</font>")
document.write("</td></tr></table>")
document.write("</div>")

document.write("<div id='statustextsign' style='position:absolute;left:0px;top:0px'> </div>")

for (i=0;i<=numberofimages;i++) {
    document.write("<div id='span"+i+"' style='position:absolute;left:0px;top:0px'>&nbsp;</div>")
}

document.write("<div id='crashsign' style='position:absolute;visibility:hidden;left:0px;top:0px'><img src='igrica/crash.gif'></div>")

document.write("<div id='marker' style='position:absolute;visibility:hidden;left:0px;top:0px'><img src='igrica/marker.gif'></div>")

document.close()

onload=positiongame

}