// CREDITS:
// Survive: the winning game nobody can win.
// by Urs Dudli and Peter Gehrig 
// Copyright (c) 2001 Peter Gehrig and Urs Dudli. All rights reserved.
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.24fun.com/fast/index.html
// info@24fun.com
// 2/5/2001

// IMPORTANT: 
// If you add this script to a script-library or a script-archive 
// you have to insert a link to http://www.24fun.com/fast/index.html 
// right into the webpage where the script will be displayed.

// CONFIGURATION:
// Go to http://www.24fun.com/fast/index.html, open category 'game' and 
// download the ZIP-file of this script containing
// the script-file with step-by-step instructions for easy configuration as well as
// all gameimages.


// average speed of the flying deathstones. Higher means faster
var floatingspeed=8

// width of gamezone (pixels)
var bgpicturewidth=442

// height of gamezone (pixels)
var bgpictureheight=280

// leftposition of sttusmessages in relation to gamezone
var statustextdistanceleft=307

// topposition of statusmessages in relation to gamezone
var statustextdistancetop=110

// leftposition of timesign in relation to backgroundpicture
var timesigndistanceleft=356

// topposition of timesign in relation to backgroundpicture
var timesigndistancetop=214

// topposition of levelbox in relation to backgroundpicture
var leveldistancetop=173

// leftposition of levelbbox in relation to backgroundpicture
var leveldistanceleft=356

// leftposition of levelmarker in relation to backgroundpicture
var markerdistanceleft=0

// topposition of levelmarker in relation to backgroundpicture
var markerdistancetop=183

// horizontal startposition of surviver 
var xsurviverstart=163+bgpictureposleft

// vertical startposition of surviver 
var ysurviverstart=137+bgpicturepostop

// horizontal difference of surviver to the left position of cursor
var xdifftocursor=160

// vertical distance of surviver to top position of cursor
var ydifftocursor=35

// horizontal difference of surviver to the left position of cursor
var xsurviver=xsurviverstart

// vertical distance of surviver to top position of cursor
var ysurviver=ysurviverstart

// do not edit the variables below
var surviverimgwidth
var surviverimgheight
var x,y
var youlostgame=false
var gamehasstarted=false
var alreadycrashed=false

var shotimg=new Array("shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif", "shot.gif")
var statustextcontent=""

var tempo=10

var timer
var timetimer
var setleveltimer

var starttime
var nowtime
var check_standtimer
var numberofimages=shotimg.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]=3
	imgheight[i]=3
}
var marginleft=bgpictureposleft+47
var marginright=bgpictureposleft+289
var margintop=bgpicturepostop+49
var marginbottom=bgpictureheight+bgpicturepostop-49

var spancontent=new Array()
var imgpreload=new Array()
for (i=0;i<=shotimg.length;i++) {
	imgpreload[i]=new Image()
	imgpreload[i].src=shotimg[i]
}

for (i=0;i<=numberofimages;i++) {
	spancontent[i]="<img src='"+shotimg[i]+"'>"
}

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.statustextshadow.style.posLeft=bgpictureposleft+statustextdistanceleft+1
		document.all.statustextshadow.style.posTop=bgpicturepostop+statustextdistancetop+1
        document.all.timesign.style.posLeft=bgpictureposleft+timesigndistanceleft
		document.all.timesign.style.posTop=bgpicturepostop+timesigndistancetop
        document.all.timeshadow.style.posLeft=bgpictureposleft+timesigndistanceleft+1
		document.all.timeshadow.style.posTop=bgpicturepostop+timesigndistancetop+1
		document.all.level.style.posLeft=bgpictureposleft+leveldistanceleft
		document.all.level.style.posTop=bgpicturepostop+leveldistancetop
        document.all.levelshadow.style.posLeft=bgpictureposleft+leveldistanceleft+1
		document.all.levelshadow.style.posTop=bgpicturepostop+leveldistancetop+1
        document.all.levelfade.style.posLeft=marginleft+90
		document.all.levelfade.style.posTop=margintop+22
		statustext("Cliquez sur Start !")
	}
}

function startgame() {
	resetvalues()
    startnewtime()
	statustext("Survive!")
	setValues()
}

function startnewtime() {
	starttime= new Date()
	starttime=starttime.getTime()
    checktime()
}

function resetvalues() {
	youlostgame=false
	gamehasstarted=true
	alreadycrashed=false
	clearTimeout(setleveltimer)
	clearTimeout(timer)
    clearTimeout(timetimer)
}

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
			thisspan.posLeft=marginleft-8
			thisspan.posTop=margintop-8
			thisspan.visibility="visible"
		}
		for (i=0;i<=numberofimages;i++) {
			var thisspan = eval("document.all.span"+i)
			imgwidth[i]=thisspan.offsetWidth
			imgheight[i]=thisspan.offsetHeight
			
		}
        surviverimgwidth=document.all.surviver.offsetWidth
        surviverimgheight=document.all.surviver.offsetHeight
		document.all.bgpicture.style.posLeft=bgpictureposleft
		document.all.bgpicture.style.posTop=bgpicturepostop
		document.all.bgpicture.style.visibility="visible"
		document.all.surviver.style.posLeft=xsurviver
		document.all.surviver.style.posTop=ysurviver
		document.all.surviver.style.visibility="visible"
        gamehasstarted=true
		numberofimages=1
		setlevel()
		checkmovement()
	}
}

function setlevel() {
	if (numberofimages<=20) {
		level.innerHTML="<font size=1 face=Verdana color=yellow>NIVEAU: "+numberofimages+"</font>"
        levelshadow.innerHTML="<font size=1 face=Verdana color=red>NIVEAU: "+numberofimages+"</font>"
        levelfade.innerHTML="<font face='Arial Black' color=black>"+numberofimages+"</font>"
		numberofimages++
		setleveltimer=setTimeout("setlevel()",4000)
	}
	
	else {
		clearTimeout(setleveltimer)
		wongame()
	}
}

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)
	}
	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]
    		}
		}
}

function checkposition() {
	if (document.all) {
		var surviverimgleft=xsurviver
		var surviverimgright=xsurviver+surviverimgwidth
		var surviverimgtop=ysurviver
		var surviverimgbottom=ysurviver+surviverimgheight
		for (i=0;i<=numberofimages;i++) {             
			var thisspan=eval("document.all.span"+i+".style")
			
			if (thisspan.posLeft>=surviverimgleft&&thisspan.posLeft<surviverimgright&&thisspan.posTop>=surviverimgtop&&thisspan.posTop<surviverimgbottom) {
				youlostgame=true
				break;
			}
			if (thisspan.posLeft>marginright-imgwidth[i]) {
				thisspan.posLeft-=Math.abs(stepx[i]+1)
				var newrange=marginbottom-margintop
				thisspan.posTop=randommaker(newrange)+margintop
				stepx[i]=randommaker(floatingspeed)*-1	
			}
			if (thisspan.posLeft<marginleft) {
				thisspan.posLeft+=Math.abs(stepx[i])
				var newrange=marginbottom-margintop
				thisspan.posTop=randommaker(newrange)+margintop
				stepx[i]=randommaker(floatingspeed)			
			}	
			if (thisspan.posTop>marginbottom-imgheight[i]) {
				thisspan.posTop-=Math.abs(stepy[i])
				var newrange=marginright-marginleft
				thisspan.posLeft=randommaker(newrange)+marginleft
				stepy[i]=randommaker(floatingspeed)*-1

			}
			if (thisspan.posTop<margintop) {
				thisspan.posTop+=Math.abs(stepy[i])
				var newrange=marginright-marginleft
				thisspan.posLeft=randommaker(newrange)+marginleft
				stepy[i]=randommaker(floatingspeed)
			}
		}
	}
}

function lostgame() {
	if (gamehasstarted) {
		clearTimeout(setleveltimer)
		clearTimeout(timer)
        clearTimeout(timetimer)
        youlostgame=true
    	statustext("Crash! Recommencez!")
		showcrashsign()
		document.all.surviver.style.visibility="hidden"
	}
}

function wongame() {
	if (gamehasstarted) {
		clearTimeout(setleveltimer)
		clearTimeout(timer)
        clearTimeout(timetimer)
        youlostgame=true
		statustext("Ah bon! CLIQUEZ «START»!")
		document.all.surviver.style.visibility="hidden"
		hidestones()
	}
}

function showcrashsign() {
	if (document.all && !alreadycrashed) {
		alreadycrashed=true
		crashsign.innerHTML="<img src='crash.gif'>"
		document.all.crashsign.style.posLeft=xsurviver-25
		document.all.crashsign.style.posTop=ysurviver-25
		document.all.crashsign.style.visibility="visible"
	}
	var crashtimer=setTimeout("hidecrashsign()",800)
}

function hidecrashsign() {
	if (document.all) {
		document.all.crashsign.style.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 && document.all) {
	    nowtime= new Date()
	    nowtime=nowtime.getTime()
        nowtime=(nowtime-starttime)/1000
        timesign.innerHTML="<font size=1 face=Verdana color=yellow>"+nowtime+"</font>"
        timeshadow.innerHTML="<font size=1 face=Verdana color=red>"+nowtime+"</font>"
        timetimer=setTimeout("checktime()",200)
    }
    else {
        clearTimeout(timetimer)
    } 
}

function statustext(text) {
	statustextsign.innerHTML="<font size=2 face='Arial Black' color=yellow>"+text+"</font>"
	statustextshadow.innerHTML="<font size=2 face='Arial Black' color=red>"+text+"</font>"
}



function handlerMM(e){
	x = (document.layers) ? e.pageX : document.body.scrollLeft+event.clientX
	y = (document.layers) ? e.pageY : document.body.scrollTop+event.clientY
	xsurviver=x-xdifftocursor
	ysurviver=y-ydifftocursor
	if (xsurviver>(marginright-surviverimgwidth)) {xsurviver=marginright-surviverimgwidth}
	if (xsurviver<(marginleft)) {xsurviver=marginleft}
	if (ysurviver>(marginbottom-surviverimgheight)) {ysurviver=marginbottom-surviverimgheight}
	if (ysurviver<(margintop)) {ysurviver=margintop}
	if (gamehasstarted) {
		document.all.surviver.style.posLeft=xsurviver
		document.all.surviver.style.posTop=ysurviver
	}
}

if (document.all){
	document.onmousemove = handlerMM;
}

if (document.all) {
document.write("<div id='bgpicture' style='position:absolute;visibility:hidden;left:0px;top:0px'>")
document.write("<IMG SRC='bgallin.gif' USEMAP='#bgpicturemap' BORDER=0>")
document.write("<MAP NAME='bgpicturemap'>")
document.write("<AREA SHAPE=RECT COORDS='310,88,435,102' HREF='http://www.24fun.com' target='_blank'>")
document.write("<AREA SHAPE=RECT COORDS='307,172,328,232' HREF='#' onClick='startgame()'>")
document.write("</MAP>")
document.write("</div>")
 
document.write("<div id='timeshadow' style='position:absolute;visibility:visible;left:0px;top:0px'></div>")
document.write("<div id='timesign' style='position:absolute;visibility:visible;left:0px;top:0px'></div>")

document.write("<div id='levelshadow' style='position:absolute;left:0px;top:0px'>&nbsp;</div>")
document.write("<div id='level' style='position:absolute;left:0px;top:0px'>&nbsp;</div>")

document.write("<DIV id='levelfade' style='position:absolute;filter:alpha(opacity=30);font-size:70pt'></DIV>")

document.write("<div id='statustextshadow' style='position:absolute;left:0px;top:0px;width:120px'> </div>")
document.write("<div id='statustextsign' style='position:absolute;left:0px;top:0px;width:120px'> </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='marker' style='position:absolute;visibility:hidden;left:0px;top:0px'><img src='shot.gif'></div>")

document.write("<div id='surviver' style='position:absolute;filter:alpha(opacity=80);visibility:hidden;left:0px;top:0px'><img src='surviver.gif' width='30'></div>")

document.write("<div id='crashsign' style='position:absolute;visibility:hidden;left:0px;top:0px'><img src='crash.gif'></div>")

document.close()

onload=positiongame
}
