// Sets up GA tracker
try {
	var pageTracker = _gat._getTracker("UA-3741125-56");
	pageTracker._trackPageview();
} catch (err) {
}

// Adds appropriate event tracking to various DOM elements
(function ($) {
	$(function () {
		// Workout which page we're on
		var body = $('body');

		function tidyString(str) {
			return $.trim(str.replace(new RegExp(/\s{2,}/g), ' '));
		}

		function addSelect(selector, category, action) {
			$(selector).change(function () {
				pageTracker._trackEvent(category, action, tidyString($(this).val()));
			});
		}

		if (body.is('#scatterChart')) {
			addSelect('#slctFundManager', 'SearchFilter', 'Fund manager');
			addSelect('#slctFundSector', 'SearchFilter', 'Fund sector');
			addSelect('#slctFundResults', 'SearchResult', 'Fund');
			addSelect('#slctTimescale', 'InterfaceFilter', 'Timescale');
		} else if (body.is('#performanceChart')) {
			addSelect('input[name=rgInstrumentType]', 'SearchFilter', 'Comparator');
			addSelect('#slctFundManager', 'SearchFilter', 'Fund manager');
			addSelect('#slctFundSector', 'SearchFilter', 'Fund sector');
			addSelect('#slctFundResults', 'SearchResult', 'Fund');
			addSelect('#slctTimevalue', 'InterfaceFilter', 'Timescale');
		} else if (body.is('#portfolioOverview')) {
			addSelect('#Indices', 'SearchFilter', 'Index');
			addSelect('#Sectors', 'SearchFilter', 'FE Sector');
			addSelect('#DurationMonths', 'InterfaceFilter', 'Timescale');
			$('#viewPortfolioOverview').click(function () {
				pageTracker._trackEvent('PDF', 'Downloaded', 'Portfolio overview');
			});
		} else if (body.is('#fundInformation')) {
			addSelect('#ProductTypeCB', 'SearchFilter', 'Product type');
			addSelect('#FundRange', 'SearchFilter', 'Fund range');
			addSelect('#Manager', 'SearchFilter', 'Manager');
			addSelect('#CrownRatingCB', 'SearchFilter', 'Crown rating');
			addSelect('#Sector', 'SearchFilter', 'Sector');
			addSelect('#ItemsPerPageCB', 'InterfaceFilter', 'Per page');

			// The tab navigation
			$('.tabsNavigation #tabFirst, .tabsNavigation #tabSecond, .tabsNavigation #tabThird, .tabsNavigation #tabFourth, .tabsNavigation #tabFifth').click(function () {
				pageTracker._trackEvent('Interface', 'Tab change', tidyString($(this).text()));
			});

			// Downloading the PDFs
			$('#Prices tr .fundName a').click(function () {
				pageTracker._trackEvent('Document', 'Downloaded', tidyString($(this).text()));
			});

			// Viewing the charts
			$('#Prices a[href^=Chart.aspx]').click(function () {
				pageTracker._trackEvent('Chart', 'Viewed', tidyString($(this).parents('tr').find('.fundName').text()));
			});

			// Viewing the charts
			// @@TODO: get the correct ID for this element
			$('#updateShortlist').click(function () {
				$('#Prices input:checkbox:checked').each(function () {
					pageTracker._trackEvent('Shortlist', 'Added', tidyString($(this).parents('tr').find('.fundName').text()));
				});
			});
		} else if (body.is('#ShortListPage')) {
			$('#compareFunds').click(function () {
				$('#Prices input:checkbox:checked').each(function () {
					pageTracker._trackEvent('Fund', 'Compared', tidyString($(this).parents('tr').find('.fundName').text()));
				});
			});
		}
	});
})(jQuery);

