
(function($){$.fn.launchPadCarousel=function(options){options=$.extend({},$.fn.launchPadCarousel.defaults,options);var carouselDiv;var itemsDiv;var items;var slider;var itemsWidth;var thisRef;var currentIndex=0;var previousIndex=-1;var allowAnimation=true;var highestItemHeight=0;var newZIndex=100;var previousWasManualSlideChange=false;thisRef=this;carouselDiv=$(this);itemsDiv=$(this).find('.launchPadCarouselItems');items=itemsDiv.find('.launchPadCarouselItem');itemsWidth=parseInt(itemsDiv.width());slider=$(this).parent().find('.launchPadCarouselSlider');infoDiv=$(this).parent().find('.launchPadCarouselInfo');this.fnLaunchPadCarouselCalcuate=function(){var fnRecalculateItem=function(){$(this).stop();var animateParams={};var animateParamsParent={};var parentLeftAmount=0;var parentTopAmount=0;var myIndex=itemsDiv.find('.launchPadCarouselItem .img').index(this);var myDistanceFromCurrent=Math.abs(currentIndex-myIndex);var myParent=$(this).parents('.launchPadCarouselItem:first');var originalHeight=$(this).data('originalHeight');if(originalHeight==undefined){$(this).data('originalHeight',parseInt($(this).height()));originalHeight=$(this).data('originalHeight');}
var originalWidth=$(this).data('originalWidth');if(originalWidth==undefined){$(this).data('originalWidth',parseInt($(this).width()));originalWidth=$(this).data('originalWidth');}
var myWidth=(myIndex==currentIndex?options.itemMaxWidth:Math.round(options.itemMinWidth*Math.log(items.length-myDistanceFromCurrent)));if(myWidth<options.itemMinWidth){myWidth=options.itemMinWidth;}
var myHeight=Math.round(parseInt(originalHeight)*(myWidth/parseInt(originalWidth)));$(myParent).data('intendedWidth',myWidth);$(myParent).data('intendedHeight',myHeight);if(myHeight>highestItemHeight){highestItemHeight=myHeight;}
if(myIndex!=currentIndex){parentTopAmount=Math.round((highestItemHeight-myHeight)/2);}
parentLeftAmount=Math.round(50*myDistanceFromCurrent);if(myIndex>currentIndex){parentLeftAmount=0-(parentLeftAmount*0.5);}
if(myIndex>currentIndex){newZIndex--;}else{newZIndex++;}
myParent.css('zIndex',newZIndex);animateParams=$.extend(animateParams,{width:myWidth+'px',height:myHeight+'px'});animateParamsParent=$.extend(animateParamsParent,{left:parentLeftAmount+'px',top:parentTopAmount+'px'});$(this).animate(animateParams,options.animationLength);myParent.stop().animate(animateParamsParent,options.animationLength);};if(highestItemHeight==0){$(this).find('.img:first').each(function(){if($(this).height()>highestItemHeight){highestItemHeight=$(this).height();}});}
$(this).find('.img:first').each(fnRecalculateItem);};this.refresh=function(callback){if(currentIndex!=previousIndex){var item=$(items[currentIndex]).data('model');infoDiv.html(item.CarouselItemDetails);previousIndex=currentIndex;}
newZIndex=100;if(allowAnimation){allowAnimation=false;window.setTimeout(function(){allowAnimation=true;},150);items.each(this.fnLaunchPadCarouselCalcuate);var totalItemsWidth=0;items.each(function(){if(items.index(this)<currentIndex){totalItemsWidth+=$(this).data('intendedWidth');}});itemsDiv.stop().animate({left:'-'+(currentIndex==0?0:Math.round((options.itemMaxWidth*Math.log(currentIndex)))*(options.adjustment+Math.log(currentIndex)))+'px'},options.animationLength);var newSliderValue=Math.round((currentIndex/items.length)*200);if(currentIndex==items.length-1){newSliderValue=slider.slider('option','max');}
previousWasManualSlideChange=true;slider.slider('option','value',newSliderValue);}
if(callback){window.setTimeout(function(){callback();},options.animationLength);}};$(this).css({overflow:'hidden',position:'relative'}).width(options.width+'px');itemsDiv.css('position','absolute');items.each(function(){$(this).css('float','left').css('position','relative');$(this).click(function(){currentIndex=itemsDiv.find('.launchPadCarouselItem').index(this);thisRef.refresh();});});var fnSlideChange=function(event,ui){if(previousWasManualSlideChange){previousWasManualSlideChange=false;}else{currentIndex=Math.round(ui.value/Math.round(200/items.length));if(currentIndex>=items.length){currentIndex=(items.length-1);}
allowAnimation=true;thisRef.refresh();previousIndex=currentIndex;}};slider.slider({max:200,slide:fnSlideChange,change:fnSlideChange});var fnNext=function(){currentIndex++;if(currentIndex>=items.length){currentIndex=(items.length-1);}
thisRef.refresh();};var fnPrevious=function(){currentIndex--;if(currentIndex<0){currentIndex=0;}
thisRef.refresh();};$('.launchPadCarouselBack').click(fnPrevious);$('.launchPadCarouselNext').click(fnNext);$(window).keyCodePressed(37,fnPrevious);$(window).keyCodePressed(39,fnNext);currentIndex=Math.floor(items.length/2);window.setTimeout(function(){thisRef.refresh();},250);return this;};$.fn.launchPadCarousel.defaults={width:600,itemMaxWidth:200,itemMinWidth:50,itemMaxHeight:144,animationLength:250,adjustment:0.5};})(jQuery);
