What can I do to optimize this jQuery content gallery script?

Hi Experts,

I am finishing up a "featured content gallery" adapted from the jQuery Tools scrollable and tooltip demos.  It finally does everything I need and seems to work in IE, FF, Safari, Chrome, and Opera.  But now the code could use some tweaking to help it run more efficiently and reduce page load time.  It is implemented in Drupal 6.  Data flows from CCK custom content type fields to a views template for use by jQuery.  This is my first time with jQuery and I would love to learn more about how to better structure this code. Any tips or modifications will be appreciated.

View demo [under development] at: http://www.tacoma.uw.edu/uwt/
// jQuery Tools functions for homepage feature

//use global var to store values from alt and title attributes
//so we can remove/empty them; this is needed ino order to disable browser's default tooltips
var myThumbImgAlt;
var myFullImgPath;

// execute your scripts when the DOM is ready. this is a good habit
$(document).ready(function() {

	//:random filter from http://blog.mastykarz.nl/jquery-random-filter/
	jQuery.jQueryRandom = 0;
		random: function(a, i, m, r) {
			if (i == 0) {
				jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
			return i == jQuery.jQueryRandom;

	// initialize scrollable together with the tooltip 
		// use this single tooltip element for all trigger elements
		tip: '.tooltip',
		relative: true,
		offset: [320, 42],
		position: 'top center',
		cancelDefault: true, //doesn't prevent default tooltip in IE8
		predelay: 200,
		delay: 200
	$(".items img").hover(function() {
			myThumbImgAlt = $(this).attr("alt");
			myFullImgPath = $(this).attr("title");
			//$(this).removeAttr("alt"); //doesn't prevent default tooltip in IE8
			//$(this).removeAttr("title"); //doesn't prevent default tooltip in IE8
			$(this).attr("alt", "");
			$(this).attr("title", "");
			//event.preventDefault(); //doesn't prevent default tooltip in IE8
			//event.preventDefault(); //doesn't prevent default tooltip in IE8
			$(this).attr("alt", myThumbImgAlt);
			$(this).attr("title", myFullImgPath);

	$(".items img").click(function() {
		// calclulate large image's URL from thumbnail title
		if(myFullImgPath === undefined){
			//we haven't hovered yet and the title is still in tact
			var url = $(this).attr("title");
			//get large image's URL from local variable (title removed on hover)
			var url = myFullImgPath;

		// get handle to element that wraps the image and make it semitransparent
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);
		// the large image
		var img = new Image();
		// call this function after it's loaded
		img.onload = function() {
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			// change the image
			wrap.find("img").attr("src", url);

		// begin loading the large image
		img.src = url;

		//get right-side gradient background image from thumbnail class
		var bgColor = $(this).attr("class").replace("thumb_img bg_", "");
		var bgImageUrl = "url(sites/all/themes/uwt/css/theme/features/images/fpf-gradients/" + bgColor + ".png)";
		$("div#large_image_caption_bg").css("background-image", bgImageUrl);

		//get large image title and description from thumbnail content
		var htmlStr = $(this).parent().prev("div").html();
		//get large image href and alt attributes from thumbnail image
		var fullAltStr = $(this).attr("alt");
		var fullTitleStr = $(this).attr("alt");
		var fullHrefStr =  $(this).attr("longdesc");
		$("img#large_image").attr("title", fullTitleStr);
		$("img#large_image").attr("alt", fullAltStr);
		$("a#large_image_link").attr("href", fullHrefStr);
		$("a.caption_heading_anchor").attr("href", fullHrefStr);

		//unset .active from all thumbnails and add it to the clicked image for styling

	// when page loads simulate a "click" on the first image
	//let's change this to a random image per http://blog.mastykarz.nl/jquery-random-filter/

});//end document ready function

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksymerAuthor Commented:
Bug report: I just noticed that when I click again on the active thumbnail while the fadeIn is still in progress, the gradient background image disappears.  I tried to add .not() to omit the active class from the click function, but it's not working.  This problem exists only in IE8.  Note: we are using jQuery 1.3.2.  Any ideas?
$(".items img").not(".active").click(function() {...

Open in new window

RobinSoftware EngineerCommented:
One tip I can give to reduce page load time is to reduce the image sizes!

E.g. the first image :

metro_coffee_700.png is 301KB

Saving it in Photoshop with 'Save for Web...', and setting it to JPEG, Medium, 55% gives you a
27KB (!) jpeg file, which looks (to me) just as good.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ksymerAuthor Commented:
@robinu: That's a good suggestion and I'll pass that on to our PR dept.  However, I'm still looking for best practices for writing the jQuery so I can learn from any mistakes.  Thanks!
RobinSoftware EngineerCommented:
Well, maybe a jQuery expert steps in.
A good suggestion is however the book 'jQuery Novice to Ninja' from SitePoint.
ksymerAuthor Commented:
I was looking for ways to optimize jQuery scripting.  Answers were tangential.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.