$(document).ready(function(){
	// Required Variables.
	var imageContainerWidth = 620; // The width of the container that holds our images.
	var thumbContainerHeight = 55; // The default minimum height of the thumb ul container [div]. We work this out to accomodate different sized thumbs.
	var thumbContainerWidth = 0; // The width of the thumb container [ul]. Defaults to 0. We work this out based on the amount of thumbs showing, and each of their widths.
	var thumbMargin = 10; // The margin on our thumbs.
	var thumbTimeout = setTimeout('', 0);
	var firstPageLoad	= true;
	
	// Setup the gallery.
	jQuery(function($) {
		$('ul.gallery').apngallery({
			insert		: "#galleryCurrentImage",
			preload     : true,
			history		: true,
			onLoading	: function(caption,thumb){
				// fetch the thumbnail container
				var _li = thumb.parent();

				// fade out inactive thumbnails
				_li.siblings().children('div.overlay').fadeTo(0, 0);
				
				// ************
				// *
				// * This event fires upon initiation of a new image - prior to it loading.
				// * Tracking or other events could go in here.
				// *
				// ************
								
				// Analytics tracking on every image view after the initial load
				if (!firstPageLoad) {
					// Omniture call
					s.prop23	= '';					
					s_code=s.t();
					
					// Get the query string without the hash
					var query_string = location.href.match(/\?[^#]*/);
					
					// Reload the ifram for netratings
					$("#_analyticsFrame").attr('src','/news/imageFooter.cfm'+query_string);
				} else {
					firstPageLoad	= false;
				}
				
			},
			onImage		: function(image,caption,thumb) {
				// fetch the thumbnail container
				var _li = thumb.parent();

				// fade in the image
				image.css('display','none').fadeIn(800);
				
				// Set the current item counter.
				$('#galleryCountCurrent').text(_li.prevAll().length + 1);
				
				// resize our caption container - uses the jSizes jquery plugin to return actual pixel dimensions where required.
				var captionPadding = caption.padding().left + caption.padding().right;
				var captionWidth = image.attr('width') - captionPadding; // Set the captionWidth to our image width, minus the padding.
				if (captionWidth > (imageContainerWidth - captionPadding)) captionWidth = imageContainerWidth - captionPadding; // In those cases where our image is too big - cap it.
				caption.css('width',captionWidth); // Set the caption's width.
				
				// Set a timeout on our fade in, and scroll - to avoid wierd issues when people click through too quickly.
				clearTimeout(thumbTimeout);
				thumbTimeout = setTimeout(function(){
												   
					// Fade in the active thumbnail.
					_li.children('div.overlay').show().fadeTo(200, 1);
				
					//Scrolling.
					var ourOffset = ($('.galleryThumbs').width() / 2) - (thumb.width() / 2);
					$(".galleryThumbs").scrollTo(thumb, 500, { offset:-ourOffset });
					
				}, 300);
				
				updateShareLinks();
			},
			onThumb		: function(thumb) { // Thumbnail effects.
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// Prepare the rollover images - but setting them to display and transparency of 0.
				_li.siblings().not('.active').children('div.overlay').show().fadeTo(0,0);
				
				// hover effects
				_li.hover(
					function() { _li.not('.active').children('div.overlay').show().fadeTo(200, 1); },
					function() { _li.not('.active').children('div.overlay').fadeTo(200, 0); }
				)
				
				// Calculate thumb's container width.
				thumbContainerWidth = (thumbContainerWidth + thumb.width() + thumbMargin);
				if (thumb.height() > thumbContainerHeight) thumbContainerHeight = thumb.height(); // Sets our known minimum.
				
				// If our thumb size is 55, set it so.
				if (thumb.height() == 55) _li.children('div.overlay').addClass('overlay55');

			}
		}); 
	}).ready(function(){
		thumbContainerWidth = (thumbContainerWidth - (thumbMargin - 1)); // We remove one pixel - and the final right margin from our last element.
		$('ul.gallery').css('width', thumbContainerWidth); // Set the width of our thumb UL container.
		
		thumbContainerHeight = thumbContainerHeight + (thumbMargin * 2); // Container height = the largest thumb height plus the margin * 2.
		if ($.browser.msie && $.browser.version <= 7) thumbContainerHeight += 18; // If it's ie6, add an additional 18px to account for the scrollbar.
		$('ul.gallery').css('height', thumbContainerHeight); // Set the height of the container.
		
		// Set the total count in our nav.
		$('#galleryCount').text($('ul.gallery li').siblings().length);
		
		// Show and hide our next / previous buttons - using hoverIntent to cause a delay on show AND hide.
		$("#galleryCurrentImage").hoverIntent({ 
			sensitivity: 7,
			interval: 200,
			over: function() { $("#galleryCurrentImage .galleryNav").fadeIn(300); },
			timeout: 300,
			out: function() { $("#galleryCurrentImage .galleryNav").fadeOut(300); }
		});
	});
});


function updateShareLinks(){                    	
	var current_hash = escape(location.href.match(/#.*$/));	                     	
	$('a.updatableShareLink').each( function() {																					
		var oldLink = $(this).attr('href');
		var newLink = oldLink.replace(/(\w+=http.+?)%23.+?(?=&|$)/,'$1' + current_hash);						
		if (oldLink != newLink) $(this).attr('href',newLink);								
	});						
}
