$.fn.imageMorph=(function(src,width,height,callback){var SPEED=300;return this.each(function(){var img=$(this),newImg=$('<img>'),throbber=img.parent().find('.throbber');if(img.hasClass('morpher')){img.remove();return;}
if(!throbber.length){throbber=$('<span class="throbber"></span>');img.parent().append(throbber);throbber.hide();}
var throbberTimeout=setTimeout(function(){throbber.show();},60);newImg.css({position:'absolute',top:0,left:0,width:img.width()+'px',height:img.height()+'px',opacity:0,filter:'alpha(opacity=0)',cursor:'pointer'}).addClass('morpher');img.parent().append(newImg).css({width:img.width()+'px',height:img.height()+'px'});newImg.load(function(){var css={width:width+'px',height:height+'px',opacity:1,filter:'alpha(opacity=100)'};img.parent().stop().animate(css,SPEED);img.stop().animate(css,SPEED);clearTimeout(throbberTimeout);throbber.hide();newImg.animate(css,SPEED,function(){img.remove();newImg.removeClass('morpher');(callback||function(){})();});});newImg.attr('src',src);});});(function($){function nextImage(){traverseThumbs('next');};function prevImage(){traverseThumbs('prev');};function traverseThumbs(direction){var currentSrc=$('.gallery-container .main-image div img').attr('src'),thumbLink=$('.gallery-container ul.thumbs a[main_image_url='+currentSrc+']');if(direction){thumbLink=thumbLink.parent()[direction]().find('a');}
if(thumbLink.length){thumbLink.click();}
else{if(direction=='prev'){$('.gallery-container ul.thumbs li:last-child a').click();}
else{$('.gallery-container ul.thumbs li:first-child > a:first-child').click();}}};$(function(){$('.gallery-container ul.thumbs a').click(function(){var me=$(this);$('.gallery-container .main-image div img').imageMorph(me.attr("main_image_url"),me.parent().find('.meta.main_image_width').html(),me.parent().find('.meta.main_image_height').html(),function(){$('.main-image-caption').html(me.parent().find('.meta.caption').html());$('.main-image-date').text(me.parent().find('.meta.date_added').html());});$('.gallery-container ul.thumbs a').removeClass('current');me.addClass('current');return false;}).hover(function(){$('.caption-bottom').text($(this).find('img').attr('alt'));},function(){$('.caption-bottom').text('');});$('.gallery-container .main-image div').click(nextImage);$('ul.gallery-index li').hover(function(){$(this).addClass('hovered');},function(){$(this).removeClass('hovered');}).click(function(){window.location=$(this).find("a").attr("href");return false;});});})(jQuery);
