/**
 * Class which logs all actions to Google Analytics for a given pagename and version.
 * 
 * @version 2
 * @since 2009-04-15
 * @author Jasper van Wanrooy
 */

function jpxLogger() { }

jpxLogger.basePath       = '/0/';
jpxLogger.namePage       = 'name';
jpxLogger.versionPage    = 'version';
jpxLogger.analyticsCode  = 'tracker_code';

/**
 * Array which keeps track of the filenames which are already logged.
 * The key is the filename, the value is the amount of times it is logged.
 */
jpxLogger.loggedActions  = new Array;

jpxLogger.initialize = function() {
	if(!jpxLogger.tracker) {
		if(typeof(_gat) != 'undefined') {
			jpxLogger.tracker = _gat._getTracker(jpxLogger.analyticsCode);
			jpxLogger.tracker._initData();
			return true;
		}
		else {
			return false;
		}
	}
	return true;
}

/**
 * Function which logs an action to Google Analytics.
 * 
 * @param string actionname
 * @param bool only_once
 */
jpxLogger.logAction = function(actionname, only_once) {
	if(jpxLogger.initialize()) {
		/**
		 * Determine the current filename.
		 */
		var filename = jpxLogger.basePath + actionname + '_' + jpxLogger.typePage + '_' + jpxLogger.versionPage + '.html';
		
		/**
		 * When an action should be logged only once --> check whether it is already logged.
		 */
		if(only_once && jpxLib.arrayKeyExists(filename, jpxLogger.loggedActions)) {
			return;
		}
		
		/**
		 * Log the action.
		 */
		jpxLogger.tracker._trackPageview(filename);
		
		/**
		 * Increase the logging stat of the filename.
		 */
		if(jpxLib.arrayKeyExists(filename, jpxLogger.loggedActions) === false) {
			jpxLogger.loggedActions[filename] = 0;
		}
		jpxLogger.loggedActions[filename] += 1;
	}
}
