var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25203340-1']);
_gaq.push(['_setDomainName', '.opm.gov']);
_gaq.push(['_trackPageview']);

(function () {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$(document).ready(function () {
	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|atom|xml)$/i;
	$('a[href]').each(function () {
		var href = $(this).attr('href');
		if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			$(this).click(function () {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
				if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
					setTimeout(function () { location.href = href; }, 200);
					return false;
				}
			});
		} else if (href && href.match(/^mailto\:/i)) {
			$(this).click(function () {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
			});
		} else if (href && href.match(filetypes)) {
			$(this).click(function () {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var uriPath = new URI(href);
				var baseUrl = new URI($(location).attr('href'));

				//alert('STATIC Link Clicked - _gaq.push(["_trackEvent", "Download", "Click-' + extension + '", ' + uriPath.resolve(baseUrl) + ']);');
				_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, uriPath.resolve(baseUrl)]);
				if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
					setTimeout(function () { location.href = href; }, 200);
					return false;
				}
			});
		}
	});
});


/*
* http://code.google.com/p/js-uri/
* An URI datatype.  Based upon examples in RFC3986.
*/

// Constructor for the URI object.  Parse a string into its components.
function URI(str) {
	if (!str) str = "";
	// Based on the regex in RFC2396 Appendix B.
	var parser = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
	var result = str.match(parser);
	this.scheme = result[1] || null;
	this.authority = result[2] || null;
	this.path = result[3] || null;
	this.query = result[4] || null;
	this.fragment = result[5] || null;
}

// Restore the URI to it's stringy glory.
URI.prototype.toString = function () {
	var str = "";
	if (this.scheme) {
		str += this.scheme + ":";
	}
	if (this.authority) {
		str += "//" + this.authority;
	}
	if (this.path) {
		str += this.path;
	}
	if (this.query) {
		str += "?" + this.query;
	}
	if (this.fragment) {
		str += "#" + this.fragment;
	}
	return str;
};

// Introduce a new scope to define some private helper functions.
(function () {
	// RFC3986 §5.2.3 (Merge Paths)
	function merge(base, rel_path) {
		var dirname = /^(.*)\//;
		if (base.authority && !base.path) {
			return "/" + rel_path;
		}
		else {
			return base.path.match(dirname)[0] + rel_path;
		}
	}

	var DoubleDot = /\/((?!\.\.\/)[^\/]*)\/\.\.\//;

	function remove_dot_segments(path) {
		if (!path) return "";
		var newpath = path.replace(/\/\.\//g, '/');
		newpath = newpath.replace(/\/\.$/, '/');
		while (newpath.match(DoubleDot)) {
			newpath = newpath.replace(DoubleDot, '/');
		}
		newpath = newpath.replace(/\/([^\/]*)\/\.\.$/, '/');
		while (newpath.match(/\/\.\.\//)) {
			newpath = newpath.replace(/\/\.\.\//, '/');
		}
		return newpath;
	}

	// RFC3986 §5.2.2. Transform References;
	URI.prototype.resolve = function (base) {
		var target = new URI();
		if (this.scheme) {
			target.scheme = this.scheme;
			target.authority = this.authority;
			target.path = remove_dot_segments(this.path);
			target.query = this.query;
		}
		else {
			if (this.authority) {
				target.authority = this.authority;
				target.path = remove_dot_segments(this.path);
				target.query = this.query;
			}
			else {
				if (!this.path) {
					target.path = base.path;
					if (this.query) {
						target.query = this.query;
					}
					else {
						target.query = base.query;
					}
				}
				else {
					if (this.path.charAt(0) === '/') {
						target.path = remove_dot_segments(this.path);
					} else {
						target.path = merge(base, this.path);
						target.path = remove_dot_segments(target.path);
					}
					target.query = this.query;
				}
				target.authority = base.authority;
			}
			target.scheme = base.scheme;
		}

		target.fragment = this.fragment;

		return target;
	};
})();


