/*
* Feature Carousel, Version 1.2.1
* http://www.bkosolutions.com
*
* Copyright 2011 Brian Osborne
* Licensed under GPL version 3
* brian@bkosborne.com
*
* http://www.gnu.org/licenses/gpl.txt
*/
(function(a){a.fn.featureCarousel=function(b){b=a.extend({},a.fn.featureCarousel.defaults,b||{});return a(this).each(function(){var o={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};g(function(){q();e();k();j();p(true,1)});function g(w){if(b.preload==true){var u=o.featuresContainer.find("img");var t=0;var v=u.length;u.each(function(){a(this).load(function(){t++;if(t==v){w()}});if(this.complete||a.browser.msie){a(this).trigger("load")}})}else{w()}}function s(t){return o.featuresArray[t-1]}function h(t){a.each(o.featuresArray,function(){if(a(this).data().setPosition==t){return a(this)}})}function c(t){if((t-1)==0){return o.totalFeatureCount}else{return t-1}}function i(t){if((t+1)>o.totalFeatureCount){return 1}else{return t+1}}function q(){o.containerWidth=o.featuresContainer.width();o.containerHeight=o.featuresContainer.height();var t=a(o.containerIDTag).find(".carousel-image:first");if(b.largeFeatureWidth>1){o.largeFeatureWidth=b.largeFeatureWidth}else{if(b.largeFeatureWidth>0&&b.largeFeatureWidth<1){o.largeFeatureWidth=t.width()*b.largeFeatureWidth}else{o.largeFeatureWidth=t.outerWidth()}}if(b.largeFeatureHeight>1){o.largeFeatureHeight=b.largeFeatureHeight}else{if(b.largeFeatureHeight>0&&b.largeFeatureHeight<1){o.largeFeatureHeight=t.height()*b.largeFeatureHeight}else{o.largeFeatureHeight=t.outerHeight()}}if(b.smallFeatureWidth>1){o.smallFeatureWidth=b.smallFeatureWidth}else{if(b.smallFeatureWidth>0&&b.smallFeatureWidth<1){o.smallFeatureWidth=t.width()*b.smallFeatureWidth}else{o.smallFeatureWidth=t.outerWidth()/2}}if(b.smallFeatureHeight>1){o.smallFeatureHeight=b.smallFeatureHeight}else{if(b.smallFeatureHeight>0&&b.smallFeatureHeight<1){o.smallFeatureHeight=t.height()*b.smallFeatureHeight}else{o.smallFeatureHeight=t.outerHeight()/2}}}function e(){if(b.displayCutoff>0&&b.displayCutoff<o.totalFeatureCount){o.totalFeatureCount=b.displayCutoff}o.featuresContainer.find(".carousel-feature").each(function(t){if(t<o.totalFeatureCount){o.featuresArray[t]=a(this)}});if(o.featuresContainer.find(".carousel-feature").first().css("borderLeftWidth")!="medium"){o.borderWidth=parseInt(o.featuresContainer.find(".carousel-feature").first().css("borderLeftWidth"))*2}o.featuresContainer.find(".carousel-feature").each(function(){a(this).css({left:(o.containerWidth/2)-(o.smallFeatureWidth/2)-(o.borderWidth/2),width:o.smallFeatureWidth,height:o.smallFeatureHeight,top:b.smallFeatureOffset+b.topPadding,opacity:0})}).find(".carousel-image").css({width:o.smallFeatureWidth});if(b.captionBelow){o.featuresContainer.find(".carousel-caption").css("position","relative")}if(o.totalFeatureCount<4){o.itemsToAnimate=o.totalFeatureCount}else{o.itemsToAnimate=4}o.featuresContainer.find(".carousel-caption").hide()}function k(){a.each(o.featuresArray,function(x){a(this).data("setPosition",x+1)});var w=c(b.startingFeature);o.currentCenterNum=w;var u=s(w);u.data("position",1);var t=u.prevAll();t.each(function(x){a(this).data("position",(o.totalFeatureCount-x))});var v=u.nextAll();v.each(function(x){if(a(this).data("setPosition")!=undefined){a(this).data("position",(x+2))}});if(b.counterStyle=="caption"){a.each(o.featuresArray,function(){var x=c(a(this).data("position"));var y=a("<span></span>");y.addClass("numberTag");y.html("("+x+" of "+o.totalFeatureCount+") ");a(this).find(".carousel-caption p").prepend(y)})}}function j(){if(b.trackerIndividual){var A=a("<div></div>");A.addClass("tracker-individual-container");for(var t=0;t<o.totalFeatureCount;t++){var z=a(o.featuresContainer.find(".carousel-image")[t]).attr("alt");var y=a("<span>"+z+"</span>");y.addClass("tracker-individual-blip");y.css("cursor","pointer");y.attr("id","tracker-"+(t+1));A.append(y)}A.before("<hr />").after("<hr class='divider' />");a(o.containerIDTag).after(A);A.hide().show()}if(b.trackerSummation){var w=a("<div></div>");w.addClass("tracker-summation-container");var x=a("<span></span>").addClass("tracker-summation-current").text(b.startingFeature);var u=a("<span></span>").addClass("tracker-summation-total").text(o.totalFeatureCount);var v=a("<span></span>").addClass("tracker-summation-middle").text(" of ");w.append(x).append(v).append(u);a(o.containerIDTag).append(w)}}function r(t,v){if(b.trackerIndividual){var x=o.featuresContainer.find(".tracker-individual-container");var w=a("#tracker-"+t);var u=a("#tracker-"+v);w.removeClass("tracker-individual-blip-selected");u.addClass("tracker-individual-blip-selected")}if(b.trackerSummation){var x=o.featuresContainer.find(".tracker-summation-container");x.find(".tracker-summation-current").text(v)}}function f(t){clearTimeout(o.timeoutVar);if(!t&&b.autoPlay!=0){var u=(Math.abs(b.autoPlay)<b.carouselSpeed)?b.carouselSpeed:Math.abs(b.autoPlay);o.timeoutVar=setTimeout(function(){(b.autoPlay>0)?p(true,1):p(false,1)},u)}}function d(t){a.each(o.featuresArray,function(){var u;if(t==false){u=i(a(this).data().position)}else{u=c(a(this).data().position)}a(this).data("position",u)})}function l(t,z){var B,u,v,D,A,y,w;var x=t.data("position");var C;if(z==true){C=c(x)}else{C=i(x)}if(x==1){b.leavingCenter(t)}if(C==1){B=o.largeFeatureWidth;u=o.largeFeatureHeight;v=b.topPadding;A=t.css("z-index");D=(o.containerWidth/2)-(o.largeFeatureWidth/2)-(o.borderWidth/2);w=1}else{B=o.smallFeatureWidth;u=o.smallFeatureHeight;v=b.smallFeatureOffset+b.topPadding;A=1;w=0.4;if(C==o.totalFeatureCount){D=b.sidePadding}else{if(C==2){D=o.containerWidth-o.smallFeatureWidth-b.sidePadding-o.borderWidth}else{D=(o.containerWidth/2)-(o.smallFeatureWidth/2)-(o.borderWidth/2);w=0}}}if(x==1){t.find(".carousel-caption").hide()}t.animate({width:B,height:u,top:v,left:D,opacity:w},b.carouselSpeed,b.animationEasing,function(){if(C==1){if(b.captionBelow){t.css("height","auto")}t.find(".carousel-caption").fadeTo("fast",0.85);b.movedToCenter(t)}o.rotationsRemaining=o.rotationsRemaining-1;t.css("z-index",A);if(b.trackerIndividual||b.trackerSummation){if(C==1){var E=o.featuresContainer.find(".carousel-feature").index(t)+1;var G;if(z==false){G=i(E)}else{G=c(E)}r(G,E)}}var F=o.rotationsRemaining/o.itemsToAnimate;if(F%1==0){o.currentlyMoving=false;d(z);if(o.rotationsRemaining>0){n(z)}}f(false)}).find(".carousel-image").animate({width:B,height:u},b.carouselSpeed,b.animationEasing).end()}function n(u){o.currentlyMoving=true;var t,x,v,w;if(u==true){t=s(i(o.currentCenterNum));x=s(o.currentCenterNum);v=s(i(i(o.currentCenterNum)));w=s(c(o.currentCenterNum));o.currentCenterNum=i(o.currentCenterNum)}else{t=s(c(o.currentCenterNum));x=s(c(c(o.currentCenterNum)));v=s(o.currentCenterNum);w=s(i(o.currentCenterNum));o.currentCenterNum=c(o.currentCenterNum)}if(u){x.css("z-index",3)}else{v.css("z-index",3)}t.css("z-index",4);l(x,u);l(t,u);l(v,u);if(o.totalFeatureCount>3){l(w,u)}}function p(v,t){if(o.currentlyMoving==false){var u=t*o.itemsToAnimate;o.rotationsRemaining=u;n(v)}}function m(w,t){var u=1,v=1,x;x=w;while((x=c(x))!=t){u++}x=w;while((x=i(x))!=t){v++}return(u<v)?u*-1:v}a(b.leftButtonTag).live("click",function(){p(false,1)});a(b.rightButtonTag).live("click",function(){p(true,1)});o.featuresContainer.find(".carousel-feature").click(function(){var t=a(this).data("position");if(t==2){p(true,1)}else{if(t==o.totalFeatureCount){p(false,1)}}}).mouseover(function(){if(o.currentlyMoving==false){var t=a(this).data("position");if(t==2||t==o.totalFeatureCount){a(this).css("opacity",0.8)}}if(b.pauseOnHover){f(true)}if(b.stopOnHover){b.autoPlay=0}}).mouseout(function(){if(o.currentlyMoving==false){var t=a(this).data("position");if(t==2||t==o.totalFeatureCount){a(this).css("opacity",0.4)}}if(b.pauseOnHover){f(false)}});a("a",o.containerIDTag).live("click",function(u){var t=a(this).parentsUntil(o.containerIDTag);t.each(function(){var v=a(this).data("position");if(v!=undefined){if(v!=1){if(v==o.totalFeatureCount){p(false,1)}else{if(v==2){p(true,1)}}u.preventDefault();return false}else{b.clickedCenter(a(this))}}})});a(".tracker-individual-blip").live("click",function(){var w=a(this).attr("id").substring(8);var u=o.featuresContainer.find(".carousel-feature").eq(w-1).data("position");var t=o.currentCenterNum;if(w!=t){var v=m(1,u);if(v<0){p(false,(v*-1))}else{p(true,v)}}})})};a.fn.featureCarousel.defaults={largeFeatureWidth:0,largeFeatureHeight:0,smallFeatureWidth:0.5,smallFeatureHeight:0.5,topPadding:20,sidePadding:50,smallFeatureOffset:50,startingFeature:1,carouselSpeed:1000,autoPlay:4000,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);
