// optimisation, mise en cache des $("elem") 
var e = []; //tableau contenant le cache des éléments

function ce(id) { 
    //Retourne l'objet (du cache, et s'il n'existe pas, va la chercher...)

    if ( !e[id] ) {
        e[id] = $(id);
    }

    return e[id];
}

var App = {
	
	init: function() {

        Chrono.start("main");
        Debug.init();
        Debug.log("start");
        			
        if( $("#news, .hid").length > 0 ) {
            Hiddable.init();
        }		
			
		Debug.log("end main init");
	}
};

var Hiddable = {
	init: function() {
		//On ajoute la classe hiddable à tous les h3 qui sont dans #news

		$("#news h4, .hid h4").addClass("hiddable");

		$(".hiddable").next("div").hide();
		$("h4").click(function() {
			if( !$(this).hasClass("active") ) {
				Hiddable.hideAll();
				$(this).addClass("active").next("div").show("fast");
			} else {
				$(this).removeClass("active").next("div").hide("fast");
			}
		});
	},
	
	hideAll: function() {
		$(".hiddable").next("div").hide("fast");
		$("h4").removeClass("active");
	}
};

var Chrono = {
    //Chronomère de l'application et les fonctions qui lui sont associées.
    //Attention, pour ne pas trop fausser les mesures, on ne vérifie pas si les compteurs existent!!! donc il faut les lancer avant de les stopper ou de les lire...

    data: new Array(), //Liste des timestamps de départ
    
    start: function( counter ) {
        //Démarrage d'un compteur
        var d = new Date();
        this.data[counter] = new Array();
        this.data[counter]['start'] = d;
    },
    
    stop: function( counter ) {
        this.data[counter]['stop'] = new Date();
    },
    
    inter: function( counter ) {
        //Retourne un temps intermédiaire

        var s = new Date();
        var d = s - this.data[counter]['start'] ;
        //Debug.log("chrono : " + counter + " : " + d );
        return d;
    },
    
    read: function( counter ) {
        var d = this.data[counter]['stop'] - this.data[counter]['start'] ;
        Debug.log("chrono : " + counter + " : " + d );
    },
    
    sr: function( counter ) {
        //stop and read
        
        this.stop( counter );
        this.read( counter );
    }
};

var Debug = {
    
    level: null, //Niveau de débug
    
    init: function() {
        this.level = ce("body").attr("data-debug");
        ce('#log').click(function() {
            //alert("reset");
            Debug.clear();
        });
       
        //this.mouseTracking();
        
    },
    
    mouseTracking: function() {
    
        if( this.level > 0 ) {
            //on traque la souris
            $().mousemove( function(e) {
                var x = e.pageX;
                var y = e.pageY;
                var x1 = x - 272;
                var y1 = y - 105;
                
                ce("#mousePos").html("x:" + x + " y:" + y + "<br>x: " + x1  + " y:" + y1);
            });
        }
        
    },
    
    log: function( msg ) {
        
        if( this.level > 0 ) {
            var t = Chrono.inter("main");
            ce('#log').prepend("<p>@" + t + "ms : <br>" + msg + "</p>");       
        }
        
    },
    
    clear: function() {
        ce('#log').empty();
    },
    
    stop: function() {
        //onFaitToutPlanter();
        throw "exit";
    }
    
};

/////////
// INIT
////////
$(document).ready(App.init);
