/**
 * @author Tjerk
 * 
 * light-weight event tracking
 */
(function($, undefined) {

// TODO get rid of pageTracker completely!

////////////////// PRIVATE ///////////////////////////
function _trackEvent(cat, action, label, value)
{
	if (!cat) {
		/* no tracking if category is missing */
		return;
	}

	if (window._gaq !== undefined) {
		_gaq.push(['_trackEvent', cat, action, label ? label : '', value])
	} else {
		pageTracker._trackEvent(cat, action, label ? label : '', value);
	}
}

function _action()
{
	return window.CUSTOM_ACTION === undefined ? location.pathname : CUSTOM_ACTION;
}

////////////////// PUBLIC ///////////////////////////
//for js generated elements
trackBindEvent = function(evt) {
	_trackEvent(evt.data.cat, _action(), evt.data.label)
}

trackLiveEvent = function(cat, label) {
	_trackEvent(cat, _action(), label);
}

$(function() {
	$("a").click(function() {
		_trackEvent(/*category*/$(this).attr("rel"), /*action*/_action(), /*label*/$(this).attr("rev"), /*value*/0);
	});

	$('.xdomain').live('click', function(evt) {
		var fn, ref;

		if (this.href) { // anchor
			fn = '_link', ref = this.href
		} else if (this.elements) { // form
			fn = '_linkByPost', ref = this
		} else {
			return
		}

		evt.preventDefault()

		if (window._gaq !== undefined) {
			_gaq.push(['_link', this.href])
		} else {
			pageTracker._link(this.href)
		}
	})

	// track events on hover tabs
	$(".hover_tab").hover(function() {
		_trackEvent(/*category*/"Hover_" + $(this).attr("rel"), _action(), /*label*/$(this).attr("rev"), /*value*/0);
	});	

	// calculate load time
	if (window._lst !== undefined) {
		_trackEvent('LoadTime_sec', _action(), 'Page_LoadTime', new Date().getTime() - _lst);
	}
});

})(jQuery);
