(function($) {
	imageCache = [];
	function cacheImage(src) {
		var img = document.createElement('img');
		$(img).attr('src', src);
		imageCache.push(img);
	}
	
	function hoverImage() {
		$(this).attr('src', $(this).data('hover'));
	}
	
	function defaultImage() {
		$(this).attr('src', $(this).data('default'));
	}
	
	function hoverSubmenu() {
		$(this).find('img.dynamic').each(function() {
			$(this).attr('src', $(this).data('hover'));
		});
	}
	
	function leaveSubmenu() {
		$(this).find('img.dynamic').each(function() {
			$(this).attr('src', $(this).data('default'));
		});
	}
	
	$(document).ready(function() {
		$('img.dynamic').each(function() {
			var submenu = false;
			var className = this.className;
			var classes = className.split(/\s+/);
			$(this).data('default', $(this).attr('src'));
			for(var i=0,n=classes.length; i<n; ++i) {
				var matches = classes[i].match(/^hover-(.+)$/);
				if(matches && matches[1]) {
					var hover = matches[1];
					cacheImage(hover);
					$(this).data('hover', hover).hover(hoverImage, defaultImage);
				}
			}
		});

		$('#access .top-level-item').each(function() {
			if(0 < $(this).find('.submenu').length) {
				$(this).find('img.dynamic').unbind('mouseenter', hoverImage).unbind('mouseleave', defaultImage);
				$(this).hover(hoverSubmenu, leaveSubmenu);
			}
		});

		$('#top-navigation .top-level-item').bind('focus mouseenter', function() {
			$(this).extend($('#branding')).css('z-index', '10000');
			$(this).find('.submenuWrapper').fadeIn(400);
		}).bind('blur mouseleave', function() {
			$(this).find('.submenuWrapper').fadeOut(400, function() {$(this).parent().extend($('#branding')).css('z-index', 'inherit');});
		});

		$('ul').each(function() {
			var items = $(this).children('li');
			items.first().addClass('first');
			items.last().addClass('last');
		});

		$('a.external').click(function(e) {
			e.preventDefault();
			window.open($(this).attr('href'));
		});
		
		$('#content article .entry-content').each(function() {
			jQuery(this).children('p').first().addClass('first');
		});

		$('.slideshow').each(function() {
			$(this).css('overflow', 'hidden');
			var nav = document.createElement('nav');
			var prev = document.createElement('a');
			$(prev).addClass('prev');
			var next = document.createElement('a');
			$(next).addClass('next');
			$(nav).append(prev).append(next);
			$(this).append(nav);
			$(this).hover(function() {$(this).find('nav').fadeIn();}, function() {$(this).find('nav').fadeOut();});
			$(this).find('.slides').cycle({
				'fx': 'fade',
				'pause': 1,
				'prev': 'nav .prev',
				'next': 'nav .next',
				'speed': 300,
				'timeout': 4000
			});
		});
		
		if(('undefined' != typeof jquery_scripts) && jquery_scripts) $.each(jquery_scripts, function(indx, func) {
			func(jQuery);
		});
	});
})(jQuery);

