var GoogleAnalytics = function()
{
	var pageTracker = null;	
	var pageviewTracked = false;
		
	var trackPageview = function(page)
	{
		if(pub.trackerCode == null)
			alert("Configuration error in google analytics: the trackerCode is null. Please set the tracker code (UA-xxxxxx-x) setting the property GoogleAnalytics.trackerCode.");
	
		if(!pageviewTracked)
		{
			try
			{
				pageTracker = _gat._getTracker(pub.trackerCode);
				pageTracker._trackPageview(page);
			} 
			catch(err) { }
			
			pageviewTracked = true;
		}
	};
	
	var setCustomVar = function(index, name, value)
	{
		if(!pageviewTracked)
			alert("Invalid call to GoogleAnalytics.setCustomVar(): call the trackPageview() method first.");
		
		if(name == null)
		{
			alert("Invalid call to GoogleAnalytics.setCustomVar(): the argument 'name' is null.");
			return;
		}
		
		if(pageTracker == null)
		{
			// the pageTracker can be null if the google analytics server is temporarily unavailable.
			// in this case we ignore the setCustomVar() function call and just return.
			return;
		}

		// If the value is null, then an empty string is used.
		value = value || "";
		
		try
		{
			pageTracker._setCustomVar(
				index,
				name.toString(),
				value.toString(),
				2
			);
		}
		catch(err) { }		
	};
	
	var trackEvent = function(category, action, label, value)
	{
		if(!pageviewTracked)
			alert("Invalid call to GoogleAnalytics.trackEvent(): call the trackPageview() method first.");
			
		if(category == null)
		{
			alert("Invalid call to GoogleAnalytics.trackEvent(): the argument 'category' is null.");
			return;
		}

		if(action == null)
		{
			alert("Invalid call to GoogleAnalytics.trackEvent(): the argument 'action' is null.");
			return;
		}
		
		if(pageTracker == null)
		{
			// the pageTracker can be null if the google analytics server is temporarily unavailable.
			// in this case we ignore the trackEvent() function call and just return.
			return;
		}		
		
		try
		{
			pageTracker._trackEvent(
				category.toString(),
				action.toString(),
				label,
				value
			);
		}
		catch(err) { }		
	};

	var pub = {
		trackerCode : null,
		trackPageview : trackPageview,
		setCustomVar : setCustomVar,
		trackEvent : trackEvent
	};
	
	return pub;
}();
