/* * * * * Misc. jQuery application of design * * * * */

/* PAGE TEMPLATE STYLES */

$(document).ready(function() {

// Wrapping images in galleryBox in anchor tags with title attributes derived from the image's alt tag (for fancybox title display)
// and href attribute derived from the image's src (with the "_thumb" stripped out)
// (if the image's name does not contain "_thumb" it will just link the anchor to the original - presumably small - image)
// and setting all images in galleryBox to be part of the same fancybox gallery (adding the same "rel" tag to each).
	$('div.galleryBox img').each(function(index) {
		// This regular expression stores everything before it hits "_thumb" (not case sensitive) in a back reference ($1)
		// and then stores everything after that in another back reference ($2).
		var regexp = /(.*)_thumb\.(.*)/i;
		var imageSource = $(this).attr('src').replace(regexp,'$1.$2');
		$(this).wrap(function() {
			return '<a class="lightbox" rel="gallery" title="' + $(this).attr('alt') + '" href="' + imageSource + '" />';
		});
	});

// Turning off empty gallery boxes (with no images or links - so "Edit" link will appear when admin is logged in).
	if($('div.galleryBox img').length) {
		$('div.galleryContainer').show();
	}
	else if($('div.galleryBox a').length) {
		$('div.galleryContainer').show();
	}

// Applying additional class to galleryBox when logged in AND/OR when editing the gallery content. This class is used to allow multiple rows of gallery images to be displayed in the admin.
	if($('div.galleryBox a.toggleEditorLabel').length || $('div.galleryBox div.CMWrapper').length) {
		$('div.galleryBox').addClass('galleryBoxAdmin');
	}

// Initialize fancybox (lightbox) for anchor AND image tags with class "lightbox".
// NOTE - The fancybox must be initialize AFTER the galleryBox has been automatically propagated.
	$('a.lightbox').fancybox();
	$('a img.lightbox').parent().fancybox();


// Set CSS class for each last (rightmost) td in the targeted element.
	$('.photoTable tr td:last-child').addClass('last');


});


