/* LiveWhale Frontend Gallery Effects */
/* (requires jQuery 1.3.1) */

(function($){ 

$(function() { // on page load
	
	
	
	if(livewhale.is_gallery) { // if on a gallery detail page	
		if(!$('.lw_gallery_previous').length) $('.lw_gallery_nav').prepend('<a class="lw_gallery_previous" href="#" style="display:none;">« Previous</a>');
		if(!$('.lw_gallery_next').length) $('.lw_gallery_nav').prepend('<a class="lw_gallery_next" href="#" style="display:none;">Next »</a>');
		$('.lw_gallery_nav a').attr('href','#');
		$('#lw_gallery_thumbnails a').click(function() {
			var href = $(this).parent().find('.lw_gallery_full').val(),
				caption = $(this).parent().find('.lw_gallery_caption').val(),
				image = $('#lw_gallery_image'),
				prev = $(this).parent().prev(),
				next = $(this).parent().next();
			if(image.find('.lw_next_image').length) return false; 
			$(this).addClass('lw_gallery_selected').parent().siblings().find('a').removeClass('lw_gallery_selected');
			var next_image = image.clone().removeAttr('id').height('auto').addClass('lw_next_image').hide().appendTo(image);
			var spinner = $('<div class="lw_spinner"/>').appendTo(image);
			if(next.length) next_image.find('.lw_gallery_next').show();
			else next_image.find('.lw_gallery_next').hide();
			if(prev.length) next_image.find('.lw_gallery_previous').show();
			else next_image.find('.lw_gallery_previous').hide();
			next_image.find('.lw_gallery_caption').html(caption);
			$('<img src="'+href+'" alt="Gallery image"/>').load(function() {
				next_image.find('img').replaceWith(this);
				image.animate({height:next_image.height()},500,function() {
					spinner.remove();
					next_image.find('a').click(function() { return false; });
					next_image.fadeIn(750,function() {
						image.html(next_image.html()).append(next_image);
						next_image.remove();
						image.find('.lw_gallery_next').click(function() {
							$('#lw_gallery_thumbnails').find('.lw_gallery_selected').parent().next().children().eq(0).click();
							return false;
						});
						image.find('.lw_gallery_previous').click(function() {
							$('#lw_gallery_thumbnails').find('.lw_gallery_selected').parent().prev().children().eq(0).click();
							return false;
						});
					});
				});
			});
			return false;
		});
		$('#lw_gallery_image .lw_gallery_next').click(function() {
			$('#lw_gallery_thumbnails').find('.lw_gallery_selected').parent().next().children().eq(0).click();
			return false;
		});
		$('#lw_gallery_image .lw_gallery_previous').click(function() {
			$('#lw_gallery_thumbnails').find('.lw_gallery_selected').parent().prev().children().eq(0).click();
			return false;
		});
	}
	
	
	
	if(livewhale.is_gallery_inline) { // if not on a gallery page
		$('.lw_gallery').each(function() {
			var gallery = $(this),
				image = gallery.find('.lw_gallery_image'),
				thumbs = gallery.find('.lw_gallery_thumbnails li'),
				caption = gallery.find('.lw_gallery_caption'),
				width = gallery.width();
			$('<img src="'+image.find('img').attr('src')+'"/>').load(function() { // after the initial image has loaded
				image.height(image.height()); // fix the image area height
				image.wrapInner('<div style="position:absolute;"/>');
			});
			thumbs.width((width-2*thumbs.length)/thumbs.length+'px') // set thumb width
			thumbs.eq(0) // select the first thumb
				.addClass('lw_gallery_selected'); // and select it
			thumbs.find('>a') // then select the image links
				.click(function() { // and attach the click event
					$(this).parent().addClass('lw_gallery_selected') // grab the parent li and select it
						.siblings() // grab its siblings
							.removeClass('lw_gallery_selected'); // and deselect them
					var spinner = $('<div class="lw_gallery_spinner"/>').appendTo(image), // add the loading animation to the div
						imagePath = $(this).attr('href'),
						captionText = $(this).siblings('.lw_gallery_thumbcaption').val(); // and grab the caption text
					$('<img src="'+imagePath+'"/>').load(function() { // create the new image and attach an onload
						var newImage = $('<div style="position:absolute;"><img src="'+imagePath+'" alt="Gallery Image"/><div class="lw_gallery_caption">'+captionText+'</div>').prependTo(image), // prepend it to the image div
							height = newImage
								.height(); // and store its height
						spinner.remove(); // since the image has now loaded, remove the spinner
						if(height<newImage.next().height()) { // if the height of the new image is less than the height of the old image
							image.stop().animate({height:height+'px'},300,'easeInOutSine',function() { // animate the image div height to the target height
								newImage.next().fadeOut(300,function() { // then fade out the old image
									$(this).remove(); // and remove it
								});
							});
						} else { // otherwise, if the new image is taller
							newImage.next().fadeOut(300,function() { // first, fade out the old image
									$(this).remove(); // and remove it
									image.stop().animate({height:height+'px'},300,'easeInOutSine'); // then animate the image div to the target height
								});
						}
					});
					return false; // cancel the original click
				});	

			});
	};

	
});

})(livewhale.jQuery);