var Straf=20;
var NewVal=0;
var Speed=0;
var Km=0;
var Result=0;
var Time=0;
var id=null;
var id1=null;
var Game=0;
var NewCenter=0;
var Luft=9;
var C1=135;
var C2=70;
var C3=20;
var C4=90;


var ColorBridge = Array();
ColorBridge[0]='#CC0000';
ColorBridge[1]='#006666';
ColorBridge[2]='#FF0000';
ColorBridge[3]='#CC00CC';
ColorBridge[4]='#330066';
ColorBridge[5]='#000000';
ColorBridge[6]='#0000FF';
ColorBridge[7]='#003300';
ColorBridge[8]='#990000';
ColorBridge[9]='#999900';


function Init()
 {
 window.moveTo(0,0);
 window.resizeTo(screen.availWidth,screen.availHeight);
 }

function Begin()
{
    NewCenter=Desktop.offsetWidth/2;
    NewVal=Desktop.offsetWidth/2+90;
    i=Math.round(9*Math.random());
    document.all.Bridge.style.background=ColorBridge[i];

    document.all.MyCar.style.top=Desktop.offsetHeight/2-MyCar.offsetHeight/2;
    document.all.MyCar.style.left=Desktop.offsetWidth/2+90;
    document.all.Car1.style.top=Math.random()*1000;
    document.all.Car2.style.top=Math.random()*1000;
    document.all.Car3.style.top=Math.random()*1000;
    document.all.Car4.style.top=900;

   ReShow();

    Speed=0;
    clearTimeout(id);
    clearTimeout(id1);

    if (Result<0) { 
             Stop();
             window.alert('Game Over :-( ');
            }
    else {
            ShowTime();
            GoCars();
           }
}

function ReShow()
{
 document.all.Road.style.left=Desktop.offsetWidth/2-150;
 document.all.Line.style.left=Desktop.offsetWidth/2-5;
 document.all.Yellow1.style.left=Desktop.offsetWidth/2-160;
 document.all.Yellow2.style.left=Desktop.offsetWidth/2+150;
 document.all.Lines.style.left=Desktop.offsetWidth/2-5;
 document.all.Bridge.style.left=Desktop.offsetWidth/2-190;
 document.all.Car1.style.left=Desktop.offsetWidth/2-C1;
 document.all.Car2.style.left=Desktop.offsetWidth/2-C2;
 document.all.Car3.style.left=Desktop.offsetWidth/2+C3;
 document.all.Car4.style.left=Desktop.offsetWidth/2+C4;
 document.all.MyCar.style.left=Desktop.offsetWidth/2-NewCenter+MyCar.offsetLeft;
 document.all.MyCar.style.top=Desktop.offsetHeight/2-MyCar.offsetHeight/2;
 NewVal=Desktop.offsetWidth/2-NewCenter+NewVal;
 NewCenter=Desktop.offsetWidth/2;
}

function Go()
{
    Time=0;
    Km=0;
    Result=0;     
    Game=1;
    Begin();
}

function Stop()
{
    clearTimeout(id);
    clearTimeout(id1);
    Result=0;
    document.all.ResultShow.value=Result;
    Speed=0;
    document.all.SpeedShow.value=Speed/2;
    Time=0;
    document.all.TimeShow.value=Time;
    Km=0;
    document.all.KmShow.value=Math.round(Km*100)/100;
   Game=0;
}

function ReadKey()
{
 if (event.keyCode==37) {CarLeft();} //68
 if (event.keyCode==39) {CarRight();} //70
 if (event.keyCode==38 && Game==1) {UpSpeed();} //70
 if (event.keyCode==40 && Game==1) {DownSpeed();} //70
 event.returnValue=true;
}


function ShowTime() {
 Time=Time+1;
 document.all.TimeShow.value=Time;
 Km=Km+Speed/2/3600;
 document.all.KmShow.value=Math.round(Km*100)/100;
 Result=Result+Math.round(Speed/100);
 document.all.ResultShow.value=Result;
 id=setTimeout("ShowTime()",1000);
}

function PointInCar(PX,PY)
{ 
 if (PX>=MyCar.offsetLeft && PX<=MyCar.offsetLeft+MyCar.offsetWidth && PY>=MyCar.offsetTop && PY<=MyCar.offsetTop+MyCar.offsetHeight)
 {return 1;}
 else 
 {return 0;}
}

function GetUdar(El)
{
 var f2=0;
 if (PointInCar(El.offsetLeft,El.offsetTop)==1) {f2=1;}
 if (PointInCar(El.offsetLeft+El.offsetWidth,El.offsetTop)==1) {f2=1;}
 if (PointInCar(El.offsetLeft,El.offsetTop+El.offsetHeight)==1) {f2=1;}
 if (PointInCar(El.offsetLeft+El.offsetWidth,El.offsetTop+El.offsetHeight)==1) {f2=1;}
 return f2;
}


function GoCars()
{

T1=Lines.offsetTop+1*Math.round(Speed/10);
if (T1>Desktop.offsetHeight)
{
T1=-Lines.offsetHeight;
}
document.all.Lines.style.top=T1;


T2=Bridge.offsetTop+1*Math.round(Speed/10);
if (T2>Desktop.offsetHeight*2)
{
T2=-Bridge.offsetHeight-1000*Math.random();
i=Math.round(9*Math.random());
document.all.Bridge.style.background=ColorBridge[i];
}
document.all.Bridge.style.top=T2;


Temp1=Car1.offsetTop+Math.round(10*Math.random())+Math.round(Speed/10);
if (Temp1>Desktop.offsetHeight+Math.random()*1000)
{
Temp1=-Car1.offsetTop;
F=Math.round(12*Math.random());
document.all.PCar1.src='car1'+F+'.gif';
}
document.all.Car1.style.top=Temp1;

F=Math.random();
if (Car1.offsetLeft>Desktop.offsetWidth/2-C1-Luft && F<0.5) {document.all.Car1.style.left=Car1.offsetLeft-1;}
if (Car1.offsetLeft<Desktop.offsetWidth/2-C1+Luft && F>0.5) {document.all.Car1.style.left=Car1.offsetLeft+1;}


if (GetUdar(Car1)==1) 
{Result=Result-Straf;window.alert('Failure !');Begin();}

Temp2=Car2.offsetTop+Math.round(10*Math.random())+Math.round(Speed/10);
if (Temp2>Desktop.offsetHeight+Math.random()*1000)
{
Temp2=-Car2.offsetTop;
F=Math.round(12*Math.random());
document.all.PCar2.src='car1'+F+'.gif';}
document.all.Car2.style.top=Temp2;
F=Math.random();
if (Car2.offsetLeft>Desktop.offsetWidth/2-C2-Luft && F<0.5) {document.all.Car2.style.left=Car2.offsetLeft-1;}
if (Car2.offsetLeft<Desktop.offsetWidth/2-C2+Luft && F>0.5) {document.all.Car2.style.left=Car2.offsetLeft+1;}

if (GetUdar(Car2)==1) 
{Result=Result-Straf;window.alert('Failure !');Begin();}

Temp3=Car3.offsetTop-Math.round(10*Math.random())+Math.round(Speed/10);

if (Temp3<Desktop.offsetTop-Car3.offsetHeight)
{
Temp3=Desktop.offsetHeight+500*Math.random();
F=Math.round(12*Math.random());
document.all.PCar3.src='car2'+F+'.gif';}

if (Temp3>Desktop.offsetHeight+200)
{
Temp3=Desktop.offsetTop-Car3.offsetHeight-200*Math.random();
F=Math.round(12*Math.random());
document.all.PCar3.src='car2'+F+'.gif';
}

document.all.Car3.style.top=Temp3;
F=Math.random();
if (Car3.offsetLeft>Desktop.offsetWidth/2+C3-Luft && F<0.5) {document.all.Car3.style.left=Car3.offsetLeft-1;}
if (Car3.offsetLeft<Desktop.offsetWidth/2+C3+Luft && F>0.5) {document.all.Car3.style.left=Car3.offsetLeft+1;}


if (GetUdar(Car3)==1) 
{Result=Result-Straf;window.alert('Failure !');Begin();}


Temp4=Car4.offsetTop-Math.round(10*Math.random())+Math.round(Speed/10);

if (Temp4<Desktop.offsetTop-Car4.offsetHeight)
{
Temp4=Desktop.offsetHeight+600*Math.random();
F=Math.round(12*Math.random());
document.all.PCar4.src='car2'+F+'.gif';
}

if (Temp4>Desktop.offsetHeight+200)
{
Temp4=Desktop.offsetTop-Car4.offsetHeight-200*Math.random();
F=Math.round(12*Math.random());
document.all.PCar4.src='car2'+F+'.gif';
}

document.all.Car4.style.top=Temp4;
F=Math.random();
if (Car4.offsetLeft>Desktop.offsetWidth/2+C4-Luft && F<0.5) {document.all.Car4.style.left=Car4.offsetLeft-1;}
if (Car4.offsetLeft<Desktop.offsetWidth/2+C4+Luft && F>0.5) {document.all.Car4.style.left=Car4.offsetLeft+1;}

if (GetUdar(Car4)==1) 
{Result=Result-Straf;;window.alert('Failure !');Begin(); }

clearTimeout(id1);
if (Game==1) { id1=setTimeout("GoCars()",50); }
}

function CarLeft()
{
 if (Speed>0)
 {
  NewVal=NewVal-10;
  if (NewVal>Road.offsetLeft)
  {document.all.MyCar.style.left=NewVal;}
  else
  {NewVal=NewVal+10;}
 }
}

function CarRight()
{
 if (Speed>0)
 {
  NewVal=NewVal+10;
  if (NewVal+MyCar.offsetWidth< Road.offsetLeft+Road.offsetWidth)
  {document.all.MyCar.style.left=NewVal;}
  else
  {NewVal=NewVal-10;}
 }
}

function UpSpeed()
{
  Speed=Speed+1;
  document.all.SpeedShow.value=Speed/2;
}

function DownSpeed()
{ 
  if (Speed>0) {Speed=Speed-10;}
  if (Speed<0) {Speed=0;}  
  document.all.SpeedShow.value=Speed/2; 
}

  NewCenter=Desktop.offsetWidth/2;
  NewVal=Desktop.offsetWidth/2+90;
  document.all.MyCar.style.left=Desktop.offsetWidth/2+90;
  ReShow();


