/*
 * Feature Carousel, Version 1.0
 * http://www.bkosolutions.com
 *
 * Copyright 2010 Brian Osborne
 * Licensed under GPL version 3
 *
 * 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 s={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};o(function(){p();c();e();r();i(true,1)});function o(w){if(b.preload==true){var u=s.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(this).trigger("load")}})}else{w()}}function k(t){return s.featuresArray[t-1]}function l(t){a.each(s.featuresArray,function(){if(a(this).data().setPosition==t){return a(this)}})}function h(t){if((t-1)==0){return s.totalFeatureCount}else{return t-1}}function f(t){if((t+1)>s.totalFeatureCount){return 1}else{return t+1}}function p(){s.containerWidth=s.featuresContainer.width();s.containerHeight=s.featuresContainer.height();var t=a(s.containerIDTag).find("div img:first");if(b.largeFeatureWidth>1){s.largeFeatureWidth=b.largeFeatureWidth}else{if(b.largeFeatureWidth>0&&b.largeFeatureWidth<1){s.largeFeatureWidth=t.width()*b.largeFeatureWidth}else{s.largeFeatureWidth=t.outerWidth()}}if(b.largeFeatureHeight>1){s.largeFeatureHeight=b.largeFeatureHeight}else{if(b.largeFeatureHeight>0&&b.largeFeatureHeight<1){s.largeFeatureHeight=t.height()*b.largeFeatureHeight}else{s.largeFeatureHeight=t.outerHeight()}}if(b.smallFeatureWidth>1){s.smallFeatureWidth=b.smallFeatureWidth}else{if(b.smallFeatureWidth>0&&b.smallFeatureWidth<1){s.smallFeatureWidth=t.width()*b.smallFeatureWidth}else{s.smallFeatureWidth=t.outerWidth()/2}}if(b.smallFeatureHeight>1){s.smallFeatureHeight=b.smallFeatureHeight}else{if(b.smallFeatureHeight>0&&b.smallFeatureHeight<1){s.smallFeatureHeight=t.height()*b.smallFeatureHeight}else{s.smallFeatureHeight=t.outerHeight()/2}}}function c(){if(b.displayCutoff>0&&b.displayCutoff<s.totalFeatureCount){s.totalFeatureCount=b.displayCutoff}s.featuresContainer.children("div").each(function(t){if(t<s.totalFeatureCount){s.featuresArray[t]=a(this)}});if(s.featuresContainer.children("div").first().css("borderLeftWidth")!="medium"){s.borderWidth=parseInt(s.featuresContainer.children("div").first().css("borderLeftWidth"))*2}s.featuresContainer.children("div").each(function(){a(this).css({left:(s.containerWidth/2)-(s.smallFeatureWidth/2)-(s.borderWidth/2),width:s.smallFeatureWidth,height:s.smallFeatureHeight,top:b.smallFeatureOffset+b.topPadding,opacity:0})}).find("img:first").css({width:s.smallFeatureWidth});if(s.totalFeatureCount<4){s.itemsToAnimate=s.totalFeatureCount}else{s.itemsToAnimate=4}s.featuresContainer.find("div > div").hide()}function e(){a.each(s.featuresArray,function(x){a(this).data("setPosition",x+1)});var w=h(b.startingFeature);s.currentCenterNum=w;var v=k(w);v.data("position",1);var u=v.prevAll();u.each(function(x){a(this).data("position",(s.totalFeatureCount-x))});var t=v.nextAll();t.each(function(x){if(a(this).data("setPosition")!=undefined){a(this).data("position",(x+2))}});if(b.counterStyle==3){a.each(s.featuresArray,function(){var y=h(a(this).data("position"));var x=a("<span></span>");x.addClass("numberTag");x.html("("+y+" of "+s.totalFeatureCount+") ");a(this).find("div p").prepend(x)})}}function r(){if(b.counterStyle==1||b.counterStyle==2){var u=a("<ul></ul>");u.addClass("blipsContainer");for(var v=0;v<s.totalFeatureCount;v++){var t;if(b.counterStyle==2){t=""}else{t=v+1}var w=a("<div>"+t+"</div>");w.addClass("blip");w.css("cursor","pointer");w.attr("id","blip_"+(v+1));var x=a("<li></li>");x.append(w);x.css("float","left");x.css("list-style-type","none");u.append(x)}a(s.containerIDTag).append(u);u.hide().show()}}function n(x,u){var t=s.featuresContainer.find(".blipsContainer");var v=t.find("#blip_"+x);var w=t.find("#blip_"+u);v.removeClass("blipSelected");w.addClass("blipSelected")}function j(){if(s.timeoutVar!=null){s.timeoutVar=clearTimeout(s.timeoutVar)}if(b.autoPlay!=0){var t=(Math.abs(b.autoPlay)<b.carouselSpeed)?b.carouselSpeed:Math.abs(b.autoPlay);s.timeoutVar=setTimeout(function(){if(b.autoPlay>0){i(true,1)}else{if(b.autoPlay<0){i(false,1)}}},t)}}function q(t){a.each(s.featuresArray,function(){var u;if(t==false){u=f(a(this).data().position)}else{u=h(a(this).data().position)}a(this).data("position",u)})}function m(y,C){var w,t,v,z,u,D,A;var B=y.data("position");var x;if(C==true){x=h(B)}else{x=f(B)}if(x==1){w=s.largeFeatureWidth;t=s.largeFeatureHeight;v=b.topPadding;u=y.css("z-index");z=(s.containerWidth/2)-(s.largeFeatureWidth/2)-(s.borderWidth/2);A=1}else{w=s.smallFeatureWidth;t=s.smallFeatureHeight;v=b.smallFeatureOffset+b.topPadding;u=1;A=0.4;if(x==s.totalFeatureCount){z=b.sidePadding}else{if(x==2){z=s.containerWidth-s.smallFeatureWidth-b.sidePadding-s.borderWidth}else{z=(s.containerWidth/2)-(s.smallFeatureWidth/2)-(s.borderWidth/2);A=0}}}if(x!=1){y.find("div").hide()}y.animate({width:w,height:t,top:v,left:z,opacity:A},b.carouselSpeed,b.animationEasing,function(){if(x==1){y.find("div").fadeTo("fast",0.6)}s.rotationsRemaining=s.rotationsRemaining-1;y.css("z-index",u);if(b.counterStyle==1||b.counterStyle==2){if(x==1){var E=s.featuresContainer.children("div").index(y)+1;var F;if(C==false){F=f(E)}else{F=h(E)}n(F,E)}}var G=s.rotationsRemaining/s.itemsToAnimate;if(G%1==0){s.currentlyMoving=false;q(C);if(s.rotationsRemaining>0){d(C)}}j()}).find("img:first").animate({width:w,height:t},b.carouselSpeed,b.animationEasing).end()}function d(w){s.currentlyMoving=true;var x,v,t,u;if(w==true){x=k(f(s.currentCenterNum));v=k(s.currentCenterNum);t=k(f(f(s.currentCenterNum)));u=k(h(s.currentCenterNum));s.currentCenterNum=f(s.currentCenterNum)}else{x=k(h(s.currentCenterNum));v=k(h(h(s.currentCenterNum)));t=k(s.currentCenterNum);u=k(f(s.currentCenterNum));s.currentCenterNum=h(s.currentCenterNum)}if(w){v.css("z-index",3)}else{t.css("z-index",3)}x.css("z-index",4);m(v,w);m(x,w);m(t,w);if(s.totalFeatureCount>3){m(u,w)}}function i(v,u){if(s.currentlyMoving==false){var t=u*s.itemsToAnimate;s.rotationsRemaining=t;d(v)}}function g(x,w){var u=1,t=1,v;v=x;while((v=h(v))!=w){u++}v=x;while((v=f(v))!=w){t++}return(u<t)?u*-1:t}a(".leftButton").click(function(){i(false,1)});a(".rightButton").click(function(){i(true,1)});s.featuresContainer.children("div").click(function(){var t=a(this).data("position");if(t==2){i(true,1)}else{if(t==s.totalFeatureCount){i(false,1)}}}).mouseover(function(){if(s.currentlyMoving==false){var t=a(this).data("position");if(t==2||t==s.totalFeatureCount){a(this).css("opacity",0.8)}}}).mouseout(function(){if(s.currentlyMoving==false){var t=a(this).data("position");if(t==2||t==s.totalFeatureCount){a(this).css("opacity",0.4)}}});a("a",s.containerIDTag).live("click",function(u){var t=a(this).parentsUntil(s.containerIDTag);t.each(function(){var v=a(this).data("position");if(v!=undefined){if(v!=1){if(v==s.totalFeatureCount){i(false,1)}else{if(v==2){i(true,1)}}u.preventDefault();return false}}})});a(".blip").live("click",function(){var t=a(this).attr("id").substring(5);var v=s.featuresContainer.children("div").eq(t-1).data("position");var u=s.currentCenterNum;if(t!=u){var w=g(1,v);if(w<0){i(false,(w*-1))}else{i(true,w)}}})})};a.fn.featureCarousel.defaults={largeFeatureWidth:0,largeFeatureHeight:0,smallFeatureWidth:0.5,smallFeatureHeight:0.5,topPadding:20,sidePadding:30,smallFeatureOffset:50,startingFeature:1,carouselSpeed:1000,autoPlay:0,counterStyle:1,preload:true,displayCutoff:0,animationEasing:"swing"}})(jQuery);

