$(document).ready(function() {
    

   $('#slideshow-home').cycle();

	// Open external links in a new window
	$('a[href^="http"]').each(function() {
		if(this.href.indexOf(location.hostname.replace(/www./gi, "")) == -1) {
			$(this).click(function() { window.open(this.href, "_blank"); return false; });
		}
	});
	
	// Open PDFs in a new window
	$('a[href$=".pdf"]').each(function() {
		$(this).click(function() { window.open(this.href, "_blank"); return false; });
	});
		
	// Redirects for select "jump" lists
	$("select#directory-filter").change(function(){
		if($(this).val() != '') {
			var target = $(this).val();
			var hash   = target;
			var nested = null;
			
			if(target.indexOf(':') != -1)
			{
				// process nested entries
				var str = (target.split(':'));
				target = str[0];
				nested = str[1];
			}

			$(".directory-entries").hide();
			if(target == 'all') {
				window.location.hash = '';
				$(target).find('.entry').show();
				$(".directory-entries").show('slow');
			} else {
				window.location.hash = hash;
				if(nested != null) {
					$(target).find('.entry').hide();
					$("#"+nested).show();
				}
				$(target).show('slow');
			}
		}
	});
	
	// Detect hash in url for for directory pages
	var target = window.location.hash;
	var nested = null;
	
	if(target && $("select#directory-filter").length) {
		$("select#directory-filter").val(target);
		
		if(target.indexOf(':') != -1)
		{
			// process nested entries
			var str = (target.split(':'));
			target = str[0];
			nested = str[1];
		}
		
		$(".directory-entries").hide();
		
		if(nested != null) {
			$(target).find('.entry').hide();
			$("#"+nested).show();
		}
		$(target).show('slow');
	}
	
	// Smooth-scroll internal links
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
			var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
			var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($target) {
				var targetOffset = $target.offset().top;
				$(this).click(function() {
					$('html, body').animate({scrollTop: targetOffset}, 1000);
					return false;
				});
			}
		}
	});

});
