var intervalAction = new Array();


function fermerDiv(id) {
	if (id.style == undefined ) {
		document.getElementById(id).style.display = 'none';
	}
	else {
		id.style.display="none";
	}
	document.getElementById("fondOpaque").style.display="none";
}


function fond() {
	document.getElementById('fondOpaque').style.display='block';	
}

function insererDiv(div, text) {
	document.getElementById(div).innerHTML=text;
}

function afficherDiv (id) {
	document.getElementById(id).style.display='block';
}

function openInfo($lieu, $description) {
	$contenu = '<u><b>'+$lieu+'</b></u><p>'+$description+'</p>';
	document.getElementById('map_info_contenu').innerHTML=$contenu;
}

function closeInfo() {
	document.getElementById('map_info').style.display='none';
}

function centerPopup(element) {
	var height = document.getElementById(element).offsetHeight;//hauteur de l'élément à positionner
	var width = document.getElementById(element).offsetWidth;//largeur de l'élément à positionner
	var posY = (height/2);//Calcul de la position en Y
	var posX = (width/2);//Calcul de la position en X
	var margMax = document.body.clientHeight / 2; 
	if (margMax == 0) {
		margMax = window.innerHeight / 2;	
	}
	
	
	if (posY > margMax) { posY = margMax - 250; }
	document.getElementById(element).style.top="50%";
	document.getElementById(element).style.marginTop="-"+posY+"px";
	document.getElementById(element).style.left="50%";
	document.getElementById(element).style.marginLeft="-"+posX+"px";
}


function creerArray(valeur) {
	var $tab = new Array();
	for (var i = 0; i < creerArray.arguments.length; i++) {
		$tab.push(creerArray.arguments[i]);
	}

	return $tab;
}



var spawner = true;
function desactiverSpawn (b) {
	if (document.getElementById('carteIso') != null) {
		if (spawner == true) {
			document.getElementById('carteIso').activerSpawn(false);	
			spawner = false;
		}
		else if (spawner == false) {
			document.getElementById('carteIso').activerSpawn(true);	
			spawner = true;
		}
	}
}

var play = false, fondu, fondu2, idDiv, depart = 1;
function disparitionFondu (div, temps) {
	
	if (this.play == false) {
		this.idDiv = div;
		this.fondu = setInterval(disparitionFondu, temps);
		this.play = true;
	}
	else {
		if (this.depart == 1) {
			clearInterval(this.fondu);
			this.fondu2 = setInterval(disparitionFondu, 50);
		}
		
		var id = document.getElementById(this.idDiv);
		id.style.opacity = this.depart;
		id.style.filter = 'alpha(opacity = '+this.depart*100+')';
		
		if (this.depart < -0.05) {
			clearInterval(this.fondu2);
			this.play = false;
		}
		this.depart -= 0.05;
	}
	
	
	
}



var alpha, depart, width=0, height=0, heightFin, widthFin, depart2 = 0, id, play = false;
function effetFonduAvecFond(id, alphaDepart, width, height) {
	if (play == false) {
		this.depart = alphaDepart;
		this.id = id;
		this.heightFin = height;
		this.widthFin = width;
	}
	document.getElementById(this.id).style.opacity=depart;
	document.getElementById(this.id).style.filter = 'alpha(opacity=' + depart*100 + ')';
	document.getElementById("fondOpaque").style.opacity=depart2;
	document.getElementById("fondOpaque").style.filter = 'alpha(opacity=' + depart2*100 + ')';
	document.getElementById(this.id).style.height=this.height+"px";
	document.getElementById(this.id).style.width=this.width+"px";
	
	depart += 0.05; 
	if (this.height <= this.heightFin) {
		this.height += (this.heightFin/15);
		
	}
	if (this.width <= this.widthFin) {
		this.width += (this.widthFin/15);
	}
	if (depart2 <= 0.8) {
		depart2 += 0.05;
	}
	
	if (play == false) {
		alpha = setInterval( effetFonduAvecFond, 40);
		play = true;
	}
	if (depart >= 1) {
		clearInterval(alpha);
		play = false;
		centerPopup(this.id);
	}
	
}





function choixTchat () {
	return document.getElementById('choixTchat').value;
}

function transparent (id, alpha) {
	var doc = document.getElementById(id);
	if (doc != null) {
		doc.style.opacity=alpha;
		doc.style.filter = 'alpha(opacity=' + alpha*100 + ')';
	}
}

function opaque (id) {
	
	var doc = document.getElementById(id);
	if (doc != null) {
		doc.style.opacity=1;
		doc.style.filter = 'alpha(opacity=100)';
	}
}

function comparer2Mots(mot1, mot2) {
	if (mot1 == mot2) {
		return true;
	}
	else { return false; }
}

function touchePresse(e) {
	for (prop in e)
	{
		if(prop == 'which') {
			return e.which;
		}
	}
	return e.keyCode;
}

function redirection(lien) {	
	window.location.replace(lien);
}

function page(page) {	
	window.location.replace('controleur/page.php?page='+page+'');
}


function incremente(id,sens, force, rapi, consti, volonte) {

 var total_pts = document.getElementById('total').value;
 var valeur = document.getElementById(id).value;	 
 var valeur2 = document.getElementById(id+"2");	
 
	if (total_pts > 0) {
	
		if(sens == '+')
		{
			valeur++;
			document.getElementById(id).value=valeur;
			valeur2.innerHTML = valeur;
			total_pts--;
			document.getElementById('total').value=total_pts;
			document.getElementById('total2').innerHTML=total_pts;
		}
		
	}
	
	if((sens == '-') && (valeur > force) && (id == 'force'))
	{	
		valeur--;
		document.getElementById(id).value=valeur;
		valeur2.innerHTML = valeur;
		total_pts++;
		document.getElementById('total').value=total_pts;
		document.getElementById('total2').innerHTML=total_pts;
	}
	else if((sens == '-') && (valeur > rapi) && (id == 'rapi'))
	{	
		valeur--;
		document.getElementById(id).value=valeur;
		valeur2.innerHTML = valeur;
		total_pts++;
		document.getElementById('total').value=total_pts;
		document.getElementById('total2').innerHTML=total_pts;
	}
	else if((sens == '-') && (valeur > consti) && (id == 'consti'))
	{	
		valeur--;
		document.getElementById(id).value=valeur;
		valeur2.innerHTML = valeur;
		total_pts++;
		document.getElementById('total').value=total_pts;
		document.getElementById('total2').innerHTML=total_pts;
	} 
	else if((sens == '-') && (valeur > volonte) && (id == 'volonte')) {	
		 valeur--;
		 document.getElementById(id).value=valeur;
		 valeur2.innerHTML = valeur;
		 total_pts++;
		 document.getElementById('total').value=total_pts;
		 document.getElementById('total2').innerHTML=total_pts;
	}
 
}


function chargement(idDiv) {
	document.getElementById(idDiv).innerHTML="<div style=\"text-align:center; padding:20px;\"><img src='images/chargement.gif' alt='Chargement...'/></div>";
}

function mission($idM, $no, $nex) {
	window.location.replace('controleur/mission.php?fonction=mission&idM='+$idM+'&n='+$no+'&nt='+$nex+'');
}

function apprentissageTechnique($idSort, relance) {
	if (relance == 1) {
		window.location.replace('controleur/jeu.php?fonction=reprendreApprentissage&id='+$idSort);
	}
	else {
		window.location.replace('controleur/jeu.php?fonction=lancerApprentissage&id='+$idSort);
	}
}


// ------------- Fonction concernant les combats -----
var sec = 30, divi, inter = false, stope = false, type;
function decompteCombat (tps, tpsActu, div) {
	
	if (sec <= 0) {
		clearInterval(inter);
		attaque('attaquePrincipale', '', 'false');
	}
	else {
		if (sec == 30) {
			sec = tps - tpsActu;
			inter = setInterval(decompteCombat, 1000);	
			divi = div;
		}
		sec--;
	}
	document.getElementById(divi).innerHTML = sec;
	
}
function decompteCombatPvp (tps, tpsActu, div, typ) {
	
	if ( (sec <= 0) && (stope == false) && (type == 1) ) {
		//clearInterval(inter);
		pvpAttaque('attaquePrincipale', 'false');
	}
	else {
		if ( (stope == true) && (sec < 0) && (sec >= -4) ) {
			
		}
		//else if ( (stope == true) && (sec < -6)  ) {
			//rechargeIndex();	
		//}
		
		if (inter == false) {
			sec = tps - tpsActu;
			inter = setInterval(decompteCombatPvp, 1000);
			divi = div;
			type = typ;
		}
		//if (sec == 10) { observationAtk(); }
		sec--;
	}
	
	if ( (stope == true) && (sec < 0) && (sec >= -20) ) {
		sec = 25;
		document.getElementById(divi).innerHTML = "<img src='images/avatar/load.gif'/>";
	}
	if ( (type == 0) && (sec < 0) ) {
		document.getElementById(divi).innerHTML = "<img src='images/avatar/load.gif'/>";
	}
	else {
		document.getElementById(divi).innerHTML = sec;
	}
	
}

var actionCombat = false;
function attaque(nom, type, directe, multi) {
	
	if (actionCombat == false) {
		if (directe == 'true') {
			document.getElementById('contenuInformation').style.display='block';
					document.getElementById('messageInformation').innerHTML = "Tenter une attaque directe ? <br/> <a style=\"cursor:pointer;\" onClick=\"attaque('"+nom+"', '"+type+"', '1', '"+multi+"');\">Oui</a> | <a style=\"cursor:pointer;\" onClick=\"attaque('"+nom+"', '"+type+"', '0', '"+multi+"');\">Non</a>";;
	 
		}
		else { 
			actionCombat = true;
			if (multi == true) {
				verifierAttaque (nom, directe);
			}
			else {
				window.location.replace('controleur/combat.php?fonction=confrontation&atk='+nom+'&directe='+directe+''); 
			}
		}
	}
}

function attaquePvp(nom, type, directe, multi) {
	if (actionCombat == false) {
		if (directe == 'true') {
			document.getElementById('contenuInformation').style.display='block'
			document.getElementById('messageInformation').innerHTML = "Tenter une attaque directe ? <br/> <a style=\"cursor:pointer;\" onClick=\"attaquePvp('"+nom+"', '"+type+"', '1', '"+multi+"');\">Oui</a> | <a style=\"cursor:pointer;\" onClick=\"attaquePvp('"+nom+"', '"+type+"', '0', '"+multi+"');\">Non</a>";
			 
		}
		else { 
			actionCombat = true;
			if (multi == true) {
				verifierAttaquePVP (nom, directe);
			}
			else {
				pvpAttaque(nom, directe);
			}
		}
	}
	
}

function changeSpe(nom) {

	document.getElementById('titreSpe').innerHTML=nom;
	if (nom == 'BAKUDO') {	
		document.getElementById('hado').style.display='none';
		document.getElementById('zanjutsu').style.display='none';
		document.getElementById('bakudo').style.display='block';
		document.getElementById('transformation2').style.display='none';
	} 
	else if (nom == 'HADO') {
		document.getElementById('hado').style.display='block';
		document.getElementById('zanjutsu').style.display='none';
		document.getElementById('bakudo').style.display='none';
		document.getElementById('transformation2').style.display='none';
	}
	else if (nom == 'ZANJUTSU') {
		document.getElementById('hado').style.display='none';
		document.getElementById('zanjutsu').style.display='block';
		document.getElementById('bakudo').style.display='none';
		document.getElementById('transformation2').style.display='none';
	}
	else if (nom == 'TRANSFORMATION') {
		document.getElementById('hado').style.display='none';
		document.getElementById('zanjutsu').style.display='none';
		document.getElementById('bakudo').style.display='none';
		document.getElementById('transformation2').style.display='block';
	}

}


// ---- Fonction appelé par Flash
var activation = true;
function desactiverCarteFlash() {
	
	if (activation == true) {
		if(navigator.appName.indexOf("Microsoft") != -1){
			window.carteIso.desactiverRequete();
		}else{
			document.getElementById('carteIso').desactiverRequete();
		}
		
		
		activation = false;
	}

}

function activerCarteFlash() {
	
	if (activation == false) {
		if(navigator.appName.indexOf("Microsoft") != -1){
			window.carteIso.activerRequete();
		}else{
			document.getElementById('carteIso').activerRequete();
		}
		activation = true;
	}
}


function reloadXP(xp, niveau) {
	
	var i = 1, k = 0, xpTab = new Array();
	xpTab[0] = 0;
	while ( i <= niveau) {
		k = i - 1;
		xpTab[i] = xpTab[k] + (150 * i);
		i++;
	}
	
	if (xp >= xpTab[niveau]) {
		//document.getElementById('xpPerso').innerHTML = '<a onclick="afficheCadre(\'vue/passageNiveau\');" style="text-decoration:blink; color:red; font-weight:bold; font-size:14px; cursor:pointer;">Niveau superieur !</a>';
	}
	else {
		document.getElementById('xpPerso').innerHTML = 'Xp : '+xp+' / '+xpTab[niveau]+'';	
	}
}


function changerAvatar (action, tps) {
	if(navigator.appName.indexOf("Microsoft") != -1){
		window.carteIso.changeAvatar(action, tps);
	}else{
		document.getElementById('carteIso').changeAvatar(action, tps);
	}
	
	if (intervalAction['type'] == null) {
		intervalAction['type'] = 'Annuler';
		intervalAction['action'] = action;
		intervalAction[action] = new Array();
		intervalAction[action].push(setInterval(transformation, tps));
	}
	
}

function afficherNomZone ($nom) {
	document.getElementById('nomZone').innerHTML = $nom;
}

function combatBoss ($idBoss, $code) {
	window.location.replace('controleur/combat.php?fonction=spawn&type=boss&id='+$idBoss+'&tm='+$code+'');
}

function gestionEvenement ($type, $text, $temps, $idTuile) {

	if(navigator.appName.indexOf("Microsoft") != -1){
		$document = "window.carteIso";
	}else{
		$document = "document.getElementById('carteIso')";
	}


	document.getElementById('fermerEvent').innerHTML = '<div id="fermer" onClick="fermerDiv(document.getElementById(\'afficheInformationEvent\'));" ><span style="font-size:10px;">Fermer</span> X</div>';
	
	if ($type == "info") {
		$text += '<br/><br/><a onclick="document.getElementById(\'afficheInformationEvent\').style.display=\'none\';">Ok</a>';
	}
	
	else if ($type == "teleportation") {
		var $text2 = '<a onclick="redirection(\'controleur/teleportation.php?fonction='+$temps+'&token='+$idTuile+'\');">Vers '+$text+'</a>';
		$text = $text2;
	}
	else if ($type == "shinigami") {
		$text += '<div style="float:left;">';
		$text += '<a onclick="redirection(\'controleur/page.php?page=monCompte&id='+$temps[7]+'\')">Pseudo : '+$idTuile+' </a><br/> Niveau '+$temps[1]+'<br/>';
		
		if ($temps[5] == undefined) {
			$text += 'Clan : Neutre<br/>';
		}
		else { 
			$text += 'Clan : '+$temps[5]+'<br/>';
			
		}
				
		$text += '</div>';
		
		if ($temps[5] == "Yuken") { $text += '<img src="images/objet/embleme/Yuken.png" style="float:left; margin-left:5px;"/>'; }
		else if ($temps[5] == "Makkura") { $text += '<img src="images/objet/embleme/Makkura.png" style="float:left"/>'; }
		
		//$text += '<div style="clear:both;"></div>';
		
		if ($temps[3] <= 0) {
			$text += '<br/><center style="padding-left:15px;"> Agonise au sol, il ne peut faire aucun mouvement</center>';
			$text += '<br/><p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
			$text += '<a onclick="executeControleur(\'listeSortRez\', \'jeu\', \'listSort\', \''+$temps[7]+'\');"><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Essayer une rÃ©surrection</a>';
			$text += '<br/><span id="listSort" style="color:#333;"></span>';
			$text += '</p>';
			$text += '<p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
			$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\'Fouille\', \''+$temps[7]+'\', \'\');"><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Le fouiller</a>';
			$text += '</p>';
		}
		else {
			if ($temps[0] == 1) { 
				$text += '<br/><center>Un combat fait rage en ce moment, il serait trop risquÃ© d\'attaquer</center>';
			}
			else if ($temps[6] == 1) {
				$text += '<br/><center><a onclick="executeControleur(\'attaquer\', \'pvp\', \'contenuEvent\', \''+$temps[7]+'\');">Attaquer</a></center>';
			}
			else if ($temps[6] == 0) {
				$text += '<br/><center>Il est impossible d\'attaquer dans cette zone</center>';
			}
			
			$text += '<br/>';
			$text += '<p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
			$text += '<a style="cursor:pointer;" onclick="demandeGroupe(\''+$temps[7]+'\');"><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Envoyer une invitation de groupe</a>';
			$text += '</p>';
			$text += '<p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
			$text += '<a style="cursor:pointer;" onclick="executeControleur(\'listeSortSoin\', \'jeu\', \'listSort\', \''+$temps[7]+'\');"><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Utiliser un sort de soin</a>';
			$text += '<br/><span id="listSort" style="color:#333;"></span>';
			$text += '</p>';
			//$text += '<br/><center><a>Utiliser un sort de boost</center></a>';
			
		}
	}
	
	
	else if ($type == "Dormir") {
		$text += 'Temps de repos : <input id="tempsAct" type="text" style="width:20px; height:10px; font-size:10px;" value="'+$temps+'" onKeyUp="$(\'#tps\').text(this.value);" /><br/><br/>';
		$text += '<img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> <a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', $(\'#tempsAct\').val());">Se reposer  <span id="tps">'+$temps+'</span>  minutes.</a>';
	}
	else if ($type == "Boire") {
		$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', \''+$temps+'\');">Boire</a>';
	} 
	else if ($type == "Pnj") {
		$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', \''+$temps+'\');">Parler</a>';
	}
	else if ($type == "Boss") {
		$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', \''+$temps+'\');">Attaquer</a>';
	}
	else if ($type == "Ouvrir") {
		$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', \''+$temps+'\');">Ouvrir</a>';
	}
	else if ($type == "Recolte") {
		$text += '<a style="cursor:pointer;" onclick="'+$document+'.lancerAction(\''+$type+'\', \''+$idTuile+'\', \''+$temps+'\');">Recolter</a>';
	}
	else if ($type == "Rez") {
		document.getElementById('fermerEvent').innerHTML = "";
		$text += 'Vous Ãªtes mortellement blessÃ©(e).';
		$text += '<p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
		$text += '<a onclick="document.getElementById(\'afficheInformationEvent\').style.display=\'none\'; document.getElementById(\'carteIso\').lancerAction(\'Rez\', \'\', \'\');" ><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Envoyez un message avec le dernier souffle qu\'il vous reste aux Shinigami Medecin ?</a>';
		$text += '</p>';
		$text += '<p style="background-color:#1A1A1A; margin-left:-9px; margin-right:-9px;">';
		$text += '<a onclick="document.getElementById(\'afficheInformationEvent\').style.display=\'none\'; document.getElementById(\'carteIso\').lancerAction(\'Rapatriement\', \'\', \'\');"><img src="images/ficherPerso/fleche.png" style="margin-left:5px;"/> Demandez un rapatriement d\'urgence ? (3 x plus long)</a>';
		$text += '</p>';

	}
	else if ($type == "Decompte") {
		window.document.title = $text+' Bakoro.com | Deviens Shinigami.';
		document.getElementById('fermerEvent').innerHTML = "";
		$text += '<br/><br/><center><a onclick="document.getElementById(\'afficheInformationEvent\').style.display=\'none\'; document.getElementById(\'carteIso\').annulerDecompte(\'0\');" >Annuler </a></center>';
		
	}
	else if ($type == "DecompteFin") {
		document.getElementById('fermerEvent').innerHTML = "";
		$text += '<br/><br/><center><a onclick="document.getElementById(\'afficheInformationEvent\').style.display=\'none\'; document.getElementById(\'carteIso\').confirmerDecompte();" >Ok</a></center>';
		window.document.title = 'Bakoro.com | Deviens Shinigami.';
	}
	document.getElementById('afficheInformationEvent').style.display='block';
	document.getElementById('contenuEvent').innerHTML=$text;
	
	
	if ($type == "fermer") {
		document.getElementById('afficheInformationEvent').style.display='none';
		window.document.title = 'Bakoro.com | Deviens Shinigami.';
	}
}




function introduction() {
	var text = "ArrivÃ©(e) dans une piÃ¨ce assez vaste et rectangulaire, vous apercevez plusieurs siÃ¨ges avec au fond un Ã©norme tableau, plusieurs personnes sont dÃ©jÃ  assisesâ€¦	En fait, il ne manquait plus que vous. <br/>Lâ€™homme se dirigea au fond de la salle pour que tout le monde puisse lâ€™entendre.</p>	<p>	<i>HÃ© Bien, je vois que vous Ãªtes enfin tous rÃ©unis. Je vais donc pouvoir vous expliquer le pourquoi de cette rÃ©union. Je me prÃ©sente, je me nomme Akumo Itsuki, je suis le directeur de cette acadÃ©mie. Celle-ci a pour but de recenser et dâ€™entrainer nos futurs Shinigamis, vous vous demandez certainement pourquoi je vous parle de Shinigami. <br/>Nâ€™avez-vous jamais remarquÃ© des choses bizarres dans votre vie : des choses que vous seul pouviez voir, que vous seul pouviez toucher.. Â»</i><p>** Tousse puis sourit quelques instants **</p> <i> Â« Et bien, câ€™est pour Ã§a que vous Ãªtes ici, vous avez un don, lâ€™honneur de peut-Ãªtre devenir un jour Shinigami. Et cette acadÃ©mie sert donc Ã  cela, vous aider Ã  pÃ©nÃ©trer dans la voie du Shinigami, chemin parsemÃ© d'embÃ»ches et de dÃ©faites, tout comme de victoires !	<br/>Cependant, je ne vous oblige pas Ã  rester parmi nous, mais sachez quâ€™une telle opportunitÃ© ne se reprÃ©sentera pas une nouvelle fois. Alors que ceux qui veulent continuer cette aventure avec nous restent ici. Les autres vous pouvez disposer, une personne vous attendra Ã  la sortie. Â»	</i></p>	<p>	AprÃ¨s quelques instants, vous voyez autour de vous plusieurs personnes sâ€™esquiver vers la sortie en marmonnant des propos injurieux comme quoi ce vieux fou leur faisait perdre leur temps...	</p>	<P>	Â« Bien, je vois quâ€™il reste plus de monde que je ne lâ€™aurais cru. Nous allons enfin pouvoir commencer votre apprentissage. La connaissance et la comprÃ©hension dâ€™un monde qui dÃ©passe votre entendement, une puissance enfouie profondÃ©ment en vous, un regard nouveau sur le monde vous est offert et vous lâ€™avez acceptÃ© alors soyez en digne et avancez vers votre destinÃ©e. Â»	</p>	<p>	<center><h3><a onClick=\"fermerDiv('gestionErreur'); executeControleur ('introduction', 'jeu', 'messageInformation'); afficherDiv('contenuInformation');\">Et câ€™est ainsi que votre histoire commence...</a>	</h3></center></p>";	
	
	document.getElementById('contenuErreur').innerHTML = text;
}