﻿
/**
* This provides us with a namespace so as not to interfere with the operation of any other included scripts.
*/
var arbGA = {};

arbGA.options = {
    "debug": 0,
    "async": 1,
    "trackAsEvents": 0,
    "trackOutgoing": 1,
    "trackMailto": 1,
    "trackDownload": 1,
    "trackDownloadExtensions": "7z|aac|avi|csv|doc|docx|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xlsx|xml|zip"
};

arbGA.track = function() {
    /**
    * 	Provide Google Analytics Tracking for extra site events such as:
    *	> downloads
    *   > anchors
    *	> mailto
    *	> outgoing
    *
    *	Compatible with new Asynchronous calls or older Syncronous calls.
    *	Can track as Events or Pageviews.
    *
    *	@author	Matt Wurm &lt;matt.wurm@areeba.com.au&gt;
    */

    // Attach onclick event to document only and catch clicks on all elements.
    $(document.body).bind('click.arbGA', function(event) {
        // Catch only the first parent link of a clicked element.

        $(event.target).parents("a:first,area:first").andSelf().filter("a,area").each(function() {

            // Expression to check for absolute internal links.
            var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
            // Expression to check for download links.
            var isDownload = new RegExp("\\.(" + arbGA.options.trackDownloadExtensions + ")$", "i");

            // Is the clicked URL internal?
            if (isInternal.test(this.href)) {
                // Is download tracking activated and the file extension configured for download tracking?
                if (arbGA.options.trackDownload && isDownload.test(this.href)) {
                    // Download link clicked.
                    var extension = isDownload.exec(this.href);
                    if (arbGA.options.trackAsEvents) {
                        arbTrackEvent("download", extension[1].toLowerCase(), this.href.replace(isInternal, ''));
                    } else {
                        arbTrackPageview("virtual/download" + this.href.replace(isInternal, '') + window.location.pathname);
                    }
                } else if ($(this).is("a[href^=#],area[href^=#]")) {
                    if (arbGA.options.trackAsEvents) {
                        arbTrackEvent("self", "click", this.href.replace(isInternal, ''));
                    } else {
                        arbTrackPageview("virtual/self" + this.href.replace(isInternal, ''));
                    }
                }
            }
            else {
                if (arbGA.options.trackMailto && $(this).is("a[href^=mailto:],area[href^=mailto:]")) {
                    // Mailto link clicked.
                    if (arbGA.options.trackAsEvents) {
                        arbTrackEvent("mail", "click", this.href.substring(7));
                    } else {
                        arbTrackPageview("virtual/mail/" + this.href.substring(7) + window.location.pathname);
                    }
                }
                else if (arbGA.options.trackOutgoing && this.href) {
                    // External link clicked.
                    if (arbGA.options.trackAsEvents) {
                        arbTrackEvent("outgoing", "click", this.href);
                    } else {
                        arbTrackPageview("virtual/outgoing/" + this.href + window.location.pathname);
                    }
                }
            }
        });
    });

    function arbTrackEvent(category, action, label, value) {
        if (arbGA.options.debug) {
            arbGA.debug('_trackEvent(' + category + ', ' + action + ', ' + label + ', ' + value + ')');
        } else {
            if (label.length > 0) {
                if (arbGA.options.async) {
                    _gaq.push(['_trackEvent', category, action, label, value]);
                } else {
                    _pageTracker._trackEvent(category, action, label, value);
                }
            } else {
                if (arbGA.options.async) {
                    _gaq.push(['_trackEvent', category, action, label]);
                } else {
                    _pageTracker._trackEvent(category, action, label);
                }
            }
        }
    }

    function arbTrackPageview(path) {
        if (arbGA.options.debug) {
            arbGA.debug('_trackPageview(' + path + ')');
            arbGA.debug('_trackPageLoadTime(' + path + ')');//ERB 12May2011
        } else {
            if (arbGA.options.async) {
                _gaq.push(['_trackPageview', path]);
                _gaq.push(['_trackPageLoadTime', path]); //ERB 12May2011
            } else {
            _pageTracker._trackPageview(path);
            _pageTracker._trackPageLoadTime(path); //ERB 12May2011
            }
        }
    }

};
arbGA.debug = function(msg) {
    if (arbGA.options.debug) {
        console.log(msg);
        alert(msg);
    }
};

$(document).ready(function() {
    arbGA.track();
});
