/*!
 * Unister Frontend-Core tools.js
 * http://www.unister.de/
 *
 * Copyright 2011, Unister GmbH
 * All rights reserved
 *
 * Date: Mon Feb 21 00:00:00 2011 - 0500
 * 
 */

/*
 * Loggt Ausgaben in einem Array mit, gibt diese nur in der Konsole aus, sofern eine existiert.
 * Gibt es keine Konsole, wird die Funktionsweise des Codes nicht beeinträchtigt
 * Benutzung: log('inside coolFunc',this,arguments);
 * http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
 */
window.log = function(){
    log.history = log.history || [];   // logs in einem array speichern
    log.history.push( arguments );
    if( this.console ){
        console.log( Array.prototype.slice.call(arguments) );
    }
};

$(window).load(function(){
    (function (win){
        var performance =
            win.performance ||
            win.webkitPerformance ||
            win.mozPerformance ||
            win.msPerformance ||
            {},
            timings = performance.timing || {},
            testLoad = function(){
                if(timings.loadEventEnd > 0){
                    log( 'Ladezeit in Ms: ' + (timings.loadEventEnd - timings.navigationStart) );
                    win.clearTimeout(timer);
                }else{
                    var timer = win.setTimeout(testLoad, 1000);
                }
            };

        //test if loadEventEnd
        testLoad();
    })(window);

});

/*
 * Profiling über die Konsole für den IE8
 * @todo TESTING, da scheinbar noch buggy?yahoo profiler
 */
/*if( console && !console.profile) {
    console._timers = {};
    console.profile = function(name){
        console._timers[name] = new Date();
    };
    console.profileEnd = function(name){
        var time = new Date() - console._timers[name];
        console.info(name + ': ' + time + 'ms')
    }
}*/

/*
 * Prüfung und Sammlung von document.writes,
 * ermöglicht whitelisting per regex, wenn gewünscht
 */

// (function(doc){
//   var write = doc.write;
//   doc.write = function(q){ 
//     log('document.write(): ',arguments); 
//     //regex für erlaubte document.writes
//     if (/./.test(q)) write.apply(doc,arguments);  
//   };
// })(document);
