/*
 * Waterwheel Carousel
 * Version 1.2.3
 * http://www.bkosborne.com
 *
 * Copyright 2011 Brian Osborne
 * Licensed under GPL version 3
 * http://www.gnu.org/licenses/gpl.txt
 * 
 * Plugin written by Brian Osborne
 * for use with the jQuery JavaScript Framework
 * http://www.jquery.com
 *
 */
(function(d){d.fn.waterwheelCarousel=function(c){c=d.extend({},d.fn.waterwheelCarousel.defaults,c||{});return d(this).each(function(){function n(a){clearInterval(b.intervalTimer);if(!a&&c.autoPlay!=0)b.intervalTimer=setInterval(function(){c.autoPlay>0?h(!1,1):h(!0,1)},Math.abs(c.autoPlay))}function p(){b.items=b.itemsContainer.find("img");for(var a=0;a<b.items.length;a++)b.items[a]=d(b.items[a]);b.itemsContainer.css("position","relative").find("img").each(function(a){var g,f;c.orientation=="horizontal"?
(g=b.containerWidth/2-d(this).width()/2,f=c.centerOffset):(g=c.centerOffset,f=b.containerHeight/2-d(this).height()/2);d(this).css({left:g,top:f,visibility:"visible",position:"absolute","z-index":c.flankingItems+2,opacity:1}).data({currentPosition:0,oldPosition:0,width:d(this).width(),owidth:d(this).width(),height:d(this).height(),oheight:d(this).height(),top:f,left:g,opacity:1,index:a}).show()})}function q(a,e){var g=a.data().currentPosition,d=Math.abs(e),o=b.itemWidths[Math.abs(e)],h=b.itemHeights[Math.abs(e)],
m=Math.abs(a.data().width-o),k=Math.abs(a.data().height-h),l=0,i;i=c.orientation=="horizontal"?k/2:m/2;if(e>-1&&e<g||e<1&&e>g)l-=i,l+=b.waveDistances[Math.abs(e)];else if(e>-1&&e>g||e<1&&e<g)l+=i,l-=b.waveDistances[Math.abs(e)-1];i=0;i=Math.abs(e)<Math.abs(g)?b.itemDistances[Math.abs(e)]:b.itemDistances[Math.abs(e)-1];if(e>0||e==0&&g==1)i+=c.orientation=="horizontal"?m:k;e<g&&(i*=-1);var m=e==0?1:b.itemOpacities[Math.abs(e)-1],k=a.data().top,j=a.data().left;c.orientation=="horizontal"?(k=a.data().top+
l,j=a.data().left+i):(k=a.data().top+i,j=a.data().left+l);d=c.flankingItems+2-d;a.data("width",o);a.data("height",h);a.data("top",k);a.data("left",j);a.data("oldPosition",g);a.data("currentPosition",e);a.data("depth",d);a.data("opacity",m)}function j(a,e){var d=a.data("currentPosition"),f;f=e==!1?d-1:d+1;Math.abs(f)<=c.flankingItems+1?(b.itemsAnimating++,q(a,f),a.css("z-index",a.data().depth),a.animate({left:a.data().left,width:a.data().width,height:a.data().height,top:a.data().top,opacity:a.data().opacity},
b.currentSpeed,c.animationEasing,function(){if(f==0)b.currentCenterItem=a;b.itemsAnimating--;if(b.itemsAnimating==0)b.carouselRotationsLeft-=1,b.currentlyMoving=!1,b.carouselRotationsLeft>0?h(e,0):(b.currentSpeed=c.speed,b.currentCenterItem!==null&&c.movedToCenter(b.currentCenterItem))})):Math.abs(f)>c.flankingItems&&(a.data("oldPosition",d),a.data("currentPosition",f))}function r(a){if(b.currentlyMoving==!0)return!1;if(a==!0&&b.items[0].data().currentPosition==0)return!1;if(a==!1&&b.items[b.totalItems-
1].data().currentPosition==0)return!1;return!0}function h(a,e){if(r(a)){b.currentlyMoving=!0;b.itemsAnimating=0;b.carouselRotationsLeft+=e;if(c.quickerForFurther==!0){if(e>1)b.currentSpeed=c.speed/e;b.currentSpeed=b.currentSpeed<100?100:b.currentSpeed}else b.currentSpeed=c.speed;for(var g=0;g<b.items.length;g++){var f=d(b.items[g]),h=f.data().currentPosition;h>=c.flankingItems*-1-1&&h<=c.flankingItems+1?j(f,a):(f.data("oldPosition",h),a==!0?f.data("currentPosition",h+1):f.data("currentPosition",h-
1))}}}var b={itemsContainer:d(this).find(".carousel-images"),totalItems:d(this).find(".carousel-images img").length,containerWidth:d(this).width(),containerHeight:d(this).height(),currentCenterItem:null,items:[],itemDistances:[],waveDistances:[],itemWidths:[],itemHeights:[],itemOpacities:[],carouselRotationsLeft:0,currentlyMoving:!1,itemsAnimating:0,currentSpeed:c.speed,intervalTimer:null};b.itemsContainer.find("img").hide();(function(a){var e=b.itemsContainer.find("img"),c=0,f=e.length;e.each(function(){d(this).load(function(){c++;
c==f&&a()});this.complete&&d(this).trigger("load")})})(function(){b.itemDistances[0]=c.startingItemSeparation;b.waveDistances[0]=c.startingWaveSeparation;b.itemWidths[0]=b.itemsContainer.find("img:first").width();b.itemHeights[0]=b.itemsContainer.find("img:first").height();b.itemOpacities[0]=0.75;for(var a=1;a<c.flankingItems+1;a++)b.itemDistances[a]=b.itemDistances[a-1]*c.itemSeparationFactor,b.waveDistances[a]=b.waveDistances[a-1]*c.waveSeparationFactor,b.itemWidths[a]=b.itemWidths[a-1]*c.itemDecreaseFactor,
b.itemHeights[a]=b.itemHeights[a-1]*c.itemDecreaseFactor,b.itemOpacities[a]=b.itemOpacities[a-1]*c.opacityDecreaseFactor,a==c.flankingItems&&(b.itemWidths[a+1]=b.itemWidths[a]*c.itemDecreaseFactor,b.itemHeights[a+1]=b.itemHeights[a]*c.itemDecreaseFactor);b.itemOpacities[b.itemOpacities.length-1]=0;p();c.startingItem=c.startingItem==0?Math.round(b.totalItems/2):c.startingItem;b.carouselRotationsLeft=1;var e,d,a=1;for(e=c.startingItem-2;e>=0;e--){for(d=0;d<a;d++)j(b.items[e],!1);a++}a=1;for(e=c.startingItem;e<
b.items.length;e++){for(d=0;d<a;d++)j(b.items[e],!0);a++}n()});d(this).find(".carousel-images img").live("click",function(){n(!0);var a=d(this).data().currentPosition,b=Math.abs(a);a<0?h(!0,b):a>0?h(!1,b):c.clickedCenter(d(this))});d(this).find(".carousel-images a").live("click",function(a){var b=d(this).find("img").width()==d(this).find("img").data().owidth?!0:!1;if(c.linkHandling==1||c.linkHandling==2&&!b)return a.preventDefault(),!1});d(this).find(".carousel-controls .carousel-prev").live("click",
function(a){h(!0,1);a.preventDefault();return!1});d(this).find(".carousel-controls .carousel-next").live("click",function(a){h(!1,1);a.preventDefault();return!1})})};d.fn.waterwheelCarousel.defaults={startingItem:0,startingItemSeparation:150,itemSeparationFactor:0.5,startingWaveSeparation:30,waveSeparationFactor:0.75,itemDecreaseFactor:0.8,opacityDecreaseFactor:0.5,centerOffset:40,flankingItems:4,speed:300,animationEasing:"linear",quickerForFurther:!0,movedToCenter:d.noop,clickedCenter:d.noop,linkHandling:2,
autoPlay:0,orientation:"horizontal"}})(jQuery);
