(function(a){a.fn.featureCarousel=function(b){b=a.extend({},a.fn.featureCarousel.defaults,b||{});return a(this).each(function(){function s(a,b){var c=1,d=1,e;e=a;while((e=g(e))!=b){c++}e=a;while((e=h(e))!=b){d++}return c<d?c*-1:d}function r(a,b){if(c.currentlyMoving==false){var d=b*c.itemsToAnimate;c.rotationsRemaining=d;q(a)}}function q(a){c.currentlyMoving=true;var b,d,f,i;if(a==true){b=e(h(c.currentCenterNum));d=e(c.currentCenterNum);f=e(h(h(c.currentCenterNum)));i=e(g(c.currentCenterNum));c.currentCenterNum=h(c.currentCenterNum)}else{b=e(g(c.currentCenterNum));d=e(g(g(c.currentCenterNum)));f=e(c.currentCenterNum);i=e(h(c.currentCenterNum));c.currentCenterNum=g(c.currentCenterNum)}if(a){d.css("z-index",3)}else{f.css("z-index",3)}b.css("z-index",4);p(d,a);p(b,a);p(f,a);if(c.totalFeatureCount>3){p(i,a)}}function p(a,d){var e,f,i,j,k,l,p;var r=a.data("position");var s;if(d==true)s=g(r);else s=h(r);if(r==1){b.leavingCenter(a)}if(s==1){e=c.largeFeatureWidth;f=c.largeFeatureHeight;i=b.topPadding;k=a.css("z-index");j=c.containerWidth/2-c.largeFeatureWidth/2-c.borderWidth/2;p=1}else{e=c.smallFeatureWidth;f=c.smallFeatureHeight;i=b.smallFeatureOffset+b.topPadding;k=1;p=1;if(s==c.totalFeatureCount){j=b.sidePadding}else if(s==2){j=c.containerWidth-c.smallFeatureWidth-b.sidePadding-c.borderWidth}else{j=c.containerWidth/2-c.smallFeatureWidth/2-c.borderWidth/2;p=0}}if(r==1){a.find(".carousel-caption").hide()}a.animate({width:e,height:f,top:i,left:j,opacity:p},b.carouselSpeed,b.animationEasing,function(){if(s==1){if(b.captionBelow)a.css("height","auto");a.find(".carousel-caption").fadeTo("fast",.85);b.movedToCenter(a)}c.rotationsRemaining=c.rotationsRemaining-1;a.css("z-index",k);if(b.trackerIndividual||b.trackerSummation){if(s==1){var e=c.featuresContainer.find(".carousel-feature").index(a)+1;var f;if(d==false)f=h(e);else f=g(e);m(f,e)}}var i=c.rotationsRemaining/c.itemsToAnimate;if(i%1==0){c.currentlyMoving=false;o(d);if(c.rotationsRemaining>0)q(d)}n(false)}).find(".carousel-image").animate({width:e,height:f},b.carouselSpeed,b.animationEasing).end()}function o(b){a.each(c.featuresArray,function(){var c;if(b==false){c=h(a(this).data().position)}else{c=g(a(this).data().position)}a(this).data("position",c)})}function n(a){clearTimeout(c.timeoutVar);if(!a&&b.autoPlay!=0){var d=Math.abs(b.autoPlay)<b.carouselSpeed?b.carouselSpeed:Math.abs(b.autoPlay);c.timeoutVar=setTimeout(function(){b.autoPlay>0?r(true,1):r(false,1)},d)}}function m(a,d){if(b.trackerIndividual){var e=c.featuresContainer.find(".tracker-individual-container");var f=e.find("#tracker-"+a);var g=e.find("#tracker-"+d);f.removeClass("tracker-individual-blip-selected");g.addClass("tracker-individual-blip-selected")}if(b.trackerSummation){var e=c.featuresContainer.find(".tracker-summation-container");e.find(".tracker-summation-current").text(d)}}function l(){if(b.trackerIndividual){var d=a("<ul></ul>");d.addClass("tracker-individual-container");for(var e=0;e<c.totalFeatureCount;e++){var f=e+1;var g=a("<div>"+f+"</div>");g.addClass("tracker-individual-blip");g.css("cursor","pointer");g.attr("id","tracker-"+(e+1));var h=a("<li></li>");h.append(g);h.css("float","left");h.css("list-style-type","none");d.append(h)}a(c.containerIDTag).append(d);d.hide().show()}if(b.trackerSummation){var i=a("<div></div>");i.addClass("tracker-summation-container");var j=a("<span></span>").addClass("tracker-summation-current").text(b.startingFeature);var k=a("<span></span>").addClass("tracker-summation-total").text(c.totalFeatureCount);var l=a("<span></span>").addClass("tracker-summation-middle").text(" of ");i.append(j).append(l).append(k);a(c.containerIDTag).append(i)}}function k(){a.each(c.featuresArray,function(b){a(this).data("setPosition",b+1)});var d=g(b.startingFeature);c.currentCenterNum=d;var f=e(d);f.data("position",1);var h=f.prevAll();h.each(function(b){a(this).data("position",c.totalFeatureCount-b)});var i=f.nextAll();i.each(function(b){if(a(this).data("setPosition")!=undefined){a(this).data("position",b+2)}});if(b.counterStyle=="caption"){a.each(c.featuresArray,function(){var b=g(a(this).data("position"));var d=a("<span></span>");d.addClass("numberTag");d.html("("+b+" of "+c.totalFeatureCount+") ");a(this).find(".carousel-caption p").prepend(d)})}}function j(){if(b.displayCutoff>0&&b.displayCutoff<c.totalFeatureCount){c.totalFeatureCount=b.displayCutoff}c.featuresContainer.find(".carousel-feature").each(function(b){if(b<c.totalFeatureCount){c.featuresArray[b]=a(this)}});if(c.featuresContainer.find(".carousel-feature").first().css("borderLeftWidth")!="medium"){c.borderWidth=parseInt(c.featuresContainer.find(".carousel-feature").first().css("borderLeftWidth"))*2}c.featuresContainer.find(".carousel-feature").each(function(){a(this).css({left:c.containerWidth/2-c.smallFeatureWidth/2-c.borderWidth/2,width:c.smallFeatureWidth,height:c.smallFeatureHeight,top:b.smallFeatureOffset+b.topPadding,opacity:0})}).find(".carousel-image").css({width:c.smallFeatureWidth});if(b.captionBelow){c.featuresContainer.find(".carousel-caption").css("position","relative")}if(c.totalFeatureCount<4){c.itemsToAnimate=c.totalFeatureCount}else{c.itemsToAnimate=4}c.featuresContainer.find(".carousel-caption").hide()}function i(){c.containerWidth=c.featuresContainer.width();c.containerHeight=c.featuresContainer.height();var d=a(c.containerIDTag).find(".carousel-image:first");if(b.largeFeatureWidth>1)c.largeFeatureWidth=b.largeFeatureWidth;else if(b.largeFeatureWidth>0&&b.largeFeatureWidth<1)c.largeFeatureWidth=d.width()*b.largeFeatureWidth;else c.largeFeatureWidth=d.outerWidth();if(b.largeFeatureHeight>1)c.largeFeatureHeight=b.largeFeatureHeight;else if(b.largeFeatureHeight>0&&b.largeFeatureHeight<1)c.largeFeatureHeight=d.height()*b.largeFeatureHeight;else c.largeFeatureHeight=d.outerHeight();if(b.smallFeatureWidth>1)c.smallFeatureWidth=b.smallFeatureWidth;else if(b.smallFeatureWidth>0&&b.smallFeatureWidth<1)c.smallFeatureWidth=d.width()*b.smallFeatureWidth;else c.smallFeatureWidth=d.outerWidth()/2;if(b.smallFeatureHeight>1)c.smallFeatureHeight=b.smallFeatureHeight;else if(b.smallFeatureHeight>0&&b.smallFeatureHeight<1)c.smallFeatureHeight=d.height()*b.smallFeatureHeight;else c.smallFeatureHeight=d.outerHeight()/2}function h(a){if(a+1>c.totalFeatureCount){return 1}else{return a+1}}function g(a){if(a-1==0){return c.totalFeatureCount}else{return a-1}}function f(b){a.each(c.featuresArray,function(){if(a(this).data().setPosition==b)return a(this)})}function e(a){return c.featuresArray[a-1]}function d(d){if(b.preload==true){var e=c.featuresContainer.find("img");var f=0;var g=e.length;e.each(function(){a(this).load(function(){f++;if(f==g){d()}});if(this.complete||a.browser.msie){a(this).trigger("load")}})}else{d()}}var c={currentCenterNum:b.startingFeature,containerWidth:0,containerHeight:0,largeFeatureWidth:0,largeFeatureHeight:0,smallFeatureWidth:0,smallFeatureHeight:0,totalFeatureCount:a(this).children("div").length,currentlyMoving:false,featuresContainer:a(this),featuresArray:[],containerIDTag:"#"+a(this).attr("id"),timeoutVar:null,rotationsRemaining:0,itemsToAnimate:0,borderWidth:0};d(function(){i();j();k();l();r(true,1);a("#ajax-loader").hide()});a(b.leftButtonTag).live("click",function(){r(false,1)});a(b.rightButtonTag).live("click",function(){r(true,1)});c.featuresContainer.find(".carousel-feature").click(function(){var b=a(this).data("position");if(b==2){r(true,1)}else if(b==c.totalFeatureCount){r(false,1)}}).mouseover(function(){if(c.currentlyMoving==false){var d=a(this).data("position");if(d==2||d==c.totalFeatureCount){a(this).css("opacity",1)}}if(b.pauseOnHover)n(true);if(b.stopOnHover)b.autoPlay=0}).mouseout(function(){if(c.currentlyMoving==false){var d=a(this).data("position");if(d==2||d==c.totalFeatureCount){a(this).css("opacity",1)}}if(b.pauseOnHover){n(false)}});a("a",c.containerIDTag).live("click",function(d){var e=a(this).parentsUntil(c.containerIDTag);e.each(function(){var e=a(this).data("position");if(e!=undefined){if(e!=1){if(e==c.totalFeatureCount){r(false,1)}else if(e==2){r(true,1)}d.preventDefault();return false}else{b.clickedCenter(a(this))}}})});a(".tracker-individual-blip").live("click",function(){var b=a(this).attr("id").substring(8);var d=c.featuresContainer.find(".carousel-feature").eq(b-1).data("position");var e=c.currentCenterNum;if(b!=e){var f=s(1,d);if(f<0){r(false,f*-1)}else{r(true,f)}}})})};a.fn.featureCarousel.defaults={largeFeatureWidth:0,largeFeatureHeight:0,smallFeatureWidth:.5,smallFeatureHeight:.5,topPadding:20,sidePadding:50,smallFeatureOffset:50,startingFeature:1,carouselSpeed:1e3,autoPlay:4e3,pauseOnHover:true,stopOnHover:false,trackerIndividual:true,trackerSummation:true,preload:true,displayCutoff:0,animationEasing:"swing",leftButtonTag:"#carousel-left",rightButtonTag:"#carousel-right",captionBelow:false,movedToCenter:a.noop,leavingCenter:a.noop,clickedCenter:a.noop}})(jQuery)
