new Image().src="images/invader.gif";
new Image().src="images/invader2.gif";
new Image().src="images/invader3.gif";
new Image().src="images/invader4.gif";
new Image().src="images/explosion.gif";
new Image().src="images/explosion2.gif";


//^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ 
//^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ 
//^_^ ^_^ ^_^ ^_^   Copyright © javatwist.imingo.net - 2005   ^_^ ^_^ ^_^ ^_^  
//^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ 
//^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ 

var f, f1, canon, vies, para, cadreim, flag, flag2, r, r2, mort;
var tit, texte, texte2, timer, lettres, couleur, compt;

///////////////////////
// VARIABLES ONLOAD  //
///////////////////////
function variables(){
vies=document.getElementById('vies');
for(i=0;i<3;i++){
var vie=document.createElement('img');
vie.src="images/canon.gif";
vies.appendChild(vie)};
canon=document.getElementById('curs');
f1=document.getElementById('cadre2');
f=document.getElementById('cadre2').getElementsByTagName('div');
cadreim=document.getElementById('cadre3');
couleur='#ffffff'
compt=0;
texte="JSpace Invaders";
texte2="Game Over";
}

///////////////////////
// LANCEMENT "START" //
///////////////////////
function debut(bouton,e){
if(e.pageX){e.stopPropagation()}
else{window.event.cancelBubble=true}

if(mort){clearTimeout(mort)};
canon.src="images/canon.gif";
canon.style.top="276px";
while(tit.lastChild){tit.removeChild(tit.lastChild)}

clearTimeout(lettres);

flag=false;
flag2=false;
r=1;
r2=1;

while(cadreim.lastChild){cadreim.removeChild(cadreim.lastChild)}

for(j=0;j<15;j++){
new init(Math.random()*-200,Math.random()*600,15,'images/invader.gif',200,10,10)};
bouton.style.visibility='hidden';
}

/////////////////////
// PERTE D'UNE VIE //
/////////////////////
function perdre(){
canon.src="images/explosion2.gif";
canon.style.top="0px";
canon.style.left="200px";
canon.style.left="200px";
mort=setTimeout("canon.style.top='-200px'",2000);
flag=true;
vies.removeChild(vies.getElementsByTagName('img')[vies.getElementsByTagName('img').length-1]);
if(vies.getElementsByTagName('img').length!=0){
document.getElementById('depart').style.visibility='visible';}
else{
couleur='#ff0000';
logo(texte2);
while(cadreim.lastChild){cadreim.removeChild(cadreim.lastChild)};
}
}

//////////////////
// NOUVEL ALIEN //
//////////////////
function init(h,l,large,imag,delai,desc,sc,opt){
var haz;
var ex=document.createElement('img');
cadreim.appendChild(ex);
ex.src=imag;
this.bouge=bouge();

function bouge(){
if(h<320 && ex.src.search(imag)!=-1){h+=desc;}
else {if(imag!="images/invader4.gif"){
h=Math.random()*-200;l=Math.random()*600;ex.src=imag}
else if(!flag2){flag2=true;cadreim.removeChild(ex);};
};

if((h>=276)&&(h<300)&&(ex.src.search(imag)!=-1)&&(l-parseInt(canon.style.left)>=-15)&&(l-parseInt(canon.style.left)<=25)){
if(imag!="images/invader4.gif"){r=0;}else{r2=0};
};

if(h>0){
if(f.length!=0){
for(i=0;i<f.length;i++){
if((parseInt(f[i].style.top)<h+23)&&(f[i].style.display!='none')&&(ex.src.search("images/explosion.gif")==-1)&&(parseInt(f[i].style.left)-l>=5)&&(parseInt(f[i].style.left)-l<=20)&&(ex.src.search("images/invader4.gif")==-1)){
if(opt==true){para=opt;setTimeout("para=false",2000);};
ex.src="iimages/explosion.gif";f[i].style.display='none';compt+=sc;
if(compt%1000==0){init(Math.random()*-200,Math.random()*600,15,'images/invader4.gif',200,5)};
if(compt%300==0){init(Math.random()*-200,Math.random()*600,15,'images/invader2.gif',100,20,50)};
if(compt%400==0){init(Math.random()*-200,Math.random()*600,40,'images/invader5.gif',200,20,80)};
if(compt%500==0){init(Math.random()*-200,Math.random()*600,15,'images/invader3.gif',200,20,100,true)};
document.getElementById('total').firstChild.data=compt;
}}}};

haz=String(Math.random()-Math.random()).substring(0,1);
l+=parseInt(haz+Math.random()*large);
ex.style.left=l+"px";
ex.style.top=h+"px";

if(r==0){if(!flag){perdre()};
clearTimeout(timer)}
else{timer=setTimeout(bouge,delai)};

if(r2==0){if(!flag2){
flag2=true;
if(vies.getElementsByTagName('img').length<5){
var ange=document.createElement('img');
vies.appendChild(ange);
ange.src="images/canon.gif";
cadreim.removeChild(ex);
}}}

}
}

/////////////////
// NOUVEAU TIR //
/////////////////
function tir(n){
var feu=document.createElement('div');
feu.style.left=parseInt(canon.style.left)+17.5+"px";
for(i=0;i<3;i++){
var feu2=document.createElement('div');
feu.appendChild(feu2)}
f1.appendChild(feu);
feu.style.top=n+'px';
feu.style.position='absolute';
this.visee=visee();

function visee(){
if(n>-200){n-=10;feu.style.top=n+'px';setTimeout(visee,10)}
else{f1.removeChild(feu);};
}
}

//////////////////////////
// DEPLACEMENT DU CANON //
//////////////////////////
function glisse(e){
if((document.getElementById('curs').src.search('images/explosion2.gif')==-1) && (!para)){
if(e.pageX){
if(e.pageX<700 && e.pageX>100){
document.getElementById('curs').style.left=e.pageX-100-20;}}
else{
if(event.clientX<700 && event.clientX>100){
document.getElementById('curs').style.left=event.clientX-100-20;}}
}
}

/////////////////////////
// ANIMATION DE TEXTES //
/////////////////////////
function tourne(q,let){
var nd=document.createElement('span');
nd.style.color=couleur;
nd.appendChild(document.createTextNode(let));
tit.appendChild(nd);
this.tt=tt();

function tt(){
if(String.fromCharCode(q)!=let){
if(q<124){q++}else{q=32};
if(nd){
nd.firstChild.data=String.fromCharCode(q);};
}
lettres=setTimeout(tt,10)
}
}

///////////////////////////
// LANCEMENT D'ANIMATION //
///////////////////////////
function logo(quoi){
tit=document.getElementById('titre');
for(i=0;i<quoi.length;i++){
new tourne(32+Math.floor(Math.random()*91),quoi.charAt(i))}
}

