// Initialize total_seconds to zero.
var total_seconds = 0;

// Initialize time check to now.
var now = new Date();
var time_check = now.getTime();

// Upon activity...
function timeBrowserActivity() {
    // If elapsed time since last activity is < one second...
    var now = new Date();
    if (now.getTime() - time_check <= 1000) {
        // Ignore activity.
        return true;
    }
    // If elapsed time is > one second...but < 30 seconds...
    else if (now.getTime() - time_check > 1000 && now.getTime() - time_check < 30000) {
        // Set total_seconds to the difference since the last time check.
        total_seconds += (now.getTime() - time_check) / 1000;
        time_check = now.getTime();
        return true;
    }
    // if elapsed time is > 30 seconds...
    else {
        // Ignore the large block of time by resetting the time check and not recording anything.
        time_check = now.getTime();
        return false;
    }
}

// These events will probably be triggered when the user is "using" this node.
dojo.event.connect(window, 'onscroll', timeBrowserActivity);
dojo.event.connect(window, 'onkeypress', timeBrowserActivity);
dojo.event.connect(window, 'onmousemove', timeBrowserActivity);
dojo.event.connect(window, 'onclick', timeBrowserActivity);

// Save result.
// node_title comes from in the html.
// Time is saved for any URL with node=Title.
if (null != node_title && '' != node_title) {
    dojo.event.connect(window, 'onunload', function(){
        // Skip short page views...user may just be browsing past.
        if (Math.round(total_seconds) > 5) {
            dojo.io.bind({
                url: '/io/activity.php',
                content: { node: node_title, activity: Math.round(total_seconds) }
            });
        }
    });
}


