$(window).load(function(){


/*	========================================
	SLIDESHOW
	======================================== */

	var Slideshow = {

		init: function() {
			// create an array and populate it with all mid size image links in the slideshow
			ssImages = new Array();
			$('ul#bm-gallery-thumbs a.thumbnail').each(function(){ 
				ssImages.push($(this).attr('href')); 
			});

			// create an array and populate it with all full size image links, for the lightbox
			lbImages = new Array();
			$('ul#bm-gallery-thumbs a.thumbnail-lrg').each(function(){ 
				lbImages.push($(this).attr('href')); 
			});
		
			// Create an array containing the captions for each image
			// Every thumbnail must have a corresponding caption otherwise they will become mismatched
			// You can still have a blank caption by using: <p class="caption">&nbsp;<p>
			captions = new Array();
			$('ul#bm-gallery-thumbs .caption').each(function(){
				captions.push($(this).html());
			});
			
			alt_texts = new Array();
			$('ul#bm-gallery-thumbs .alt_text').each(function(){
				alt_texts.push($(this).html());
			});
			
			// Set the initial caption to match first image
			Slideshow.insertCaption(captions[0]);	
			
			// Show the caption and prev/next links
			$('div#bm-gallery-caption').css('display', 'block');
			$('div#bm-gallery-loader a.prev').css('display', 'block');
			$('div#bm-gallery-loader a.next').css('display', 'block');
			
			// Do initial resizeWrapper call to fit first image
			Slideshow.resizeWrapper($('#bm-gallery-loader img').width(), $('#bm-gallery-loader img').height());
		},
		
		loadImage: function(ssImage, lbImage, caption, alt_text) {
			// Proceed only if the clicked image is not already being shown
			if (ssImage != $('div#bm-gallery-loader img').attr('src')) {
				// fade out current image
				$('div#bm-gallery-loader img').fadeOut('slow');
				// show AJAX spinner
				//$('#loader').addClass('loading');
				// create an image object and attach a load event handler
				var img = new Image();
				$(img).load(function () {
					$(img).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
					//$(img).hide();
					// completely remove the previous image from the DOM
					$('div#bm-gallery-loader a.image img').remove();
					
					//$('#bm-gallery-loader').append('<a href="#" rel="lightbox-bm"><img src="' + img.src +'"/>');
					$('div#bm-gallery-loader a.image').append(img);
					$('div#bm-gallery-loader a.image').attr('href', lbImage);
					
					// fade in the new image
					$(img).fadeIn('slow');
					Slideshow.resizeWrapper($(img).width(), $(img).height());
					Slideshow.insertCaption(caption);
				}).error(function () {
					// notify the user that the image could not be loaded
				}).attr('src', ssImage).attr('alt', alt_text);
			}
		},
		
		highlightThumb: function(number) {
			$('ul#bm-gallery-thumbs a.thumbnail').each(function(){
				$(this).removeClass('highlighted');
			});
			$('ul#bm-gallery-thumbs li:eq('+number+') a.thumbnail').addClass('highlighted');
		},
		
		insertCaption: function(caption) {
			if (caption == undefined || caption == "") {
				caption = "";
			}
			$('div#bm-gallery-caption').empty();
			$('div#bm-gallery-caption').append('<p>' + caption + '<\/p>');
		},
	
		// resize the nav container so that Prev/Next buttons are correctly positioned
		resizeWrapper: function(w, h) {
			// IE6 is very sensitive about this calculation, it can be no lower than image width + 43!!!
			// Because a.prev and a.next are floated around the image and contained within bm-gallery-wrapper
			// if the width is set any narrower than this IE6 has a freak-out
			$('div.bm-gallery-wrapper').width(w+46);	
			$('div.bm-gallery-wrapper a.prev').height(h);
			$('div.bm-gallery-wrapper a.next').height(h);
		},
		
		modifyLightbox: function() {
			$('div#lightbox-infoBox').addClass('hideLightboxCaption');
		}

	}
	Slideshow.init();
	Slideshow.modifyLightbox();

	
	
/*	========================================
	EVENT HANDLERS
	======================================== */
		
	// Attach click events to thumbnail links
	$('ul#bm-gallery-thumbs li a').click(function(){
		// find the position in the array of the clicked thumbnail
		position = $(ssImages).index($(this).attr('href'));
		Slideshow.loadImage(ssImages[position], lbImages[position], captions[position], alt_texts[position]);
		Slideshow.highlightThumb(position);
		return false;
	});
	
	// Attach click events to Prev button
	$('div.bm-gallery-wrapper a.prev').click(function(){
		// find the position of the currently loaded image inside the group of thumbnails
		position = $(ssImages).index($('div#bm-gallery-loader img').attr('src'));

		// this loop decrements our counter each time a thumbnail is clicked
		// we are at the first thumbnail in the list
		if (position == 0) {
			// set position to point to the end of the list
			position = ssImages.length-1;
		} else {
			// set position to the previous image in the list
			position -= 1;
		}
		//Slideshow.getCurrentImage();
	
		// load the relevant image
		Slideshow.loadImage(ssImages[position], lbImages[position], captions[position], alt_texts[position]);
		// highlight the appropriate thumbnail
		Slideshow.highlightThumb(position);
		return false;
	});

	// Attach click events to Prev button
	$('div.bm-gallery-wrapper a.next').click(function(){
		// find the position of the currently loaded image inside the group of thumbnails
		position = $(ssImages).index($('div#bm-gallery-loader img').attr('src'));

		if (position == ssImages.length -1) {
			// set position to point to the beginning of the list
			position = 0;
		} else {
			// set position to the next image in the list
			position += 1;
		}
		// load the relevant image
		Slideshow.loadImage(ssImages[position], lbImages[position], captions[position], alt_texts[position]);
		// highlight the appropriate thumbnail
		Slideshow.highlightThumb(position);
		return false;
	});
	
});