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

(function($){ 

$(function() { // on page load
	if(livewhale.is_gallery) { // if on a gallery detail page	
		var images = $('#lw_gallery_images');
		if(!$('.lw_gallery_previous').length) $('.lw_gallery_nav').prepend('<a class="lw_gallery_previous" href="#" style="display:none;">&laquo; Previous</a>');
		if(!$('.lw_gallery_next').length) $('.lw_gallery_nav').prepend('<a class="lw_gallery_next" href="#" style="display:none;">Next &raquo;</a>');
		$('.lw_gallery_nav a').attr('href','#').live('click',function() {
			var item = $('#lw_gallery_thumbnails').find('.lw_gallery_selected').parent();
			if($(this).is('.lw_gallery_next')) item.next().find('a').click();
			else item.prev().find('a').click();
			return false;
		});
		$('#lw_gallery_thumbnails a').click(function() {
			if(images.find('.lw_next_image').length||$(this).is('.lw_gallery_selected')) return false;
			$('.lw_gallery_selected').removeClass('lw_gallery_selected');
			$(this).addClass('lw_gallery_selected');
			var item = $(this).parent(),
				src = item.find('.lw_gallery_full').val(),
				caption = item.find('.lw_gallery_caption').val(),
				credit = item.find('.lw_gallery_credit').val(),
				spinner = $('<div class="lw_spinner"/>').appendTo(images);
			if(item.prev().length>0) $('.lw_gallery_previous').show();
			else $('.lw_gallery_previous').hide();
			if(item.next().length>0) $('.lw_gallery_next').show();
			else $('.lw_gallery_next').hide();
			var img = new Image();
			var nextImage = $('<div class="lw_gallery_image lw_next_image"><div class="lw_gallery_caption">'+caption+'</div><div class="lw_gallery_credit">'+credit+'</div></div>').prepend(img).appendTo(images).css({opacity:0,display:'block'});
			$(img).load(function() { // when the image has loaded
				var targetHeight = nextImage.height();
				nextImage.height('100%');
				spinner.remove();
				images.animate({height:targetHeight},750);
				nextImage.fadeTo(750,1,function() {
					nextImage.removeClass('lw_next_image').siblings('.lw_gallery_image').remove();
				});
			}).attr('src',src).attr('alt','Gallery image');
			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(),
			   originalThumbWidth,
			   resizedThumbWidth,
			   resizedThumbRatio,
			   resizedThumbHeight,
			   newWidth,
			   newHeight;
			$('<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;"/>');
			});

			// calculation for setting below
			originalThumbWidth = thumbs.eq(0).find('img').eq(0).attr('width');
			resizedThumbWidth = (width-2*thumbs.length)/thumbs.length;
			resizedThumbRatio = resizedThumbWidth / originalThumbWidth;
			resizedThumbHeight = thumbs.eq(0).find('img').height() * resizedThumbRatio;
			newWidth = Math.floor(resizedThumbWidth) + 'px';
			newHeight = Math.floor(resizedThumbHeight) + 'px';

			if (thumbs.length * originalThumbWidth >= width) {
				thumbs
					.width(newWidth)  // set width of LIs in thumbnail list
					.find('img')      // set CSS and HTML width and height for thumbnails
						.attr('width', resizedThumbWidth)
						.attr('height', resizedThumbHeight)
						.width(newWidth)
						.height(newHeight);
			} else {
				thumbs
					.width(newWidth);
			}

			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
				});	
				
				if ($(this).find('.lw_slideshow_interval').length) if (ms=$(this).find('.lw_slideshow_interval').html()) if (ms.match(/[0-9]+/) && ms>=100) { // if this is a valid slideshow inline gallery
					len=livewhale.slideshows.length; // get the # of slideshows
					thumbs=$(this).find('.lw_gallery_thumbnails li a'); // get the thumbs for this gallery
					if (thumbs.length>1) { // if there is more than 1 image
						$(this).find('.lw_gallery_image').mouseover(function() {livewhale.slideshow_on=false;}); // disable slideshow on mouseover
						$(this).find('.lw_gallery_image').mouseout(function() {livewhale.slideshow_on=true;}); // enable slideshow on mouseover
						livewhale.slideshows[len]=new Array(thumbs,0); // add a slideshow entry
						livewhale.slideshow_timers[len]=setInterval('if (livewhale.slideshow_on) if (livewhale.slideshows['+len+'][0].length>livewhale.slideshows['+len+'][1]+1) {livewhale.slideshows['+len+'][1]++;} else livewhale.slideshows['+len+'][1]=0;livewhale.slideshows['+len+'][0].eq(livewhale.slideshows['+len+'][1]).click();',ms); // add a timer for this slideshow
					};
				};
				
			});
		};
	
});

})(livewhale.jQuery);
