if (!nws) {
    var nws = {}
};

if (!nws.utils) {
    nws.utils = {};
}

/*  *************************************************************
    Benchmarks
    ************************************************************* */
    nws.utils.benchmarks = {};

    /*  -------------------------------------------------------------
        nws.utils.benchmarks.simple

        var benchmark = new nws.utils.benchmarks.simple('Mein toller Benchmark');
        benchmark.bench('start');
        do_something();
        benchmark.bench('did_something');
        benchmark.report();
        ------------------------------------------------------------- */
    nws.utils.benchmarks.simple = function(title) {
        var start = new Date().getTime();

        var benches = [];

        this.bench = function(msg) {
            var now = new Date().getTime();
            var obj = {
                id: benches.length,
                msg: msg,
                time: now,
                difference_overall: now - start
            };

            if (benches.length) {
                obj.difference_previous = now - benches[ benches.length - 1 ].time;
            }

            benches.push(obj);
        }

        this.report = function(output_report, order_by) {
            var sort_function;
            if (order_by === 'runtime') {
                sort_function = function(a, b) {
                    a = (typeof a.difference_previous === 'undefined') ? 0 : a.difference_previous;
                    b = (typeof b.difference_previous === 'undefined') ? 0 : b.difference_previous;

                    var result = 0;
                    if (a < b) {
                        result = 1;
                    }
                    else {
                        result = -1;
                    }

                    return result;
                }
            }

            var output = 'Benchmark ' + (title ? '"' + title + '"' : '' ) + '\n\n';

            var records = jQuery.extend(true, [], benches); // deep copy von benches, siehe http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object
            if (sort_function) {
                records = records.sort(sort_function);
            }

            var overvall_difference = 0;
            for (var i in records) {
                var item = records[i];

                output += item.id + ': ' + item.msg;

                if (typeof item.difference_previous === 'undefined') {
                    output += ' at ' + item.time;
                }
                else {
                    output += ' needed ' + item.difference_previous + ' ms';
                    overvall_difference += item.difference_previous;
                }

                output += '\n';
            }

            output += '\nOverall: ' + (benches[benches.length - 1].time - start) + ' ms (' + overvall_difference + ' ms)';

            if (output_report == 'alert') {
                alert(output);
            }
            else if (output_report == 'console.log') {
                console.log(output);
            }

            return {
                start:   start,
                overall: benches[benches.length - 1].time - start,
                benches: records
            };
        }
    }

