﻿$(document).ready(function() {
	// login link
	$sblinks = $('a[href^="/Login.aspx"]');
	if ( $sblinks.length ) {
		Shadowbox.setup($sblinks, {
			height: 300,
			width: 450
		});
	}
	
	// global search form
	// without this junk, FF won't work when enter is pressed because MS thought one big form on the page was a good idea
	$('form#aspnetForm').submit(function() {
		$searchInput = $('.searchBox input');
		var search = $searchInput.val();
		if ( $searchInput.length > 0 && search ) {
			window.location.href = '/Pages/Search.aspx?k=' + search;
			return false;
		}
	});
	
	/**
	 * Extra GA tracking
	 */
	// External links
	var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
	var isJS = new RegExp("^javascript:", "i");
	$('a').click(function() {
		if ( !isInternal.test(this.href) && !isJS.test(this.href) ) {
			try {
				pageTracker._trackEvent("outbound", "click", this.href);
			}
			catch (err) {}
		}
	});
		
	// International links
	$('body.page-aboutus-international .regions a').click(function() {
		try {
			pageTracker._trackPageview('/outbound/international/' + $(this).text());
		}
		catch (err) {}
	});
	
	// Social icons
	$('.socialIcons a').click(function() {
		try {
			pageTracker._trackPageview('/outbound/social/' + $(this).attr('class'));
		}
		catch (err) {}
	});
	
	// ShareThis link click
	// We have to repeatedly poll for the link until we find it because
	// it's added to the page by the ShareThis script
	setTimeout(function() {
		var $link = $('.topNavTabs a.stbutton');
		if ( $link.length > 0 ) {
			$link.click(function() {
				try {
					pageTracker._trackPageview('/outbound/sharethis/masthead');
				}
				catch (err) {}
			});
		}
		else {
			setTimeout(this, 250);
		}
	}, 250);
});
