/**
 * Classe gerant les transaction AJAX de maniere Synchrone ou Ayncrhone
 * @author 		Yann Ponzoni
 * @copyright	Feerik
 * @date		Avril 2009
 * @version		2.4 (Juillet 2009 - Prise en charge de la MAJ des tooltips)
 */
    

    /** Fonction mettant à jour le compteur d'argent **/
	function updateMonnaie() {
		
		var xCom = new xJAX();
		xCom.setTarget('m=joueur&sa=updateCash');
		xCom.send(true, 'POST');
	}
	
	
	/***
	 * Classe xJAX
	 * @constructor
	 */
	function xJAX() {
		
		// Initialisations
	    this.httpRequest	= null;
	    this.sTarget		= '';
	    this.sParams		= '';
	    this._init();
	    
	    if (!this.httpRequest) {
	        alert('ERR_UPDATENAV');
	        return false;
	    }
	}
	
	
	/**
	 * Envoie la requete AJAX
	 * @param pbAsync	Boolean (true: AJAX, false: SJAX)
	 * @param psMethod	String	(GET ou POST)
	 * @return
	 */
	xJAX.prototype.send	= function(pbAsync, psMethod) {
		
		// Si Asynchrone
		if (pbAsync)
			this.httpRequest.onreadystatechange = function() {
			
				if (this.readyState == 4 && this.status == 200) {

					var xmlDocument = this.responseXML;
			        if (window.ActiveXObject) {
						xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
						xmlDocument.async	= "false";
						xmlDocument.loadXML(this.responseText);
			        }
			        
			        if (xmlDocument != null) {

				    	// Traitement des divisions à mettre à jour
				    	var updateDivs = xmlDocument.getElementsByTagName('update');
				    	for (var i=0; i<updateDivs.length; i++) {
				    		
				    		// Initialisations
				    		var sTargetDiv	= updateDivs[i].getAttribute('target');
				    		var sValue		= updateDivs[i].firstChild.nodeValue;
				    		
				    		// Affectation de l'attribut 'innerHTML'
				        	document.getElementById(sTargetDiv).innerHTML = sValue;
				    	}

				    	// Traitement des tooltips a mettre a jour
				    	var aToolTips = xmlDocument.getElementsByTagName('tooltip');
				    	for (var i=0; i<aToolTips.length; i++) {
				    		
				    		// Initialisations
				    		var sTargetNode	= aToolTips[i].getAttribute('target');
				    		var sValue		= aToolTips[i].firstChild.nodeValue;

				    		// Affectation de l'attribut 'title'
				        	document.getElementById(sTargetNode).title = sValue;
				    	}
				    	
				    	// Messages via alert();
				    	var aErrors = xmlDocument.getElementsByTagName('error');
				    	for (var i=0; i<aErrors.length; i++) {
				    		//alert(aErrors[i].firstChild.nodeValue);
						jQuery.gritter.add({
							// (string | mandatory) the heading of the notification
							title: aErrors[i].firstChild.nodeValue,
							// (string | mandatory) the text inside the notification
							text: ' ',
							// (string | optional) the image to display on the left
							image: '../img/icones/icn_good.png',
							// (bool | optional) if you want it to fade out on its own or just sit there
							sticky: false, 
							// (int | optional) the time you want it to be alive for before fading out
							time: ''
						});

				    	}

				    	// Style
				    	var aStyle = xmlDocument.getElementsByTagName('style');
				    	for (var i=0; i<aStyle.length; i++) {
				    		
				    		// Initialisations
				    		var sTargetDiv	= aStyle[i].getAttribute('target');
				    		var sCSSType	= aStyle[i].getAttribute('type');
				    		var sValue		= aStyle[i].firstChild.nodeValue;

				    		// Modifications du style
				    		jQuery("#"+sTargetDiv).css(sCSSType, sValue);
				    	}
				    	
				    	// Redirection
				    	var aRedirections = xmlDocument.getElementsByTagName('redirect');
				    	if (aRedirections.length > 0) {
				    		location.href = aRedirections[0].getAttribute('target');
				    	}
						
						// Callback
				    	var aCallback = xmlDocument.getElementsByTagName('callback');
				    	for (var i=0; i<aCallback.length; i++){

				    		// Init
				    		var fonction = aCallback[i].getAttribute('fonction');
				    		var message =  aCallback[i].firstChild.nodeValue;
				    		eval(fonction+'('+message+');');

				    	}
				    	
			        }
				}
			};

		
		if (psMethod == 'GET') {
			this.httpRequest.open('GET', 'xjax.php?'+ this.sTarget +'&'+ this.sParams, pbAsync);
		    this.httpRequest.send(null);
		}
		else if (psMethod == 'POST') {
			this.httpRequest.open('POST', 'xjax.php?'+ this.sTarget, pbAsync);
		    this.httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
			this.httpRequest.send(this.sParams);
		}

	    // Si Synchrone
		if (!pbAsync) {

			var xmlDocument = this.httpRequest.responseXML;
	        if (window.ActiveXObject) {
				xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
				xmlDocument.async	= "false";
				xmlDocument.loadXML(this.httpRequest.responseText);
	        }
	        
	        if (xmlDocument != null) {
	        	
		    	// Traitement des divisions à mettre à jour
		    	var updateDivs = xmlDocument.getElementsByTagName('update');
		    	for (var i=0; i<updateDivs.length; i++) {
		    		
		    		// Initialisations
		    		var sTargetDiv	= updateDivs[i].getAttribute('target');
		    		var sValue		= updateDivs[i].firstChild.nodeValue;
		    		
		    		// Affectation de l'attribut 'innerHTML'
		        	document.getElementById(sTargetDiv).innerHTML = sValue;
		    	}

		    	// Traitement des tooltips a mettre a jour
		    	var aToolTips = xmlDocument.getElementsByTagName('tooltip');
		    	for (var i=0; i<aToolTips.length; i++) {
		    		
		    		// Initialisations
		    		var sTargetNode	= aToolTips[i].getAttribute('target');
		    		var sValue		= aToolTips[i].firstChild.nodeValue;

		    		// Affectation de l'attribut 'title'
		        	document.getElementById(sTargetNode).title = sValue;
		    	}

		    	// Erreurs eventuelles
		    	var aErrors = xmlDocument.getElementsByTagName('error');
		    	for (var i=0; i<aErrors.length; i++) {
		    		//alert(aErrors[i].firstChild.nodeValue);
			jQuery.gritter.add({
				// (string | mandatory) the heading of the notification
				title: ' ',
				// (string | mandatory) the text inside the notification
				text: aErrors[i].firstChild.nodeValue,
				// (string | optional) the image to display on the left
				image: '../img/icones/icn_error.png',
				// (bool | optional) if you want it to fade out on its own or just sit there
				sticky: false, 
				// (int | optional) the time you want it to be alive for before fading out
				time: ''
			});

		    	}

		    	// Style
		    	var aStyle = xmlDocument.getElementsByTagName('style');
		    	for (var i=0; i<aStyle.length; i++) {
		    		
		    		// Initialisations
		    		var sTargetDiv	= aStyle[i].getAttribute('target');
		    		var sCSSType	= aStyle[i].getAttribute('type');
		    		var sValue		= aStyle[i].firstChild.nodeValue;

		    		// Modifications du style
		    		if (sCSSType == 'width')	{ document.getElementById(sTargetDiv).style.width	= sValue; 	continue; } 
		    		if (sCSSType == 'display')	{ document.getElementById(sTargetDiv).style.display	= sValue;	continue; }
		    	}

		    	// Redirection
		    	var aRedirections = xmlDocument.getElementsByTagName('redirect');
		    	if (aRedirections.length > 0) {
		    		location.href = aRedirections[0].getAttribute('target');
		    	}
				
				// Callback
				var aCallback = xmlDocument.getElementsByTagName('callback');
				for (var i=0; i<aCallback.length; i++){

				    // Init
				    var fonction = aCallback[i].getAttribute('fonction');
				    var message =  aCallback[i].firstChild.nodeValue;
				    eval(fonction+'('+message+');');
				}
	        }
		}
	};

	xJAX.prototype.setTarget	= function(psTarget) 	{ this.sTarget = psTarget;	};
	xJAX.prototype.setParam		= function(psParam)		{ this.sParams = psParam;	};
	
	
	/**
	 * Initialise la requete AJAX
	 * @return XMLHttpRequest
	 */
	xJAX.prototype._init	= function() {

        if (window.XMLHttpRequest) { // Mozilla, Safari, Chromium, Opera...
            this.httpRequest = new XMLHttpRequest();
            if (this.httpRequest.overrideMimeType) {
            	this.httpRequest.overrideMimeType('text/xml');
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
            	this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            //	this.httpRequest.overrideMimeType('text/xml');
            }
            catch (e) {
                try {
                	this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                	;
                }
            }
        }
    };
    
    
	xJAX.prototype.success = function(xmlDocument)
	{

		if (xmlDocument != null)
		{
			
			// Traitement des divisions Ã  mettre Ã  jour
	    	var updateDivs = xmlDocument.getElementsByTagName('update');
	    	alert(updateDivs);
			
	    	for (var i=0; i<updateDivs.length; i++) {
	    		var sTargetDiv	= updateDivs[i].getAttribute('target');
	    		var sValue		= updateDivs[i].firstChild.nodeValue;
	    		var bSlide		= updateDivs[i].getAttribute('slide');

				if (bSlide>0)
	    		{
		    		if ($("#"+sTargetDiv).html()!="")
		    		{
			    		xJAX.prototype.slideComplet(sTargetDiv, sValue);
		    		}
		    		else
		    		{
						xJAX.prototype.slideDemi(sTargetDiv, sValue);
		    		}
	    		}
	    		else {
	    			$("#"+sTargetDiv).html(sValue);
	    		}
	    	}

	    	// Erreurs eventuelles
	    	var aErrors = xmlDocument.getElementsByTagName('error');
	    	for (var i=0; i<aErrors.length; i++) {
	    		alert(aErrors[i].firstChild.nodeValue);
	    	}

	    	// Style
	    	var aStyle = xmlDocument.getElementsByTagName('style');
	    	for (var i=0; i<aStyle.length; i++) {

	    		// Initialisations
	    		var sTargetDiv	= aStyle[i].getAttribute('target');
	    		var sCSSType	= aStyle[i].getAttribute('type');
	    		var sValue		= aStyle[i].firstChild.nodeValue;

	    		// Modifications du style
	    		if (sCSSType == 'width')	{ document.getElementById(sTargetDiv).style.width	= sValue; 	continue; }
	    		if (sCSSType == 'display')	{ document.getElementById(sTargetDiv).style.display	= sValue;	continue; }
	    	}

	    	// Redirection
	    	var aRedirections = xmlDocument.getElementsByTagName('redirect');
	    	if (aRedirections.length > 0) {
	    		location.href = aRedirections[0].getAttribute('target');
	    	}

	    	// Popup
	    	var aPopup	=	xmlDocument.getElementsByTagName('message');
	    	for (var i=0; i<aPopup.length; i++){

	    		// Init
	    		var message = aPopup[i].firstChild.nodeValue;
	    		var titre =	aPopup[i].getAttribute('titre');
	    		var image = aPopup[i].getAttribute('image');
	    		var sticky = aPopup[i].getAttribute('sticky');
	    		var temps =aPopup[i].getAttribute('temps');

	    		afficher_message(message, titre, image, sticky, temps);
	    	}

			// Big popup dhtml
			var aBigPopup = xmlDocument.getElementsByTagName('bigPopup');
			for (var i=0; i<aBigPopup.length; i++)
			{
				var nb_alea = Math.floor(Math.random() * 9999999);
				var html = aBigPopup[i].firstChild.nodeValue;
				var width = aBigPopup[i].getAttribute('width');
				var height = aBigPopup[i].getAttribute('height');

				$("body").append("<div id='popup_"+nb_alea+"' style='display:none'>"+html+"</div>");
				$.modal($("#popup_"+nb_alea), { containerCss : {width: width+"px", height: height+"px" }, overlayClose :true });
			}

	    	// Callback
	    	var aCallback = xmlDocument.getElementsByTagName('callback');
	    	for (var i=0; i<aCallback.length; i++){

	    		// Init
	    		var fonction = aCallback[i].getAttribute('fonction');
	    		var message =  aCallback[i].firstChild.nodeValue;
	    		eval(fonction+'('+message+');');

	    	}
		}
	}
    
