var user;

var score=0;
var artisteCorrect;
var titreCorrect;

var nbArtisteCorrect=0;
var nbTitreCorrect=0;

var nbDernierTitreAffiche=0;

var checkPseudo=null;

function connexion()
{
	// definie l'user
	
	
	chgHautConnexion();
	document.getElementById("dernierTitre").innerHTML = '';
	actualiseAffichagePanneauJoueur();
	jouer();
}

function autorisationConnexion()
{
	user = document.getElementById("choixPseudo").value;
	
	requete('./php/newJoueur.php?user='+user,"checkPseudo=");
	autorisationConnexionAttente();
	return false;
}

function autorisationConnexionAttente()
{
	if(checkPseudo!=null)
	{
		if(checkPseudo=="true")
		{
			fonduHaut();
		}
		else
		{
			checkPseudo=null;
			document.getElementById('checkConnexion').innerHTML = 'Pseudo d&eacute;j&agrave; utilis&eacute;';
		}
			
	}
	else
		setTimeout('autorisationConnexionAttente()', 200);
}

function checkReponse(event)
{
	var repUser = document.getElementById('reponseUser').value;
	if(event.keyCode==13) //code ascii du retour chariot
    {
		if(lectureEnCour)
		{
			document.getElementById('reponseUser').value = '';
			requete('./php/requetereponse.php?reponse='+repUser,"reponse=");
			attenteCheckReponse();
		}
		else
		{
			messageBox('Trop tard la chanson est termin&eacute;e!');
		}
	}
}

function attenteCheckReponse()
{
	if(reponse != "undefined")
	{	
		checkScore();
		if(1 == reponse )
		{
			artiste=true;
			nbArtisteCorrect++;
		}
		else if(2 == reponse )
		{
			titre=true;
			nbTitreCorrect++;
		}
		else if(3 == reponse )
		{
			artiste=true;
			titre=true;
			nbArtisteCorrect++;
			nbTitreCorrect++;
		}
		else if(reponse > 3)
		{
			if(artiste)
			{
				titre=true;
				nbTitreCorrect++;
			}
			else if(titre)
			{
				artiste=true;
				nbArtisteCorrect++;
			}
			else
			{
				artiste=true;
				titre=true;
				nbArtisteCorrect++;
				nbTitreCorrect++;
			}
				
		}
		/*else
		{
			document.getElementById('reponse').innerHTML = 'Mauvaise reponse';
			setTimeout("document.getElementById('reponse').innerHTML = '';", 3000);
		}*/
		refreshReponse();
		reponse = "undefined";
	}
	else
	{
		setTimeout("attenteCheckReponse()",100);
	}
}

function checkScore()
{
	if(reponse>=1 && (titre != artiste) )
	{
		score+=2;
	}
	else if(reponse==1 && !artiste || reponse==2 && !titre)
	{
		score+=1;
	}
	else if(reponse==3 && !artiste && !titre)
	{
		score+=3;
	}
	
	if(reponse == 33)
	{
		score+=6;
	}
	else if(reponse>3)
	{
		score+=Math.floor(reponse/10);
	}
	
	document.getElementById('score').innerHTML = "Score <strong>"+score+"</strong>";
}
function refreshReponse()
{
	if(artiste==true)
		document.getElementById('artiste').style.visibility = 'visible';
	else
		document.getElementById('artiste').style.visibility = 'hidden';
	
	if(titre==true)
		document.getElementById('titre').style.visibility = 'visible';
	else
		document.getElementById('titre').style.visibility = 'hidden';
		
	document.getElementById('artisteCorrect').innerHTML = 'Artistes correct <strong>'+nbArtisteCorrect+'/'+nbChansonParPartie+'</strong>';
	document.getElementById('titreCorrect').innerHTML = 'Titres correct <strong>'+nbTitreCorrect+'/'+nbChansonParPartie+'</strong>';
}

function actualiseAffichagePanneauJoueur()
{
	document.getElementById('pseudoJoueur').innerHTML = user;
	checkScore();
	refreshReponse();
}