$(document).ready(function(){

		// Auto-populate form fields with their titles
		$('.populate').each(function(){
			var e = $(this);
			if (!e.val()) { e.val(e.attr('title')); }
			e.focus(function(){
				var e = $(this);
				if (e.attr('title') == e.val()) { e.val(''); }
			});
					
			e.blur(function(){
				var e = $(this);
				if (!e.val()) { e.val(e.attr('title')); }
			});
		});	
		
		// Simple animation for #top links
		$("a[href=#top]").click(function(){
			$("html, body").animate(
				{ scrollTop:0 }, 500
			);
			return false;
		});
					
		// Make external links open in new windows	
		$('a').each(function(){
			if (this.href.match('^https?://(?!'+document.location.hostname+')')) {
				$(this).attr('target', '_blank');
				$(this).addClass('external');
			}
		});
		
		// PDF tracking for Analytics (asynchronous)
		$('a[href$=.pdf]').click(function(){
			_gaq.push(['_trackEvent', 'PDF', $(this).attr("href")]);
		});
		
		// Add PDF class to PDF links
		$('div.two-col a[href$=.pdf], div.issue-col li a[href$=.pdf], ul.search-results a[href$=.pdf], div.related-sidebar a[href$=.pdf]').each(function(){
			$(this).addClass('pdf');
		});	
		
		// Menus
		$("ul#nav li").hoverIntent(showMenu, hideMenu);
		
		function showMenu(){
			$(this).addClass('hover');
			$("ul", this).slideDown('fast');
		}
		
		function hideMenu(){
			current = $(this);
			$("ul", this).slideUp('fast', function(){$(current).removeClass('hover')});
		}
		
		// Home Page Features
		$("#feature div.info div").hide();
		$("#feature h2 a").click(function(){
			var target = $(this).attr("href");
			$(this).toggleClass("current");
			$(target).slideToggle('fast');
			return false;
		});
		$("#feature ul").cycle({
			pager: '#pager',
			pause: 1,
			timeout: 5000
		});
		
		// Issues page masonry
		$('ul.issues').addClass('masonry').masonry({ singleMode: true });
		
		// Tags truncation
		if($('div.tags li').length > 0)
		{
			$('div.tags li#more-tags a').show().click(function(){
				$('div.tags li').show();
				$('div.tags li#more-tags').hide();
				return false;
			});
			$('div.tags li:gt(9):not(#more-tags)').hide();
		}
});
