var potato = potato || {};
potato.modules  = potato.modules || {};

/**
 * Asynchronous Google Analytics Tracker.
 * This snippet must be inserted before all other scripts.
 */
var _gaq = [],
    _trackOnStart = null;

/**
 * Speed up the onload event with setTimeout.
 */
setTimeout(function() {
    _trackOnStart = new Date();
    var g = document.createElement('script'),
        s = document.getElementsByTagName('script')[0];
    g.async = true;
    g.src = '//www.google-analytics.com/ga.js';
    s.parentNode.insertBefore(g, s);
}, 0);

/**
 * @constructor
 * @param {String} accountID Sets the web property ID (e.g. UA-65432-1) for the tracking object.
 */
potato.modules.Tracker = function(accountID){
    
    if(accountID == undefined)
        throw '[Tracker] You must have to set account ID (e.g. UA-65432-1) to Tracker.';
        
    
    
    /**
     * @param {String} pageURL Optional parameter to indicate what page URL to track metrics under.
     * When using this option, use a beginning slash (/) to indicate the page URL.
     */
    this.trackPageView = function(){
        _gaq.push(['_trackPageview', arguments[0]]);
    }
    
    /**
     * @param category (required) {String} The name you supply for the group of objects you want to track.
     * @param action   (required) {String} A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object. 
     * @param label    (optional) {String} An optional string to provide additional dimensions to the event data. 
     * @param value    (optional) {String} An integer that you can use to provide numerical data about the user event.
    */
    this.trackEvent = function(category, action ){
        _gaq.push(['_trackEvent', category, action, arguments[2], arguments[3]]);
    }

    onLoad = function(){
        var trackOnLoad = new Date(),
            t = trackOnLoad.getTime() - _trackOnStart.getTime();
        t = parseInt(t/100) * 100;
        _gaq.push(
            ['_setAccount', accountID],
            ['_setCustomVar', 1, 'loadingPageTime', t.toString(), 2],
            ['_trackPageview']
        );
    }
    
    onLoad();
}
