$(document).ready(
	function() {
		var anchor = $('a');
		
		anchor.each(onclick);
		
		function onclick() {
			var elem = this;
			var thisptr = $(this);
			var href = this.href;
			if(!isHudsonLink(href)) return;
			if(!isPDF(href)) return;
			//mprint("isPDF href="+href);
			thisptr.click(doCount);
			
			function doCount(evt) {
				var lhref = fixLink(href);
				//mprint("counting /PDF/"+lhref);
				evt.preventDefault();
				//return;
				_gaq.push(['_trackPageview',"/PDF/"+ href]);
				
				setTimeout(reloc,100);
				
				function reloc() {
					location = elem.href;
					// to always open in new window
					//window.open(elem.href);
				}
			}
		
		}
		function getDomain(url) {
			var urlParts = url.split("/") 
			return urlParts[2];
		}

		function isHudsonLink(href) {
			var lhref = href.toLowerCase();
			if(lhref.indexOf("http://hudsontma.org")==0 
				|| lhref.indexOf("http://www.hudsontma.org")==0
				|| lhref.indexOf("http://localhost")==0
				) {
					return true;
			}
			return false;
		}
		function fixLink(href) {
			var a = ["http://hudsontma.org","http://www.hudsontma.org","http://localhost"];

			var lhref = href.toLowerCase();
			var len=0, i, n = a.length;
			for(i=0;i<n;i++) {
				if(lhref.indexOf(a[i])==0) {
					lhref = lhref.substr(a[i].length);
					break;
				}
			}
			if(lhref.charAt(0) == '/')
				lhref = lhref.substr(1);
			return lhref;
		}
	
		function isPDF(href) {
			lhref = href.toLowerCase();
			if(lhref.match(/pdf$/)) return true;
			return false;
		}
		
	}
);

