
var bx = 0;
var by = 0;
var incx = 5;
var incy = 1;
var stop=true;

function updateBack() {
	
	bx+= incx;
	by+= incy;
	if (bx>=110)
		bx -=110;
	if (by>=110)
		by -=110;
	document.body.style.backgroundPosition= bx+"px "+by+"px";
	loopBackground();
}

function loopBackground() {
	
	if (!stop)
		setTimeout(function() {	return updateBack.apply(null, []);}, 50);
}

function launchLoopBack(a){
	if (!status) {
		if (stop) {
			stop = false;
			loopBackground();
		} else
			stop = true;
	}
}

var lockSerie = [1,1,1,0,1,1,0,1,1,1,1];
var lockWord = "truneltruneltrunel"
var popWord = "kika";
var currentSerie = 0;
var status = false;

function keyLoupe(e) {

	var code;
	if (window.event)
		code = event.keyCode;
	else
		code = e.charCode;
	if (lockWord.charAt(currentSerie)==String.fromCharCode(code))
	{
		currentSerie++;
		checkLockWord();
	} else{
		currentSerie = 0;
		resized = false;
	}
	return;
}
function clicLoupe(e){

	/*if (lockSerie[currentSerie] == 1) {
		currentSerie++;
		checkLock();
	} else
		currentSerie = 0;*/
		
}				
function checkLock(){
	if (currentSerie>=lockSerie.length) {
	var eltStyle =document.getElementById('loupe').style;
		if (status) {
			eltStyle.opacity = 0.0;
			eltStyle.filter="alpha(opacity=0)";
			eltStyle.zIndex = 0;
		}
		else {
			eltStyle.opacity = 1.0;
			eltStyle.filter="alpha(opacity=100)";
			eltStyle.zIndex = 999;
		}
		status = !status;
	}
}
function checkLockWord(){
	if (currentSerie>=lockWord.length) {
		var eltStyle =document.getElementById('loupe').style;
		if (status) {
			lloop = false;
			eltStyle.opacity = 0.0;
			eltStyle.filter="alpha(opacity=0)";
			eltStyle.zIndex = 0;
		}
		else {
		
			stop = true;
			eltStyle.opacity = 1.0;
			eltStyle.filter="alpha(opacity=100)";
			eltStyle.zIndex = 999;
			lloop = true;
			loopLoupe();
		}
		currentSerie = 0;
		status = !status;
	}
}

var lx = 141;
var ly = 141;
var sx = 900;
var sy = 700;
var mx = 12;
var my = 8;
var usery = 0;
var lloop = true;
var score = 0;
var max = 0;
var sensx = 1;
var sensy = 1;

function moveLoupe() {
	
	lx += mx*sensx;
	ly += my*sensy;
	 
	var eltStyle = document.getElementById('loupe').style;
	eltStyle.backgroundPosition = (lx-100)+"px "+(ly-100)+"px";
	document.getElementById('joueur1').style.top = (ly-75)+"px";

	if (lx+100>=sx) {
	
		if ((usery-100)>ly || ly>(usery+100)){
		
			stopScore();
		}
		else {
		
			mx += 2;
			my += 2;
			sensx *= -1;
			addScore();
		}							
	} else {
	if (40>=lx-100)
		sensx *= -1;
	if (ly+100>=sy)
		sensy *= -1;
	if (0>=ly-100) 
		sensy *= -1;
	}
						
	loopLoupe();
}
function addScore(){
	score++;
	if (score>max)
		max = score;
	printScore();
}
function stopScore(){
	score = 0;
	lx = 141;
	ly = 141;
	mx = 10;
	my = 10;
	printScore();
}
function printScore(){
	document.getElementById('score').innerHTML = "Current : "+score+" / Max : "+max;
}
function moveJoueur2(e){
	
	usery = e.clientY;
	document.getElementById('joueur2').style.top = (usery-75)+"px";
}

function loopLoupe() {
	
	if (lloop)
		setTimeout(function() {	return moveLoupe.apply(null, []);}, 50);
}
document.onkeypress = keyLoupe;