/**
 * @author      JP
 * @since       15/12/2009
 * @brief       Récupère le flux APE et le traite
 *                      Dans le FLUX APE : TCHAT + NOTIFICATIONS + UPDATE MAP
 */
function ape()
{

	var v=Math.floor(Math.random()*1000000);
	$.post('http://'+document.URL.split('/')[2]+'/game/ape.php?v='+v, {}, function(d)
	{
		if( d == 0 ||  d == "0")
		{
			ape();
			return false;
		}
		
		if (d != null) {
		    
			if( d.reload )
			{
				location = 'http://'+document.URL.split('/')[2]+"/";
			}
			//Messages
			if (d.m) {
				var a = d.m.length;
				var nm = 0;
				while (a--) {
					var m = d.m[a];
					if (document.URL.match('game')) {
						sendToActionScript('{"count":"' + m.nbMessage + '"}', "toutouStageJump", "addMessage");
					}
					nm++;
				}
				
				var message = nm > 1 ? "Tu as reçu " + nm + " nouveaux messages." : "Tu as reçu un nouveau message.";

				if (nm > 0) 
				{
					pop(message);
				}
				ape();
			}
			
			//Ami
			if (d.a) {
				var a = d.a.length;
				var nw = 0;
				while (a--) {
					var m = d.a[a];
					if (document.URL.match('game')) {
						sendToActionScript('{"count":"' + m.nbAmis + '"}', "toutouStageJump", m.action);
					}
					if (m.action == "addFriend") {
						nw++;
					}
				}
				var message = nw > 1 ? "Tu as reçu " + nw + " nouvelles demandes d'amitié." : "Tu as reçu une nouvelle demande d'amitié.";
				if (nw > 0 && m.notif == 1) {
					pop(message);
				}
				ape();
			}
			
			//Game
			if (d.g) {
				var a = d.g.length;
				while (a--) {
					var m = d.g[a];
					if (document.URL.match('game')) {
						sendToActionScript('{"niveau":' + m.flash + '}', 'toutouStageJump', 'userSet');
					}
					popPerso(m.display, 'Medaille');
				}
				ape();
			}

		  //OS
	      if (d.o) {
	      // console.debug(d.o);
	        var a = d.o;
	          if (document.URL.match('game')) {
	            sendToActionScript('{"os_trouves":' + a + '}', 'toutouStageJump', 'plusOs');
	          }
	        ape();
	      }
	      
	    //Jauges
	      if (d.j) {
	      // console.debug(d.j);
	        var a = d.j;
	          if (document.URL.match('game')) {
	            sendToActionScript(a, 'toutouStageJump', 'plusJauges');
	          }
	        ape();
	      }
	      
	    //Déclencher: faire chier/pipi le chien
	      if (d.c) {
	      // console.debug(d.c);
	    	  var a = d.c;
	          if (document.URL.match('game')) {
	            sendToActionScript('{"faire_caca":' + a + '}', 'toutouStageJump', 'plusCaca');
	          }
	          ape();
	      }
	     
	      //Visiter quinquin, dans sa niche par défaut, ou dans le jardin si c'est précisé
	      if (d.v) {
	      // console.debug(d.c);
	        var a = d.v;
	        if (document.URL.match('game')) 
	    	{
	        	sendToActionScript(a, 'toutouStageJump', 'visite');
	        }
	        ape();
	      }
	      
	      // Messages popups precis : Niche clean, on a voter pour toi etc...
	      if (d.k) {
	        var a = d.k;
	        if (document.URL.match('game')) 
	    	{
	        	pop(a);
	        }
	        ape();
	      }
		}
	}, "json");
}

