﻿(function ($) {
    $.googletrack = function (opts) {
        opts = jQuery.extend({
            senders: $('a'),
            category: 'External'
        }, opts);

        function initGA() {
            try {
                addTracking();
            } catch (err) {
                console.log(err.toString());
            }
        };

        function addTracking() {
            $._gaq = _gaq || [];
            $._gaq.push(['_setAccount', 'UA-17818538-1']);
            $._gaq.push(['_trackPageview']);

            if (opts.category == "FilterByCountry") {
                opts.senders.each(function () {
                    $(this).click(function (event) {
                        _gaq.push(['_trackEvent', opts.category, 'Submit', $(this).parent().find("select").val()]);
                    });
                });
            }
            else if (opts.category == "Webcast|Podcast") {
                opts.senders.each(function () {
                    $(this).click(function (event) {
                        if ($(this).attr('href') != undefined) {
                            _gaq.push(['_trackEvent', opts.category, 'Click-Download', $(this).attr('href')]);
                        }
                    });
                });
            }
            else if (opts.category == "Homepage") {
                opts.senders.each(function () {
                    $(this).click(function (event) {
                        if ($(this).attr('href') != undefined) {
                            _gaq.push(['_trackEvent',  opts.category, 'Click-Ad', $(this).attr('href')]);
                        }
                    });
                });
            }
        };

        initGA();
    }
})(jQuery);
