/**
 * EME Loader manager
 *
 * @package    issila
 * @subpackage issila
 * @author     Gerome CUIGNET
 * @version    SVN: $Id: ajaxInfo.js,v 1.2.2.5 2010-10-20 07:11:17 gcuignet Exp $
 * 
 * Requiert jQuery 
 * 
 * Cet objet gère l'affichage d'un loader avec affichage status d'execution pour tous les apels XHR 
 * effectués avec jQuery (get, post, load, getJSON, ajax, etc...)
 */



window.ajaxInfo = {
		// Initialise le loader et l'affiche
		// On ne peut pas utiliser this dans les fonctions de l'objet car jQuery ne permet pas de
		// définir le périmètre d'exécution dans les bind. Il faut utiliser window.ajaxInfo.xxx .
		handleStart: function () {
				window.ajaxInfo.loaderDiv.removeClass('ui-state-default')
        												 .removeClass('ui-state-error')
                                 .addClass('ui-state-active');
				window.ajaxInfo.refreshPosition();
				window.ajaxInfo.loaderDiv.fadeIn(0)
				window.ajaxInfo.loaderDiv[0].style.display = 'block';
				// FIXME : les messages doivent être chargés depuis le catalogue PHP
        window.ajaxInfo.loaderDiv.html('<img src="/images/loading.gif" align="top"> Chargement...');
        // Nouvel appel -> pas d'erreur
        window.ajaxInfo.hadErrors = false;
      	// Nettoyage du timeout si besoin
        if (window.ajaxInfo.timeout != null) {
        	clearTimeout(window.ajaxInfo.timeout);
        	window.ajaxInfo.timeout = null;
        }
    },
    handleSend: function () {
    },
    handleStop: function () {
    		if (!window.ajaxInfo.hadErrors) {
    				window.ajaxInfo.loaderDiv.removeClass('ui-state-active')
						 												 .removeClass('ui-state-error')
        		                         .addClass('ui-state-default');
    				window.ajaxInfo.refreshPosition();
    				// FIXME : les messages doivent être chargés depuis le catalogue PHP
        		window.ajaxInfo.loaderDiv.html('Chargement terminé');
          	// Nettoyage du timeout si besoin
            if (window.ajaxInfo.timeout != null) {
            	clearTimeout(window.ajaxInfo.timeout);
            }
            window.ajaxInfo.timeout = setTimeout('window.ajaxInfo.fadeOut()', 2000);
    		}
    },
    handleError: function() {
        // Il faut stocker le status en erreur pour ne pas que le stop s'exécute
        window.ajaxInfo.hadErrors = true;
				window.ajaxInfo.refreshPosition();
    		window.ajaxInfo.loaderDiv.removeClass('ui-state-active')
				 												 .removeClass('ui-state-default')
        												 .addClass('ui-state-error');
				// FIXME : les messages doivent être chargés depuis le catalogue PHP
        window.ajaxInfo.loaderDiv.html('Erreur de chargement');
        window.ajaxInfo.timeout = setTimeout('window.ajaxInfo.fadeOut()', 4000);
    },
    fadeOut: function() {
    	window.ajaxInfo.loaderDiv.fadeOut(1000, function() {window.ajaxInfo.loaderDiv[0].style.display = 'none';});
    	// Nettoyage du timeout si besoin
      if (window.ajaxInfo.timeout != null) {
      	clearTimeout(window.ajaxInfo.timeout);
      	window.ajaxInfo.timeout = null;
      }
    },
    refreshPosition: function() {
    	window.ajaxInfo.loaderDiv[0].style.display = 'none';
    	try {
				var top = (window.innerHeight + window.scrollY - 26);
				var left = (window.innerWidth + window.scrollX - 158);
				if (window.scrollMaxX != 0 ) { // le innerHeight et innerWidth ne prennent pas en compte les scrollers
					top -= 17;
				}
				if (window.scrollMaxY != 0 ) { // le innerHeight et innerWidth ne prennent pas en compte les scrollers
					left -= 17;
				}
				window.ajaxInfo.loaderDiv[0].style.top = top + 'px';
				window.ajaxInfo.loaderDiv[0].style.left = left + 'px';
			} catch (e) { // IE SUX!!!
				var top = document.documentElement.clientHeight + document.documentElement.scrollTop - 26;
				var left = document.documentElement.clientWidth + document.documentElement.scrollLeft - 158;
				window.ajaxInfo.loaderDiv[0].style.top = top + 'px';
				window.ajaxInfo.loaderDiv[0].style.left = left + 'px';
				
			}
    },
    loaderDiv: null,	// Contient le noeud jQuery du loader
    timeout: null,		// Pour stocker le timeout utilisé par le fadeOut
    hadErrors: false	// Permet de savoir si la requête courante est revenue en erreur
}

jQuery(document).ready(function() {
	  // initialisation des évènements. 
		// il faut binder dur document pour récupérer les event levés par $.ajax.
	jQuery(document).bind('ajaxStop', 	window.ajaxInfo.handleStop)
                  .bind('ajaxStart', window.ajaxInfo.handleStart)
    	            .bind('ajaxSend', 	window.ajaxInfo.handleSend)
    	            .bind('ajaxError', window.ajaxInfo.handleError);
    // Création du loader div
  	var tmpDiv = document.createElement('DIV');
  	tmpDiv.id = "eme-loader";
  	tmpDiv.style.display = 'none';
  	document.body.appendChild(tmpDiv);
  	// Stockage du noeud jQuery
  	window.ajaxInfo.loaderDiv = $(tmpDiv);
});
