﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1-vsdoc.js" />


/*
    trackExternalLinks to google analytics, you can select <a> elements directly or a ancestor element.
    $('a.external').trackExternalLinks();
    $('#sidebar').trackExternalLinks();
    $('div.widget').trackExternalLinks();
*/

(function ($) {
    $.fn.trackExternalLinks = function (settings) {
        var config = {
            category: 'External Links',
            action: 'clickExternalLink'
        };
        if (settings) $.extend(config, settings);
        var links;
        if ($(this).is('a')) {
            links = $(this);
        }
        else {
            links = $(this).find('a');
        }
        $(links).each(function (index) {
            $(this).click(function () {
                try {
                    var loc = $(this).attr('href');
                    if (loc != undefined) {
                        if (loc.substring(0, 4) === 'http') {//if it doesn't start with href it is not an external link.
                            _gaq.push(['_trackEvent', config.category, config.action, loc]);
                            setTimeout('function(){}', 100);
                        }
                    }
                } catch (err) { }
            });
        });
        return this;
    }
})(jQuery);
