//
// Duck Hunt like in Javascript
// by 3on
//


// get_bird_x , get_bird_y
// set_bird_x , set_bird_y
// kill_bird , bird_escape, show, hide, rand, bird_move

var Xmin = 20;
var Xmax = 95;
var Ymin = 20;
var Ymax = 110;
var vivant,xo,yo;
var vitesse = 1000;

function get_bird_x(id)
{
	var e = document.getElementById(id);
	return(parseInt(e.style.left));
} 
function get_bird_y(id)
{
	var e = document.getElementById(id);
	return(parseInt(e.style.top));
}

function set_bird_x(id, x)
{
	var e = document.getElementById('oiseau');
	e.style.left = x+"px";
}
function set_bird_y(id, y)
{
	var e = document.getElementById(id);
	e.style.top = y+"px";
}

function show(id)
{
	var e = document.getElementById(id);
	e.style.visibility = 'visible';
}
function hide(id)
{
	var e = document.getElementById(id);
	e.style.visibility = 'hidden';
}

function kill_bird(nb)
{
	if (vivant)
	{
	vivant = false;
	vitesse = vitesse - 50;
	
	var css = document.getElementById('oiseau');
	css = css.style;
	css.backgroundImage = "url('images/oiseau-mort.png')";
	// Chien
	var d = document.getElementById('dog');
	d.setAttribute('onclick', 'start("oiseau", this )')
	setTimeout("hide('oiseau')", 300)
	setTimeout("show('dog')", 800)
	}
}

function rand(ni, nf)
{
	return Math.floor( Math.random() * nf + ni);
}

function bird_move(nb)
{
	if(vivant){
	var e = document.getElementById('oiseau');
	show('oiseau');
	hide('start');
	hide('difficulte');
	e.setAttribute('onclick', 'shout("'+nb+'")');
	//var x2 = rand(Xmin / 10, Xmax / 10) * 10;
	var x2 = rand(Xmin, Xmax );
	var y2 = rand(Ymin, Ymax);
	var x1 = get_bird_x('oiseau');
	var y1 = get_bird_y('oiseau');
	xo = x1;
	yo = y1;
	
	//set_bird_x( 'oiseau',  rand(Xmin, Xmax));
	//set_bird_y( 'oiseau', rand(Ymin, Ymax));
	if(nb < 5){
		//nb = parseInt(nb) + 1;
		fly(x1,y1,x2,y2);
		setTimeout("bird_move("+nb+")", vitesse);}
	else{
		bird_escape();
	}
	}
	else{
		bird_escape();
	}

}

function bird_escape()
{
	if (vivant){
	var e = document.getElementById('oiseau');
	var f = document.getElementById('cible-fond');
	var d = document.getElementById('dog');
	e.style.backgroundImage = "url('images/oiseau-secasse.png')";
	e.style.width = "32px";
	e.style.width = "31px";
	f.style.backgroundImage = "url('images/fond-viseur-out.png')";
	d.style.backgroundImage = "url('images/dog-laugthing.png')";
	d.setAttribute('onclick', 'start("oiseau", this )')
	setTimeout("hide('oiseau')", 700);
	setTimeout("f.style.backgroundImage = \'url('images/fond-viseur.png')\'", 1000);
	setTimeout("show('dog')", 1500);
	}
}

function shout(nb)
{
	//var r = rand(1, 9);
	nb = parseInt(nb) + 1;
	/*if(nb == 4)
	{
		bird_escape(nb)
	}*/
	if(true)
	{
		kill_bird(nb);
	}
	else
	{
		bird_move(nb);
	}
}

function start(id, t)
{
	set_bird_x( id,  rand(Xmin, Xmax));
	set_bird_y( id, rand(Ymin, Ymax));
	hide('dog');
	hide('oiseau');
	
	var e = document.getElementById(id);
	e.style.backgroundImage = "url('images/oiseau-volant.png')";
	e.style.width = "27px";
	e.style.height = "31px";
	//e.setAttribute('onclick', 'bird_move(1)');
	t.setAttribute('onmouseover', '');
	
	var f = document.getElementById('cible-fond');
	f.style.backgroundImage = "url('images/fond-viseur.png')";
	
	var ci = document.getElementById('cible');
	ci.style.backgroundImage = "url('images/viseur-jeux.png')";
	//ci.setAttribute('onmouseout', 'stop()');
	
	var c = document.getElementById('dog');
	c.style.backgroundImage = "url('images/dog-catch.png')";
	hide('dog');
	c.setAttribute('onclick', '');
	
	var s = document.getElementById('start');
	s.style.backgroundImage = "url('images/start.png')";
	s.setAttribute('onclick', 'bird_move(0)');
	show('start');
	
	var dif = document.getElementById('difficulte');
	dif.innerHTML = 'difficulté: '+vitesse;
	show('difficulte');
	vivant = true;
}

function stop()
{
	var ci = document.getElementById('cible');
	ci.style.backgroundImage = "url('images/FTA-v.png')";
	ci.setAttribute('onmouseover', "start('oiseau', this)");
	hide('dog');
	hide('oiseau');
}

function fly(x1,y1,x2,y2)
{
	var a = (y2 - y1) / (x2 - x1);
	var b = y1 - x1 * a;
	
	xint = x1 + (x2 - x1)/10;
	yint = a * xint + b;
	
	set_bird_x('oiseau',xint);
	set_bird_y('oiseau',yint);
	if(
			((x2 >= xo) && (xint < x2) | (x2 < xo) && (xint >= x2)) && 
			((y2 >= yo) && (yint < y2) | (y2 < yo) && (yint >= y2)) && vivant ){
		setTimeout('fly('+xint+','+yint+','+x2+','+y2+')',9);
		alert('hy');
	}
}



