// CREDITS:
// Darts  
// by P.M. Achterberg
// p.m.achterberg@wanadoo.nl, http://turn.to/pat
// featured by http://www.24fun.com
// 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

// IMPORTANT: 
// If you add this script to a script-library or a script-archive 
// you have to insert a link to http://turn.to/pat
// right into the webpage where the script will be displayed.

// CONFIGURATION:
// Go to http://www.24fun.com, open category 'game' and 
// download the ZIP-file of this script containing
// all imgaes, sounds and the script-file with step-by-step instructions.

document.write("<bgsound src='#' id=music loop=1 autostart='true'>")
document.write("<div id='but301' style='position: absolute; top:10px; left:435px'>")
document.write("<a href='javascript:startgame(301)'><img src='images/301.gif' border=0></a></div>")
document.write("<div id='but501' style='position: absolute; top:10px; left:515px'>")
document.write("<a href='javascript:startgame(501)'><img src='images/501.gif' border=0></a></div>")
document.write("<div id='tekst' style='position: absolute; top:52px; left:90px'>")
document.write("<form name='message'>")
document.write("<input type='text' value='' name='waarde' size=30 STYLE='color: CC0000; background: FFFF66; font-family: Arial; font-size: 12pt;'>")
document.write("</form>")
document.write("</div>")
document.write("<DIV id='logo' style='position: absolute; top:5px; left:60px'>")
document.write("<img src='images/logo.gif'>")
document.write("</div>")
document.write("<DIV id='res' style='position: absolute; top:425px; left:470px'>")
document.write("<a href='javascript:reload()'><img src='images/restart.gif' border=0></a>")
document.write("</div>")
document.write("<DIV id='dartbord' style='position: absolute; top:80px; left:40px'>")
document.write("<img src='images/dartbord.jpg'>")
document.write("</div>")
document.write("<DIV id='scorebord' style='position: absolute; top:190px; left:440px'>")
document.write("<img src='images/scorebord.jpg'>")
document.write("</div>")
document.write("<table border=0><tr><td width=20><DIV ID='ticker1' style='position:absolute; top:205px; left:470px'></DIV></td></tr></table>")
document.write("<table border=0><tr><td width=20><DIV ID='ticker2' style='position:absolute; top:205px; left:525px'></DIV></td></tr></table>")
document.write("<DIV id='plaats1' style='position: absolute; top:150px; left:400px; visibility:hidden'>")
document.write("<img src='images/plaats.gif' name='pijl1'>")
document.write("</div>")
document.write("<DIV id='plaats2' style='position: absolute; top:150px; left:400px; visibility:hidden'>")
document.write("<img src='images/plaats.gif' name='pijl2'>")
document.write("</div>")
document.write("<DIV id='plaats3' style='position: absolute; top:150px; left:400px; visibility:hidden'>")
document.write("<img src='images/plaats.gif' name='pijl3'>")
document.write("</div>")
document.write("<DIV id='bal' style='position: absolute; top:40px; left:440px'>")
document.write("<a href='javascript:StopBall()'><img src='images/vierkant.gif' border=0></a>")
document.write("</div>")
document.write("<DIV id='uitleg' style='position: absolute; top:20px; left:610px'>")
document.write("<table border=0><tr><td width=140 align='center'>")
document.write("<font face='Comic Sans MS' color='FFFF66' size=3>Cómo Jugar...<br><hr>")
document.write("<font size=2>Haz click en el botón del juego que quieres jugar, 301 ó 501.<br>Después deja que el puntero de arriba se mueva. Haz click sobre el botón cuando el puntero esté donde quieres. Haz lo mismo con el de la derecha. Al hacer click en el botón, saldrá el dardo. Tienes que terminar con un doble<br><br> Los rectángulos de fuera valen doble, los de dentro, triple. El centro rojo vale 25 puntos y el negro 50.</font></font>")
document.write("</td></tr></table>")
document.write("</div>")
document.write("<div id='point1' STYLE='position:absolute;visibility:visible;'>")
document.write("<img src='images/schuif.gif' width=15 height=15 alt='' border='0'>")
document.write("</div>")
document.write("<div id='point2' STYLE='position:absolute;visibility:visible; top:100px; left:335px'>")
document.write("<img src='images/schuif.gif' width=15 height=15 alt='' border='0'>")
document.write("</div>")

var XX1=568; // X position of the scrolling objects
var XX2=45;
var xstep=1;
var delay_time=10;
var delay_pijl=40;
var stoppen1=false;
var stoppen2=false;
var pijlen=1;
var pijlscore=0;
var pijl1=0;
var pijl2=0;
var pijl3=0;
var info = '';
var info2= '';
var YY1=60;  
var top1=50
var bottom1=170; // Vaste waarde grootte van amplitude
var yon1=0;  // Naar boven of naar beneden
var YY2=560;  
var top2=440;
var bottom2=560; // Vaste waarde grootte van amplitude
var yon2=0;  // Naar boven of naar beneden
var pijltel = 0;
var ZZ1=0;
var ZZ2=0;
var beurt=0;
var gamerun=false;
var krijt = new Array();
var pijltjes = new Array();

for (i=0;i<11;i++) {
  pijltjes[i] = new Image()
  pijltjes[i].src = 'images/'+ i + '.gif'
}

var ns4=document.layers?1:0
var ie=document.all?1:0
var ns6=document.getElementById&&!document.all?1:0

if(ie){
   point1.style.top=YY1;
   point1.style.left=XX1;
   point2.style.top=XX2;
   point2.style.left=bottom2;
} else { alert('Internet Explorer only !') }

function reloc2() {
  if(yon1==0){YY1=YY1-xstep;}
  else{YY1=YY1+xstep;}
		
  if(YY1<top1){yon1=1;YY1=top1;}
  if(YY1>=bottom1){yon1=0;YY1=bottom1;}
  point1.style.left=XX1;
  point1.style.top=YY1+document.body.scrollTop;
}

function reloc1() {
  if(yon2==0){YY2=YY2-xstep;}
  else{YY2=YY2+xstep;}
		
  if(YY2<top2){yon2=1;YY2=top2;}
  if(YY2>=bottom2){yon2=0;YY2=bottom2;}
  point2.style.top=XX2;
  point2.style.left=YY2+document.body.scrollTop;
}

function loopfunc1() {
  if (stoppen1) { return }
  reloc1();
  setTimeout("loopfunc1()",delay_time);
}

function loopfunc2() {
  if (stoppen2) { return }
  reloc2();
  setTimeout("loopfunc2()",delay_time);
}

function animeer1() {
  document.pijl1.src = pijltjes[pijltel].src
  pijltel++;
  if (pijltel < 11) { setTimeout("animeer1()",delay_pijl) }
}
function animeer2() {
  document.pijl2.src = pijltjes[pijltel].src
  pijltel++;
  if (pijltel < 11) { setTimeout("animeer2()",delay_pijl) }
}
function animeer3() {
  document.pijl3.src = pijltjes[pijltel].src
  pijltel++;
  if (pijltel < 11) { setTimeout("animeer3()",delay_pijl) }
}

function StopBall() {
if (!gamerun) { alert('Choose a game first!'); return }
  ZZ1 = YY1-110;
  ZZ2 = YY2-500;
  if (stoppen1) {
    stoppen2 = true;
    stoppen1 = false;
    if (pijlen == 1) {
      plaats1.style.visibility='visible';
      plaats1.style.top=210+(ZZ1*3); //midden top=210, left=187 waardenYY: top50-170 left440-560
      plaats1.style.left=187+(ZZ2*3);
      pijltel = 0;
      animeer1();
      setTimeout("geluid(1)",400);
    }
    if (pijlen == 2) {
      plaats2.style.visibility='visible';
      plaats2.style.top=208+(ZZ1)*3; //midden top=210, left=187 waardenYY: top50-170 left440-560
      plaats2.style.left=187+(ZZ2*3);
      pijltel = 0;
      animeer2();
      setTimeout("geluid(2)",400);
    }
    if (pijlen == 3) {
      plaats3.style.visibility='visible';
      plaats3.style.top=208+(ZZ1*3); //midden top=210, left=187 waardenYY: top50-170 left440-560
      plaats3.style.left=187+(ZZ2*3);
      pijltel = 0;
      animeer3();
      setTimeout("geluid(3)",400);
    }
    pijlen++;
    if (pijlen == 4) {
      setTimeout("opnieuw()",2000);
    } else {
      setTimeout("loopfunc1()",1000);
    }
  }
  else {
    stoppen1 = true;
    stoppen2 = false;
    loopfunc2();
  }
}

function score(pl) {
  var hoek=0;
  var factor=1;
  pijlscore=0;

  hoek=Math.atan2(ZZ2,ZZ1);
  if ((hoek<= 2.9800497297791683 )&&(hoek>2.671409189943056)) {pijlscore=1}
  if ((hoek<= 2.671409189943056  )&&(hoek>2.345778200261375)) {pijlscore=18}
  if ((hoek<= 2.345778200261375  )&&(hoek>2.040979790441634)) {pijlscore=4}
  if ((hoek<= 2.040979790441634  )&&(hoek>1.7323392506055215)) {pijlscore=13}
  if ((hoek<= 1.7323392506055215 )&&(hoek>1.4092534029842718)) {pijlscore=6}
  if ((hoek<= 1.4092534029842718 )&&(hoek>1.1006128631481593)) {pijlscore=10}
  if ((hoek<= 1.1006128631481593 )&&(hoek>0.7853981633974483)) {pijlscore=15}
  if ((hoek<= 0.7853981633974483 )&&(hoek>0.47018346364673724)) {pijlscore=2}
  if ((hoek<= 0.47018346364673724)&&(hoek>0.16154292381062482)) {pijlscore=17}
  if ((hoek<= 0.16154292381062482)&&(hoek>-0.1398223687841933)) {pijlscore=3}
  if ((hoek<=-0.1398223687841933 )&&(hoek>-0.4570687565477896)) {pijlscore=19}
  if ((hoek<=-0.4570687565477896 )&&(hoek>-0.7749818734664786)) {pijlscore=7}
  if ((hoek<=-0.7749818734664786 )&&(hoek>-1.0939044591293614)) {pijlscore=16}
  if ((hoek<=-1.0939044591293614 )&&(hoek>-1.4068671609708354)) {pijlscore=8}
  if ((hoek<=-1.4068671609708354 )&&(hoek>-1.734725492618958)) {pijlscore=11}
  if ((hoek<=-1.734725492618958  )&&(hoek>-2.0476881944604317)) {pijlscore=14}
  if ((hoek<=-2.0476881944604317 )&&(hoek>-2.356194490192345)) {pijlscore=9}
  if ((hoek<=-2.356194490192345  )&&(hoek>-2.684523897042004)) {pijlscore=12}
  if ((hoek<=-2.684523897042004  )&&(hoek>-2.9872738776636742)) {pijlscore=5}
  if ((hoek<=-2.9872738776636742 )||(hoek>2.9800497297791683)) {pijlscore=20}
  a=Math.sqrt((ZZ2*ZZ2*9)+(ZZ1*ZZ1*9))
  if (a<=6) { factor=2; }
  if (a<=13) { pijlscore=25; }
  if ((a>=77)&&(a<=85)) { factor=3 }
  if ((a>=128)&&(a<=136)) { factor=2 }
  if (a>136) { factor=0 }
  info='';
  if (factor==2) { info += 'Doble ';  }
  if (factor==3) { info += 'Triple ';  }
  if (factor==0) { info += 'Fuera...'; }
  else { info += pijlscore }
  if ((a>6)&&(a<=13)) { info = 'Centro rojo' }
  if (a<=6) { info = '¡ Centro negro !' }
  pijlscore = pijlscore*factor;
  totaalscore = totaalscore-pijlscore;
  if ((totaalscore < 0)||(totaalscore==1)) {
    totaalscore = totaalscore+pijlscore+pijl1+pijl2;
    info += ' - No vale, demasiados puntos...';
    leeg();
    pl=3;
  } else {
    if (totaalscore==0) {
      if (factor != 2) {
        totaalscore = totaalscore+pijlscore+pijl1+pijl2;
        info += ' - No vale, no hiciste un doble...'
        pl=3;
        leeg();
      } else {
        ap = (beurt*3) + pl;
        info = '¡¡¡Ganaste!!!\nHas necesitado '+ap+' dardos ';
        document.message.waarde.value=info;
	beurt++;
	krijt[beurt] = 0;
	bord();
        info += '\n\nHaz click en Aceptar para otra partida...';
	alert(info);
	window.location.reload(true);
      }
    } else {
      info += ' ('+totaalscore+' left)';
    }
  }
  document.message.waarde.value=info;
  if (pl == 1) { pijl1 = pijlscore }
  if (pl == 2) { pijl2 = pijlscore }
  if (pl == 3) {
    beurt++
    pijl3 = pijlscore;
    beurtscore = pijl1+pijl2+pijl3;
    if (beurtscore==180) { document.all.music.src="sounds/180.wav" }
    info2 += '\n' +totaalscore;
    krijt[beurt]=totaalscore;
    bord();
  }
}

function leeg() {
      pijl1 = 0;
      pijl2 = 0;
      pijl3 = 0;
      pijlscore=0;
      pijlen=1;
      pl=0;
      plaats1.style.visibility='hidden';
      plaats2.style.visibility='hidden';
      plaats3.style.visibility='hidden';
}

function opnieuw() {
  leeg();
  loopfunc1();
}

function geluid(pl) {
  document.all.music.src="sounds/plok.wav";
  score(pl);
 }

function bord() {
// """""""""""""""""""""*
	b0=0;
	b1=beurt;
	b2=0;
	if (beurt > 8) { b1=8; b2=beurt }
	if (beurt > 17) { b0=beurt-17; b1=beurt-9; b2=beurt }
	messagecontent1='<span style="position:relative;font-family:Arial;color:CC0000;font-size:14pt;font-weight:3">'	
	messagecontent1+='<font color="000000"><i>'
	for (i=b0;i<=b1;i++) {
          if (i!=beurt) { messagecontent1+='<strike>' }
	  messagecontent1+=krijt[i]+' ';
          if (i!=beurt) { messagecontent1+='</strike>' }
	}
	messagecontent1+='</i></font>'
	messagecontent1+='</span>'
	ticker1.innerHTML=messagecontent1 
   if (b2 > 0) {
	messagecontent2='<span style="position:relative;font-family:Arial;color:CC0000;font-size:14pt;font-weight:3">'	
	messagecontent2+='<font color="000000"><i>'
	for (i=b1+1;i<=b2;i++) {
          if (i!=beurt) { messagecontent2+='<strike>' }
	  messagecontent2+=krijt[i]+' ';
          if (i!=beurt) { messagecontent2+='</strike>' }
	}
	messagecontent2+='</i></font>'
	messagecontent2+='</span>'
	ticker2.innerHTML=messagecontent2
   }
// """""""""""""""""""""*
}

function startgame(spel) {
  if (gamerun) { return }
  gamerun=true;
  info2=spel
  krijt[0]=info2;
  bord()
  if (info2 == '301') { totaalscore = 301 }
  if (info2 == '501') { totaalscore = 501 }
  loopfunc1();
}

function reload() {
  results = confirm("¿Quieres empezar de nuevo?");
  if (results=='1') { window.location.reload(true) }
}
