(function($){
	$(function(){
		
		$('img.hover').each(function(){
			$(this).hover(
				function(){
					$(this).attr( 'src', $(this).attr('src').replace(/(\..*?)$/, '_hover' + "$1" ) );
				},
				function(){
					$(this).attr( 'src', $(this).attr('src').replace(/\_hover(\..*?)$/, "$1" ) );
				}
			);
			
			var img = new Image();
			img.src = $(this).attr('src').replace(/(\..*?)$/, '_hover' + "$1" );
		});
		
		$('.hover').not('img').each(function(){
			if ( $(this).css('position') != 'absolute' || $(this).css('position') != 'relative' ) {
				$(this).css('position', 'relative');
			}
			$(this).append('<div></div>');
			var sprite = $(this).children().filter(':last');
			sprite.css({ background: '#000', margin: 0, padding: 0, border: 0, position: 'absolute', top: 0, left: 0, width: $(this).outerWidth(), height: $(this).outerHeight(), opacity: 0 });
			sprite.hover(
				function(){
					$(this).parent().addClass('hovered');
					$(this).find('img').each(function(){
						$(this).attr( 'src', $(this).attr('src').replace(/(\..*?)$/, '_hover' + "$1" ) );
					});
				},
				function(){
					$(this).parent().removeClass('hovered');
					$(this).find('img').each(function(){
						$(this).attr( 'src', $(this).attr('src').replace(/\_hover(\..*?)$/, "$1" ) );
					});
				}
			).mouseup(function(){
				$(this).parent().removeClass('hovered');
			});
			
			if ( $(this).find('img').length > 0 ) {
				$(this).find('img').each(function(){
					var img = new Image();
					img.src = $(this).attr('src').replace(/(\..*?)$/, '_hover' + "$1" );
				});
			}
			
			if ( $(this).find('a').length > 0 ) {
				sprite.click(function(){
					var href = $(this).parent().find('a').eq(0).attr('href');
					if ( href.match(/^http/) )
						window.open( href, '_blank' );
					else
						window.location.href = href;
					return false;
				});
			}
		});
	});
})(jQuery);
