
function GetCirclePos(cX,cY,r,theta){theta=theta*Math.PI*2;return{x:cX+(r*Math.cos(theta)),y:cY+(r*Math.sin(theta))};}
function GetEllipsePos(cX,cY,rX,rY,theta){theta=theta*Math.PI*2;return{x:cX+(rX*Math.cos(theta)),y:cY+(rY*Math.sin(theta))};}
function NormalisedModulus(value){if(value>=-0.0&&value<1.0){return value;}
var fractPart=value%1.0;return fractPart<0?1+fractPart:fractPart;}
function CalculateDepthLevels(count){return Math.floor(count/2)+1;}
function DefaultDepthLevelFn(theta){theta-=0.25;if(theta>0.5){theta-=1;}
return Math.abs(theta*2);}
function ScaleItemByDepthTheta(layoutData,options){ScaleItemByDepthTheta.defaults={minZIndex:50,minSize:0.5};options=$.extend({},ScaleItemByDepthTheta.defaults,options);var minWidth=layoutData.originalSize.width*options.minSize;var minHeight=layoutData.originalSize.height*options.minSize;var newWidth=Math.round(minWidth+((layoutData.originalSize.width-minWidth)*(1-layoutData.depthTheta)));var newHeight=Math.round(minHeight+((layoutData.originalSize.height-minHeight)*(1-layoutData.depthTheta)));layoutData.item.css("width",newWidth+"px").css("height",newHeight+"px").css("z-index",(options.minZIndex+layoutData.maxDepthLevels)-layoutData.depthLevel);}
function jsCarousel(options){jsCarousel.defaults={animate:true,duration:"slow",layoutFn:ScaleItemByDepthTheta};options=$.extend({},jsCarousel.defaults,options);var carousel={options:options,items:[],depthLevels:0,itemThetaStep:0,thetaOffset:0,currentItem:-1,jItems:[],activate:function(item){if(options.onActivateItem){options.onActivateItem(item);}},deactivateItem:function(){if(options.onDeactivateItem){options.container.queue("fx",function(){options.onDeactivateItem();});}},get_offsetfor:function(index){return NormalisedModulus(0.25-(index/this.items.length));},set_scroll:function(offset){this.thetaOffset=NormalisedModulus(offset);var tempCoOrd=null;var theta=0;var depthTheta=0;var depthLevel=0;for(var f=0;f<this.items.length;f++){theta=NormalisedModulus((f/this.items.length)+this.thetaOffset);tempCoOrd=options.pathFn(theta);depthTheta=options.depthLevelFn(theta);depthLevel=Math.abs(Math.round(depthTheta*(this.depthLevels-1)));if(options.layoutFn){options.layoutFn({item:this.jItems[f],originalSize:this.jItems[f].data("initialSize"),depthTheta:depthTheta,depthLevel:depthLevel,maxDepthLevels:this.depthLevels,coOrd:tempCoOrd});}
this.jItems[f].css("top",(tempCoOrd.y-(this.jItems[f].height()/2))+"px").css("left",(tempCoOrd.x-(this.jItems[f].width()/2))+"px");}},scroll:function(vector,animate,duration,callback){if(!animate){this.set_scroll(this.thetaOffset+vector);if(callback){callback();}}
else{var animator={offset:this.thetaOffset};$(animator).animate({offset:this.thetaOffset+vector},{duration:duration,step:function(value){carousel.set_scroll(value);},complete:callback});}},nudge_right:function(animate,duration){carousel.deactivateItem();this.scroll(-this.itemThetaStep,animate,duration,function(){carousel.currentItem=(carousel.currentItem===(carousel.items.length-1)?0:(carousel.currentItem+1));carousel.activate(carousel.jItems[carousel.currentItem]);});},nudge_left:function(animate,duration){carousel.deactivateItem();this.scroll(this.itemThetaStep,animate,duration,function(){carousel.currentItem=(carousel.currentItem===0?(carousel.items.length-1):(carousel.currentItem-1));carousel.activate(carousel.jItems[carousel.currentItem]);});},move_toindex:function(index,animate,duration){if(index==this.currentItem){return;}
var newOffset=this.get_offsetfor(index);var vector=newOffset-this.thetaOffset;if(Math.abs(vector)>0.5){vector=(vector>0)?-(1-vector):-(-1-vector);}
carousel.deactivateItem();options.container.queue("fx",function(){carousel.scroll(vector,animate,duration,function(){carousel.currentItem=index;carousel.activate(carousel.jItems[index]);});$(this).dequeue();});},init:function(){this.items=options.container.find(options.itemSelector);if(!this.items.length){throw"No Items for Carousel found with the selector "+options.itemSelector;}
this.depthLevels=CalculateDepthLevels(this.items.length);this.itemThetaStep=1.0/this.items.length
if(!this.options.depthLevelFn){this.options.depthLevelFn=DefaultDepthLevelFn;}
$.each(this.items,function(i,item){var jItem=$(item);carousel.jItems.push(jItem);jItem.css("position","absolute");jItem.data("initialSize",{width:jItem.width(),height:jItem.height()});jItem.click(function(){carousel.move_toindex(i,options.animate,options.duration);});});this.set_scroll(0.25-(options.initialItem/this.items.length));this.activate(carousel.jItems[options.initialItem]);}};carousel.init();return carousel;}
