/**
 * @class ep.debug
 * 
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			ep.debug
 */

define("ep/debug", [
	"jquery",
	"ep",
	"util/scope"
], function ($, ep, scope) {

	// set console (sometimes not set in time and hence throws an error)
	try{
		window.console =			window.console || {};
		window.console.debug =		window.console.debug || function(){return;};
		window.console.log =		window.console.log || function(){return;};
		window.console.warn =		window.console.warn || function(){return;};
	}
	catch(error){
	}

	ep.extend( scope('ep.debug'),{
		/**
		 * take ststistic for given function
		 * 
		 * The `ep.debug.statistic()` without a parameter prints the previously taken statistic to the debug console
		 * activate Firebug to see the output or open the debug console in chrome for example
		 * 
		 * The `ep.debug.statistic()` function take a statistic and collects running time for the given function
		 * the statistics which are taken are:
		 * 
		 *  + sum      | total running time for all iterations of functions with given statistic id
		 *  + count    | count of iterations
		 *  + min      | fastest run time
		 *  + max      | slowest run time
		 *  + innersum  | time inside the function when it was calling itself
		 * 
		 * ### Examples
		 * Run a function named randomTest 100 times and take the statistic. Then print the statistic to the console
		 * 
		 * JavaScript:
		 * 
		 *     function randomTest(){
		 *       for(var i = 0; i < 100; i++){
		 *         var a = Math.random();
		 *       }
		 *      };
		 *      for(var i = 0; i < 100; i++){
		 *        ep.debug.statistic('randomStatistic',randomTest);
		 *      }
		 *      ep.debug.statistic();
		 * 
		 * Results:
		 * 
		 *     print statistic
		 *     statistic for id: helloWorld : sum: 14 : count: 100 : min: 0 : max: 1 : innersum: 0
		 * 
		 * 
		 * ### Dependencies
		 * 
		 *  + `ep`
		 * 
		 * @param {undefined} id a unique id for the current function
		 * @param {undefined} function a function for which the statistic should be taken
		 * 
		 * @method statistic
		 * @static 
		 * @member ep.debug
		 * 
		 * @since 6.11.0
		 */
		statistic: function(/*string*/statId, /*func*/statFunc){
			var stats = ep.debug.statistic.data;
			if(arguments.length){
				//if there are arguments we take the statistic for a function
				var startTime = new Date();
				ep.debug.statistic.isRunning = true;
				statFunc();
				ep.debug.statistic.isRunning = false;

				var runningTime = new Date() - startTime;
				// sum, count, min, max, innersum
				if(!stats[statId]){
					stats[statId] = {
						"sum"      : 0,
						"count"    : 0,
						"min"      : 0,
						"max"      : 0,
						"innersum" : 0
					};
				}
				var currentStat = stats[statId];
				if(ep.debug.statistic.isRunning){
					currentStat.innersum += runningTime;
				}
				else{
					currentStat.sum += runningTime;
				}
				currentStat.count++;
				if(runningTime > currentStat.max){
					currentStat.max = runningTime;
				}
				if(runningTime < currentStat.min){
					currentStat.min = runningTime;
				}
			}
			else{
				//if there are no arguments we print the statistic to the console
				var sum = 0;
				console['log']('print statistic');
				$.each(stats,function(index,value){
					console['log']('statistic for id:',
						index,': sum: ',stats[index].sum,
						': count: ',stats[index].count,
						': min: ',stats[index].min,
						': max: ',stats[index].max,
						': innersum: ',stats[index].innersum);
				});
			}
		}
	});

	ep.extend(ep.debug.statistic,{
		data: {}, //define data-object for statistic function
		isRunning: false
	});

	return ep;

});