var rel, rel_parts, dl, ext;
var _gaq = _gaq || [];
var dl_types = ['doc','jpg','xls','ppt','pdf','zip','txt','mov','flv','avi','mp3'];

$(document).ready(function() {
	$('a').each(function() {
		var href = $(this).attr("href");

		// check for no href
		if (href == undefined || href == '#')
			return true;
		// check for external links
		else if (href.indexOf("http://") == 0) {
			rel = $(this).attr("rel");

			// check if this is a shadowbox video
			if (rel != '') {
				rel_parts = rel.split(";");

				if (rel_parts[0] == 'shadowbox') {
					$(this).click(function() {
						_gaq.push(['_trackEvent', 'Videos', 'Play', href ]);
					});
				}
			}
			// check for outbound clicks
			else if (href.indexOf("loveandrespect.com") == -1) {
				$(this).click(function() {
					_gaq.push(['_trackEvent', 'Outbound', href.match(/:\/\/(.[^/]+)/)[1], href ]);

					if ($(this).attr("target") != "_blank") {
						setTimeout('document.location = "' + href + '"', 100);
					}
					else {
						return true;
					}

					return false;
				});
			}
		}
		else {
			// check for downloads
			ext = href.split('.').reverse();
			ext = ext[0].toLowerCase();

			if (jQuery.inArray(ext, dl_types) != -1) {
				$(this).click(function() {
					_gaq.push(['_trackEvent', 'Downloads', ext.toUpperCase(), href ]);

					if ($(this).attr("target") != "_blank") {
						setTimeout('document.location = "' + href + '"', 100);	
					}
				});
			}
		}

		return true;
	});
});
